KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package org.apache.taglibs.xtags.xpath;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspWriter JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
28
29 import org.dom4j.Document;
30 import org.dom4j.Element;
31 import org.dom4j.Node;
32 import org.dom4j.XPath;
33 import org.dom4j.io.XMLWriter;
34
35 /** A tag which performs a copy operation like the XSLT tag - a shallow copy
36   *
37   * @author James Strachan
38   */

39 public class CopyTag extends AbstractTag {
40
41     /** Holds the node selected for this tag */
42     private Node node;
43     
44     /** Holds the current XMLWriter used in this tag */
45     private XMLWriter writer;
46     
47     /** Holds value of property xpath. */
48     private XPath xpath;
49     
50
51     public CopyTag() {
52     }
53
54     // Tag interface
55
//-------------------------------------------------------------------------
56
public int doStartTag() throws JspException JavaDoc {
57         node = getNode();
58         if ( node != null ) {
59             try {
60                 writer = TagHelper.getXMLWriter( pageContext, this );
61                 if ( node instanceof Element ) {
62                     writer.writeOpen( (Element) node );
63                 }
64                 else {
65                     writer.write( node );
66                 }
67             }
68             catch (IOException JavaDoc e) {
69                 handleException(e);
70             }
71         }
72         return EVAL_BODY_INCLUDE;
73     }
74     
75     public int doEndTag() throws JspException JavaDoc {
76         if ( node instanceof Element && writer != null ) {
77             try {
78                 writer.writeClose( (Element) node );
79             }
80             catch (IOException JavaDoc e) {
81                 handleException(e);
82             }
83             finally {
84                 node = null;
85                 writer = null;
86             }
87         }
88         return EVAL_PAGE;
89     }
90
91     public void release() {
92         super.release();
93         node = null;
94         writer = null;
95         xpath = null;
96     }
97
98     // Properties
99
//-------------------------------------------------------------------------
100

101     /** Setter for property select.
102      * @param select New value of property select.
103      */

104     public void setSelect(String JavaDoc select) {
105         if ( select != null && ! select.equals( "." ) ) {
106             xpath = createXPath( select );
107         }
108         else {
109             xpath = null;
110         }
111     }
112     
113     /** Returns the node selected for this tag
114      */

115     protected Node getNode() {
116         Object JavaDoc input = getInputNodes();
117         if ( xpath != null ) {
118             input = xpath.selectNodes( input );
119         }
120         return getNode( input );
121     }
122     
123     protected Node getNode(Object JavaDoc input) {
124         if ( input instanceof Element ) {
125             return (Element) input;
126         }
127         else if ( input instanceof Node ) {
128             // don't output documents
129
if ( !( input instanceof Document ) ) {
130                 return (Node) input;
131             }
132         }
133         else if ( input instanceof List JavaDoc ) {
134             List JavaDoc list = (List JavaDoc) input;
135             if ( list.size() > 0 ) {
136                 return getNode( list.get(0) );
137             }
138         }
139         return null;
140     }
141
142 }
143
Popular Tags