diff --git a/java/src/org/apache/xml/resolver/Catalog.java b/java/src/org/apache/xml/resolver/Catalog.java index e731958..ab1d5fd 100644 --- a/java/src/org/apache/xml/resolver/Catalog.java +++ b/java/src/org/apache/xml/resolver/Catalog.java @@ -61,18 +61,23 @@ import java.io.FileNotFoundException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.io.DataInputStream; + import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; + import java.net.URL; import java.net.MalformedURLException; + +import javax.xml.parsers.SAXParserFactory; + import org.apache.xml.resolver.CatalogManager; import org.apache.xml.resolver.helpers.PublicId; import org.apache.xml.resolver.readers.CatalogReader; import org.apache.xml.resolver.readers.SAXCatalogReader; import org.apache.xml.resolver.readers.TR9401CatalogReader; import org.apache.xml.resolver.readers.OASISXMLCatalogReader; -import javax.xml.parsers.SAXParserFactory; +import org.apache.xml.resolver.helpers.FileURL; /** * Represents OASIS Open Catalog files. @@ -836,10 +841,10 @@ public class Catalog { // properly... try { // tack on a basename because URLs point to files not dirs - String userdir = fixSlashes(System.getProperty("user.dir")); - catalogCwd = new URL("file:" + userdir + "/basename"); + catalogCwd = FileURL.makeURL("basename"); } catch (MalformedURLException e) { - String userdir = fixSlashes(System.getProperty("user.dir")); + String userdir = System.getProperty("user.dir"); + userdir.replace('\\', '/'); catalogManager.debug.message(1, "Malformed URL on cwd", userdir); catalogCwd = null; } diff --git a/java/src/org/apache/xml/resolver/CatalogManager.java b/java/src/org/apache/xml/resolver/CatalogManager.java index e844464..fcfdc04 100644 --- a/java/src/org/apache/xml/resolver/CatalogManager.java +++ b/java/src/org/apache/xml/resolver/CatalogManager.java @@ -363,25 +363,40 @@ public class CatalogManager { * defaultVerbosity. */ private int queryVerbosity () { + String defaultVerbStr = Integer.toString(defaultVerbosity); + String verbStr = System.getProperty(pVerbosity); if (verbStr == null) { if (resources==null) readProperties(); - if (resources==null) return defaultVerbosity; - try { - verbStr = resources.getString("verbosity"); - } catch (MissingResourceException e) { - return defaultVerbosity; + if (resources != null) { + try { + verbStr = resources.getString("verbosity"); + } catch (MissingResourceException e) { + verbStr = defaultVerbStr; + } + } else { + verbStr = defaultVerbStr; } } + int verb = defaultVerbosity; + try { - int verb = Integer.parseInt(verbStr.trim()); - return verb; + verb = Integer.parseInt(verbStr.trim()); } catch (Exception e) { System.err.println("Cannot parse verbosity: \"" + verbStr + "\""); - return defaultVerbosity; } + + // This is a bit of a hack. After we've successfully got the verbosity, + // we have to use it to set the default debug level, + // if the user hasn't already set the default debug level. + if (verbosity == null) { + debug.setDebug(verb); + verbosity = new Integer(verb); + } + + return verb; } /** diff --git a/java/src/org/apache/xml/resolver/apps/XParseError.java b/java/src/org/apache/xml/resolver/apps/XParseError.java index 9dbbe11..7d3c18a 100644 --- a/java/src/org/apache/xml/resolver/apps/XParseError.java +++ b/java/src/org/apache/xml/resolver/apps/XParseError.java @@ -59,8 +59,10 @@ package org.apache.xml.resolver.apps; import java.net.URL; import java.net.MalformedURLException; -import org.xml.sax.*; -import org.xml.sax.helpers.*; +import org.xml.sax.SAXParseException; +import org.xml.sax.ErrorHandler; + +import org.apache.xml.resolver.helpers.FileURL; /** * An ErrorHandler for xparse. @@ -101,17 +103,8 @@ public class XParseError implements ErrorHandler { showErrors = errors; showWarnings = warnings; - String dir = System.getProperty("user.dir"); - String file = ""; - - if (dir.endsWith("/")) { - file = "file:" + dir + "file"; - } else { - file = "file:" + dir + "/" + file; - } - try { - URL url = new URL(file); + URL url = FileURL.makeURL("basename"); baseURI = url.toString(); } catch (MalformedURLException mue) { // nop; diff --git a/java/src/org/apache/xml/resolver/apps/resolver.java b/java/src/org/apache/xml/resolver/apps/resolver.java index 3003bad..b533f7b 100644 --- a/java/src/org/apache/xml/resolver/apps/resolver.java +++ b/java/src/org/apache/xml/resolver/apps/resolver.java @@ -62,15 +62,10 @@ import java.net.URL; import java.net.MalformedURLException; import java.util.Vector; -import org.xml.sax.*; -import org.xml.sax.helpers.*; - -import javax.xml.parsers.*; -import javax.xml.transform.*; - import org.apache.xml.resolver.Catalog; import org.apache.xml.resolver.CatalogManager; import org.apache.xml.resolver.helpers.Debug; +import org.apache.xml.resolver.helpers.FileURL; import org.apache.xml.resolver.tools.CatalogResolver; /** @@ -237,9 +232,7 @@ public class resolver { // Calculate the appropriate BASE URI try { // tack on a basename because URLs point to files not dirs - String userdir = System.getProperty("user.dir"); - userdir.replace('\\', '/'); - base = new URL("file:///" + userdir + "/basename"); + base = FileURL.makeURL("basename"); } catch (MalformedURLException e) { String userdir = System.getProperty("user.dir"); userdir.replace('\\', '/'); diff --git a/java/src/org/apache/xml/resolver/helpers/BootstrapResolver.java b/java/src/org/apache/xml/resolver/helpers/BootstrapResolver.java index 6b5caf3..88c9e39 100644 --- a/java/src/org/apache/xml/resolver/helpers/BootstrapResolver.java +++ b/java/src/org/apache/xml/resolver/helpers/BootstrapResolver.java @@ -225,17 +225,8 @@ public class BootstrapResolver implements EntityResolver, URIResolver { URL url = new URL(uri); return url.toString(); } catch (MalformedURLException mue) { - String dir = System.getProperty("user.dir"); - String file = ""; - - if (dir.endsWith("/")) { - file = "file://" + dir + uri; - } else { - file = "file://" + dir + "/" + uri; - } - try { - URL fileURL = new URL(file); + URL fileURL = FileURL.makeURL(uri); return fileURL.toString(); } catch (MalformedURLException mue2) { // bail diff --git a/java/src/org/apache/xml/resolver/helpers/FileURL.java b/java/src/org/apache/xml/resolver/helpers/FileURL.java new file mode 100644 index 0000000..ae264b2 --- /dev/null +++ b/java/src/org/apache/xml/resolver/helpers/FileURL.java @@ -0,0 +1,122 @@ +// FileURL.java - Construct a file: scheme URL + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 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 acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" 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 name, without prior written + * permission of the Apache Software Foundation. + * + * 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.xml.resolver.helpers; + +import java.net.URL; +import java.net.MalformedURLException; + +/** + * Static method for dealing with file: URLs. + * + *

This class defines a static method that can be used to construct + * an appropriate file: URL from parts. It's defined here so that it + * can be reused throught the resolver.

+ * + *

(Yes, I'd rather have called this class FileURI, but + * given that a jave.net.URL is returned, it seemed...even more + * confusing.)

+ * + * @author Norman Walsh + * Norman.Walsh@Sun.COM + * + * @version 1.0 + */ +public abstract class FileURL { + protected FileURL() { } + + /** + * Construct a file: URL for a path name. + * + *

URLs in the file: scheme can be constructed for paths on + * the local file system. Several possibilities need to be considered: + *

+ * + * + * + *

This method is declared static so that other classes + * can use it directly.

+ * + * @param pathname The path name component for which to construct a URL. + * + * @return The appropriate file: URL. + * + * @throws MalformedURLException if the pathname can't be turned into + * a proper URL. + */ + public static URL makeURL(String pathname) throws MalformedURLException { + if (pathname.startsWith("/")) { + return new URL("file://" + pathname); + } + + String userdir = System.getProperty("user.dir"); + userdir.replace('\\', '/'); + + if (userdir.endsWith("/")) { + return new URL("file://" + userdir + pathname); + } else { + return new URL("file://" + userdir + "/" + pathname); + } + } +} diff --git a/java/src/org/apache/xml/resolver/tools/CatalogResolver.java b/java/src/org/apache/xml/resolver/tools/CatalogResolver.java index 98eb1bb..0ff5c66 100644 --- a/java/src/org/apache/xml/resolver/tools/CatalogResolver.java +++ b/java/src/org/apache/xml/resolver/tools/CatalogResolver.java @@ -61,15 +61,21 @@ import java.io.InputStream; import java.net.URL; import java.net.MalformedURLException; -import org.xml.sax.*; -import org.xml.sax.helpers.*; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.InputSource; +import org.xml.sax.EntityResolver; -import javax.xml.parsers.*; -import javax.xml.transform.*; import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.Source; +import javax.xml.transform.URIResolver; +import javax.xml.transform.TransformerException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParserFactory; import org.apache.xml.resolver.Catalog; import org.apache.xml.resolver.CatalogManager; +import org.apache.xml.resolver.helpers.FileURL; /** * A SAX EntityResolver/JAXP URIResolver that uses catalogs. @@ -356,17 +362,8 @@ public class CatalogResolver implements EntityResolver, URIResolver { URL url = new URL(uri); return url.toString(); } catch (MalformedURLException mue) { - String dir = System.getProperty("user.dir"); - String file = ""; - - if (dir.endsWith("/")) { - file = "file://" + dir + uri; - } else { - file = "file://" + dir + "/" + uri; - } - try { - URL fileURL = new URL(file); + URL fileURL = FileURL.makeURL(uri); return fileURL.toString(); } catch (MalformedURLException mue2) { // bail diff --git a/java/src/org/apache/xml/resolver/tools/ResolvingParser.java b/java/src/org/apache/xml/resolver/tools/ResolvingParser.java index 35346ac..d06f291 100644 --- a/java/src/org/apache/xml/resolver/tools/ResolvingParser.java +++ b/java/src/org/apache/xml/resolver/tools/ResolvingParser.java @@ -62,12 +62,22 @@ import java.net.URL; import java.net.MalformedURLException; import java.util.Locale; -import org.xml.sax.*; +import org.xml.sax.Parser; +import org.xml.sax.InputSource; +import org.xml.sax.Locator; +import org.xml.sax.ErrorHandler; +import org.xml.sax.DTDHandler; +import org.xml.sax.DocumentHandler; +import org.xml.sax.AttributeList; +import org.xml.sax.EntityResolver; +import org.xml.sax.SAXException; -import javax.xml.parsers.*; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.parsers.SAXParser; import org.apache.xml.resolver.Catalog; import org.apache.xml.resolver.CatalogManager; +import org.apache.xml.resolver.helpers.FileURL; /** * A SAX Parser that performs catalog-based entity resolution. @@ -305,7 +315,7 @@ public class ResolvingParser } if (allowXMLCatalogPI) { - if (catalogManager.allowOasisXMLCatalogPI()) { + if (catalogManager.getAllowOasisXMLCatalogPI()) { catalogManager.debug.message(4,"oasis-xml-catalog PI", pidata); if (catalog != null) { @@ -433,12 +443,10 @@ public class ResolvingParser parser.setDocumentHandler(this); parser.setDTDHandler(this); - String userdir = System.getProperty("user.dir"); URL cwd = null; - userdir.replace('\\', '/'); try { - cwd = new URL("file:///" + userdir + "/basename"); + cwd = FileURL.makeURL("basename"); } catch (MalformedURLException mue) { cwd = null; } diff --git a/java/src/org/apache/xml/resolver/tools/ResolvingXMLFilter.java b/java/src/org/apache/xml/resolver/tools/ResolvingXMLFilter.java index 9af96ec..9a1dce2 100644 --- a/java/src/org/apache/xml/resolver/tools/ResolvingXMLFilter.java +++ b/java/src/org/apache/xml/resolver/tools/ResolvingXMLFilter.java @@ -61,14 +61,17 @@ import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; -import org.xml.sax.*; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.Attributes; import org.xml.sax.helpers.XMLFilterImpl; -import javax.xml.parsers.*; - import org.apache.xml.resolver.Catalog; import org.apache.xml.resolver.CatalogManager; +import org.apache.xml.resolver.helpers.FileURL; + /** * A SAX XMLFilter that performs catalog-based entity resolution. * @@ -313,7 +316,7 @@ public class ResolvingXMLFilter extends XMLFilterImpl { } if (allowXMLCatalogPI) { - if (catalogManager.allowOasisXMLCatalogPI()) { + if (catalogManager.getAllowOasisXMLCatalogPI()) { catalogManager.debug.message(4,"oasis-xml-catalog PI", pidata); if (catalog != null) { @@ -347,12 +350,10 @@ public class ResolvingXMLFilter extends XMLFilterImpl { /** Save the base URI of the document being parsed. */ private void setupBaseURI(String systemId) { - String userdir = System.getProperty("user.dir"); URL cwd = null; - userdir.replace('\\', '/'); try { - cwd = new URL("file:///" + userdir + "/basename"); + cwd = FileURL.makeURL("basename"); } catch (MalformedURLException mue) { cwd = null; }