morgand 01/09/06 08:19:25 Modified: latka/src/java/org/apache/commons/latka Latka.java Suite.java latka/src/java/org/apache/commons/latka/xml SuiteHandler.java XMLPreprocessor.java Added: latka/src/java/org/apache/commons/latka/xml FindVariablesHandler.java Log: changed all String file URI references to URLs, should be more flexible Revision Changes Path 1.17 +13 -11 jakarta-commons/latka/src/java/org/apache/commons/latka/Latka.java Index: Latka.java =================================================================== RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/Latka.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- Latka.java 2001/09/04 05:37:13 1.16 +++ Latka.java 2001/09/06 15:19:25 1.17 @@ -148,15 +148,15 @@ XMLPreprocessor preprocessor = new XMLPreprocessor(); Reader xmlReader = null; - if (suite._reader != null) { - xmlReader = preprocessor.preprocessReader(suite._reader); + if (suite.getReader() != null) { + xmlReader = preprocessor.preprocessXml(suite.getReader()); } else { - xmlReader = preprocessor.preprocessURI(suite._uri); + xmlReader = preprocessor.preprocessXml(suite.getURL()); } source = new InputSource(xmlReader); - if (suite._uri != null) { - source.setSystemId(suite._uri); + if (suite.getURL() != null) { + source.setSystemId(suite.getURL().toString()); } SAXParserFactory factory = SAXParserFactory.newInstance(); @@ -264,7 +264,7 @@ System.out.println(LATKA_USAGE); } - String uri = args[0]; + String urlString = args[0]; if (args.length > 1) { @@ -287,15 +287,17 @@ } - Suite suite = new Suite(uri); - + String xml = null; XMLReporter listener = new XMLReporter(); - runTests(suite,listener); + try { - String xml = null; + URL url = new URL(urlString); + Suite suite = new Suite(url); - try { + + runTests(suite,listener); + xml = listener.getDocumentAsString(); logXML(xml); } catch (IOException e) { 1.6 +12 -3 jakarta-commons/latka/src/java/org/apache/commons/latka/Suite.java Index: Suite.java =================================================================== RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/Suite.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- Suite.java 2001/09/04 05:37:13 1.5 +++ Suite.java 2001/09/06 15:19:25 1.6 @@ -60,6 +60,7 @@ package org.apache.commons.latka; import java.io.Reader; +import java.net.URL; /** * References a Latka XML suite, stored either inside a Reader @@ -72,7 +73,7 @@ public class Suite { protected Reader _reader = null; - protected String _uri = null; + protected URL _url = null; /** * Create a test suite from an XML document located in the @@ -90,8 +91,16 @@ * * @param file URI of a Latka XML suite */ - public Suite(String uri) { - _uri = uri; + public Suite(URL url) { + _url = url; + } + + public URL getURL() { + return _url; + } + + public Reader getReader() { + return _reader; } } 1.12 +4 -1 jakarta-commons/latka/src/java/org/apache/commons/latka/xml/SuiteHandler.java Index: SuiteHandler.java =================================================================== RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/xml/SuiteHandler.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- SuiteHandler.java 2001/08/29 18:11:09 1.11 +++ SuiteHandler.java 2001/09/06 15:19:25 1.12 @@ -61,6 +61,8 @@ import java.io.IOException; +import java.net.URL; + import java.util.Properties; import org.apache.commons.latka.LatkaProperties; @@ -141,7 +143,8 @@ XMLPreprocessor process = new XMLPreprocessor(); try { - InputSource source = new InputSource(process.preprocessURI(systemId)); + URL url = new URL(systemId); + InputSource source = new InputSource(process.preprocessXml(url)); source.setSystemId(systemId); return source; } catch (IOException e) { 1.10 +148 -20 jakarta-commons/latka/src/java/org/apache/commons/latka/xml/XMLPreprocessor.java Index: XMLPreprocessor.java =================================================================== RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/xml/XMLPreprocessor.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- XMLPreprocessor.java 2001/09/05 21:19:06 1.9 +++ XMLPreprocessor.java 2001/09/06 15:19:25 1.10 @@ -60,20 +60,31 @@ package org.apache.commons.latka.xml; import java.io.BufferedReader; -import java.io.FileReader; +import java.io.InputStreamReader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; +import java.net.URL; + import java.util.HashSet; import java.util.Properties; import java.util.Set; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + import org.apache.commons.latka.LatkaProperties; +import org.apache.log4j.Category; + import org.apache.regexp.RE; import org.apache.regexp.RESyntaxException; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + /** * Methods for massaging the Latka XML. Includes methods * for finding and substituting variables in the XML @@ -86,6 +97,9 @@ protected static final String _variableExpr = "\\$\\{(.*?)\\}"; + protected static Category _log = + Category.getInstance(XMLPreprocessor.class); + /** * Processes a Stream, preparing it for the final XML * processing. In essence, this consists of finding @@ -102,20 +116,10 @@ * @see org.apache.commons.latka.Latka#runTests(Suite,LatkaEventInfo) * Latka.runTests(Suite,LatkaEventInfo) */ - public Reader preprocessReader(Reader reader) throws IOException { - BufferedReader buffReader = new BufferedReader(reader); + public Reader preprocessXml(Reader reader) throws IOException { - StringBuffer buff = new StringBuffer(); + String processedString = resolveVariables(stringFromReader(reader)); - String line = buffReader.readLine(); - while (line != null) { - buff.append(line); - buff.append("\n"); - line = buffReader.readLine(); - } - - String processedString = resolveVariables(buff.toString()); - return new StringReader(processedString); } @@ -130,21 +134,119 @@ * not be substituted. * @see #preprocessReader(Reader) */ - public Reader preprocessURI(String uri) throws IOException { - return preprocessReader(new FileReader(uri)); + public Reader preprocessXml(URL url) throws IOException { + Reader reader = new InputStreamReader(url.openConnection().getInputStream()); + return preprocessXml(reader); } + /** - * Given an XML suite, find the names of all variables that - * must be set in the LatkaProperties. + * Given an XML suite contained in the reader, find the names + * of all variables that must be set in the LatkaProperties. + * Will use SAX to recurse through external entities. * - * @param xmlString XML String representing a Latka suite + * @param reader Reader representing a Latka suite + * @return Array of variables that must be set. If no variables + * are present in the suite, a zero-length array is + * returned. + * @see org.apache.commons.latka.LatkaProperties + */ + public Set findVariables(Reader reader) + throws IOException { + + // accumulate the sets from the Reader and all + // external entities here + Set set = getLocalVariables(reader); + + // now that we have variables from the initial URI, + // perform a SAX parse to find additional variables + // in external entities + InputSource source = new InputSource(reader); + set.addAll(getVariablesFromEntites(source)); + + return set; + + } + + /** + * Given an XML suite contained in the file uri, find the names + * of all variables that must be set in the LatkaProperties. + * Will use SAX to recurse through external entities. + * + * @param reader Reader representing a Latka suite + * @return Array of variables that must be set. If no variables + * are present in the suite, a zero-length array is + * returned. + * @see org.apache.commons.latka.LatkaProperties + */ + public Set findVariables(URL url) + throws IOException { + + _log.debug("Going to read variables from URL: " + url); + + // accumulate the sets from the URI and all + // external entities here + Reader reader = new InputStreamReader(url.openConnection().getInputStream()); + Set set = getLocalVariables(reader); + + _log.debug("variables read"); + + // now that we have variables from the initial URI, + // perform a SAX parse to find additional variables + // in external entities + InputSource source = new InputSource(url.toString()); + set.addAll(getVariablesFromEntites(source)); + + return set; + + } + + /** + * This method uses a special SAX handler to recurse + * through the entities of a Latka Suite, finding all + * the variables that are referenced. + * + * @param inputSource + * SAX InputSource containing the Latka Suite + * @return Set of unique Latka variables references in the entities + * @exception IOException + * if any errors occur during the XML processing + */ + protected Set getVariablesFromEntites(InputSource inputSource) + throws IOException { + SAXParserFactory factory = SAXParserFactory.newInstance(); + + FindVariablesHandler handler = + new FindVariablesHandler(); + + try { + SAXParser parser = factory.newSAXParser(); + + parser.parse(inputSource, handler); + } catch (ParserConfigurationException e) { + throw new IOException(e.toString()); + } catch (SAXException e) { + throw new IOException(e.toString()); + } + + return handler.getVariables(); + } + + /** + * Read the text in the Reader and find the names of all variables + * that must be set in the LatkaProperties. This method does not + * recurse through entities like findVariables. + * + * @param Reader XML Reader containing a Latka suite fragment * @return Array of variables that must be set. If no variables * are present in the suite, a zero-length array is * returned. * @see org.apache.commons.latka.LatkaProperties */ - public Set findVariables(String xmlString) { + public Set getLocalVariables(Reader reader) throws IOException { + + String xmlString = stringFromReader(reader); + xmlString = stripXmlComments(xmlString); HashSet set = new HashSet(); @@ -237,5 +339,31 @@ return xmlString; } + + /** + * Place the contents of a Reader into a String. + * + * @param reader text in a Reader + * @return text stored as a String + * @exception IOException + * if the Reader could not be accessed + */ + protected String stringFromReader(Reader reader) + throws IOException { + + BufferedReader buffReader = new BufferedReader(reader); + + StringBuffer buff = new StringBuffer(); + + String line = buffReader.readLine(); + while (line != null) { + buff.append(line); + buff.append("\n"); + line = buffReader.readLine(); + } + + return buff.toString(); + } + -} +} 1.1 jakarta-commons/latka/src/java/org/apache/commons/latka/xml/FindVariablesHandler.java Index: FindVariablesHandler.java =================================================================== /* * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * [Additional notices, if required by prior licensing conditions] * */ package org.apache.commons.latka.xml; import java.io.InputStreamReader; import java.io.IOException; import java.net.URL; import java.util.HashSet; import java.util.Set; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * This handler is used by the * {@link XMLPreprocessr#findVariablesFro,URI(String)} * method to read the external entities of an XML * suite and find any variables referenced there. * The only purpose of the suite is to leverage * SAX's awareness of these entities' locations. * * @author Morgan Delagrange * @see XMLPreprocessr#findVariablesFromURI(String) */ public class FindVariablesHandler extends DefaultHandler { protected Set _set = new HashSet(); /** * After the parse is complete, this method returns * the unique variables referenced in the external entities. * * @return Set of unique variables */ public Set getVariables() { return _set; } /** * For each entity, find all the variables and assign them to the * Set. * * @param publicId public id of entity, as defined by SAX * @param systemId file URI of the external entity * @return InputSource used by SAX to find more entities * @exception SAXException * if the entity could not be read */ public InputSource resolveEntity(String publicId, String systemId) throws SAXException { XMLPreprocessor process = new XMLPreprocessor(); try { URL url = new URL(systemId); InputStreamReader reader = new InputStreamReader(url.openConnection().getInputStream()); _set.addAll(process.getLocalVariables(reader)); InputSource source = new InputSource(systemId); return source; } catch (IOException e) { e.printStackTrace(); throw new SAXException(e); } } }