| <?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>DateTimeFormatterTool.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> > <a href="index.source.html" class="el_package">org.apache.turbine.services.pull.util</a> > <span class="el_source">DateTimeFormatterTool.java</span></div><h1>DateTimeFormatterTool.java</h1><pre class="source lang-java linenums">package org.apache.turbine.services.pull.util; |
| |
| /* |
| * 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 |
| * "License"); 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 |
| * "AS IS" 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.ZoneId; |
| import java.time.format.DateTimeFormatter; |
| import java.time.format.DateTimeParseException; |
| import java.time.temporal.TemporalAccessor; |
| import java.util.Date; |
| import java.util.Locale; |
| |
| import org.apache.fulcrum.localization.LocalizationService; |
| 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.ServiceManager; |
| import org.apache.turbine.services.TurbineServices; |
| import org.apache.turbine.services.localization.DateTimeFormatterInterface; |
| import org.apache.turbine.services.localization.DateTimeFormatterService; |
| import org.apache.turbine.services.localization.RundataLocalizationService; |
| import org.apache.turbine.util.RunData; |
| |
| /** |
| * This pull tool is used to format {@link TemporalAccessor} and |
| * {@link #map(String, DateTimeFormatter, Locale)} (different flavors) |
| * objects into strings. |
| * |
| * This tool extends {@link DateFormatter} to simplify configuration |
| * and to allow legacy {@link Date} inputs. |
| * |
| * The methods may throw {@link java.time.temporal.UnsupportedTemporalTypeException} or |
| * {@link DateTimeParseException}. |
| * if the source and the target format do not match appropriately. |
| * |
| */ |
| <span class="nc" id="L54">public class DateTimeFormatterTool extends DateFormatter</span> |
| implements DateTimeFormatterInterface |
| { |
| |
| @TurbineService |
| private DateTimeFormatterService dtfs; |
| |
| <span class="fc" id="L61"> private static final Logger log = LogManager.getLogger(DateTimeFormatterTool.class);</span> |
| |
| /** Fulcrum Localization component */ |
| @TurbineService |
| private LocalizationService localizationService; |
| |
| protected Locale locale; |
| |
| <span class="nc" id="L69"> private boolean overrideFromRequestLocale = false;</span> |
| |
| /** |
| * Initialize the application tool. The data parameter holds a different |
| * type depending on how the tool is being instantiated: |
| * <ul> |
| * <li>For global tools data will be null</li> |
| * <li>For request tools data will be of type RunData</li> |
| * <li>For session and persistent tools data will be of type User</li> |
| * </ul> |
| * |
| * the {@link #getDefaultFormat()} from {@link #getFormatPattern()} |
| * with {@link DateTimeFormatterService#getLocale()} |
| * and zoneId {@link DateTimeFormatterService#getZoneId()} is used. |
| * |
| * Customizations: |
| * Locale could be fetched from request, if #USE_REQUEST_LOCALE_KEY is set to |
| * <code>true</code> (by default it is <code>false</code>.Then it will be retrieved either from |
| * {@link RundataLocalizationService#getLocale(RunData)} (if set in urbien role configuration) |
| * or {@link LocalizationService#getLocale(jakarta.servlet.http.HttpServletRequest)}. |
| * |
| * @param data initialization data |
| */ |
| @Override |
| public void init(Object data) |
| { |
| <span class="nc" id="L95"> log.info("Initializing DateTimeFormatterTool with service {}",</span> |
| dtfs); |
| <span class="nc bnc" id="L97" title="All 2 branches missed."> if (dtfs == null)</span> |
| { |
| <span class="nc" id="L99"> ServiceManager serviceManager = TurbineServices.getInstance();</span> |
| <span class="nc" id="L100"> dtfs = (DateTimeFormatterService)serviceManager.getService(DateTimeFormatterService.SERVICE_NAME);</span> |
| } |
| |
| <span class="nc" id="L103"> overrideFromRequestLocale = Turbine.getConfiguration()</span> |
| <span class="nc" id="L104"> .getBoolean(USE_REQUEST_LOCALE_KEY, false);</span> |
| // dtfs should be already initialized |
| <span class="nc bnc" id="L106" title="All 4 branches missed."> if (overrideFromRequestLocale && data instanceof RunData)</span> |
| { |
| // Pull necessary information out of RunData while we have |
| // a reference to it. |
| <span class="nc" id="L110"> locale = localizationService.getLocale(((RunData) data).getRequest());</span> |
| <span class="nc" id="L111"> log.info("Override {} with request locale {} from {}", dtfs.getLocale(), locale, localizationService);</span> |
| } |
| <span class="nc" id="L113"> }</span> |
| |
| public DateTimeFormatterService getDtfs() { |
| <span class="nc" id="L116"> return dtfs;</span> |
| } |
| |
| /** |
| * Refresh the application tool. This is |
| * necessary for development work where you |
| * probably want the tool to refresh itself |
| * if it is using configuration information |
| * that is typically cached after initialization |
| */ |
| @Override |
| public void refresh() |
| { |
| // empty |
| <span class="nc" id="L130"> }</span> |
| |
| @Override |
| public DateTimeFormatter getDefaultFormat() |
| { |
| <span class="nc" id="L135"> return getDtfs().getDefaultFormat();</span> |
| } |
| |
| @Override |
| public String getFormatPattern() { |
| <span class="nc" id="L140"> return getDtfs().getFormatPattern();</span> |
| } |
| |
| /** |
| * Formats the given datetime as a String with the #{@link DateTimeFormatterTool#getDefaultFormat()}. |
| * using the default date format. |
| * |
| * @param temporalAccessor {@link TemporalAccessor to format} |
| * @return String value of the date |
| */ |
| @Override |
| public <T extends TemporalAccessor> String format(T temporalAccessor) |
| { |
| <span class="nc" id="L153"> return getDtfs().format(temporalAccessor, getDtfs().getFormatPattern(), getLocale());</span> |
| } |
| |
| @Override |
| public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString) |
| { |
| <span class="nc" id="L159"> return getDtfs().format(temporalAccessor, dateFormatString, getLocale());</span> |
| } |
| |
| @Override |
| public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString, Locale locale) |
| { |
| <span class="nc" id="L165"> return getDtfs().format(temporalAccessor, dateFormatString, locale);</span> |
| } |
| |
| @Override |
| public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString, Locale locale, |
| ZoneId zoneId) { |
| <span class="nc" id="L171"> return getDtfs().format(temporalAccessor, dateFormatString, locale, zoneId);</span> |
| } |
| |
| @Override |
| public String map( String src, String outgoingFormatPattern, Locale locale, String incomingFormatPattern) |
| { |
| <span class="nc" id="L177"> return getDtfs().map(src, outgoingFormatPattern, locale, incomingFormatPattern);</span> |
| } |
| |
| @Override |
| public String map( String src, java.time.format.DateTimeFormatter outgoingFormat, |
| Locale locale, java.time.format.DateTimeFormatter incomingFormat) |
| { |
| <span class="nc" id="L184"> return getDtfs().map(src, outgoingFormat, locale, incomingFormat);</span> |
| } |
| |
| @Override |
| public String mapTo( String src, DateTimeFormatter outgoingFormat ) |
| { |
| <span class="nc" id="L190"> return getDtfs().map( src, outgoingFormat, getLocale(), getDtfs().getDefaultFormat() );</span> |
| } |
| |
| @Override |
| public String mapFrom( String src, DateTimeFormatter incomingFormat ) |
| { |
| <span class="nc" id="L196"> return getDtfs().map( src, getDtfs().getDefaultFormat(), getLocale(), incomingFormat );</span> |
| } |
| |
| @Override |
| public String map( String src, DateTimeFormatter outgoingFormat, Locale locale ) |
| { |
| <span class="nc" id="L202"> return getDtfs().map( src, outgoingFormat, locale, getDtfs().getDefaultFormat() );</span> |
| } |
| |
| public Locale getLocale() { |
| <span class="nc" id="L206"> return locale;</span> |
| } |
| |
| public void setLocale(Locale locale) { |
| <span class="nc" id="L210"> this.locale = locale;</span> |
| <span class="nc" id="L211"> }</span> |
| |
| @Override |
| public ZoneId getZoneId() { |
| <span class="nc bnc" id="L215" title="All 2 branches missed."> return (getDtfs()!= null)? getDtfs().getZoneId():ZoneId.systemDefault();</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> |