KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Locale JavaDoc;
19
20 import org.apache.cocoon.forms.FormsConstants;
21 import org.apache.cocoon.forms.util.DomHelper;
22 import org.apache.cocoon.i18n.I18nUtils;
23 import org.w3c.dom.Element JavaDoc;
24
25 import com.ibm.icu.text.DateFormat;
26
27 /**
28  * Builds {@link Icu4jDateConvertor}s.
29  *
30  * @version $Id: Icu4jDateConvertorBuilder.java 326838 2005-10-20 06:26:53Z sylvain $
31  */

32 public class Icu4jDateConvertorBuilder implements ConvertorBuilder {
33     public Convertor build(Element JavaDoc configElement) throws Exception JavaDoc {
34         Icu4jDateConvertor convertor = new Icu4jDateConvertor();
35
36         if (configElement == null)
37             return convertor;
38
39         String JavaDoc 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 JavaDoc("Invalid value \"" + style + "\" for style attribute at " + DomHelper.getLocation(configElement));
51         }
52
53         String JavaDoc variant = configElement.getAttribute("variant");
54         if (!variant.equals("")) {
55             if (variant.equals(Icu4jDateConvertor.DATE) ||
56                     variant.equals(Icu4jDateConvertor.TIME) ||
57                     variant.equals(Icu4jDateConvertor.DATE_TIME)) {
58                 convertor.setVariant(variant);
59             } else {
60                 throw new Exception JavaDoc("Invalid value \"" + variant + "\" for variant attribute at " + DomHelper.getLocation(configElement));
61             }
62         }
63
64         Element JavaDoc patternsEl = DomHelper.getChildElement(configElement, FormsConstants.DEFINITION_NS, "patterns", false);
65         if (patternsEl != null) {
66             Element JavaDoc patternEl[] = DomHelper.getChildElements(patternsEl, FormsConstants.DEFINITION_NS, "pattern");
67             for (int i = 0; i < patternEl.length; i++) {
68                 String JavaDoc locale = patternEl[i].getAttribute("locale");
69                 String JavaDoc pattern = DomHelper.getElementText(patternEl[i]);
70                 if (pattern.equals(""))
71                     throw new Exception JavaDoc("pattern element does not contain any content at " + DomHelper.getLocation(patternEl[i]));
72                 if (locale.equals(""))
73                     convertor.setNonLocalizedPattern(pattern);
74                 else {
75                     Locale JavaDoc loc = I18nUtils.parseLocale(locale);
76                     convertor.addFormattingPattern(loc, pattern);
77                 }
78             }
79         }
80
81         return convertor;
82     }
83 }
84
Popular Tags