KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > xtags > xpath > AddTag


1 /*
2  * Copyright 2001,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
17 package org.apache.taglibs.xtags.xpath;
18
19 import java.io.StringReader JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24
25 import org.dom4j.Document;
26 import org.dom4j.DocumentFactory;
27 import org.dom4j.DocumentException;
28 import org.dom4j.Element;
29 import org.dom4j.Node;
30 import org.dom4j.XPath;
31 import org.dom4j.io.SAXReader;
32
33 import org.apache.taglibs.xtags.util.JspVariableContext;
34
35 /** The add tag parses it's body (as an XML fragment) and appends the contents to the
36   * current node. The current node must be an Element.
37   *
38   * @author James Elson
39   */

40 public class AddTag extends AbstractBodyTag {
41     
42     private XPath beforeXPath;
43     private XPath afterXPath;
44     
45     public AddTag() {
46     }
47
48     public int doEndTag() throws JspException JavaDoc {
49         Object JavaDoc element = TagHelper.getInputNodes(pageContext, this, false );
50         if (element == null) {
51             throw new JspException JavaDoc( "No current node to add content to" );
52         }
53         if (! (element instanceof Element) ) {
54             throw new JspException JavaDoc( "Current node is not an Element" );
55         }
56         if (bodyContent != null) {
57             try {
58                 StringReader JavaDoc sreader = new StringReader JavaDoc("<dummy>"+bodyContent.getString()+"</dummy>");
59                 SAXReader reader = new SAXReader();
60                 Document doc = reader.read(sreader);
61                 Element root = doc.getRootElement();
62                 List JavaDoc nodes = root.content();
63                 while (! nodes.isEmpty() ) {
64                     Node node = (Node)nodes.remove(0);
65                     node.detach();
66                     ((Element)element).add( node );
67                 }
68             }
69             catch (DocumentException e) {
70                 handleException(e);
71             }
72         }
73         
74         return EVAL_PAGE;
75     }
76
77     public void release() {
78         super.release();
79         beforeXPath = null;
80         afterXPath = null;
81     }
82
83     
84     // Properties
85
//-------------------------------------------------------------------------
86

87     /** Sets an XPath expression used to determine a child element of the current element.
88      * The body contents will be inserted just before the first node that matches this
89      * XPath.
90      */

91     public void setAfter(String JavaDoc after) {
92         this.afterXPath = createXPath( after );
93     }
94
95     /** Sets an XPath expression used to determine a child element of the current element.
96      * The body contents will be inserted just after the first node that matches this
97      * XPath.
98      */

99     public void setBefore(String JavaDoc before) {
100         this.beforeXPath = createXPath( before );
101     }
102     
103     /** A factory method to create new XPath instances */
104     protected XPath createXPath(String JavaDoc xpathExpression) {
105         XPath xpath = getDocumentFactory().createXPath( xpathExpression );
106         xpath.setVariableContext( JspVariableContext.getInstance( pageContext ) );
107         return xpath;
108     }
109
110     /** @return the factory used to create XPath instances */
111     protected DocumentFactory getDocumentFactory() {
112         return DocumentFactory.getInstance();
113     }
114 }
115
Popular Tags