KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > binding > InsertNodeJXPathBindingBuilder


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
26 import org.w3c.dom.DocumentFragment JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.w3c.dom.NodeList JavaDoc;
30
31 /**
32  * InsertNodeJXPathBindingBuilder provides a helper class for the Factory
33  * implemented in {@link JXPathBindingManager} that helps construct the
34  * actual {@link InsertNodeJXPathBinding} out of the configuration in the
35  * provided configElement which looks like:
36  * <pre><code>
37  * &lt;fb:insert-node&gt;
38  * &lt;!-- in here comes a template that will be inserted in the target
39  * document --&gt;
40  * &lt;/fb:insert-node&gt;
41  * </code></pre>
42  *
43  * @version $Id: InsertNodeJXPathBindingBuilder.java 328353 2005-10-25 12:59:42Z sylvain $
44  */

45 public class InsertNodeJXPathBindingBuilder
46     extends JXPathBindingBuilderBase {
47
48     /**
49      * Creates an instance of {@link InsertNodeJXPathBinding} configured
50      * with the nested template of the bindingElm.
51      */

52     public JXPathBindingBase buildBinding(
53         Element JavaDoc bindingElm,
54         JXPathBindingManager.Assistant assistant) throws BindingException {
55
56         try {
57             CommonAttributes commonAtts = JXPathBindingBuilderBase.getCommonAttributes(bindingElm);
58
59             DocumentFragment JavaDoc domTemplate = null;
60
61             String JavaDoc 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 JavaDoc document = SourceUtil.toDOM(source);
69                     Element JavaDoc element = document.getDocumentElement();
70
71                     String JavaDoc xpath = DomHelper.getAttribute(bindingElm, "xpath", null);
72                     if (xpath != null) {
73                         XPathProcessor xpathProcessor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);
74                         try {
75                             Node JavaDoc 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 JavaDoc))
79                                 throw new BindingException("XPath expression \"" + xpath + "\" did not return an element node.");
80                             element = (Element JavaDoc)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                 // FIXME: using the binding's document prevents it to be garbage collected.
95
// --> create a new Document and use doc.importNode();
96
domTemplate = bindingElm.getOwnerDocument().createDocumentFragment();
97                 NodeList JavaDoc nested = bindingElm.getChildNodes();
98                 int size = nested.getLength();
99                 for (int i = 0; i < size; i++) {
100                     Node JavaDoc node = nested.item(i).cloneNode(true);
101                     if (node.getNodeType() == Node.ELEMENT_NODE) {
102                         LocationAttributes.remove((Element JavaDoc)node, true);
103                     }
104                     domTemplate.appendChild(node);
105                 }
106             }
107             
108 // do inheritance
109
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 JavaDoc e) {
119             throw new BindingException("Error building the insert-node binding defined at " + DomHelper.getLocation(bindingElm), e);
120         }
121     }
122 }
123
Popular Tags