Coverage report

  %line %branch
org.apache.commons.jelly.tags.xml.ParseTagSupport
62% 
95% 

 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 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: 1.6 $
 41  
  */
 42  4
 public abstract class ParseTagSupport extends TagSupport {
 43  
 
 44  
     /** The Log to which logging calls will be made. */
 45  4
     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  39
     public ParseTagSupport() {
 57  39
     }
 58  
 
 59  
 
 60  
     // Properties
 61  
     //-------------------------------------------------------------------------
 62  
     /** The variable name that will be used for the Document variable created
 63  
      */
 64  
     public String getVar() {
 65  54
         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  27
         this.var = class="keyword">var;
 72  27
     }
 73  
 
 74  
     /**
 75  
      * Returns the text to be parsed
 76  
      * @return String
 77  
      */
 78  
     public String getText() {
 79  17
         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  1
         this.text = text;
 88  1
     }
 89  
 
 90  
 
 91  
     /** @return the SAXReader used for parsing, creating one lazily if need be  */
 92  
     public SAXReader getSAXReader() throws SAXException {
 93  3
         if (saxReader == null) {
 94  3
             saxReader = createSAXReader();
 95  
         }
 96  3
         return saxReader;
 97  
     }
 98  
 
 99  
     /** Sets the SAXReader used for parsing */
 100  
     public void setSAXReader(SAXReader saxReader) {
 101  0
         this.saxReader = saxReader;
 102  0
     }
 103  
 
 104  
 
 105  
     // Implementation methods
 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  16
         SAXContentHandler handler = new SAXContentHandler();
 119  16
         XMLOutput newOutput = new XMLOutput(handler);
 120  
 
 121  
         try {
 122  16
             handler.startDocument();
 123  16
             invokeBody( newOutput);
 124  16
             handler.endDocument();
 125  16
             return handler.getDocument();
 126  
         } catch (SAXException e) {
 127  0
             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  1
         if ( log.isDebugEnabled() ) {
 136  0
             log.debug( "About to parse: " + text );
 137  
         }
 138  
 
 139  
         try {
 140  1
             return getSAXReader().read( new StringReader( text ) );
 141  
         }
 142  
         catch (DocumentException e) {
 143  0
             throw new JellyTagException(e);
 144  
         }
 145  
         catch (SAXException e) {
 146  0
             throw new JellyTagException(e);
 147  
         }
 148  
     }
 149  
 
 150  
     /**
 151  
      * Parses the given source
 152  
      */
 153  
     protected Document parse(Object source) throws JellyTagException {
 154  
         // #### we should allow parsing to output XML events to
 155  
         // the output if no var is specified
 156  
 
 157  
 
 158  
         try {
 159  2
             if (source instanceof String) {
 160  2
                 String uri = (String) source;
 161  2
                 source = context.getResource(uri);
 162  
             }
 163  
 
 164  2
             if (source instanceof URL) {
 165  2
                 return getSAXReader().read((URL) source);
 166  
             }
 167  0
             else if (source instanceof File) {
 168  0
                 return getSAXReader().read((File) source);
 169  
             }
 170  0
             else if (source instanceof Reader) {
 171  0
                 return getSAXReader().read((Reader) source);
 172  
             }
 173  0
             else if (source instanceof InputStream) {
 174  0
                 return getSAXReader().read((InputStream) source);
 175  
             }
 176  
             else {
 177  0
                 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  0
             throw new JellyTagException(e);
 187  
         }
 188  
         catch (SAXException e) {
 189  0
             throw new JellyTagException(e);
 190  
         }
 191  
         catch (MalformedURLException e) {
 192  0
             throw new JellyTagException(e);
 193  
         }
 194  
     }
 195  
 }

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