1 16 package org.apache.cocoon.woody.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.service.ServiceException; 23 import org.apache.avalon.framework.service.ServiceManager; 24 import org.apache.avalon.framework.service.Serviceable; 25 import org.apache.cocoon.woody.Constants; 26 import org.apache.cocoon.woody.datatype.convertor.Convertor; 27 import org.apache.cocoon.woody.datatype.convertor.DefaultFormatCache; 28 import org.apache.cocoon.woody.util.DomHelper; 29 import org.apache.excalibur.source.Source; 30 import org.apache.excalibur.source.SourceResolver; 31 import org.apache.excalibur.xml.sax.XMLizable; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 35 import java.util.Locale ; 36 37 45 public class DefaultSelectionListBuilder implements SelectionListBuilder, Serviceable { 46 47 private ServiceManager serviceManager; 48 49 public void service(ServiceManager manager) throws ServiceException { 50 this.serviceManager = manager; 51 } 52 53 public SelectionList build(Element selectionListElement, Datatype datatype) throws Exception { 54 SelectionList selectionList; 55 String src = selectionListElement.getAttribute("src"); 56 if (src.length() > 0) { 57 boolean dynamic = DomHelper.getAttributeAsBoolean(selectionListElement, "dynamic", false); 58 if (!dynamic) { 59 selectionListElement = readSelectionList(src); 60 selectionList = buildStaticList(selectionListElement, datatype); 61 } else { 62 selectionList = new DynamicSelectionList(datatype, src, serviceManager); 63 } 64 } else { 65 selectionList = buildStaticList(selectionListElement, datatype); 67 } 68 69 return selectionList; 70 } 71 72 private SelectionList buildStaticList(Element selectionListElement, Datatype datatype) throws Exception { 73 StaticSelectionList selectionList = new StaticSelectionList(datatype); 74 Convertor convertor = null; 75 Convertor.FormatCache formatCache = new DefaultFormatCache(); 76 77 NodeList children = selectionListElement.getChildNodes(); 78 for (int i = 0; children.item(i) != null; i++) { 79 Node node = children.item(i); 80 if (convertor == null && node instanceof Element && Constants.WD_NS.equals(node.getNamespaceURI()) && "convertor".equals(node.getLocalName())) { 81 Element convertorConfigElement = (Element )node; 82 try { 83 convertor = datatype.getBuilder().buildConvertor(convertorConfigElement); 84 } catch (Exception e) { 85 throw new SAXException ("Error building convertor from convertor configuration embedded in selection list XML.", e); 86 } 87 } else if (node instanceof Element && Constants.WD_NS.equals(node.getNamespaceURI()) && "item".equals(node.getLocalName())) { 88 if (convertor == null) { 89 convertor = datatype.getConvertor(); 90 } 91 Element element = (Element )node; 92 String stringValue = element.getAttribute("value"); 93 Object value; 94 if ("".equals(stringValue)) { 95 value = null; 97 } else { 98 value = convertor.convertFromString(stringValue, Locale.US, formatCache); 99 if (value == null) { 100 throw new Exception ("Could not convert the value \"" + stringValue + 101 "\" to the type " + datatype.getDescriptiveName() + 102 ", defined at " + DomHelper.getLocation(element)); 103 } 104 } 105 106 XMLizable label = null; 107 Element labelEl = DomHelper.getChildElement(element, Constants.WD_NS, "label"); 108 if (labelEl != null) { 109 label = DomHelper.compileElementContent(labelEl); 110 } 111 selectionList.addItem(value, label); 112 } 113 } 114 115 return selectionList; 116 } 117 118 private Element readSelectionList(String src) throws Exception { 119 SourceResolver resolver = null; 120 Source source = null; 121 try { 122 resolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE); 123 source = resolver.resolveURI(src); 124 InputSource inputSource = new InputSource (source.getInputStream()); 125 inputSource.setSystemId(source.getURI()); 126 Document document = DomHelper.parse(inputSource); 127 Element selectionListElement = document.getDocumentElement(); 128 if (!Constants.WD_NS.equals(selectionListElement.getNamespaceURI()) || !"selection-list".equals(selectionListElement.getLocalName())) 129 throw new Exception ("Excepted a wd:selection-list element at " + DomHelper.getLocation(selectionListElement)); 130 return selectionListElement; 131 } finally { 132 if (source != null) 133 resolver.release(source); 134 if (resolver != null) 135 serviceManager.release(resolver); 136 } 137 } 138 } 139 | Popular Tags |