1 16 package org.apache.cocoon.forms.binding; 17 18 import org.apache.avalon.framework.service.ServiceManager; 19 import org.apache.cocoon.components.source.SourceUtil; 20 import org.apache.cocoon.forms.util.DomHelper; 21 import org.apache.cocoon.util.location.LocationAttributes; 22 import org.apache.excalibur.source.Source; 23 import org.apache.excalibur.source.SourceResolver; 24 import org.apache.excalibur.xml.xpath.XPathProcessor; 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.DocumentFragment ; 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.Node ; 29 import org.w3c.dom.NodeList ; 30 31 45 public class InsertNodeJXPathBindingBuilder 46 extends JXPathBindingBuilderBase { 47 48 52 public JXPathBindingBase buildBinding( 53 Element bindingElm, 54 JXPathBindingManager.Assistant assistant) throws BindingException { 55 56 try { 57 CommonAttributes commonAtts = JXPathBindingBuilderBase.getCommonAttributes(bindingElm); 58 59 DocumentFragment domTemplate = null; 60 61 String src = DomHelper.getAttribute(bindingElm, "src", null); 62 if (src != null) { 63 ServiceManager manager = assistant.getServiceManager(); 64 SourceResolver sourceResolver = (SourceResolver)manager.lookup(SourceResolver.ROLE); 65 Source source = null; 66 try { 67 source = sourceResolver.resolveURI(src); 68 Document document = SourceUtil.toDOM(source); 69 Element element = document.getDocumentElement(); 70 71 String xpath = DomHelper.getAttribute(bindingElm, "xpath", null); 72 if (xpath != null) { 73 XPathProcessor xpathProcessor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE); 74 try { 75 Node node = xpathProcessor.selectSingleNode(document, xpath); 76 if (node == null) 77 throw new BindingException("XPath expression \"" + xpath + "\" didn't return a result."); 78 if (!(node instanceof Element )) 79 throw new BindingException("XPath expression \"" + xpath + "\" did not return an element node."); 80 element = (Element )node; 81 } finally { 82 manager.release(xpathProcessor); 83 } 84 } 85 domTemplate = document.createDocumentFragment(); 86 domTemplate.appendChild(element); 87 } finally { 88 if (source != null) { 89 sourceResolver.release(source); 90 } 91 manager.release(sourceResolver); 92 } 93 } else if(bindingElm.hasChildNodes()) { 94 domTemplate = bindingElm.getOwnerDocument().createDocumentFragment(); 97 NodeList nested = bindingElm.getChildNodes(); 98 int size = nested.getLength(); 99 for (int i = 0; i < size; i++) { 100 Node node = nested.item(i).cloneNode(true); 101 if (node.getNodeType() == Node.ELEMENT_NODE) { 102 LocationAttributes.remove((Element )node, true); 103 } 104 domTemplate.appendChild(node); 105 } 106 } 107 108 InsertNodeJXPathBinding otherBinding = (InsertNodeJXPathBinding)assistant.getContext().getSuperBinding(); 110 if(otherBinding!=null) { 111 commonAtts = JXPathBindingBuilderBase.mergeCommonAttributes(otherBinding.getCommonAtts(),commonAtts); 112 113 if(domTemplate==null) 114 domTemplate = otherBinding.getTemplate(); 115 } 116 117 return new InsertNodeJXPathBinding(commonAtts, domTemplate); 118 } catch (Exception e) { 119 throw new BindingException("Error building the insert-node binding defined at " + DomHelper.getLocation(bindingElm), e); 120 } 121 } 122 } 123 | Popular Tags |