1 16 package org.apache.cocoon.woody.datatype.convertor; 17 18 import org.w3c.dom.Element ; 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 ; 24 25 30 public class FormattingDecimalConvertorBuilder implements ConvertorBuilder { 31 public Convertor build(Element configElement) throws Exception { 32 FormattingDecimalConvertor convertor = createConvertor(); 33 34 if (configElement == null) 35 return convertor; 36 37 String 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 ("Invalid value \"" + variant + "\" for variant attribute at " + DomHelper.getLocation(configElement)); 49 } 50 51 Element patternsEl = DomHelper.getChildElement(configElement, Constants.WD_NS, "patterns", false); 52 if (patternsEl != null) { 53 Element patternEl[] = DomHelper.getChildElements(patternsEl, Constants.WD_NS, "pattern"); 54 for (int i = 0; i < patternEl.length; i++) { 55 String locale = patternEl[i].getAttribute("locale"); 56 String pattern = DomHelper.getElementText(patternEl[i]); 57 if (pattern.equals("")) 58 throw new Exception ("pattern element does not contain any content at " + DomHelper.getLocation(patternEl[i])); 59 if (locale.equals("")) 60 convertor.setNonLocalizedPattern(pattern); 61 else { 62 Locale 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 |