blob: 6ddffd514c9298b6bbede44935e61ce668d45fc8 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>DateTimeFormatterService.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Turbine</a> &gt; <a href="index.source.html" class="el_package">org.apache.turbine.services.localization</a> &gt; <span class="el_source">DateTimeFormatterService.java</span></div><h1>DateTimeFormatterService.java</h1><pre class="source lang-java linenums">package org.apache.turbine.services.localization;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.time.DateTimeException;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.turbine.Turbine;
import org.apache.turbine.annotation.TurbineService;
import org.apache.turbine.services.TurbineBaseService;
import org.apache.turbine.util.LocaleUtils;
/**
* This service is used to format {@link TemporalAccessor} and
* {@link #map(String, DateTimeFormatter, Locale)} (different flavors)
* objects into strings.
* &lt;p&gt;
* The methods may throw {@link java.time.temporal.UnsupportedTemporalTypeException} or
* {@link DateTimeParseException}, e.g.
* if the source and the target format do not match appropriately.
* &lt;/p&gt;
*
*/
@TurbineService(&quot;DateTimeFormatterService&quot;)
<span class="fc" id="L49">public class DateTimeFormatterService</span>
extends TurbineBaseService implements DateTimeFormatterInterface
{
<span class="fc" id="L53"> private String formatPattern = null;</span>
<span class="fc" id="L55"> private DateTimeFormatter dateTimeFormat = null;</span>
<span class="fc" id="L57"> private Locale locale = null;</span>
private ZoneId zoneId;
@Override
public DateTimeFormatter getDefaultFormat()
{
<span class="nc" id="L64"> return dateTimeFormat;</span>
}
@Override
public String getFormatPattern() {
<span class="nc" id="L69"> return formatPattern;</span>
}
<span class="fc" id="L72"> private static final Logger log = LogManager.getLogger(DateTimeFormatterService.class);</span>
/**
* Initialize the service.
* &lt;p&gt; &lt;p&gt; &lt;p&gt; &lt;p&gt;
* the {@link #dateTimeFormat} from {@link #formatPattern} is initialized with
*
* &lt;ol&gt;
* &lt;li&gt;{@link Locale}: {@link LocaleUtils#getDefaultLocale()} is used by default.
* It could be overridden setting #USE_TURBINE_LOCALE_KEY to false, the
* the default Locale {@link Locale#getDefault()} is used.
* &lt;/li&gt;&lt;li&gt;{@link ZoneId}: If #DATE_TIME_ZONEID_KEY is set this {@link ZoneId}
* is used else {@link ZoneId#systemDefault()}.
* &lt;/li&gt;
* &lt;/ol&gt;
*/
@Override
public void init()
{
<span class="fc" id="L91"> formatPattern = Turbine.getConfiguration()</span>
<span class="fc" id="L92"> .getString(DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_DEFAULT);</span>
<span class="fc" id="L94"> boolean useTurbineLocale = Turbine.getConfiguration()</span>
<span class="fc" id="L95"> .getBoolean(USE_TURBINE_LOCALE_KEY, true);</span>
<span class="pc bpc" id="L97" title="2 of 4 branches missed."> Locale localeSetter = (useTurbineLocale &amp;&amp; LocaleUtils.getDefaultLocale() != null)?</span>
<span class="fc" id="L98"> LocaleUtils.getDefaultLocale()</span>
<span class="pc" id="L99"> : Locale.getDefault();</span>
<span class="fc" id="L100"> setLocale(localeSetter);</span>
<span class="fc" id="L102"> String zoneIdStr = Turbine.getConfiguration()</span>
<span class="fc" id="L103"> .getString(DATE_TIME_ZONEID_KEY);</span>
<span class="pc bpc" id="L104" title="1 of 2 branches missed."> ZoneId zoneIdSet = (zoneIdStr != null)? ZoneId.of( zoneIdStr ) :</span>
<span class="pc" id="L105"> ZoneId.systemDefault();</span>
<span class="fc" id="L106"> setZoneId(zoneIdSet);</span>
<span class="fc" id="L108"> dateTimeFormat = DateTimeFormatter.ofPattern(formatPattern)</span>
<span class="fc" id="L109"> .withLocale(localeSetter).withZone(zoneIdSet);</span>
<span class="fc" id="L111"> log.info(&quot;Initialized DateTimeFormatterService with pattern {}, locale {} and zone {}&quot;,</span>
<span class="fc" id="L112"> formatPattern, dateTimeFormat.getLocale(),</span>
<span class="fc" id="L113"> dateTimeFormat.getZone());</span>
<span class="fc" id="L114"> setInit(true);</span>
<span class="fc" id="L115"> }</span>
@Override
public &lt;T extends TemporalAccessor&gt; String format(T temporalAccessor)
{
<span class="nc" id="L120"> return dateTimeFormat.format(temporalAccessor);</span>
}
@Override
public &lt;T extends TemporalAccessor&gt; String format(T temporalAccessor, String dateFormatString)
{
<span class="nc" id="L126"> return format(temporalAccessor, dateFormatString, null, null);</span>
}
@Override
public &lt;T extends TemporalAccessor&gt; String format(T temporalAccessor, String dateFormatString, Locale locale)
{
<span class="nc" id="L132"> return format(temporalAccessor, dateFormatString, locale, null);</span>
}
@Override
public &lt;T extends TemporalAccessor&gt; String format(T temporalAccessor, String dateFormatString, Locale locale,
ZoneId zoneId) {
<span class="nc" id="L138"> String result = null;</span>
<span class="nc bnc" id="L140" title="All 4 branches missed."> if (StringUtils.isEmpty(dateFormatString) || temporalAccessor == null)</span>
{
<span class="nc" id="L142"> result = &quot;&quot;;</span>
}
else
{
<span class="nc" id="L146"> DateTimeFormatter dtf =</span>
<span class="nc" id="L147"> DateTimeFormatter.ofPattern(dateFormatString);</span>
<span class="nc bnc" id="L148" title="All 2 branches missed."> if (locale != null)</span>
{
<span class="nc" id="L150"> dtf = dtf.withLocale(locale);</span>
} else {
<span class="nc" id="L152"> log.warn(&quot;adding default locale {}&quot;, getLocale() );</span>
<span class="nc" id="L153"> dtf = dtf.withLocale( getLocale());</span>
}
<span class="nc bnc" id="L155" title="All 2 branches missed."> if (zoneId != null)</span>
{
<span class="nc" id="L157"> dtf = dtf.withZone(zoneId);</span>
} else {
<span class="nc" id="L159"> log.info(&quot;adding default zone {}&quot;, getZoneId() );</span>
<span class="nc" id="L160"> dtf = dtf.withZone(getZoneId());</span>
}
<span class="nc" id="L162"> log.info(&quot;try to format {} with {}.&quot;, temporalAccessor, dtf );</span>
try {
<span class="nc" id="L164"> result =</span>
<span class="nc" id="L165"> dtf.format(temporalAccessor);</span>
<span class="nc" id="L166"> } catch(DateTimeException e) {</span>
<span class="nc" id="L167"> log.error(&quot;An exception with date time formatting was thrown: {}&quot;, e);</span>
// check with dtf.toFormat().format(temporalAccessor)?
<span class="nc" id="L169"> throw e;</span>
<span class="nc" id="L170"> }</span>
}
<span class="nc" id="L172"> return result;</span>
}
@Override
public String map(String src, String outgoingFormatPattern, Locale locale, String incomingFormatPattern)
{
<span class="nc bnc" id="L178" title="All 4 branches missed."> if (StringUtils.isEmpty(src) || outgoingFormatPattern == null)</span>
{
<span class="nc" id="L180"> return &quot;&quot;;</span>
}
<span class="nc bnc" id="L182" title="All 2 branches missed."> if (incomingFormatPattern == null)</span>
{
<span class="nc" id="L184"> incomingFormatPattern = formatPattern;</span>
}
<span class="nc bnc" id="L186" title="All 2 branches missed."> if (incomingFormatPattern.equals( outgoingFormatPattern )) {</span>
<span class="nc" id="L187"> return &quot;&quot;;</span>
}
<span class="nc" id="L189"> DateTimeFormatter incomingFormat = DateTimeFormatter.ofPattern(incomingFormatPattern);</span>
<span class="nc" id="L190"> DateTimeFormatter outgoingFormat = DateTimeFormatter.ofPattern(outgoingFormatPattern);</span>
<span class="nc bnc" id="L191" title="All 2 branches missed."> if (locale != null)</span>
{
<span class="nc" id="L193"> outgoingFormat = outgoingFormat.withLocale( locale );</span>
//incomingFormat = incomingFormat.withLocale( locale );
}
<span class="nc" id="L196"> return map( src, outgoingFormat, locale, incomingFormat );</span>
}
@Override
public String map(String src, DateTimeFormatter outgoingFormat, Locale locale,
DateTimeFormatter incomingFormat)
{
<span class="nc bnc" id="L203" title="All 4 branches missed."> if (StringUtils.isEmpty(src) || outgoingFormat == null)</span>
{
<span class="nc" id="L205"> return &quot;&quot;;</span>
}
<span class="nc bnc" id="L207" title="All 2 branches missed."> if (incomingFormat == null)</span>
{
<span class="nc" id="L209"> incomingFormat = dateTimeFormat;</span>
}
<span class="nc bnc" id="L211" title="All 2 branches missed."> if (incomingFormat.equals( outgoingFormat )) {</span>
<span class="nc" id="L212"> return &quot;&quot;;</span>
}
<span class="nc bnc" id="L214" title="All 2 branches missed."> if (locale != null)</span>
{
<span class="nc" id="L216"> outgoingFormat = outgoingFormat.withLocale( locale );</span>
//incomingFormat = incomingFormat.withLocale( locale );
}
<span class="nc" id="L219"> return outgoingFormat.format(</span>
<span class="nc" id="L220"> incomingFormat.parse( src ));</span>
}
@Override
public String mapTo(String src, DateTimeFormatter outgoingFormat)
{
<span class="nc" id="L226"> return map( src, outgoingFormat, null, dateTimeFormat );</span>
}
@Override
public String mapFrom(String src, DateTimeFormatter incomingFormat)
{
<span class="nc" id="L232"> return map( src, dateTimeFormat, null, incomingFormat );</span>
}
@Override
public String map(String src, DateTimeFormatter outgoingFormat, Locale locale)
{
<span class="nc" id="L238"> return map( src, outgoingFormat, locale, dateTimeFormat );</span>
}
public Locale getLocale() {
<span class="nc" id="L242"> return locale;</span>
}
public void setLocale(Locale locale) {
<span class="fc" id="L246"> this.locale = locale;</span>
<span class="fc" id="L247"> }</span>
@Override
public ZoneId getZoneId() {
<span class="nc" id="L251"> return zoneId;</span>
}
public void setZoneId(ZoneId zoneId) {
<span class="fc" id="L255"> this.zoneId = zoneId;</span>
<span class="fc" id="L256"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html>