1 16 package org.apache.cocoon.forms.datatype; 17 18 import org.xml.sax.ContentHandler ; 19 import org.xml.sax.SAXException ; 20 import org.apache.cocoon.forms.FormsConstants; 21 import org.apache.cocoon.forms.datatype.convertor.Convertor; 22 import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache; 23 import org.apache.cocoon.xml.AttributesImpl; 24 import org.apache.cocoon.xml.XMLUtils; 25 import org.apache.excalibur.xml.sax.XMLizable; 26 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.Locale ; 31 32 41 public class StaticSelectionList implements SelectionList { 42 45 private Datatype datatype; 46 47 50 private List items; 51 52 public StaticSelectionList(Datatype datatype) { 53 this.datatype = datatype; 54 this.items = new ArrayList (); 55 } 56 57 public Datatype getDatatype() { 58 return datatype; 59 } 60 61 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 62 Convertor.FormatCache formatCache = new DefaultFormatCache(); 63 contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES); 64 65 Iterator itemIt = this.items.iterator(); 66 while (itemIt.hasNext()) { 67 final SelectionListItem item = (SelectionListItem) itemIt.next(); 68 item.generateSaxFragment(contentHandler, locale, formatCache); 69 } 70 71 contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL); 72 } 73 74 public List getItems() { 75 return this.items; 76 } 77 78 83 public void addItem(Object value, XMLizable label) { 84 this.items.add(new SelectionListItem(value, label)); 85 } 86 87 public final class SelectionListItem { 88 private final Object value; 89 private final XMLizable label; 90 91 public SelectionListItem(Object value, XMLizable label) { 92 this.value = value; 93 this.label = label; 94 } 95 96 public Object getValue() { 97 return value; 98 } 99 100 public void generateSaxFragment(ContentHandler contentHandler, Locale locale, Convertor.FormatCache formatCache) 101 throws SAXException { 102 String stringValue; 103 if (this.value != null) { 104 stringValue = datatype.getConvertor().convertToString(this.value, locale, formatCache); 105 } else { 106 stringValue = ""; 108 } 109 110 AttributesImpl attrs = new AttributesImpl(); 111 attrs.addCDATAAttribute("value", stringValue); 112 contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, attrs); 113 contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES); 114 if (this.label != null) { 115 this.label.toSAX(contentHandler); 116 } else { 117 contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length()); 118 } 119 contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL); 120 contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL); 121 } 122 } 123 } 124 | Popular Tags |