%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.xml.ParseTag |
|
|
1 | /* |
|
2 | * Copyright 2002,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 | 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: 1.7 $ |
|
32 | */ |
|
33 | 4 | public class ParseTag extends ParseTagSupport { |
34 | ||
35 | /** The Log to which logging calls will be made. */ |
|
36 | 4 | 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 | // Optional properties not defined in JSTL |
|
42 | /** whether XML validation is enabled or disabled */ |
|
43 | private boolean validate; |
|
44 | ||
45 | 39 | public ParseTag() { |
46 | 39 | } |
47 | ||
48 | // Tag interface |
|
49 | //------------------------------------------------------------------------- |
|
50 | public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { |
|
51 | 19 | if (getVar() == null) { |
52 | 0 | throw new MissingAttributeException("The var attribute cannot be null"); |
53 | } |
|
54 | ||
55 | 19 | Document document = getXmlDocument(output); |
56 | 19 | context.setVariable(getVar(), document); |
57 | 19 | } |
58 | ||
59 | // Properties |
|
60 | //------------------------------------------------------------------------- |
|
61 | /** Gets the source of the XML which is either a String URI, Reader or InputStream */ |
|
62 | public Object getXml() { |
|
63 | 51 | 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 | 6 | this.xml = xml; |
69 | 6 | } |
70 | ||
71 | /** @return whether XML validation is enabled or disabled */ |
|
72 | public boolean getValidate() { |
|
73 | 0 | return validate; |
74 | } |
|
75 | ||
76 | /** Sets whether XML validation is enabled or disabled */ |
|
77 | public void setValidate(boolean validate) { |
|
78 | 0 | this.validate = validate; |
79 | 0 | } |
80 | ||
81 | ||
82 | // Implementation methods |
|
83 | //------------------------------------------------------------------------- |
|
84 | ||
85 | /** |
|
86 | * Factory method to create a new SAXReader |
|
87 | */ |
|
88 | protected SAXReader createSAXReader() { |
|
89 | 3 | return new SAXReader(validate); |
90 | } |
|
91 | ||
92 | protected Document getXmlDocument(XMLOutput output) throws JellyTagException { |
|
93 | 19 | Document document = null; |
94 | 19 | Object xmlObj = this.getXml(); |
95 | ||
96 | 19 | if (xmlObj == null) { |
97 | 17 | String text = getText(); |
98 | 17 | if (text != null) { |
99 | 1 | document = parseText(text); |
100 | } |
|
101 | else { |
|
102 | 16 | document = parseBody(output); |
103 | } |
|
104 | } |
|
105 | else { |
|
106 | 2 | document = parse(xmlObj); |
107 | } |
|
108 | ||
109 | 19 | return document; |
110 | } |
|
111 | ||
112 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |