01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.woody.datatype.convertor;
18:
19: import org.w3c.dom.Element;
20: import org.apache.cocoon.woody.util.DomHelper;
21: import org.apache.cocoon.woody.Constants;
22: import org.apache.cocoon.i18n.I18nUtils;
23:
24: import java.text.DateFormat;
25: import java.util.Locale;
26:
27: /**
28: * Builds {@link FormattingDateConvertor}s.
29: *
30: * @version CVS $Id: FormattingDateConvertorBuilder.java 433543 2006-08-22 06:22:54Z crossley $
31: */
32: public class FormattingDateConvertorBuilder implements ConvertorBuilder {
33: public Convertor build(Element configElement) throws Exception {
34: FormattingDateConvertor convertor = new FormattingDateConvertor();
35:
36: if (configElement == null)
37: return convertor;
38:
39: String style = configElement.getAttribute("style");
40: if (!style.equals("")) {
41: if (style.equals("short"))
42: convertor.setStyle(DateFormat.SHORT);
43: else if (style.equals("medium"))
44: convertor.setStyle(DateFormat.MEDIUM);
45: else if (style.equals("long"))
46: convertor.setStyle(DateFormat.LONG);
47: else if (style.equals("full"))
48: convertor.setStyle(DateFormat.FULL);
49: else
50: throw new Exception("Invalid value \"" + style
51: + "\" for style attribute at "
52: + DomHelper.getLocation(configElement));
53: }
54:
55: String variant = configElement.getAttribute("variant");
56: if (!variant.equals("")) {
57: if (variant.equals("date"))
58: convertor.setVariant(FormattingDateConvertor.DATE);
59: else if (variant.equals("time"))
60: convertor.setVariant(FormattingDateConvertor.TIME);
61: else if (variant.equals("datetime"))
62: convertor.setVariant(FormattingDateConvertor.DATE_TIME);
63: else
64: throw new Exception("Invalid value \"" + variant
65: + "\" for variant attribute at "
66: + DomHelper.getLocation(configElement));
67: }
68:
69: Element patternsEl = DomHelper.getChildElement(configElement,
70: Constants.WD_NS, "patterns", false);
71: if (patternsEl != null) {
72: Element patternEl[] = DomHelper.getChildElements(
73: patternsEl, Constants.WD_NS, "pattern");
74: for (int i = 0; i < patternEl.length; i++) {
75: String locale = patternEl[i].getAttribute("locale");
76: String pattern = DomHelper.getElementText(patternEl[i]);
77: if (pattern.length() == 0)
78: throw new Exception(
79: "pattern element does not contain any content at "
80: + DomHelper
81: .getLocation(patternEl[i]));
82: if (locale.length() == 0)
83: convertor.setNonLocalizedPattern(pattern);
84: else {
85: Locale loc = I18nUtils.parseLocale(locale);
86: convertor.addFormattingPattern(loc, pattern);
87: }
88: }
89: }
90:
91: return convertor;
92: }
93: }
|