KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.datatype.convertor;
17
18 import org.w3c.dom.Element JavaDoc;
19 import org.apache.cocoon.woody.util.DomHelper;
20 import org.apache.cocoon.woody.Constants;
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 CVS $Id: FormattingDateConvertorBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
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("date"))
55                 convertor.setVariant(FormattingDateConvertor.DATE);
56             else if (variant.equals("time"))
57                 convertor.setVariant(FormattingDateConvertor.TIME);
58             else if (variant.equals("datetime"))
59                 convertor.setVariant(FormattingDateConvertor.DATE_TIME);
60             else
61                 throw new Exception JavaDoc("Invalid value \"" + variant + "\" for variant attribute at " + DomHelper.getLocation(configElement));
62         }
63
64         Element JavaDoc patternsEl = DomHelper.getChildElement(configElement, Constants.WD_NS, "patterns", false);
65         if (patternsEl != null) {
66             Element JavaDoc patternEl[] = DomHelper.getChildElements(patternsEl, Constants.WD_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