Coverage report

| %line | %branch
--- | --- | ---
org.apache.commons.jelly.tags.core.ParseTag |
2% |
75% |

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.core;
17 |  |
18 |  |
     import java.io.StringReader;
19 |  |
20 |  |
     import javax.xml.parsers.ParserConfigurationException;
21 |  |
     import javax.xml.parsers.SAXParser;
22 |  |
     import javax.xml.parsers.SAXParserFactory;
23 |  |
24 |  |
     import org.apache.commons.jelly.JellyTagException;
25 |  |
     import org.apache.commons.jelly.MissingAttributeException;
26 |  |
     import org.apache.commons.jelly.Script;
27 |  |
     import org.apache.commons.jelly.TagSupport;
28 |  |
     import org.apache.commons.jelly.XMLOutput;
29 |  |
     import org.apache.commons.jelly.parser.XMLParser;
30 |  |
     import org.apache.commons.logging.Log;
31 |  |
     import org.apache.commons.logging.LogFactory;
32 |  |
     import org.xml.sax.ContentHandler;
33 |  |
     import org.xml.sax.InputSource;
34 |  |
     import org.xml.sax.SAXException;
35 |  |
     import org.xml.sax.XMLReader;
36 |  |
37 |  |
     /**
38 |  |
      * Parses the output of this tags body or of a given String as a Jelly script
39 |  |
      * then either outputting the Script as a variable or executing the script.
40 |  |
      *
41 |  |
      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
42 |  |
      * @version $Revision: 155420 $
43 |  |
      */
