1 16 package org.apache.cocoon.forms.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.forms.FormsConstants; 25 import org.apache.cocoon.forms.util.I18nMessage; 26 import org.apache.cocoon.xml.AttributesImpl; 27 import org.apache.cocoon.xml.XMLUtils; 28 import org.apache.commons.jxpath.JXPathContext; 29 import org.apache.commons.jxpath.Pointer; 30 import org.apache.excalibur.xml.sax.XMLizable; 31 32 import org.xml.sax.ContentHandler ; 33 import org.xml.sax.SAXException ; 34 35 42 public class FlowJXPathSelectionList implements SelectionList { 43 44 private Context context; 45 private String listPath; 46 private String valuePath; 47 private String labelPath; 48 private Datatype datatype; 49 private Object model; 50 private boolean nullable = false; 51 private String nullText; 52 private boolean nullTextIsI18nKey = false; 53 private String i18nCatalog; 54 private boolean labelIsI18nKey = false; 55 56 public FlowJXPathSelectionList(Context context, 57 String listPath, 58 String valuePath, 59 String labelPath, 60 Datatype datatype, 61 String nullText, 62 boolean nullTextIsI18nKey, 63 String i18nCatalog, 64 boolean labelIsI18nKey) { 65 this.context = context; 66 this.listPath = listPath; 67 this.valuePath = valuePath; 68 this.labelPath = labelPath; 69 this.datatype = datatype; 70 this.nullText = nullText; 71 this.nullable = (nullText != null); 72 this.nullTextIsI18nKey = nullTextIsI18nKey; 73 this.i18nCatalog =i18nCatalog; 74 this.labelIsI18nKey = labelIsI18nKey; 75 } 76 77 87 public FlowJXPathSelectionList(Object model, String valuePath, String labelPath, Datatype datatype) { 88 this.model = model; 89 this.valuePath = valuePath; 90 this.labelPath = labelPath; 91 this.datatype = datatype; 92 } 93 94 public Datatype getDatatype() { 95 return this.datatype; 96 } 97 98 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 99 JXPathContext ctx = null; 100 Iterator iter = null; 101 if (model == null) { 102 Object flowData = FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context)); 103 if (flowData == null) { 104 throw new SAXException ("No flow data to produce selection list"); 105 } 106 107 ctx = JXPathContext.newContext(flowData); 109 110 iter = ctx.iteratePointers(this.listPath); 112 } else { 113 ctx = JXPathContext.newContext(model); 115 116 iter = ctx.iteratePointers("."); 118 } 119 120 contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES); 122 if( this.nullable ) { 123 final AttributesImpl voidAttrs = new AttributesImpl( ); 124 voidAttrs.addCDATAAttribute( "value", "" ); 125 contentHandler.startElement( FormsConstants.INSTANCE_NS, ITEM_EL, 126 FormsConstants.INSTANCE_PREFIX_COLON + 127 ITEM_EL, voidAttrs ); 128 129 if( this.nullText != null ) { 130 contentHandler.startElement( FormsConstants.INSTANCE_NS, LABEL_EL, 131 FormsConstants.INSTANCE_PREFIX_COLON + 132 LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES ); 133 134 if( this.nullTextIsI18nKey ) { 135 if( ( this.i18nCatalog != null ) && 136 ( this.i18nCatalog.trim( ).length( ) > 0 ) ) { 137 new I18nMessage( this.nullText, this.i18nCatalog ).toSAX( contentHandler ); 138 } else { 139 new I18nMessage( this.nullText ).toSAX( contentHandler ); 140 } 141 } else { 142 contentHandler.characters( this.nullText.toCharArray( ), 0, 143 this.nullText.length( ) ); 144 } 145 146 contentHandler.endElement( FormsConstants.INSTANCE_NS, LABEL_EL, 147 FormsConstants.INSTANCE_PREFIX_COLON + 148 LABEL_EL ); 149 } 150 151 contentHandler.endElement( FormsConstants.INSTANCE_NS, ITEM_EL, 152 FormsConstants.INSTANCE_PREFIX_COLON + 153 ITEM_EL ); 154 } 155 156 while(iter.hasNext()) { 157 String stringValue = ""; 158 Object label = null; 159 160 Pointer ptr = (Pointer)iter.next(); 162 if (ptr.getValue() != null) { 163 JXPathContext itemCtx = ctx.getRelativeContext(ptr); 164 165 Object value = itemCtx.getValue(this.valuePath); 167 168 if (value != null) { 171 stringValue = this.datatype.convertToString(value, locale); 172 } 173 174 itemCtx.setLenient(true); 176 label = itemCtx.getValue(this.labelPath); 177 if (label == null) { 178 label = stringValue; 179 } 180 } 181 182 AttributesImpl itemAttrs = new AttributesImpl(); 184 itemAttrs.addCDATAAttribute("value", stringValue); 185 contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, itemAttrs); 186 if (label != null) { 187 contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES); 188 if (label instanceof XMLizable) { 189 ((XMLizable)label).toSAX(contentHandler); 190 } else if( this.labelIsI18nKey ) { 191 String stringLabel = label.toString(); 192 193 if( ( this.i18nCatalog != null ) && 194 ( this.i18nCatalog.trim( ).length( ) > 0 ) ) { 195 new I18nMessage( stringLabel, this.i18nCatalog ).toSAX( contentHandler ); 196 } else { 197 new I18nMessage( stringLabel ).toSAX( contentHandler ); 198 } 199 } else { 200 String stringLabel = label.toString(); 201 contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length()); 202 } 203 contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL); 204 } 205 contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL); 206 } 207 208 contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL); 210 } 211 } 212 | Popular Tags |