Author: sylvain
Date: Fri Mar 11 03:00:04 2005
New Revision: 157069
URL: http://svn.apache.org/viewcvs?view=rev&rev=157069
Log:
forgot to "svn add" before the previous commit
Added:
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertor.java
(with props)
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertorBuilder.java
(with props)
Added:
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertor.java
URL:
http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertor.java?view=auto&rev=157069
==============================================================================
---
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertor.java
(added)
+++
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertor.java
Fri Mar 11 03:00:04 2005
@@ -0,0 +1,163 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+package org.apache.cocoon.forms.datatype.convertor;
+
+import java.text.ParseException;
+import java.util.Date;
+import java.util.Locale;
+
+import org.apache.cocoon.forms.Constants;
+import org.apache.cocoon.xml.AttributesImpl;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+import com.ibm.icu.text.DateFormat;
+import com.ibm.icu.text.SimpleDateFormat;
+
+/**
+ * A Convertor for [EMAIL PROTECTED] java.util.Date Date} objects backed by
ICU4J's
+ * [EMAIL PROTECTED] com.ibm.icu.text.SimpleDateFormat} class.
+ *
+ * <p>It can be configured to use one of three <strong>variants</strong>: date,
+ * time or datetime and one of four <strong>styles</strong>: long, full,
medium or short.
+ *
+ * <p>Alternatively, a <strong>formatting pattern</strong> can be used. This
can either be a locale-dependent
+ * or locale-independent formatting pattern. When looking up a formatting
pattern, a mechansim
+ * similar to resource bundle lookup is used. Suppose the locale is nl-BE,
then first a formatting
+ * pattern for nl-BE will be sought, then one for nl, and if that is not
+ * found, finally the locale-independent formatting pattern will be used.
+ *
+ * @version $Id$
+ */
+public class Icu4jDateConvertor implements Convertor {
+ //FIXME: the only difference of this class with FormattingDateConvertor is
the use of com.ibm.icu.text.SimpleDateFormat
+ // --> refactor to have it extend FormattingDateConvertor
+
+ /** See [EMAIL PROTECTED] #setStyle}. */
+ private int style;
+ /** See [EMAIL PROTECTED] #setVariant}. */
+ private String variant;
+ /** Locale-specific formatting patterns. */
+ private LocaleMap localizedPatterns;
+ /** Non-locale specific formatting pattern. */
+ private String nonLocalizedPattern;
+
+ public static final String DATE = "date";
+ public static final String TIME = "time";
+ public static final String DATE_TIME = "datetime";
+
+ public Icu4jDateConvertor() {
+ this.style = DateFormat.SHORT;
+ this.variant = DATE;
+ this.localizedPatterns = new LocaleMap();
+ }
+
+ public ConversionResult convertFromString(String value, Locale locale,
Convertor.FormatCache formatCache) {
+ SimpleDateFormat dateFormat = getDateFormat(locale, formatCache);
+ try {
+ return new ConversionResult(dateFormat.parse(value));
+ } catch (ParseException e) {
+ return ConversionResult.create("date." + this.variant);
+ }
+ }
+
+ public String convertToString(Object value, Locale locale,
Convertor.FormatCache formatCache) {
+ SimpleDateFormat dateFormat = getDateFormat(locale, formatCache);
+ return dateFormat.format((Date)value);
+ }
+
+ private final SimpleDateFormat getDateFormat(Locale locale,
Convertor.FormatCache formatCache) {
+ SimpleDateFormat dateFormat = null;
+ if (formatCache != null)
+ dateFormat = (SimpleDateFormat)formatCache.get();
+ if (dateFormat == null) {
+ dateFormat = getDateFormat(locale);
+ if (formatCache != null)
+ formatCache.store(dateFormat);
+ }
+ return dateFormat;
+ }
+
+ protected SimpleDateFormat getDateFormat(Locale locale) {
+ SimpleDateFormat dateFormat = null;
+
+ if (this.variant.equals(DATE)) {
+ //dateFormat = I18nSupport.getInstance().getDateFormat(style,
locale);
+ dateFormat = (SimpleDateFormat)DateFormat.getDateInstance(style,
locale);
+ } else if (this.variant.equals(TIME)) {
+ //dateFormat = I18nSupport.getInstance().getTimeFormat(style,
locale);
+ dateFormat = (SimpleDateFormat)DateFormat.getTimeInstance(style,
locale);
+ } else if (this.variant.equals(DATE_TIME)) {
+ //dateFormat = I18nSupport.getInstance().getDateTimeFormat(style,
style, locale);
+ dateFormat =
(SimpleDateFormat)DateFormat.getDateTimeInstance(style, style, locale);
+ }
+
+ String pattern = (String)localizedPatterns.get(locale);
+
+ if (pattern != null)
+ // Note: this was previously using applyLocalizedPattern() which
allows to use
+ // a locale-specific pattern syntax, e.g. in french "j" (jour) for
"d" and
+ // "a" (annee) for "y". But the localized pattern syntax is very
little known and thus
+ // led to some weird pattern syntax error messages.
+ dateFormat.applyPattern(pattern);
+ else if (nonLocalizedPattern != null)
+ dateFormat.applyPattern(nonLocalizedPattern);
+
+ return dateFormat;
+ }
+
+ public Class getTypeClass() {
+ return Date.class;
+ }
+
+ /**
+ *
+ * @param style one of the constants FULL, LONG, MEDIUM or SHORT defined
in the [EMAIL PROTECTED] Date} class.
+ */
+ public void setStyle(int style) {
+ this.style = style;
+ }
+
+ public void setVariant(String variant) {
+ if (DATE.equals(variant) || TIME.equals(variant) ||
DATE_TIME.equals(variant)) {
+ this.variant = variant;
+ } else {
+ throw new IllegalArgumentException("Invalid value for variant
parameter.");
+ }
+ }
+
+ public void addFormattingPattern(Locale locale, String pattern) {
+ localizedPatterns.put(locale, pattern);
+ }
+
+ public void setNonLocalizedPattern(String pattern) {
+ this.nonLocalizedPattern = pattern;
+ }
+
+ private static final String CONVERTOR_EL = "convertor";
+
+ public void generateSaxFragment(ContentHandler contentHandler, Locale
locale) throws SAXException {
+ String pattern = getDateFormat(locale).toPattern();
+
+ if (pattern != null) {
+ AttributesImpl attrs = new AttributesImpl();
+ attrs.addCDATAAttribute("pattern", pattern);
+ attrs.addCDATAAttribute("variant", this.variant);
+ contentHandler.startElement(Constants.INSTANCE_NS, CONVERTOR_EL,
Constants.INSTANCE_PREFIX_COLON + CONVERTOR_EL, attrs);
+ contentHandler.endElement(Constants.INSTANCE_NS, CONVERTOR_EL,
Constants.INSTANCE_PREFIX_COLON + CONVERTOR_EL);
+ }
+ }
+}
Propchange:
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertor.java
------------------------------------------------------------------------------
svn:keywords = Id
Added:
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertorBuilder.java
URL:
http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertorBuilder.java?view=auto&rev=157069
==============================================================================
---
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertorBuilder.java
(added)
+++
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertorBuilder.java
Fri Mar 11 03:00:04 2005
@@ -0,0 +1,83 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+package org.apache.cocoon.forms.datatype.convertor;
+
+import java.util.Locale;
+
+import org.apache.cocoon.forms.Constants;
+import org.apache.cocoon.forms.util.DomHelper;
+import org.apache.cocoon.i18n.I18nUtils;
+import org.w3c.dom.Element;
+
+import com.ibm.icu.text.DateFormat;
+
+/**
+ * Builds [EMAIL PROTECTED] Icu4jDateConvertor}s.
+ *
+ * @version $Id$
+ */
+public class Icu4jDateConvertorBuilder implements ConvertorBuilder {
+ public Convertor build(Element configElement) throws Exception {
+ Icu4jDateConvertor convertor = new Icu4jDateConvertor();
+
+ if (configElement == null)
+ return convertor;
+
+ String style = configElement.getAttribute("style");
+ if (!style.equals("")) {
+ if (style.equals("short"))
+ convertor.setStyle(DateFormat.SHORT);
+ else if (style.equals("medium"))
+ convertor.setStyle(DateFormat.MEDIUM);
+ else if (style.equals("long"))
+ convertor.setStyle(DateFormat.LONG);
+ else if (style.equals("full"))
+ convertor.setStyle(DateFormat.FULL);
+ else
+ throw new Exception("Invalid value \"" + style + "\" for style
attribute at " + DomHelper.getLocation(configElement));
+ }
+
+ String variant = configElement.getAttribute("variant");
+ if (!variant.equals("")) {
+ if (variant.equals(Icu4jDateConvertor.DATE) ||
+ variant.equals(Icu4jDateConvertor.TIME) ||
+ variant.equals(Icu4jDateConvertor.DATE_TIME)) {
+ convertor.setVariant(variant);
+ } else {
+ throw new Exception("Invalid value \"" + variant + "\" for
variant attribute at " + DomHelper.getLocation(configElement));
+ }
+ }
+
+ Element patternsEl = DomHelper.getChildElement(configElement,
Constants.DEFINITION_NS, "patterns", false);
+ if (patternsEl != null) {
+ Element patternEl[] = DomHelper.getChildElements(patternsEl,
Constants.DEFINITION_NS, "pattern");
+ for (int i = 0; i < patternEl.length; i++) {
+ String locale = patternEl[i].getAttribute("locale");
+ String pattern = DomHelper.getElementText(patternEl[i]);
+ if (pattern.equals(""))
+ throw new Exception("pattern element does not contain any
content at " + DomHelper.getLocation(patternEl[i]));
+ if (locale.equals(""))
+ convertor.setNonLocalizedPattern(pattern);
+ else {
+ Locale loc = I18nUtils.parseLocale(locale);
+ convertor.addFormattingPattern(loc, pattern);
+ }
+ }
+ }
+
+ return convertor;
+ }
+}
Propchange:
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertorBuilder.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/Icu4jDateConvertorBuilder.java
------------------------------------------------------------------------------
svn:keywords = Id