KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > datatype > convertor > FormattingDecimalConvertorBuilder


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.util.Locale JavaDoc;
24
25 /**
26  * Builds {@link FormattingDecimalConvertor}s.
27  *
28  * @version CVS $Id: FormattingDecimalConvertorBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
29  */

30 public class FormattingDecimalConvertorBuilder implements ConvertorBuilder {
31     public Convertor build(Element JavaDoc configElement) throws Exception JavaDoc {
32         FormattingDecimalConvertor convertor = createConvertor();
33
34         if (configElement == null)
35             return convertor;
36
37         String JavaDoc variant = configElement.getAttribute("variant");
38         if (!variant.equals("")) {
39             if (variant.equals("integer"))
40                 convertor.setVariant(FormattingDecimalConvertor.INTEGER);
41             else if (variant.equals("number"))
42                 convertor.setVariant(FormattingDecimalConvertor.NUMBER);
43             else if (variant.equals("percent"))
44                 convertor.setVariant(FormattingDecimalConvertor.PERCENT);
45             else if (variant.equals("currency"))
46                 convertor.setVariant(FormattingDecimalConvertor.CURRENCY);
47             else
48                 throw new Exception JavaDoc("Invalid value \"" + variant + "\" for variant attribute at " + DomHelper.getLocation(configElement));
49         }
50
51         Element JavaDoc patternsEl = DomHelper.getChildElement(configElement, Constants.WD_NS, "patterns", false);
52         if (patternsEl != null) {
53             Element JavaDoc patternEl[] = DomHelper.getChildElements(patternsEl, Constants.WD_NS, "pattern");
54             for (int i = 0; i < patternEl.length; i++) {
55                 String JavaDoc locale = patternEl[i].getAttribute("locale");
56                 String JavaDoc pattern = DomHelper.getElementText(patternEl[i]);
57                 if (pattern.equals(""))
58                     throw new Exception JavaDoc("pattern element does not contain any content at " + DomHelper.getLocation(patternEl[i]));
59                 if (locale.equals(""))
60                     convertor.setNonLocalizedPattern(pattern);
61                 else {
62                     Locale JavaDoc loc = I18nUtils.parseLocale(locale);
63                     convertor.addFormattingPattern(loc, pattern);
64                 }
65             }
66         }
67
68         return convertor;
69     }
70
71     protected FormattingDecimalConvertor createConvertor() {
72         return new FormattingDecimalConvertor();
73     }
74 }
75
Popular Tags