1 16 package org.apache.cocoon.woody.datatype; 17 18 import java.util.Iterator ; 19 import java.util.Locale ; 20 21 import org.apache.avalon.framework.context.Context; 22 import org.apache.cocoon.components.ContextHelper; 23 import org.apache.cocoon.components.flow.FlowHelper; 24 import org.apache.cocoon.woody.Constants; 25 import org.apache.cocoon.xml.AttributesImpl; 26 import org.apache.commons.jxpath.JXPathContext; 27 import org.apache.commons.jxpath.Pointer; 28 import org.apache.excalibur.xml.sax.XMLizable; 29 30 import org.xml.sax.ContentHandler ; 31 import org.xml.sax.SAXException ; 32 33 40 public class FlowJXPathSelectionList implements SelectionList { 41 42 private Context context; 43 private String listPath; 44 private String valuePath; 45 private String labelPath; 46 private Datatype datatype; 47 private Object model; 48 49 public FlowJXPathSelectionList(Context context, String listPath, String valuePath, String labelPath, Datatype datatype) { 50 this.context = context; 51 this.listPath = listPath; 52 this.valuePath = valuePath; 53 this.labelPath = labelPath; 54 this.datatype = datatype; 55 } 56 57 67 public FlowJXPathSelectionList(Object model, String valuePath, String labelPath, Datatype datatype) { 68 this.model = model; 69 this.valuePath = valuePath; 70 this.labelPath = labelPath; 71 this.datatype = datatype; 72 } 73 74 public Datatype getDatatype() { 75 return this.datatype; 76 } 77 78 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 79 JXPathContext ctx = null; 80 Iterator iter = null; 81 if (model == null) { 82 Object flowData = FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context)); 83 if (flowData == null) { 84 throw new SAXException ("No flow data to produce selection list"); 85 } 86 87 ctx = JXPathContext.newContext(flowData); 89 90 iter = ctx.iteratePointers(this.listPath); 92 } else { 93 ctx = JXPathContext.newContext(model); 95 96 iter = ctx.iteratePointers("."); 98 } 99 100 contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS); 102 103 while(iter.hasNext()) { 104 String stringValue = ""; 105 Object label = null; 106 107 Pointer ptr = (Pointer)iter.next(); 109 if (ptr.getValue() != null) { 110 JXPathContext itemCtx = ctx.getRelativeContext(ptr); 111 112 Object value = itemCtx.getValue(this.valuePath); 114 115 if (value != null) { 118 stringValue = this.datatype.convertToString(value, locale); 119 } 120 121 itemCtx.setLenient(true); 123 label = itemCtx.getValue(this.labelPath); 124 if (label == null) { 125 label = stringValue; 126 } 127 } 128 129 AttributesImpl itemAttrs = new AttributesImpl(); 131 itemAttrs.addCDATAAttribute("value", stringValue); 132 contentHandler.startElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs); 133 if (label != null) { 134 contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL, Constants.EMPTY_ATTRS); 135 if (label instanceof XMLizable) { 136 ((XMLizable)label).toSAX(contentHandler); 137 } else { 138 String stringLabel = label.toString(); 139 contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length()); 140 } 141 contentHandler.endElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL); 142 } 143 contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL); 144 } 145 146 contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL); 148 } 149 } 150 | Popular Tags |