44 |  |
     public class ParseTag extends TagSupport {
45 |  |
46 |  |
         /** The Log to which logging calls will be made. */
47 | 546 |
         private static final Log log = LogFactory.getLog(ParseTag.class);
48 |  |
49 |  |
         /** The variable that will be generated for the document */
50 |  |
         private String var;
51 |  |
52 |  |
         /** The markup text to be parsed */
53 |  |
         private String text;
54 |  |
55 |  |
         /** The XMLReader used to parser the document */
56 |  |
         private XMLReader xmlReader;
57 |  |
58 |  |
         /** The Jelly parser */
59 |  |
         private XMLParser jellyParser;
60 |  |
61 | 0 |
         public ParseTag() {
62 | 0 |
         }
63 |  |
64 |  |
         // Tag interface
65 |  |
         //-------------------------------------------------------------------------
66 |  |
67 |  |
         /* (non-Javadoc)
68 |  |
          * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
69 |  |
          */
70 |  |
         public void doTag(XMLOutput output)
71 |  |
             throws MissingAttributeException, JellyTagException {
72 |  |
73 | 0 |
             String text = getText();
74 | 0 |
             if (text != null) {
75 | 0 |
                 parseText(text);
76 | 0 |
             }
77 |  |
             else {
78 | 0 |
                 parseBody(output);
79 |  |
             }
80 |  |
81 | 0 |
             Script script = getJellyParser().getScript();
82 | 0 |
             if (var != null) {
83 | 0 |
                 context.setVariable(var, script);
84 | 0 |
             }
85 |  |
             else {
86 |  |
                 // invoke the script
87 | 0 |
                 script.run(context, output);
88 |  |
             }
89 | 0 |
         }
90 |  |
91 |  |
         // Properties
92 |  |
         //-------------------------------------------------------------------------
93 |  |
         /** The variable name that will be used for the Document variable created
94 |  |
          */
95 |  |
         public String getVar() {
96 | 0 |
             return var;
97 |  |
         }
98 |  |
99 |  |
         /** Sets the variable name that will be used for the Document variable created
100 |  |
          */
101 |  |
         public void setVar(String var) {
102 | 0 |
             this.var = class="keyword">var;
103 | 0 |
         }
104 |  |
105 |  |
         /**
106 |  |
          * Returns the text to be parsed
107 |  |
          * @return String
108 |  |
          */
109 |  |
         public String getText() {
110 | 0 |
             return text;
111 |  |
         }
112 |  |
113 |  |
         /**
114 |  |
          * Sets the text to be parsed by this parser
115 |  |
          * @param text The text to be parsed by this parser
116 |  |
          */
117 |  |
         public void setText(String text) {
118 | 0 |
             this.text = text;
119 | 0 |
         }
120 |  |
121 |  |
122 |  |
         /** @return the XMLReader used for parsing, creating one lazily if need be  */
123 |  |
         public XMLReader getXMLReader() throws ParserConfigurationException, SAXException {
124 | 0 |
             if (xmlReader == null) {
125 | 0 |
                 xmlReader = createXMLReader();
126 |  |
             }
127 | 0 |
             return xmlReader;
128 |  |
         }
129 |  |
130 |  |
         /** Sets the XMLReader used for parsing */
131 |  |
         public void setXMLReader(XMLReader xmlReader) {
132 | 0 |
             this.xmlReader = xmlReader;
133 | 0 |
         }
134 |  |
135 |  |
136 |  |
         /**
137 |  |
          * @return XMLParser
138 |  |
          */
139 |  |
         public XMLParser getJellyParser() {
140 | 0 |
             if (jellyParser == null) {
141 | 0 |
                 jellyParser = createJellyParser();
142 |  |
             }
143 | 0 |
             return jellyParser;
144 |  |
         }
145 |  |
146 |  |
         /**
147 |  |
          * Sets the jellyParser.
148 |  |
          * @param jellyParser The jellyParser to set
149 |  |
          */
150 |  |
         public void setJellyParser(XMLParser jellyParser) {
151 | 0 |
             this.jellyParser = jellyParser;
152 | 0 |
         }
153 |  |
154 |  |
155 |  |
         // Implementation methods
156 |  |
         //-------------------------------------------------------------------------
157 |  |
158 |  |
         /**
159 |  |
          * Factory method to create a new XMLReader
160 |  |
          */
161 |  |
         protected XMLReader createXMLReader() throws ParserConfigurationException, SAXException {
162 | 0 |
             SAXParserFactory factory = SAXParserFactory.newInstance();
163 | 0 |
             factory.setNamespaceAware(true);
164 | 0 |
             SAXParser parser = factory.newSAXParser();
165 | 0 |
             return parser.getXMLReader();
166 |  |
         }
167 |  |
168 |  |
169 |  |
         /**
170 |  |
          * Parses the body of this tag and returns the parsed document
171 |  |
          */
172 |  |
         protected void parseBody(XMLOutput output) throws JellyTagException {
173 | 0 |
             ContentHandler handler = getJellyParser();
174 | 0 |
             XMLOutput newOutput = new XMLOutput(handler);
175 |  |
176 |  |
             try {
177 | 0 |
                 handler.startDocument();
178 | 0 |
                 invokeBody(newOutput);
179 | 0 |
                 handler.endDocument();
180 |  |
             }
181 | 0 |
             catch (SAXException e) {
182 | 0 |
                 throw new JellyTagException(e);
183 | 0 |
             }
184 | 0 |
         }
185 |  |
186 |  |
         /**
187 |  |
          * Parses the give piece of text as being markup
188 |  |
          */
189 |  |
         protected void parseText(String text) throws JellyTagException {
190 | 0 |
             if ( log.isDebugEnabled() ) {
191 | 0 |
                 log.debug( "About to parse: " + text );
192 |  |
             }
193 |  |
194 |  |
             try {
195 | 0 |
                 getXMLReader().parse( new InputSource( class="keyword">new StringReader( text ) ) );
196 |  |
             }
197 | 0 |
             catch (Exception e) {
198 | 0 |
                 throw new JellyTagException(e);
199 | 0 |
             }
200 | 0 |
         }
201 |  |
202 |  |
         /**
203 |  |
          * Factory method to create a new Jelly parser
204 |  |
          * @return XMLParser
205 |  |
          */
206 |  |
         protected XMLParser createJellyParser() {
207 | 0 |
             XMLParser answer = new XMLParser();
208 | 0 |
             answer.setContext(context);
209 | 0 |
             return answer;
210 |  |
         }
211 |  |
     }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.