KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > convertor > FormattingDateConvertorBuilder


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.datatype.convertor;
17
18 import org.w3c.dom.Element JavaDoc;
19 import org.apache.cocoon.forms.FormsConstants;
20 import org.apache.cocoon.forms.util.DomHelper;
21 import org.apache.cocoon.i18n.I18nUtils;
22
23 import java.text.DateFormat JavaDoc;
24 import java.util.Locale JavaDoc;
25
26 /**
27  * Builds {@link FormattingDateConvertor}s.
28  *
29  * @version $Id: FormattingDateConvertorBuilder.java 326838 2005-10-20 06:26:53Z sylvain $
30  */

31 public class FormattingDateConvertorBuilder implements ConvertorBuilder {
32     public Convertor build(Element JavaDoc configElement) throws Exception JavaDoc {
33         FormattingDateConvertor convertor = new FormattingDateConvertor();
34
35         if (configElement == null)
36             return convertor;
37
38         String JavaDoc style = configElement.getAttribute("style");
39         if (!style.equals("")) {
40             if (style.equals("short"))
41                 convertor.setStyle(DateFormat.SHORT);
42             else if (style.equals("medium"))
43                 convertor.setStyle(DateFormat.MEDIUM);
44             else if (style.equals("long"))
45                 convertor.setStyle(DateFormat.LONG);
46             else if (style.equals("full"))
47                 convertor.setStyle(DateFormat.FULL);
48             else
49                 throw new Exception JavaDoc("Invalid value \"" + style + "\" for style attribute at " + DomHelper.getLocation(configElement));
50         }
51
52         String JavaDoc variant = configElement.getAttribute("variant");
53         if (!variant.equals("")) {
54             if (variant.equals(FormattingDateConvertor.DATE) ||
55                     variant.equals(FormattingDateConvertor.TIME) ||
56                     variant.equals(FormattingDateConvertor.DATE_TIME)) {
57                 convertor.setVariant(variant);
58             } else {
59                 throw new Exception JavaDoc("Invalid value \"" + variant + "\" for variant attribute at " + DomHelper.getLocation(configElement));
60             }
61         }
62
63         Element JavaDoc patternsEl = DomHelper.getChildElement(configElement, FormsConstants.DEFINITION_NS, "patterns", false);
64         if (patternsEl != null) {
65             Element JavaDoc patternEl[] = DomHelper.getChildElements(patternsEl, FormsConstants.DEFINITION_NS, "pattern");
66             for (int i = 0; i < patternEl.length; i++) {
67                 String JavaDoc locale = patternEl[i].getAttribute("locale");
68                 String JavaDoc pattern = DomHelper.getElementText(patternEl[i]);
69                 if (pattern.equals(""))
70                     throw new Exception JavaDoc("pattern element does not contain any content at " + DomHelper.getLocation(patternEl[i]));
71                 if (locale.equals(""))
72                     convertor.setNonLocalizedPattern(pattern);
73                 else {
74                     Locale JavaDoc loc = I18nUtils.parseLocale(locale);
75                     convertor.addFormattingPattern(loc, pattern);
76                 }
77             }
78         }
79
80         return convertor;
81     }
82 }
83
Popular Tags