View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/ParseTag.java,v 1.9 2002/06/26 09:24:35 jstrachan Exp $ 3 * $Revision: 1.9 $ 4 * $Date: 2002/06/26 09:24:35 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: ParseTag.java,v 1.9 2002/06/26 09:24:35 jstrachan Exp $ 61 */ 62 package org.apache.commons.jelly.tags.xml; 63 64 import java.io.InputStream; 65 import java.io.Reader; 66 import java.io.StringReader; 67 import java.io.StringWriter; 68 import java.io.Writer; 69 import java.net.MalformedURLException; 70 import java.net.URL; 71 72 import org.apache.commons.jelly.TagSupport; 73 import org.apache.commons.jelly.XMLOutput; 74 75 import org.apache.commons.logging.Log; 76 import org.apache.commons.logging.LogFactory; 77 78 import org.dom4j.Document; 79 import org.dom4j.io.SAXContentHandler; 80 import org.dom4j.io.SAXReader; 81 82 /*** 83 * An abstract base class for any tag which parsers its body as XML. 84 * 85 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 86 * @version $Revision: 1.9 $ 87 */ 88 public abstract class ParseTagSupport extends TagSupport { 89 90 /*** The Log to which logging calls will be made. */ 91 private static final Log log = LogFactory.getLog(ParseTagSupport.class); 92 93 /*** The variable that will be generated for the document */ 94 private String var; 95 96 /*** The SAXReader used to parser the document */ 97 private SAXReader saxReader; 98 99 public ParseTagSupport() { 100 } 101 102 103 // Properties 104 //------------------------------------------------------------------------- 105 /*** The variable name that will be used for the Document variable created 106 */ 107 public String getVar() { 108 return var; 109 } 110 111 /*** Sets the variable name that will be used for the Document variable created 112 */ 113 public void setVar(String var) { 114 this.var = var; 115 } 116 117 /*** @return the SAXReader used for parsing, creating one lazily if need be */ 118 public SAXReader getSAXReader() throws Exception { 119 if (saxReader == null) { 120 saxReader = createSAXReader(); 121 } 122 return saxReader; 123 } 124 125 /*** Sets the SAXReader used for parsing */ 126 public void setSAXReader(SAXReader saxReader) { 127 this.saxReader = saxReader; 128 } 129 130 131 // Implementation methods 132 //------------------------------------------------------------------------- 133 134 /*** 135 * Factory method to create a new SAXReader 136 */ 137 protected abstract SAXReader createSAXReader() throws Exception; 138 139 140 /*** 141 * Parses the body of this tag and returns the parsed document 142 */ 143 protected Document parseBody(XMLOutput output) throws Exception { 144 SAXContentHandler handler = new SAXContentHandler(); 145 XMLOutput newOutput = new XMLOutput(handler); 146 handler.startDocument(); 147 invokeBody( newOutput); 148 handler.endDocument(); 149 return handler.getDocument(); 150 151 /* 152 // the following is inefficient as it requires a parse of the text 153 // but is left here in the code to see how it could be done. 154 155 String text = getBodyText(); 156 157 if ( log.isDebugEnabled() ) { 158 log.debug( "About to parse: " + text ); 159 } 160 return getSAXReader().read( new StringReader( text ) ); 161 */ 162 } 163 164 /*** 165 * Parses the given source 166 */ 167 protected Document parse(Object source) throws Exception { 168 // #### we should allow parsing to output XML events to 169 // the output if no var is specified 170 171 if (source instanceof String) { 172 String uri = (String) source; 173 InputStream in = context.getResourceAsStream(uri); 174 return getSAXReader().read(in, uri); 175 } 176 else if (source instanceof Reader) { 177 return getSAXReader().read((Reader) source); 178 } 179 else if (source instanceof InputStream) { 180 return getSAXReader().read((InputStream) source); 181 } 182 else if (source instanceof URL) { 183 return getSAXReader().read((URL) source); 184 } 185 else { 186 throw new IllegalArgumentException( 187 "Invalid source argument. Must be a String, Reader, InputStream or URL." 188 + " Was type; " 189 + source.getClass().getName() 190 + " with value: " 191 + source); 192 } 193 } 194 }

This page was automatically generated by Maven