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