1 16 package org.apache.cocoon.forms.datatype; 17 18 import org.w3c.dom.Document ; 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.NodeList ; 21 import org.w3c.dom.Node ; 22 import org.apache.avalon.framework.context.Context; 23 import org.apache.avalon.framework.context.ContextException; 24 import org.apache.avalon.framework.context.Contextualizable; 25 import org.apache.avalon.framework.service.ServiceException; 26 import org.apache.avalon.framework.service.ServiceManager; 27 import org.apache.avalon.framework.service.Serviceable; 28 import org.apache.cocoon.forms.FormsConstants; 29 import org.apache.cocoon.forms.datatype.convertor.Convertor; 30 import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache; 31 import org.apache.cocoon.forms.datatype.convertor.ConversionResult; 32 import org.apache.cocoon.forms.util.DomHelper; 33 import org.apache.cocoon.util.Deprecation; 34 import org.apache.cocoon.util.location.LocationAttributes; 35 import org.apache.excalibur.source.Source; 36 import org.apache.excalibur.source.SourceResolver; 37 import org.apache.excalibur.xml.sax.XMLizable; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.SAXException ; 40 41 import java.util.Locale ; 42 43 53 public class DefaultSelectionListBuilder implements SelectionListBuilder, Serviceable, Contextualizable { 54 55 private ServiceManager serviceManager; 56 private Context context; 57 58 public void contextualize(Context context) throws ContextException { 59 this.context = context; 60 } 61 62 public void service(ServiceManager manager) throws ServiceException { 63 this.serviceManager = manager; 64 } 65 66 public SelectionList build(Element selectionListElement, Datatype datatype) throws Exception { 67 SelectionList selectionList; 68 String src = selectionListElement.getAttribute("src"); 69 if (src.length() > 0) { 70 boolean dynamic = false; 71 boolean usePerRequestCache = false; 72 String cacheType = DomHelper.getAttribute(selectionListElement, "cache", null); 73 74 if ("request".equals(cacheType)) { dynamic = true; 77 usePerRequestCache = true; 78 } else if ("none".equals(cacheType)){ dynamic = true; 80 } else if ("static".equals(cacheType)) { 81 } else { if (DomHelper.getAttribute(selectionListElement, "dynamic", null) != null) { 84 Deprecation.logger.warn("'@dynamic' is deprecated in <fd:selection-list> and replaced by '@cache' at " + DomHelper.getLocation(selectionListElement)); 85 } 86 dynamic = DomHelper.getAttributeAsBoolean(selectionListElement, "dynamic", false); 87 } 88 if (dynamic) { 90 selectionList = new DynamicSelectionList(datatype, src, usePerRequestCache, serviceManager, context); 91 } else { 92 selectionListElement = readSelectionList(src); 93 selectionList = buildStaticList(selectionListElement, datatype); 94 } 95 } else { 96 selectionList = buildStaticList(selectionListElement, datatype); 98 } 99 return selectionList; 100 } 101 102 private SelectionList buildStaticList(Element selectionListElement, Datatype datatype) throws Exception { 103 StaticSelectionList selectionList = new StaticSelectionList(datatype); 104 Convertor convertor = null; 105 Convertor.FormatCache formatCache = new DefaultFormatCache(); 106 107 LocationAttributes.remove(selectionListElement, true); 109 110 NodeList children = selectionListElement.getChildNodes(); 111 for (int i = 0; children.item(i) != null; i++) { 112 Node node = children.item(i); 113 if (convertor == null && node instanceof Element && FormsConstants.DEFINITION_NS.equals(node.getNamespaceURI()) && "convertor".equals(node.getLocalName())) { 114 Element convertorConfigElement = (Element )node; 115 try { 116 convertor = datatype.getBuilder().buildConvertor(convertorConfigElement); 117 } catch (Exception e) { 118 throw new SAXException ("Error building convertor from convertor configuration embedded in selection list XML.", e); 119 } 120 } else if (node instanceof Element && FormsConstants.DEFINITION_NS.equals(node.getNamespaceURI()) && "item".equals(node.getLocalName())) { 121 if (convertor == null) { 122 convertor = datatype.getConvertor(); 123 } 124 Element element = (Element )node; 125 String stringValue = element.getAttribute("value"); 126 Object value; 127 if ("".equals(stringValue)) { 128 value = null; 130 } else { 131 ConversionResult conversionResult = convertor.convertFromString(stringValue, Locale.US, formatCache); 132 if (!conversionResult.isSuccessful()) { 133 throw new Exception ("Could not convert the value \"" + stringValue + 134 "\" to the type " + datatype.getDescriptiveName() + 135 ", defined at " + DomHelper.getLocation(element)); 136 } 137 value = conversionResult.getResult(); 138 } 139 140 XMLizable label = null; 141 Element labelEl = DomHelper.getChildElement(element, FormsConstants.DEFINITION_NS, "label"); 142 if (labelEl != null) { 143 label = DomHelper.compileElementContent(labelEl); 144 } 145 selectionList.addItem(value, label); 146 } 147 } 148 149 return selectionList; 150 } 151 152 private Element readSelectionList(String src) throws Exception { 153 SourceResolver resolver = null; 154 Source source = null; 155 try { 156 resolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE); 157 source = resolver.resolveURI(src); 158 InputSource inputSource = new InputSource (source.getInputStream()); 159 inputSource.setSystemId(source.getURI()); 160 Document document = DomHelper.parse(inputSource, this.serviceManager); 161 Element selectionListElement = document.getDocumentElement(); 162 if (!FormsConstants.DEFINITION_NS.equals(selectionListElement.getNamespaceURI()) || 163 !"selection-list".equals(selectionListElement.getLocalName())) { 164 throw new Exception ("Expected a fd:selection-list element at " + 165 DomHelper.getLocation(selectionListElement)); 166 } 167 168 return selectionListElement; 169 } finally { 170 if (resolver != null) { 171 if (source != null) { 172 resolver.release(source); 173 } 174 serviceManager.release(resolver); 175 } 176 } 177 } 178 } 179 | Popular Tags |