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 org.apache.commons.jelly.JellyTagException;
19 import org.apache.commons.jelly.MissingAttributeException;
20 import org.apache.commons.jelly.XMLOutput;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.dom4j.Document;
24 import org.dom4j.io.SAXReader;
25
26 /*** A tag which parses some XML and defines a variable with the parsed Document.
27 * The XML can either be specified as its body or can be passed in via the
28 * xml property which can be a Reader, InputStream, URL or String URI.
29 *
30 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31 * @version $Revision: 155420 $
32 */
33 public class ParseTag extends ParseTagSupport {
34
35 /*** The Log to which logging calls will be made. */
36 private static final Log log = LogFactory.getLog(ParseTag.class);
37
38 /*** The xml to parse, either a String URI, a Reader or InputStream */
39 private Object xml;
40
41
42 /*** whether XML validation is enabled or disabled */
43 private boolean validate;
44
45 public ParseTag() {
46 }
47
48
49
50 public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
51 if (getVar() == null) {
52 throw new MissingAttributeException("The var attribute cannot be null");
53 }
54
55 Document document = getXmlDocument(output);
56 context.setVariable(getVar(), document);
57 }
58
59
60
61 /*** Gets the source of the XML which is either a String URI, Reader or InputStream */
62 public Object getXml() {
63 return this.xml;
64 }
65
66 /*** Sets the source of the XML which is either a String URI, a File, Reader or InputStream */
67 public void setXml(Object xml) {
68 this.xml = xml;
69 }
70
71 /*** @return whether XML validation is enabled or disabled */
72 public boolean getValidate() {
73 return validate;
74 }
75
76 /*** Sets whether XML validation is enabled or disabled */
77 public void setValidate(boolean validate) {
78 this.validate = validate;
79 }
80
81
82
83
84
85 /***
86 * Factory method to create a new SAXReader
87 */
88 protected SAXReader createSAXReader() {
89 return new SAXReader(validate);
90 }
91
92 protected Document getXmlDocument(XMLOutput output) throws JellyTagException {
93 Document document = null;
94 Object xmlObj = this.getXml();
95
96 if (xmlObj == null) {
97 String text = getText();
98 if (text != null) {
99 document = parseText(text);
100 }
101 else {
102 document = parseBody(output);
103 }
104 }
105 else {
106 document = parse(xmlObj);
107 }
108
109 return document;
110 }
111
112 }