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.woody.datatype.convertor;
18:
19: import org.w3c.dom.Element;
20: import org.apache.cocoon.woody.util.DomHelper;
21: import org.apache.cocoon.woody.Constants;
22: import org.apache.cocoon.i18n.I18nUtils;
23:
24: import java.util.Locale;
25:
26: /**
27: * Builds {@link FormattingDecimalConvertor}s.
28: *
29: * @version CVS $Id: FormattingFloatConvertorBuilder.java 433543 2006-08-22 06:22:54Z crossley $
30: */
31: public class FormattingFloatConvertorBuilder 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: Constants.WD_NS, "patterns", false);
60: if (patternsEl != null) {
61: Element patternEl[] = DomHelper.getChildElements(
62: patternsEl, Constants.WD_NS, "pattern");
63: for (int i = 0; i < patternEl.length; i++) {
64: String locale = patternEl[i].getAttribute("locale");
65: String pattern = DomHelper.getElementText(patternEl[i]);
66: if (pattern.length() == 0)
67: throw new Exception(
68: "pattern element does not contain any content at "
69: + DomHelper
70: .getLocation(patternEl[i]));
71: if (locale.length() == 0)
72: convertor.setNonLocalizedPattern(pattern);
73: else {
74: Locale loc = I18nUtils.parseLocale(locale);
75: convertor.addFormattingPattern(loc, pattern);
76: }
77: }
78: }
79:
80: return convertor;
81: }
82:
83: protected FormattingDecimalConvertor createConvertor() {
84: return new FormattingDecimalConvertor();
85: }
86: }
|