KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Reader JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.servlet.ServletContext JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.jsp.JspException JavaDoc;
30 import javax.servlet.jsp.JspWriter JavaDoc;
31 import javax.servlet.jsp.PageContext JavaDoc;
32 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
33
34 import org.dom4j.Document;
35 import org.dom4j.io.SAXReader;
36
37 import org.apache.taglibs.xtags.util.URLHelper;
38
39 import org.xml.sax.SAXException JavaDoc;
40
41 /** A tag which parses its body as an XML Document and defines a variable
42   *
43   * @author James Strachan
44   */

45 public class ParseTag extends AbstractBodyTag {
46
47     /** Allow tracing to be disabled */
48     private static final boolean TRACE = false;
49     
50     /** Holds value of property id. */
51     private String JavaDoc id;
52     
53     /** The URL of the document to parse. */
54     private URL JavaDoc url;
55     
56     /** The Reader used to parse XML */
57     private Reader JavaDoc reader;
58     
59     private Document document;
60     
61     /** Sets whether validation mode is on or off */
62     private boolean validate;
63     
64     public ParseTag() {
65     }
66
67     // Tag interface
68
//-------------------------------------------------------------------------
69
public int doStartTag() throws JspException JavaDoc {
70         document = null;
71         if ( url != null ) {
72             try {
73                 // XXXX: check cache here...
74
document = getSAXReader().read( url );
75                 defineVariable( document );
76             }
77             catch (Exception JavaDoc e) {
78                 handleException(e);
79             }
80             return SKIP_BODY;
81         }
82         return EVAL_BODY_TAG;
83     }
84     
85     public int doAfterBody() throws JspException JavaDoc {
86         if ( document == null ) {
87             try {
88                 if ( reader != null ) {
89                     document = getSAXReader().read( reader );
90                     reader = null;
91                 }
92                 else {
93                     BodyContent JavaDoc body = getBodyContent();
94                     String JavaDoc text = body.getString().trim();
95                     body.clearBody();
96                     document = getSAXReader().read( new StringReader JavaDoc(text) );
97                 }
98                 defineVariable( document );
99             }
100             catch (Exception JavaDoc e) {
101                 handleException(e);
102             }
103         }
104         return SKIP_BODY;
105     }
106
107     public void release() {
108         super.release();
109         id = null;
110         url = null;
111         reader = null;
112         document = null;
113         validate = false;
114     }
115
116     // Properties
117
//-------------------------------------------------------------------------
118

119     /** Getter for property id.
120      * @return Value of property id.
121      */

122     public String JavaDoc getId() {
123         return id;
124     }
125     
126     /** Setter for property id.
127      * @param id New value of property id.
128      */

129     public void setId(String JavaDoc id) {
130         this.id = id;
131     }
132     
133     public void setURL( URL JavaDoc url ) {
134         this.url = url;
135         
136         if ( TRACE ) {
137             logInfo( "Set URL to: " + url );
138         }
139     }
140     
141     public void setUrl( String JavaDoc url ) throws IOException JavaDoc {
142         if ( TRACE ) {
143             logInfo( "Setting absolute URL to: " + url );
144         }
145         
146         setURL( new URL JavaDoc( url ) );
147     }
148     
149     public void setUri( String JavaDoc uri ) throws IOException JavaDoc {
150         setURL( URLHelper.getResourceURL( uri, pageContext ) );
151     }
152     
153     public void setReader(Reader JavaDoc reader) {
154         this.reader = reader;
155     }
156     
157     public boolean getValidate() {
158         return validate;
159     }
160     
161     public void setValidate(boolean validate) {
162         this.validate = validate;
163     }
164     
165     
166     // Implementation methods
167
//-------------------------------------------------------------------------
168
protected SAXReader getSAXReader() throws SAXException JavaDoc {
169         // XXXX: special patch for Tomcat 4.0 beta 6
170
// which for some reason sets the value of org.xml.sax.driver
171
// to be Xerces even though it ships with crimson by default.
172
// So clearing the org.xml.sax.driver property will force JAXP to be
173
// used instead - which is a much better idea for web apps anyway.
174
//
175
// Note that dom4j 0.8 can handle invalid settings of org.xml.sax.driver
176
// gracefully and will use JAXP in preference to org.xml.sax.driver anyway
177
// so this patch is not required for dom4j versions 0.8 or above
178
try {
179             System.setProperty( "org.xml.sax.driver", null );
180         }
181         catch (Throwable JavaDoc t) {
182             // ignore any errors
183
}
184         return new SAXReader(validate);
185     }
186     
187     protected void defineVariable( Document document ) {
188         TagHelper.defineVariable( pageContext, getId(), document );
189     }
190 }
191
Popular Tags