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 org.w3c.dom.Element;
020: import org.apache.cocoon.forms.FormsConstants;
021: import org.apache.cocoon.forms.util.DomHelper;
022: import org.apache.cocoon.i18n.I18nUtils;
023:
024: import java.text.DateFormat;
025: import java.util.Locale;
026:
027: /**
028: * Builds {@link FormattingDateConvertor}s.
029: *
030: * @version $Id: FormattingDateConvertorBuilder.java 497095 2007-01-17 17:42:59Z bruno $
031: */
032: public class FormattingDateConvertorBuilder implements ConvertorBuilder {
033: public Convertor build(Element configElement) throws Exception {
034: FormattingDateConvertor convertor = new FormattingDateConvertor();
035:
036: if (configElement == null)
037: return convertor;
038:
039: String style = configElement.getAttribute("style");
040: if (!style.equals("")) {
041: int parsedStyle = parseDateTimeStyle(style);
042: if (parsedStyle == -1)
043: throw new Exception("Invalid value \"" + style
044: + "\" for style attribute at "
045: + DomHelper.getLocation(configElement));
046: convertor.setStyle(parsedStyle);
047: }
048:
049: String timeStyle = configElement.getAttribute("timeStyle");
050: if (!timeStyle.equals("")) {
051: int parsedStyle = parseDateTimeStyle(timeStyle);
052: if (parsedStyle == -1)
053: throw new Exception("Invalid value \"" + timeStyle
054: + "\" for timeStyle attribute at "
055: + DomHelper.getLocation(configElement));
056: convertor.setTimeStyle(parsedStyle);
057: }
058:
059: String variant = configElement.getAttribute("variant");
060: if (!variant.equals("")) {
061: if (variant.equals(FormattingDateConvertor.DATE)
062: || variant.equals(FormattingDateConvertor.TIME)
063: || variant
064: .equals(FormattingDateConvertor.DATE_TIME)) {
065: convertor.setVariant(variant);
066: } else {
067: throw new Exception("Invalid value \"" + variant
068: + "\" for variant attribute at "
069: + DomHelper.getLocation(configElement));
070: }
071: }
072:
073: String lenient = configElement.getAttribute("lenient");
074: if (!lenient.equals("")) {
075: if (lenient.equals("false") || lenient.equals("no")) {
076: convertor.setLenient(false);
077: } else if (lenient.equals("true") || lenient.equals("yes")) {
078: convertor.setLenient(true);
079: } else {
080: throw new Exception("Invalid value \"" + lenient
081: + "\" for lenient attribute at "
082: + DomHelper.getLocation(configElement));
083: }
084: }
085:
086: Element patternsEl = DomHelper.getChildElement(configElement,
087: FormsConstants.DEFINITION_NS, "patterns", false);
088: if (patternsEl != null) {
089: Element patternEl[] = DomHelper
090: .getChildElements(patternsEl,
091: FormsConstants.DEFINITION_NS, "pattern");
092: for (int i = 0; i < patternEl.length; i++) {
093: String locale = patternEl[i].getAttribute("locale");
094: String pattern = DomHelper.getElementText(patternEl[i]);
095: if (pattern.length() == 0)
096: throw new Exception(
097: "pattern element does not contain any content at "
098: + DomHelper
099: .getLocation(patternEl[i]));
100: if (locale.length() == 0)
101: convertor.setNonLocalizedPattern(pattern);
102: else {
103: Locale loc = I18nUtils.parseLocale(locale);
104: convertor.addFormattingPattern(loc, pattern);
105: }
106: }
107: }
108:
109: return convertor;
110: }
111:
112: private int parseDateTimeStyle(String style) {
113: if (style.equals("short"))
114: return DateFormat.SHORT;
115: else if (style.equals("medium"))
116: return DateFormat.MEDIUM;
117: else if (style.equals("long"))
118: return DateFormat.LONG;
119: else if (style.equals("full"))
120: return DateFormat.FULL;
121: else
122: return -1;
123: }
124: }
|