1 16 package org.apache.cocoon.forms.datatype.convertor; 17 18 import org.w3c.dom.Element ; 19 import org.apache.cocoon.forms.FormsConstants; 20 import org.apache.cocoon.forms.util.DomHelper; 21 import org.apache.cocoon.i18n.I18nUtils; 22 23 import java.text.DateFormat ; 24 import java.util.Locale ; 25 26 31 public class FormattingDateConvertorBuilder implements ConvertorBuilder { 32 public Convertor build(Element configElement) throws Exception { 33 FormattingDateConvertor convertor = new FormattingDateConvertor(); 34 35 if (configElement == null) 36 return convertor; 37 38 String 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 ("Invalid value \"" + style + "\" for style attribute at " + DomHelper.getLocation(configElement)); 50 } 51 52 String variant = configElement.getAttribute("variant"); 53 if (!variant.equals("")) { 54 if (variant.equals(FormattingDateConvertor.DATE) || 55 variant.equals(FormattingDateConvertor.TIME) || 56 variant.equals(FormattingDateConvertor.DATE_TIME)) { 57 convertor.setVariant(variant); 58 } else { 59 throw new Exception ("Invalid value \"" + variant + "\" for variant attribute at " + DomHelper.getLocation(configElement)); 60 } 61 } 62 63 Element patternsEl = DomHelper.getChildElement(configElement, FormsConstants.DEFINITION_NS, "patterns", false); 64 if (patternsEl != null) { 65 Element patternEl[] = DomHelper.getChildElements(patternsEl, FormsConstants.DEFINITION_NS, "pattern"); 66 for (int i = 0; i < patternEl.length; i++) { 67 String locale = patternEl[i].getAttribute("locale"); 68 String pattern = DomHelper.getElementText(patternEl[i]); 69 if (pattern.equals("")) 70 throw new Exception ("pattern element does not contain any content at " + DomHelper.getLocation(patternEl[i])); 71 if (locale.equals("")) 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 | Popular Tags |