KickJava   Java API By Example, From Geeks To Geeks.

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

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

51     public JXPathBindingBase buildBinding(
52         Element JavaDoc bindingElm,
53         JXPathBindingManager.Assistant assistant) throws BindingException {
54
55         try {
56             CommonAttributes commonAtts = JXPathBindingBuilderBase.getCommonAttributes(bindingElm);
57
58             DocumentFragment JavaDoc domTemplate = null;
59
60             String JavaDoc 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 JavaDoc document = SourceUtil.toDOM(source);
68                     Element JavaDoc element = document.getDocumentElement();
69
70                     String JavaDoc xpath = bindingElm.getAttribute("xpath");
71                     if (!xpath.equals("")) {
72                         XPathProcessor xpathProcessor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);
73                         try {
74                             Node JavaDoc 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 JavaDoc))
78                                 throw new BindingException("XPath expression \"" + xpath + "\" did not return an element node.");
79                             element = (Element JavaDoc)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 JavaDoc 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 JavaDoc e) {
102             throw new BindingException("Error building the insert-node binding defined at " + DomHelper.getLocation(bindingElm), e);
103         }
104     }
105 }
106
Popular Tags