From a7a6e7a162348f37f0e312f3715b9e581f60e0e1 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 31 Oct 2002 14:30:08 +0000 Subject: [PATCH] Merge Stephane's system-id refactoring into HEAD. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273479 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 4 +- .../ant/taskdefs/optional/TraXLiaison.java | 32 +++----- .../optional/junit/AggregateTransformer.java | 17 ++-- .../org/apache/tools/ant/util/JAXPUtils.java | 27 +++++++ .../taskdefs/optional/TraXLiaisonTest.java | 1 - .../apache/tools/ant/util/JAXPUtilsTest.java | 80 +++++++++++++++++++ 6 files changed, 129 insertions(+), 32 deletions(-) create mode 100644 src/testcases/org/apache/tools/ant/util/JAXPUtilsTest.java diff --git a/WHATSNEW b/WHATSNEW index 96b39a9ec..96e2f2b94 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -22,8 +22,8 @@ Fixed bugs: * wouldn't update an existing manifest if only an attribute of an existing section changed. -* ant.bat now supports the ANT_ARGS environment variable again (like - Ant 1.5 did). +* ant.bat now supports the ANT_ARGS and JAVACMD environment variables + again (like Ant 1.5 did). * The "plain" could throw a NullPointerException if an error occured in setUp. diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java b/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java index a977acd18..dab1fb802 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java @@ -79,6 +79,7 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.taskdefs.XSLTLiaison; import org.apache.tools.ant.taskdefs.XSLTLogger; import org.apache.tools.ant.taskdefs.XSLTLoggerAware; +import org.apache.tools.ant.util.JAXPUtils; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; @@ -144,7 +145,7 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware fos = new BufferedOutputStream(new FileOutputStream(outfile)); StreamResult res = new StreamResult(fos); // not sure what could be the need of this... - res.setSystemId(getSystemId(outfile)); + res.setSystemId(JAXPUtils.getSystemId(outfile)); Source src = getSource(fis, infile); transformer.transform(src, res); } finally { @@ -192,7 +193,7 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware } else { src = new StreamSource(is); } - src.setSystemId(getSystemId(infile)); + src.setSystemId(JAXPUtils.getSystemId(infile)); return src; } @@ -218,7 +219,7 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware StreamSource src = new StreamSource(xslStream); // Always set the systemid to the source for imports, includes... // in xsl and xml... - src.setSystemId(getSystemId(stylesheet)); + src.setSystemId(JAXPUtils.getSystemId(stylesheet)); Templates templates = getFactory().newTemplates(src); Transformer transformer = templates.newTransformer(); @@ -244,23 +245,6 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware } } - // make sure that the systemid is made of '/' and not '\' otherwise - // crimson will complain that it cannot resolve relative entities - // because it grabs the base uri via lastIndexOf('/') without - // making sure it is really a /'ed path - protected String getSystemId(File file) { - String path = file.getAbsolutePath(); - path = path.replace('\\', '/'); - - // on Windows, use 'file:///' - if (File.separatorChar == '\\') { - return FILE_PROTOCOL_PREFIX + "/" + path; - } - // Unix, use 'file://' - return FILE_PROTOCOL_PREFIX + path; - } - - /** * return the Transformer factory associated to this liaison. * @return the Transformer factory associated to this liaison. @@ -400,4 +384,12 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware logger.log(msg.toString()); } + // kept for backwards compatibility + /** + * @deprecated use org.apache.tools.ant.util.JAXPUtils#getSystemId instead + */ + protected String getSystemId(File file) { + return JAXPUtils.getSystemId(file); + } + } //-- TraXLiaison diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/AggregateTransformer.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/AggregateTransformer.java index 99bceb0cc..83b4df132 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/AggregateTransformer.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/AggregateTransformer.java @@ -64,6 +64,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; +import org.apache.tools.ant.util.JAXPUtils; import org.apache.tools.ant.types.EnumeratedAttribute; import org.w3c.dom.Document; @@ -212,20 +213,18 @@ public class AggregateTransformer { if (NOFRAMES.equals(format)){ xslname = "junit-noframes.xsl"; } - URL url = null; if (styleDir == null){ - url = getClass().getResource("xsl/" + xslname); + URL url = getClass().getResource("xsl/" + xslname); if (url == null){ throw new FileNotFoundException("Could not find jar resource " + xslname); } - } else { - File file = new File(styleDir, xslname); - if (!file.exists()){ - throw new FileNotFoundException("Could not find file '" + file + "'"); - } - url = new URL("file", "", file.getAbsolutePath()); + return url.toExternalForm(); + } + File file = new File(styleDir, xslname); + if (!file.exists()){ + throw new FileNotFoundException("Could not find file '" + file + "'"); } - return url.toExternalForm(); + return JAXPUtils.getSystemId(file); } } diff --git a/src/main/org/apache/tools/ant/util/JAXPUtils.java b/src/main/org/apache/tools/ant/util/JAXPUtils.java index 62527dc4f..7f6b4256e 100644 --- a/src/main/org/apache/tools/ant/util/JAXPUtils.java +++ b/src/main/org/apache/tools/ant/util/JAXPUtils.java @@ -53,6 +53,7 @@ */ package org.apache.tools.ant.util; +import java.io.File; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; @@ -74,6 +75,11 @@ import org.xml.sax.XMLReader; */ public class JAXPUtils { + /** + * The file protocol: 'file://' + */ + private static final String FILE_PROTOCOL_PREFIX = "file://"; + /** * Parser factory to use to create parsers. * @see #getParserFactory @@ -147,6 +153,27 @@ public class JAXPUtils { } } + /** + * This is a best attempt to provide a URL.toExternalForm() from + * a file URL. Some parsers like Crimson choke on uri that are made of + * backslashed paths (ie windows) as it is does not conform + * URI specifications. + * @param file the file to create the system id from. + * @return the systemid corresponding to the given file. + * @since Ant 1.5.2 + */ + public static String getSystemId(File file){ + String path = file.getAbsolutePath(); + path = path.replace('\\', '/'); + + // on Windows, use 'file:///' + if (File.separatorChar == '\\') { + return FILE_PROTOCOL_PREFIX + "/" + path; + } + // Unix, use 'file://' + return FILE_PROTOCOL_PREFIX + path; + } + /** * @return a new SAXParser instance as helper for getParser and * getXMLReader. diff --git a/src/testcases/org/apache/tools/ant/taskdefs/optional/TraXLiaisonTest.java b/src/testcases/org/apache/tools/ant/taskdefs/optional/TraXLiaisonTest.java index b0988c538..c2b68c0cd 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/optional/TraXLiaisonTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/optional/TraXLiaisonTest.java @@ -2,7 +2,6 @@ package org.apache.tools.ant.taskdefs.optional; import org.apache.tools.ant.taskdefs.XSLTLiaison; import org.apache.tools.ant.taskdefs.XSLTLogger; -import org.apache.tools.ant.taskdefs.condition.Os; import org.apache.tools.ant.BuildException; import java.io.File; diff --git a/src/testcases/org/apache/tools/ant/util/JAXPUtilsTest.java b/src/testcases/org/apache/tools/ant/util/JAXPUtilsTest.java new file mode 100644 index 000000000..416e5fe34 --- /dev/null +++ b/src/testcases/org/apache/tools/ant/util/JAXPUtilsTest.java @@ -0,0 +1,80 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 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", "Ant", 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 apache@apache.org. + * + * 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 + * . + */ +package org.apache.tools.ant.util; + +import junit.framework.TestCase; + +import java.io.File; + +/** + * JAXPUtils test case + * @author Stephane Bailliez + */ +public class JAXPUtilsTest extends TestCase { + public JAXPUtilsTest(String name){ + super(name); + } + + public void testGetSystemId(){ + File file = null; + if ( File.separatorChar == '\\' ){ + file = new File("d:\\jdk"); + } else { + file = new File("/user/local/bin"); + } + String systemid = JAXPUtils.getSystemId(file); + assertTrue("SystemIDs should start by file:///", systemid.startsWith("file:///")); + assertTrue("SystemIDs should not start with file:////", !systemid.startsWith("file:////")); + } +}