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.forms.datatype.convertor;
18:
19: import org.w3c.dom.Element;
20: import org.apache.cocoon.forms.FormsConstants;
21: import org.apache.cocoon.forms.util.DomHelper;
22: import org.apache.cocoon.i18n.I18nUtils;
23:
24: import java.util.Locale;
25:
26: /**
27: * Builds {@link FormattingDecimalConvertor}s.
28: *
29: * @version $Id: FormattingDecimalConvertorBuilder.java 449149 2006-09-23 03:58:05Z crossley $
30: */
31: public class FormattingDecimalConvertorBuilder implements
32: ConvertorBuilder {
33: public Convertor build(Element configElement) throws Exception {
34: FormattingDecimalConvertor convertor = createConvertor();
35:
36: if (configElement == null)
37: return convertor;
38:
39: String variant = configElement.getAttribute("variant");
40: if (!variant.equals("")) {
41: if (variant.equals("integer"))
42: convertor
43: .setVariant(FormattingDecimalConvertor.INTEGER);
44: else if (variant.equals("number"))
45: convertor.setVariant(FormattingDecimalConvertor.NUMBER);
46: else if (variant.equals("percent"))
47: convertor
48: .setVariant(FormattingDecimalConvertor.PERCENT);
49: else if (variant.equals("currency"))
50: convertor
51: .setVariant(FormattingDecimalConvertor.CURRENCY);
52: else
53: throw new Exception("Invalid value \"" + variant
54: + "\" for variant attribute at "
55: + DomHelper.getLocation(configElement));
56: }
57:
58: Element patternsEl = DomHelper.getChildElement(configElement,
59: FormsConstants.DEFINITION_NS, "patterns", false);
60: if (patternsEl != null) {
61: Element patternEl[] = DomHelper
62: .getChildElements(patternsEl,
63: FormsConstants.DEFINITION_NS, "pattern");
64: for (int i = 0; i < patternEl.length; i++) {
65: String locale = patternEl[i].getAttribute("locale");
66: String pattern = DomHelper.getElementText(patternEl[i]);
67: if (pattern.length() == 0)
68: throw new Exception(
69: "pattern element does not contain any content at "
70: + DomHelper
71: .getLocation(patternEl[i]));
72: if (locale.length() == 0)
73: convertor.setNonLocalizedPattern(pattern);
74: else {
75: Locale loc = I18nUtils.parseLocale(locale);
76: convertor.addFormattingPattern(loc, pattern);
77: }
78: }
79: }
80:
81: return convertor;
82: }
83:
84: protected FormattingDecimalConvertor createConvertor() {
85: return new FormattingDecimalConvertor();
86: }
87: }
|