dion 02/02/05 23:08:29 Modified: latka/src/java/org/apache/commons/latka/xml XMLPreprocessor.java Log: Reformatted, fixed javadoc, removed some unnecessary exceptions Added FIXME's for dubious bits Revision Changes Path 1.17 +307 -295 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.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- XMLPreprocessor.java 5 Jan 2002 05:34:50 -0000 1.16 +++ XMLPreprocessor.java 6 Feb 2002 07:08:29 -0000 1.17 @@ -84,7 +84,6 @@ import org.apache.commons.latka.LatkaProperties; import org.apache.commons.latka.Suite; -import org.apache.commons.latka.xml.LatkaVariableEntityResolver; import org.apache.log4j.Category; @@ -100,345 +99,358 @@ * stream with Latka variables. * * @author Morgan Delagrange + * @author dIon Gillard * @see org.apache.commons.latka.LatkaProperties + * @version $Id: XMLPreprocessor.java,v 1.17 2002/02/06 07:08:29 dion Exp $ */ public class XMLPreprocessor { + // FIXME: format here should be all caps? + /** Regular expression for variable name */ + protected static final String _variableExpr = "\\$\\{(.*?)\\}"; + + /** log4j Category for output to be logged to */ + protected static Category _log = + Category.getInstance(XMLPreprocessor.class); - protected static final String _variableExpr = "\\$\\{(.*?)\\}"; + /** singleton used to preprocess */ + protected static XMLPreprocessor _processor = new XMLPreprocessor(); + + /** + * Private constructor to stop auto generation of public one + */ + private XMLPreprocessor() { + } + + /** + * Get an instance of this singleton class + * + * @return an XML preprocessor object + */ + public static XMLPreprocessor instance() { + return _processor; + } + + /** + * Processes a Suite, preparing it for the final XML + * processing. In essence, this consists of finding + * variables in the base XML document, looking for corresponding Latka + * properties, and performing the substitutions. + * This will only preprocess the base XML document; + * the entities will be processed during the SAX parse + * itself. After running this method, it is guaranteed + * that the preprocessed document will be stored in + * Suite.getReader(). + * + * @param suite Latka suite to preprocess + * @throws IOException thrown if the stream could not be read, + * or if variables could not be substituted + * + * @see LatkaVariableEntityResolver#resolveEntity(String,String) + * @see org.apache.commons.latka.Latka#runTests(Suite,LatkaEventInfo) + * Latka.runTests(Suite,LatkaEventInfo) + */ + public void preprocessXml(Suite suite) throws IOException { + + // process the base document + String processedString = null; + if (suite.getReader() != null) { + processedString = resolveVariables( + stringFromReader(suite.getReader())); + } else { + processedString = resolveVariables(suite.getURL()); + } + suite.setReader(new StringReader(processedString)); + + // entities will be processed during the SAX parse + } - protected static Category _log = - Category.getInstance(XMLPreprocessor.class); + /** + * Given an XML suite, find the names + * of all variables that must be set in the LatkaProperties. + * Will use SAX to recurse through external entities. + * + * @param suite A Latka suite + * @throws IOException when variables can't be resolved or resources + * aren't accessible + * @return Array of variables that must be set. If no variables + * are present in the suite, a zero-length Set is + * returned. + * @see org.apache.commons.latka.LatkaProperties + */ + public Set findVariables(Suite suite) throws IOException { + + // first, get variables for the base document + String xml = null; + if (suite.getReader() != null) { + xml = stringFromReader(suite.getReader()).trim(); + } else { + Reader reader = new InputStreamReader(suite + .getURL() + .openConnection() + .getInputStream()); + xml = stringFromReader(reader).trim(); + } + // accumulate the sets from the Reader and all + // external entities here + Set set = getLocalVariables(xml); + + // now that we have variables from the initial document, + // perform a SAX parse to find additional variables + // in external entities + InputSource source = new InputSource(new StringReader(xml)); + if (suite.getURL() != null) { + source.setSystemId(suite.getURL().toString()); + } + set.addAll(getVariablesFromEntities(source)); - protected static XMLPreprocessor _processor = new XMLPreprocessor(); + return set; - private XMLPreprocessor() { + } - } - - - /** - * Get an instance of this singleton class - * - * @return an XML preprocessor object - */ - public static XMLPreprocessor instance() { - return _processor; - } - - /** - * Processes a Suite, preparing it for the final XML - * processing. In essence, this consists of finding - * variables in the base XML document, looking for corresponding Latka - * properties, and performing the substitutions. - * This will only preprocess the base XML document; - * the entities will be processed during the SAX parse - * itself. After running this method, it is guaranteed - * that the preprocessed document will be stored in - * Suite.getReader(). - * - * @param reader Latka suite to preprocess - * @exception IOException - * thrown if the stream could not be read, - * or if variables could not be substituted - * - * @see LatkaVariableEntityResolver#resolveEntity(String,String) - * @see org.apache.commons.latka.Latka#runTests(Suite,LatkaEventInfo) - * Latka.runTests(Suite,LatkaEventInfo) - */ - public void preprocessXml(Suite suite) throws IOException { - - // process the base document - String processedString = null; - if (suite.getReader() != null) { - processedString = resolveVariables(stringFromReader(suite.getReader())); - } else { - processedString = resolveVariables(suite.getURL()); - } - suite.setReader(new StringReader(processedString)); - - // entities will be processed during the SAX parse - } - - /** - * Given an XML suite, find the names - * of all variables that must be set in the LatkaProperties. - * Will use SAX to recurse through external entities. - * - * @param reader A Latka suite - * @return Array of variables that must be set. If no variables - * are present in the suite, a zero-length Set is - * returned. - * @see org.apache.commons.latka.LatkaProperties - */ - public Set findVariables(Suite suite) - throws IOException { - - // first, get variables for the base document - String xml = null; - if (suite.getReader() != null) { - xml = stringFromReader(suite.getReader()).trim(); - } else { - Reader reader = new InputStreamReader(suite.getURL().openConnection().getInputStream()); - xml = stringFromReader(reader).trim(); - } - // accumulate the sets from the Reader and all - // external entities here - Set set = getLocalVariables(xml); - - // now that we have variables from the initial document, - // perform a SAX parse to find additional variables - // in external entities - InputSource source = new InputSource(new StringReader(xml)); - if (suite.getURL() != null) { - source.setSystemId(suite.getURL().toString()); - } - set.addAll(getVariablesFromEntities(source)); - - return set; - - } - - /** - * @deprecated Spelling -- use {@link #getVariablesFromEntities} instead. - */ - protected Set getVariablesFromEntites(InputSource inputSource) - throws IOException { - return getVariablesFromEntities(inputSource); - } - - /** - * 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 getVariablesFromEntities(InputSource inputSource) - throws IOException { - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); + /** + * @param inputSource source of xml to use for resolution + * @throws IOException when variables can't be resolved + * @return a set of Latka variables + * @deprecated Spelling -- use {@link #getVariablesFromEntities} instead. + */ + protected Set getVariablesFromEntites(InputSource inputSource) + throws IOException { + return getVariablesFromEntities(inputSource); + } + + /** + * 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 + * @throws IOException if any errors occur during the XML processing + */ + protected Set getVariablesFromEntities(InputSource inputSource) + throws IOException { + SAXParserFactory factory = SAXParserFactory.newInstance(); + factory.setNamespaceAware(true); - FindVariablesHandler handler = - new FindVariablesHandler(); + FindVariablesHandler handler = new FindVariablesHandler(); - try { - SAXParser parser = factory.newSAXParser(); + 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()); - } + parser.parse(inputSource, handler); + } catch (ParserConfigurationException e) { + throw new IOException(e.toString()); + } catch (SAXException e) { + throw new IOException(e.toString()); + } - return handler.getVariables(); - } + return handler.getVariables(); + } - /** - * - * @see #getLocalVariables(String) - */ - protected Set getLocalVariables(Reader reader) throws IOException { - String xmlString = stringFromReader(reader).trim(); - return getLocalVariables(xmlString); - } - - /** - * Read the text and find the names of all variables - * that must be set in the LatkaProperties. This method does not - * recurse through entities like findVariables. - * - * @param a Latka suite fragment - * @return Set of variables used in this fragment. If no variables - * are present in the suite, a zero-length Set is - * returned. - * @see org.apache.commons.latka.LatkaProperties - */ - protected Set getLocalVariables(String xmlString) throws IOException { + /** + * + * @see #getLocalVariables(String) + */ + protected Set getLocalVariables(Reader reader) throws IOException { + String xmlString = stringFromReader(reader).trim(); + return getLocalVariables(xmlString); + } + + /** + * Read the text and find the names of all variables + * that must be set in the LatkaProperties. This method does not + * recurse through entities like findVariables. + * + * @param xmlString a Latka suite fragment + * @return Set of variables used in this fragment. If no variables + * are present in the suite, a zero-length Set is + * returned. + * @see org.apache.commons.latka.LatkaProperties + */ + protected Set getLocalVariables(String xmlString) { - xmlString = stripXmlComments(xmlString); - - Set set = new TreeSet(); + xmlString = stripXmlComments(xmlString); - try { - RE r = new RE(_variableExpr); // Compile expression + Set set = new TreeSet(); - //scan the input string match by match - int bufIndex = 0; + try { + RE r = new RE(_variableExpr); // Compile expression - while (r.match(xmlString, bufIndex)) { - set.add(r.getParen(1)); - bufIndex = r.getParenEnd(0); - } - } catch (RESyntaxException e) { - e.printStackTrace(); - } + //scan the input string match by match + int bufIndex = 0; + + while (r.match(xmlString, bufIndex)) { + set.add(r.getParen(1)); + bufIndex = r.getParenEnd(0); + } + } catch (RESyntaxException e) { + // FIXME: should this really be swallowed? + e.printStackTrace(); + } - return set; + return set; - } + } - /** - * @see #resolveVariables(String) - */ - protected String resolveVariables(URL url) throws IOException { - Reader reader = new InputStreamReader(url.openConnection().getInputStream()); - return resolveVariables(stringFromReader(reader)); - } + /** + * @see #resolveVariables(String) + */ + protected String resolveVariables(URL url) throws IOException { + Reader reader = new InputStreamReader(url.openConnection() + .getInputStream()); + return resolveVariables(stringFromReader(reader)); + } - /** - * Given an XML suite, find the corresponding values - * of all variables and write them into the stream. - * - * @param xmlString XML String representing a Latka suite - * @return XML String with all variables resolved - * @exception IOException - * Thrown if a Latka variables could not be resolved - */ - protected String resolveVariables(String xmlString) throws IOException { + /** + * Given an XML suite, find the corresponding values + * of all variables and write them into the stream. + * + * @param xmlString XML String representing a Latka suite + * @return XML String with all variables resolved + * @throws IOException if a Latka variable could not be resolved + */ + protected String resolveVariables(String xmlString) throws IOException { - StringBuffer output = new StringBuffer(); + StringBuffer output = new StringBuffer(); - Properties props = LatkaProperties.getProperties(); + Properties props = LatkaProperties.getProperties(); - try { + try { - xmlString = stripXmlComments(xmlString); - - // now, replace the remaining variables - RE r = new RE(_variableExpr); // Compile expression + xmlString = stripXmlComments(xmlString); - //scan the input string match by match, writing to the buffer - int bufIndex = 0; + // now, replace the remaining variables + RE r = new RE(_variableExpr); // Compile expression - while (r.match(xmlString, bufIndex)) { - // append everything to the beginning of the match - output.append(xmlString.substring(bufIndex,r.getParenStart(0))); - // move marker to the end of the match - bufIndex = r.getParenEnd(0); + //scan the input string match by match, writing to the buffer + int bufIndex = 0; - String prop = props.getProperty(r.getParen(1)); - if (prop == null) { - throw new IOException("Property " + r.getParen(1) + " was not defined."); + while (r.match(xmlString, bufIndex)) { + // append everything to the beginning of the match + output.append(xmlString.substring(bufIndex, + r.getParenStart(0))); + // move marker to the end of the match + bufIndex = r.getParenEnd(0); + + String prop = props.getProperty(r.getParen(1)); + if (prop == null) { + throw new IOException("Property " + + r.getParen(1) + + " was not defined."); + } + // append the substituted value to the end of the output + output.append(prop); + } + + // grab anything remaining that did not match + output.append(xmlString.substring(bufIndex, xmlString.length())); + } catch (RESyntaxException e) { + // FIXME: Should this really be swallowed? + e.printStackTrace(); } - // append the substituted value to the end of the output - output.append(prop); - } - - // grab anything remaining that did not match - output.append(xmlString.substring(bufIndex,xmlString.length())); - - } catch (RESyntaxException e) { - e.printStackTrace(); - } - - String outputString = output.toString(); + String outputString = output.toString(); - return outputString; + return outputString; - } - - /** - * Given a Latka suite, resolve all the entites - * (bot _not_ the Latka variables), and return one flattened - * XML document. This utility method is useful for - * viewing an XML suite as a single document. - * - * @param suite a Latka suite - * @return flattened XML - * @exception IOException - * if the Latka suite could not be transformed - */ - public String identityTransform(Suite suite) throws IOException { - - _log.debug("Setting up transform streams"); - - StreamSource source = new StreamSource(); - if (suite.getReader() != null) { - _log.debug("Setting input reader"); - source.setReader(suite.getReader()); - } - if (suite.getURL() != null) { - _log.debug("Setting input URL"); - source.setSystemId(suite.getURL().toString()); } - StringWriter output = new StringWriter(); - StreamResult result = new StreamResult(output); + /** + * Given a Latka suite, resolve all the entites + * (but _not_ the Latka variables), and return one flattened + * XML document. This utility method is useful for + * viewing an XML suite as a single document. + * + * @param suite a Latka suite + * @return flattened XML + * @throws IOException if the Latka suite could not be transformed + */ + public String identityTransform(Suite suite) throws IOException { + + _log.debug("Setting up transform streams"); + + StreamSource source = new StreamSource(); + if (suite.getReader() != null) { + _log.debug("Setting input reader"); + source.setReader(suite.getReader()); + } + if (suite.getURL() != null) { + _log.debug("Setting input URL"); + source.setSystemId(suite.getURL().toString()); + } + + StringWriter output = new StringWriter(); + StreamResult result = new StreamResult(output); - _log.debug("Performing transformation"); + _log.debug("Performing transformation"); - try { - // we should be able to do identity transforms via TransformerFactory.newTransformer(), - // but this seems broken in Xalan 2.1.0, so... - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - StreamSource xslSource = - new StreamSource(loader.getResourceAsStream("org.apache.commons.latka.identityTransform.xsl")); + try { + // we should be able to do identity transforms via + // TransformerFactory.newTransformer(), but this seems broken in + // Xalan 2.1.0, so... + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + StreamSource xslSource = new StreamSource( + loader.getResourceAsStream( + "org.apache.commons.latka.identityTransform.xsl")); - Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource); - transformer.transform(source,result); - } catch (TransformerException e) { - _log.error(e); - throw new IOException(e.toString()); - } - - if (_log.isDebugEnabled()) { - _log.debug("Transformation output: \n" + output.toString()); - } + Transformer transformer = TransformerFactory.newInstance() + .newTransformer(xslSource); + transformer.transform(source, result); + } catch (TransformerException e) { + _log.error(e); + throw new IOException(e.toString()); + } - return output.toString(); + if (_log.isDebugEnabled()) { + _log.debug("Transformation output: \n" + output.toString()); + } - } + return output.toString(); - /** - * Remove any comments from the XmlString - * - * @param xmlString String with XML comments - * @return String with no XML comments - */ - protected String stripXmlComments(String xmlString) { - try { - //remove anything commented out - RE removeComments = new RE("<!--.*?-->",RE.MATCH_SINGLELINE); - xmlString = removeComments.subst(xmlString,""); - } catch (RESyntaxException e) { - e.printStackTrace(); } - 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(); + /** + * Remove any comments from the XmlString + * + * @param xmlString String with XML comments + * @return String with no XML comments + */ + protected String stripXmlComments(String xmlString) { + try { + //remove anything commented out + RE removeComments = new RE("<!--.*?-->", RE.MATCH_SINGLELINE); + xmlString = removeComments.subst(xmlString, ""); + } catch (RESyntaxException e) { + // FIXME: Should this really be swallowed? + e.printStackTrace(); + } - String line = buffReader.readLine(); - while (line != null) { - buff.append(line); - buff.append("\n"); - line = buffReader.readLine(); + return xmlString; } - reader.close(); - - return buff.toString(); - } + /** + * 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(); + + // FIXME: Is this really the most efficient way to do it, per line? + String line = buffReader.readLine(); + while (line != null) { + buff.append(line); + buff.append("\n"); + line = buffReader.readLine(); + } + reader.close(); + return buff.toString(); + } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>