1 16 package org.apache.cocoon.woody.datatype; 17 18 import java.lang.reflect.Field ; 19 import java.lang.reflect.Modifier ; 20 import java.util.Locale ; 21 22 import org.apache.cocoon.woody.Constants; 23 import org.apache.cocoon.xml.AttributesImpl; 24 import org.xml.sax.ContentHandler ; 25 import org.xml.sax.SAXException ; 26 27 55 public class EnumSelectionList implements SelectionList { 56 public static final String I18N_NS = "http://apache.org/cocoon/i18n/2.1"; 57 public static final String I18N_PREFIX_COLON = "i18n:"; 58 public static final String TEXT_EL = "text"; 59 60 private Datatype datatype; 61 private Class clazz; 62 private boolean nullable; 63 64 68 public EnumSelectionList(String className, Datatype datatype, boolean nullable) throws ClassNotFoundException { 69 this.datatype = datatype; 70 this.nullable = nullable; 71 this.clazz = Class.forName(className); 72 } 73 74 77 public Datatype getDatatype() { 78 return datatype; 79 } 80 81 84 public void generateSaxFragment( 85 ContentHandler contentHandler, 86 Locale locale) 87 throws SAXException { 88 try { 89 contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS); 90 Field fields[] = clazz.getDeclaredFields(); 91 if (nullable) { 93 AttributesImpl voidAttrs = new AttributesImpl(); 94 voidAttrs.addCDATAAttribute("value", ""); 95 contentHandler.startElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, voidAttrs); 96 contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL); 97 } 98 for (int i = 0 ; i < fields.length ; ++i) { 99 int mods = fields[i].getModifiers(); 100 if (Modifier.isPublic(mods) && Modifier.isStatic(mods) 101 && Modifier.isFinal(mods) && fields[i].get(null).getClass().equals(clazz)) { 102 String stringValue = clazz.getName() + "." + fields[i].getName(); 103 AttributesImpl itemAttrs = new AttributesImpl(); 105 itemAttrs.addCDATAAttribute("value", stringValue); 106 contentHandler.startElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs); 107 contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL, Constants.EMPTY_ATTRS); 108 contentHandler.startElement(I18N_NS, TEXT_EL, I18N_PREFIX_COLON + TEXT_EL, Constants.EMPTY_ATTRS); 110 contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length()); 111 contentHandler.endElement(I18N_NS, TEXT_EL, I18N_PREFIX_COLON + TEXT_EL); 112 contentHandler.endElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL); 113 contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL); 114 } 115 } 116 contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL); 118 } catch (Exception e) { 119 throw new SAXException ("Got exception trying to get enum's values", e); 120 } 121 } 122 123 } 124 | Popular Tags |