001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.datatype.convertor;
018:
019: import java.util.Locale;
020:
021: import org.apache.cocoon.forms.FormsConstants;
022: import org.apache.cocoon.forms.util.DomHelper;
023: import org.apache.cocoon.i18n.I18nUtils;
024: import org.w3c.dom.Element;
025:
026: import com.ibm.icu.text.DateFormat;
027:
028: /**
029: * Builds {@link Icu4jDateConvertor}s.
030: *
031: * @version $Id: Icu4jDateConvertorBuilder.java 449149 2006-09-23 03:58:05Z crossley $
032: */
033: public class Icu4jDateConvertorBuilder implements ConvertorBuilder {
034: public Convertor build(Element configElement) throws Exception {
035: Icu4jDateConvertor convertor = new Icu4jDateConvertor();
036:
037: if (configElement == null)
038: return convertor;
039:
040: String style = configElement.getAttribute("style");
041: if (!style.equals("")) {
042: if (style.equals("short"))
043: convertor.setStyle(DateFormat.SHORT);
044: else if (style.equals("medium"))
045: convertor.setStyle(DateFormat.MEDIUM);
046: else if (style.equals("long"))
047: convertor.setStyle(DateFormat.LONG);
048: else if (style.equals("full"))
049: convertor.setStyle(DateFormat.FULL);
050: else
051: throw new Exception("Invalid value \"" + style
052: + "\" for style attribute at "
053: + DomHelper.getLocation(configElement));
054: }
055:
056: String variant = configElement.getAttribute("variant");
057: if (!variant.equals("")) {
058: if (variant.equals(Icu4jDateConvertor.DATE)
059: || variant.equals(Icu4jDateConvertor.TIME)
060: || variant.equals(Icu4jDateConvertor.DATE_TIME)) {
061: convertor.setVariant(variant);
062: } else {
063: throw new Exception("Invalid value \"" + variant
064: + "\" for variant attribute at "
065: + DomHelper.getLocation(configElement));
066: }
067: }
068:
069: String lenient = configElement.getAttribute("lenient");
070: if (!lenient.equals("")) {
071: if (lenient.equals("false") || lenient.equals("no")) {
072: convertor.setLenient(false);
073: } else if (lenient.equals("true") || lenient.equals("yes")) {
074: convertor.setLenient(true);
075: } else {
076: throw new Exception("Invalid value \"" + lenient
077: + "\" for lenient attribute at "
078: + DomHelper.getLocation(configElement));
079: }
080: }
081:
082: Element patternsEl = DomHelper.getChildElement(configElement,
083: FormsConstants.DEFINITION_NS, "patterns", false);
084: if (patternsEl != null) {
085: Element patternEl[] = DomHelper
086: .getChildElements(patternsEl,
087: FormsConstants.DEFINITION_NS, "pattern");
088: for (int i = 0; i < patternEl.length; i++) {
089: String locale = patternEl[i].getAttribute("locale");
090: String pattern = DomHelper.getElementText(patternEl[i]);
091: if (pattern.length() == 0)
092: throw new Exception(
093: "pattern element does not contain any content at "
094: + DomHelper
095: .getLocation(patternEl[i]));
096: if (locale.length() == 0)
097: convertor.setNonLocalizedPattern(pattern);
098: else {
099: Locale loc = I18nUtils.parseLocale(locale);
100: convertor.addFormattingPattern(loc, pattern);
101: }
102: }
103: }
104:
105: return convertor;
106: }
107: }
|