1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.xml;
17
18 import java.io.File;
19 import java.io.InputStream;
20 import java.io.Reader;
21 import java.io.StringReader;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24
25 import org.apache.commons.jelly.JellyTagException;
26 import org.apache.commons.jelly.TagSupport;
27 import org.apache.commons.jelly.XMLOutput;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.dom4j.Document;
31 import org.dom4j.DocumentException;
32 import org.dom4j.io.SAXContentHandler;
33 import org.dom4j.io.SAXReader;
34 import org.xml.sax.SAXException;
35
36 /***
37 * An abstract base class for any tag which parsers its body as XML.
38 *
39 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
40 * @version $Revision: 155420 $
41 */
42 public abstract class ParseTagSupport extends TagSupport {
43
44 /*** The Log to which logging calls will be made. */
45 private static final Log log = LogFactory.getLog(ParseTagSupport.class);
46
47 /*** The variable that will be generated for the document */
48 private String var;
49
50 /*** The markup text to be parsed */
51 private String text;
52
53 /*** The SAXReader used to parser the document */
54 private SAXReader saxReader;
55
56 public ParseTagSupport() {
57 }
58
59
60
61
62 /*** The variable name that will be used for the Document variable created
63 */
64 public String getVar() {
65 return var;
66 }
67
68 /*** Sets the variable name that will be used for the Document variable created
69 */
70 public void setVar(String var) {
71 this.var = var;
72 }
73
74 /***
75 * Returns the text to be parsed
76 * @return String
77 */
78 public String getText() {
79 return text;
80 }
81
82 /***
83 * Sets the text to be parsed by this parser
84 * @param text The text to be parsed by this parser
85 */
86 public void setText(String text) {
87 this.text = text;
88 }
89
90
91 /*** @return the SAXReader used for parsing, creating one lazily if need be */
92 public SAXReader getSAXReader() throws SAXException {
93 if (saxReader == null) {
94 saxReader = createSAXReader();
95 }
96 return saxReader;
97 }
98
99 /*** Sets the SAXReader used for parsing */
100 public void setSAXReader(SAXReader saxReader) {
101 this.saxReader = saxReader;
102 }
103
104
105
106
107
108 /***
109 * Factory method to create a new SAXReader
110 */
111 protected abstract SAXReader createSAXReader() throws SAXException;
112
113
114 /***
115 * Parses the body of this tag and returns the parsed document
116 */
117 protected Document parseBody(XMLOutput output) throws JellyTagException {
118 SAXContentHandler handler = new SAXContentHandler();
119 XMLOutput newOutput = new XMLOutput(handler);
120
121 try {
122 handler.startDocument();
123 invokeBody( newOutput);
124 handler.endDocument();
125 return handler.getDocument();
126 } catch (SAXException e) {
127 throw new JellyTagException(e);
128 }
129 }
130
131 /***
132 * Parses the give piece of text as being markup
133 */
134 protected Document parseText(String text) throws JellyTagException {
135 if ( log.isDebugEnabled() ) {
136 log.debug( "About to parse: " + text );
137 }
138
139 try {
140 return getSAXReader().read( new StringReader( text ) );
141 }
142 catch (DocumentException e) {
143 throw new JellyTagException(e);
144 }
145 catch (SAXException e) {
146 throw new JellyTagException(e);
147 }
148 }
149
150 /***
151 * Parses the given source
152 */
153 protected Document parse(Object source) throws JellyTagException {
154
155
156
157
158 try {
159 if (source instanceof String) {
160 String uri = (String) source;
161 source = context.getResource(uri);
162 }
163
164 if (source instanceof URL) {
165 return getSAXReader().read((URL) source);
166 }
167 else if (source instanceof File) {
168 return getSAXReader().read((File) source);
169 }
170 else if (source instanceof Reader) {
171 return getSAXReader().read((Reader) source);
172 }
173 else if (source instanceof InputStream) {
174 return getSAXReader().read((InputStream) source);
175 }
176 else {
177 throw new IllegalArgumentException(
178 "Invalid source argument. Must be a String, Reader, InputStream or URL."
179 + " Was type; "
180 + source.getClass().getName()
181 + " with value: "
182 + source);
183 }
184 }
185 catch (DocumentException e) {
186 throw new JellyTagException(e);
187 }
188 catch (SAXException e) {
189 throw new JellyTagException(e);
190 }
191 catch (MalformedURLException e) {
192 throw new JellyTagException(e);
193 }
194 }
195 }