001package org.apache.turbine.services.pull.util; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.time.ZoneId; 023import java.time.format.DateTimeFormatter; 024import java.time.format.DateTimeParseException; 025import java.time.temporal.TemporalAccessor; 026import java.util.Locale; 027 028import org.apache.logging.log4j.LogManager; 029import org.apache.logging.log4j.Logger; 030import org.apache.turbine.annotation.TurbineService; 031import org.apache.turbine.services.ServiceManager; 032import org.apache.turbine.services.TurbineServices; 033import org.apache.turbine.services.localization.DateTimeFormatterInterface; 034import org.apache.turbine.services.localization.DateTimeFormatterService; 035import org.apache.turbine.services.pull.ApplicationTool; 036 037/** 038 * This pull tool is used to format {@link TemporalAccessor} and 039 * {@link #map(String, DateTimeFormatter, Locale)} (different falvors) 040 * objects into strings. 041 * 042 * The methods may throw {@link java.time.temporal.UnsupportedTemporalTypeException} or 043 * {@link DateTimeParseException}. 044 * if the source and the target format do not match appropriately. 045 * 046 */ 047public class DateTimeFormatterTool 048 implements ApplicationTool, DateTimeFormatterInterface 049{ 050 051 @TurbineService 052 private DateTimeFormatterService dtfs; 053 054 private static final Logger log = LogManager.getLogger(DateTimeFormatterTool.class); 055 056 /** 057 * Initialize the application tool. The data parameter holds a different 058 * type depending on how the tool is being instantiated: 059 * <ul> 060 * <li>For global tools data will be null</li> 061 * <li>For request tools data will be of type RunData</li> 062 * <li>For session and persistent tools data will be of type User</li> 063 * </ul> 064 * 065 * the {@link #defaultFormat} from {@link #dateTimeFormatPattern} with default Locale {@link Locale#getDefault()} and 066 * Default zone: {@link ZoneId#systemDefault()} 067 * 068 * @param data initialization data 069 */ 070 @Override 071 public void init(Object data) 072 { 073 log.info("Initialized DateTimeFormatterTool with service {}", 074 dtfs); 075 if (dtfs == null) 076 { 077 ServiceManager serviceManager = TurbineServices.getInstance(); 078 dtfs = (DateTimeFormatterService)serviceManager.getService(DateTimeFormatterService.SERVICE_NAME); 079 } 080 // dtfs should be already initialized 081 } 082 083 public DateTimeFormatterService getDtfs() { 084 return dtfs; 085 } 086 087 /** 088 * Refresh the application tool. This is 089 * necessary for development work where you 090 * probably want the tool to refresh itself 091 * if it is using configuration information 092 * that is typically cached after initialization 093 */ 094 @Override 095 public void refresh() 096 { 097 // empty 098 } 099 100 public DateTimeFormatter getDefaultFormat() 101 { 102 return getDtfs().getDefaultFormat(); 103 } 104 105 public String getDateTimeFormatPattern() { 106 return getDtfs().getDateTimeFormatPattern(); 107 } 108 109 /** 110 * Formats the given datetime as a String with the #{@link DateTimeFormatterTool#defaultFormat}. 111 * using the default date format. 112 * 113 * @param the {@link TemporalAccessor to format 114 * @return String value of the date 115 */ 116 public <T extends TemporalAccessor> String format(T temporalAccessor) 117 { 118 return getDtfs().getDefaultFormat().format(temporalAccessor); 119 } 120 121 public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString) 122 { 123 return getDtfs().format(temporalAccessor, dateFormatString, null); 124 } 125 126 public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString, Locale locale) 127 { 128 return getDtfs().format(temporalAccessor, dateFormatString, locale); 129 } 130 131 public String map( String src, String outgoingFormatPattern, Locale locale, String incomingFormatPattern) 132 { 133 return getDtfs().map(src, outgoingFormatPattern, locale, incomingFormatPattern); 134 } 135 136 public String map( String src, java.time.format.DateTimeFormatter outgoingFormat, Locale locale, 137 java.time.format.DateTimeFormatter incomingFormat) 138 { 139 return getDtfs().map(src, outgoingFormat, locale, incomingFormat); 140 } 141 142 public String mapTo( String src, DateTimeFormatter outgoingFormat ) 143 { 144 return getDtfs().map( src, outgoingFormat, null, getDtfs().getDefaultFormat() ); 145 } 146 147 public String mapFrom( String src, DateTimeFormatter incomingFormat ) 148 { 149 return getDtfs().map( src, getDtfs().getDefaultFormat(), null, incomingFormat ); 150 } 151 152 public String map( String src, DateTimeFormatter outgoingFormat, Locale locale ) 153 { 154 return getDtfs().map( src, outgoingFormat, locale, getDtfs().getDefaultFormat() ); 155 } 156}