1 16 package org.apache.cocoon.forms.datatype; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Locale ; 23 24 import org.apache.avalon.framework.service.ServiceException; 25 import org.apache.avalon.framework.service.ServiceManager; 26 import org.apache.avalon.framework.service.Serviceable; 27 import org.apache.cocoon.forms.FormsConstants; 28 import org.apache.cocoon.forms.datatype.convertor.Convertor; 29 import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache; 30 import org.apache.cocoon.xml.AttributesImpl; 31 import org.apache.cocoon.xml.XMLUtils; 32 import org.xml.sax.ContentHandler ; 33 import org.xml.sax.SAXException ; 34 import org.apache.excalibur.xml.sax.XMLizable; 35 36 39 public abstract class AbstractJavaSelectionList implements JavaSelectionList, 40 Serviceable { 41 42 protected Datatype datatype; 43 protected ServiceManager manager; 44 45 private HashMap attributes; 46 private List items; 47 private boolean nullable; 48 private boolean rebuild; 49 50 public AbstractJavaSelectionList() { 51 this.items = new ArrayList (); 52 this.rebuild = true; 53 } 54 55 public void service(ServiceManager manager) throws ServiceException { 56 this.manager = manager; 57 } 58 59 public String getAttribute(String name) { 60 if (this.attributes == null) { 61 return null; 62 } 63 return (String ) this.attributes.get(name); 64 } 65 66 public void removeAttribute(String name) { 67 if (this.attributes != null) { 68 this.attributes.remove(name); 69 } 70 } 71 72 public void setAttribute(String name, String value) { 73 if (this.attributes == null) { 74 this.attributes = new HashMap (); 75 } 76 this.attributes.put(name, value); 77 } 78 79 public boolean isNullable() { 80 return this.nullable; 81 } 82 83 public void setDatatype(Datatype datatype) { 84 this.datatype = datatype; 85 } 86 87 public void setNullable(boolean nullable) { 88 this.nullable = nullable; 89 } 90 91 public Datatype getDatatype() { 92 return this.datatype; 93 } 94 95 98 public void markForRebuild() { 99 this.rebuild = true; 100 } 101 102 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) 103 throws SAXException { 104 if (this.rebuild) { 105 try { 106 this.items.clear(); 107 this.rebuild = build(); 108 } catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } 112 113 Convertor.FormatCache formatCache = new DefaultFormatCache(); 114 contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES); 115 116 if (this.nullable) { 117 AttributesImpl voidAttrs = new AttributesImpl(); 118 voidAttrs.addCDATAAttribute("value", ""); 119 contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, voidAttrs); 120 contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL); 121 } 122 123 Iterator itemIt = items.iterator(); 124 while (itemIt.hasNext()) { 125 SelectionListItem item = (SelectionListItem) itemIt.next(); 126 item.generateSaxFragment(contentHandler, locale, formatCache); 127 } 128 129 contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL); 130 } 131 132 137 protected abstract boolean build() throws Exception ; 138 139 148 protected void addItem(Object value, String label) { 149 this.items.add(new SelectionListItem(value, label)); 150 } 151 152 162 protected void addItem(Object value, XMLizable label) { 163 this.items.add(new SelectionListItem(value, label)); 164 } 165 166 protected List getItems() { 167 return this.items; 168 } 169 170 private final class SelectionListItem { 171 private final Object value; 172 173 private final Object label; 174 175 public SelectionListItem(Object value, String label) { 176 this.value = value; 177 this.label = label; 178 } 179 180 public SelectionListItem(Object value, XMLizable label) { 181 this.value = value; 182 this.label = label; 183 } 184 185 public Object getValue() { 186 return value; 187 } 188 189 public void generateSaxFragment(ContentHandler contentHandler, 190 Locale locale, Convertor.FormatCache formatCache) 191 throws SAXException { 192 String stringValue; 193 if (this.value == null) { 194 stringValue = ""; 195 } else { 196 stringValue = datatype.getConvertor().convertToString(this.value, locale, formatCache); 197 } 198 199 AttributesImpl attrs = new AttributesImpl(); 200 attrs.addCDATAAttribute("value", stringValue); 201 contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, attrs); 202 contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES); 203 if (this.label == null) { 204 contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length()); 205 } else if (this.label instanceof XMLizable) { 206 ((XMLizable) this.label).toSAX(contentHandler); 207 } else { 208 String stringLabel = (String ) this.label; 209 contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length()); 210 } 211 contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL); 212 contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL); 213 } 214 } 215 } 216 | Popular Tags |