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