001/* 002 * Copyright 2021 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.runtime.i18n; 017 018import java.time.ZoneId; 019import java.time.ZonedDateTime; 020import java.time.format.DateTimeFormatter; 021import java.time.format.FormatStyle; 022import java.util.Locale; 023 024import org.apache.cocoon.transformation.I18nTransformer; 025import org.apache.cocoon.xml.AttributesImpl; 026import org.xml.sax.ContentHandler; 027import org.xml.sax.SAXException; 028 029import org.ametys.core.util.DateUtils; 030 031/** 032 * Allow to use {@link ZonedDateTime} as a parameter for {@link I18nizableText}. 033 */ 034public class I18nizableDateTime implements FormatableI18nizable, I18nizableTextParameter 035{ 036 private ZonedDateTime _dateTime; 037 private DateTimeFormatter _formatter; 038 private String _pattern; 039 040 /** 041 * Build an {@link I18nizableDateTime} from {@link ZonedDateTime} and style. 042 * @param dateTime the value 043 * @param zoneId the time zone to use for formatting 044 * @param style the style to use for formatting 045 */ 046 public I18nizableDateTime(ZonedDateTime dateTime, ZoneId zoneId, FormatStyle style) 047 { 048 _dateTime = dateTime.withZoneSameInstant(zoneId); 049 _formatter = DateTimeFormatter.ofLocalizedDateTime(style); 050 _pattern = style.name(); 051 } 052 053 /** 054 * Build an {@link I18nizableDateTime} from {@link ZonedDateTime} and pattern. 055 * @param dateTime the value 056 * @param zoneId the time zone to use for formatting 057 * @param pattern the pattern to use for formatting 058 */ 059 public I18nizableDateTime(ZonedDateTime dateTime, ZoneId zoneId, String pattern) 060 { 061 _dateTime = dateTime.withZoneSameInstant(zoneId); 062 _formatter = DateTimeFormatter.ofPattern(pattern); 063 _pattern = pattern; 064 } 065 066 public void toSAX(ContentHandler handler) throws SAXException 067 { 068 AttributesImpl atts = new AttributesImpl(); 069 atts.addCDATAAttribute(I18nTransformer.I18N_NAMESPACE_URI, "pattern", "pattern", _pattern); 070 atts.addCDATAAttribute(I18nTransformer.I18N_NAMESPACE_URI, "src-pattern", "src-pattern", "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); 071 atts.addCDATAAttribute(I18nTransformer.I18N_NAMESPACE_URI, "value", "value", DateUtils.zonedDateTimeToString(_dateTime)); 072 073 handler.startElement(I18nTransformer.I18N_NAMESPACE_URI, "date-time", "i18n:date-time", atts); 074 handler.endElement(I18nTransformer.I18N_NAMESPACE_URI, "date-time", "i18n:date-time"); 075 } 076 077 public void toSAXAsParam(ContentHandler handler) throws SAXException 078 { 079 toSAX(handler); 080 } 081 082 public String format(Locale locale) 083 { 084 return _formatter.withLocale(locale).format(_dateTime); 085 } 086}