Initial checkin of Sun Resolver 1.1 sources
git-svn-id: https://svn.apache.org/repos/asf/xml/commons/trunk@225950 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
1 parent
32b041f81d
commit
92f652862f
35 files changed
+9761
No files matched your search
@@ -0,0 +1,200 @@
|
||||
// XParseError.java - An error handler for xparse
|
||||
|
||||
/*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2001 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 "Xalan" 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 and was
|
||||
* originally based on software copyright (c) 2001, International
|
||||
* Business Machines Corporation., http://www.ibm.com. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.xml.resolver.apps;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import org.xml.sax.*;
|
||||
import org.xml.sax.helpers.*;
|
||||
|
||||
/**
|
||||
* <p>An ErrorHandler for xparse.</p>
|
||||
*
|
||||
* <p>This class is just the error handler for xparse.</p>
|
||||
*
|
||||
* @see xparse
|
||||
*
|
||||
* @author Norman Walsh
|
||||
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
|
||||
*
|
||||
* @version 1.0
|
||||
*/
|
||||
public class XParseError implements ErrorHandler {
|
||||
/** Show errors? */
|
||||
private boolean showErrors = true;
|
||||
|
||||
/** Show warnings? */
|
||||
private boolean showWarnings = false;
|
||||
|
||||
/** How many messages should be presented? */
|
||||
private int maxMessages = 10;
|
||||
|
||||
/** The number of fatal errors seen so far. */
|
||||
private int fatalCount = 0;
|
||||
|
||||
/** The number of errors seen so far. */
|
||||
private int errorCount = 0;
|
||||
|
||||
/** The number of warnings seen so far. */
|
||||
private int warningCount = 0;
|
||||
|
||||
/** The base URI of the running application. */
|
||||
private String baseURI = "";
|
||||
|
||||
/** Constructor */
|
||||
public XParseError(boolean errors, boolean warnings) {
|
||||
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);
|
||||
baseURI = url.toString();
|
||||
} catch (MalformedURLException mue) {
|
||||
// nop;
|
||||
}
|
||||
}
|
||||
|
||||
/** Return the error count */
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
/** Return the fatal error count */
|
||||
public int getFatalCount() {
|
||||
return fatalCount;
|
||||
}
|
||||
|
||||
/** Return the warning count */
|
||||
public int getWarningCount() {
|
||||
return warningCount;
|
||||
}
|
||||
|
||||
/** Return the number of messages to display */
|
||||
public int getMaxMessages() {
|
||||
return maxMessages;
|
||||
}
|
||||
|
||||
/** Set the number of messages to display */
|
||||
public void setMaxMessages(int max) {
|
||||
maxMessages = max;
|
||||
}
|
||||
|
||||
/** SAX2 API */
|
||||
public void error(SAXParseException exception) {
|
||||
if (showErrors) {
|
||||
if (errorCount+warningCount < maxMessages) {
|
||||
message("Error", exception);
|
||||
}
|
||||
errorCount++;
|
||||
}
|
||||
}
|
||||
|
||||
/** SAX2 API */
|
||||
public void fatalError(SAXParseException exception) {
|
||||
if (showErrors) {
|
||||
if (errorCount+warningCount < maxMessages) {
|
||||
message("Fatal error", exception);
|
||||
}
|
||||
errorCount++;
|
||||
fatalCount++;
|
||||
}
|
||||
}
|
||||
|
||||
/** SAX2 API */
|
||||
public void warning(SAXParseException exception) {
|
||||
if (showWarnings) {
|
||||
if ((errorCount+warningCount < maxMessages)) {
|
||||
message("Warning", exception);
|
||||
}
|
||||
warningCount++;
|
||||
}
|
||||
}
|
||||
|
||||
/** Display a message to the user */
|
||||
private void message(String type, SAXParseException exception) {
|
||||
String filename = exception.getSystemId();
|
||||
if (filename.startsWith(baseURI)) {
|
||||
filename = filename.substring(baseURI.length());
|
||||
}
|
||||
|
||||
System.out.print(type
|
||||
+ ":"
|
||||
+ filename
|
||||
+ ":"
|
||||
+ exception.getLineNumber());
|
||||
|
||||
if (exception.getColumnNumber() > 0) {
|
||||
System.out.print(":" + exception.getColumnNumber());
|
||||
}
|
||||
|
||||
System.out.println(":" + exception.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Apache XML Commons Resolver Applications package</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Apache XML Commons Resolver Applications package</p>
|
||||
|
||||
<p>This package provides a few simple command-line tools for exploring
|
||||
the facilities of the resolver.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,338 @@
|
||||
// resolver.java - A simple command-line test tool for the resolver
|
||||
|
||||
/*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2001 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 "Xalan" 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 and was
|
||||
* originally based on software copyright (c) 2001, International
|
||||
* Business Machines Corporation., http://www.ibm.com. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.xml.resolver.apps;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.Vector;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.xml.sax.*;
|
||||
import org.xml.sax.helpers.*;
|
||||
|
||||
import javax.xml.parsers.*;
|
||||
import javax.xml.transform.*;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
|
||||
import org.apache.xml.resolver.Catalog;
|
||||
import org.apache.xml.resolver.tools.CatalogResolver;
|
||||
import org.apache.xml.resolver.helpers.Debug;
|
||||
import org.apache.xml.resolver.tools.CatalogResolver;
|
||||
|
||||
/**
|
||||
* <p>A simple command-line resolver.</p>
|
||||
*
|
||||
* <p>This class implements a simple command-line resolver. It takes
|
||||
* some parameters and passes them through the resolver, printing the
|
||||
* result.
|
||||
* </p>
|
||||
*
|
||||
* <p>Usage: resolver [options] keyword</p>
|
||||
*
|
||||
* <p>Where options are:</p>
|
||||
*
|
||||
* <dl>
|
||||
* <dt><code>-c</code> <em>catalogfile</em></dt>
|
||||
* <dd>Load a particular catalog file.</dd>
|
||||
* <dt><code>-n</code> <em>name</em></dt>
|
||||
* <dd>Sets the name.</dd>
|
||||
* <dt><code>-p</code> <em>publicId</em></dt>
|
||||
* <dd>Sets the public identifier.</dd>
|
||||
* <dt><code>-s</code> <em>systemId</em></dt>
|
||||
* <dd>Sets the system identifier.</dd>
|
||||
* <dt><code>-a</code></dt>
|
||||
* <dd>Absolute system URI.</dd>
|
||||
* <dt><code>-u</code> <em>uri</em></dt>
|
||||
* <dd>Sets the URI.</dd>
|
||||
* <dt><code>-d</code> <em>integer</em></dt>
|
||||
* <dd>Set the debug level.</dd>
|
||||
* </dl>
|
||||
*
|
||||
* <p>And keyword is one of: doctype, document, entity, notation, public,
|
||||
* system, or uri.</p>
|
||||
*
|
||||
* <p>The process ends with error-level 1, if there errors.</p>
|
||||
*
|
||||
* @see org.apache.xml.resolver.tools.ResolvingParser
|
||||
*
|
||||
* @author Norman Walsh
|
||||
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
|
||||
*
|
||||
* @version 1.0 */
|
||||
public class resolver {
|
||||
/** The main entry point */
|
||||
public static void main (String[] args)
|
||||
throws FileNotFoundException, IOException {
|
||||
|
||||
int debug = 0;
|
||||
Vector catalogFiles = new Vector();
|
||||
int resType = 0;
|
||||
String resTypeStr = null;
|
||||
String name = null;
|
||||
String publicId = null;
|
||||
String systemId = null;
|
||||
String uri = null;
|
||||
boolean absoluteSystem = false;
|
||||
|
||||
for (int i=0; i<args.length; i++) {
|
||||
if (args[i].equals("-c")) {
|
||||
++i;
|
||||
catalogFiles.add(args[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-p")) {
|
||||
++i;
|
||||
publicId = args[i];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-s")) {
|
||||
++i;
|
||||
systemId = args[i];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-n")) {
|
||||
++i;
|
||||
name = args[i];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-u")) {
|
||||
++i;
|
||||
uri = args[i];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-a")) {
|
||||
absoluteSystem = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-d")) {
|
||||
++i;
|
||||
String debugstr = args[i];
|
||||
try {
|
||||
debug = Integer.parseInt(debugstr);
|
||||
if (debug >= 0) {
|
||||
Debug.setDebug(debug);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// nop
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
resTypeStr = args[i];
|
||||
}
|
||||
|
||||
if (resTypeStr == null) {
|
||||
usage();
|
||||
}
|
||||
|
||||
if (resTypeStr.equalsIgnoreCase("doctype")) {
|
||||
resType = Catalog.DOCTYPE;
|
||||
if (publicId == null && systemId == null) {
|
||||
System.out.println("DOCTYPE requires public or system identifier.");
|
||||
usage();
|
||||
}
|
||||
} else if (resTypeStr.equalsIgnoreCase("document")) {
|
||||
resType = Catalog.DOCUMENT;
|
||||
} else if (resTypeStr.equalsIgnoreCase("entity")) {
|
||||
resType = Catalog.ENTITY;
|
||||
if (publicId == null && systemId == null && name == null) {
|
||||
System.out.println("ENTITY requires name or public or system identifier.");
|
||||
usage();
|
||||
}
|
||||
} else if (resTypeStr.equalsIgnoreCase("notation")) {
|
||||
resType = Catalog.NOTATION;
|
||||
if (publicId == null && systemId == null && name == null) {
|
||||
System.out.println("NOTATION requires name or public or system identifier.");
|
||||
usage();
|
||||
}
|
||||
} else if (resTypeStr.equalsIgnoreCase("public")) {
|
||||
resType = Catalog.PUBLIC;
|
||||
if (publicId == null) {
|
||||
System.out.println("PUBLIC requires public identifier.");
|
||||
usage();
|
||||
}
|
||||
} else if (resTypeStr.equalsIgnoreCase("system")) {
|
||||
resType = Catalog.SYSTEM;
|
||||
if (systemId == null) {
|
||||
System.out.println("SYSTEM requires system identifier.");
|
||||
usage();
|
||||
}
|
||||
} else if (resTypeStr.equalsIgnoreCase("uri")) {
|
||||
resType = Catalog.URI;
|
||||
if (uri == null) {
|
||||
System.out.println("URI requires a uri.");
|
||||
usage();
|
||||
}
|
||||
} else {
|
||||
System.out.println(resTypeStr + " is not a recognized keyword.");
|
||||
usage();
|
||||
}
|
||||
|
||||
if (absoluteSystem) {
|
||||
URL base = null;
|
||||
URL sysid = null;
|
||||
|
||||
// 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");
|
||||
} catch (MalformedURLException e) {
|
||||
String userdir = System.getProperty("user.dir");
|
||||
userdir.replace('\\', '/');
|
||||
Debug.message(1, "Malformed URL on cwd", userdir);
|
||||
base = null;
|
||||
}
|
||||
|
||||
try {
|
||||
sysid = new URL(base, systemId);
|
||||
systemId = sysid.toString();
|
||||
} catch (MalformedURLException e) {
|
||||
try {
|
||||
sysid = new URL("file:///" + systemId);
|
||||
} catch (MalformedURLException e2) {
|
||||
Debug.message(1, "Malformed URL on system id", systemId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CatalogResolver catalogResolver = new CatalogResolver();
|
||||
Catalog resolver = catalogResolver.getCatalog();
|
||||
|
||||
for (int count = 0; count < catalogFiles.size(); count++) {
|
||||
String file = (String) catalogFiles.elementAt(count);
|
||||
resolver.parseCatalog(file);
|
||||
}
|
||||
String result = null;
|
||||
|
||||
if (resType == Catalog.DOCTYPE) {
|
||||
System.out.println("Resolve DOCTYPE (name, publicid, systemid):");
|
||||
if (name != null) { System.out.println(" name: " + name); }
|
||||
if (publicId != null) { System.out.println(" public id: " + publicId); }
|
||||
if (systemId != null) { System.out.println(" system id: " + systemId); }
|
||||
if (uri != null) { System.out.println(" uri: " + uri); }
|
||||
result = resolver.resolveDoctype(name, publicId, systemId);
|
||||
} else if (resType == Catalog.DOCUMENT) {
|
||||
System.out.println("Resolve DOCUMENT ():");
|
||||
result = resolver.resolveDocument();
|
||||
} else if (resType == Catalog.ENTITY) {
|
||||
System.out.println("Resolve ENTITY (name, publicid, systemid):");
|
||||
if (name != null) { System.out.println(" name: " + name); }
|
||||
if (publicId != null) { System.out.println(" public id: " + publicId); }
|
||||
if (systemId != null) { System.out.println(" system id: " + systemId); }
|
||||
result = resolver.resolveEntity(name, publicId, systemId);
|
||||
} else if (resType == Catalog.NOTATION) {
|
||||
System.out.println("Resolve NOTATION (name, publicid, systemid):");
|
||||
if (name != null) { System.out.println(" name: " + name); }
|
||||
if (publicId != null) { System.out.println(" public id: " + publicId); }
|
||||
if (systemId != null) { System.out.println(" system id: " + systemId); }
|
||||
result = resolver.resolveNotation(name, publicId, systemId);
|
||||
} else if (resType == Catalog.PUBLIC) {
|
||||
System.out.println("Resolve PUBLIC (publicid, systemid):");
|
||||
if (publicId != null) { System.out.println(" public id: " + publicId); }
|
||||
if (systemId != null) { System.out.println(" system id: " + systemId); }
|
||||
result = resolver.resolvePublic(publicId, systemId);
|
||||
} else if (resType == Catalog.SYSTEM) {
|
||||
System.out.println("Resolve SYSTEM (systemid):");
|
||||
if (systemId != null) { System.out.println(" system id: " + systemId); }
|
||||
result = resolver.resolveSystem(systemId);
|
||||
} else if (resType == Catalog.URI) {
|
||||
System.out.println("Resolve URI (uri):");
|
||||
if (uri != null) { System.out.println(" uri: " + uri); }
|
||||
result = resolver.resolveURI(uri);
|
||||
} else {
|
||||
System.out.println("resType is wrong!? This can't happen!");
|
||||
usage();
|
||||
}
|
||||
|
||||
System.out.println("Result: " + result);
|
||||
}
|
||||
|
||||
public static void usage() {
|
||||
System.out.println("Usage: resolver [options] keyword");
|
||||
System.out.println("");
|
||||
System.out.println("Where:");
|
||||
System.out.println("");
|
||||
System.out.println("-c catalogfile Loads a particular catalog file.");
|
||||
System.out.println("-n name Sets the name.");
|
||||
System.out.println("-p publicId Sets the public identifier.");
|
||||
System.out.println("-s systemId Sets the system identifier.");
|
||||
System.out.println("-a Makes the system URI absolute before resolution");
|
||||
System.out.println("-u uri Sets the URI.");
|
||||
System.out.println("-d integer Set the debug level.");
|
||||
System.out.println("keyword Identifies the type of resolution to perform:");
|
||||
System.out.println(" doctype, document, entity, notation, public, system,");
|
||||
System.out.println(" or uri.");
|
||||
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,300 @@
|
||||
// xparse.java - A simple command-line XML parser
|
||||
|
||||
/*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2001 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 "Xalan" 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 and was
|
||||
* originally based on software copyright (c) 2001, International
|
||||
* Business Machines Corporation., http://www.ibm.com. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.xml.resolver.apps;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Vector;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.xml.sax.*;
|
||||
import org.xml.sax.helpers.*;
|
||||
|
||||
import javax.xml.parsers.*;
|
||||
|
||||
import org.apache.xml.resolver.tools.ResolvingParser;
|
||||
import org.apache.xml.resolver.Catalog;
|
||||
import org.apache.xml.resolver.helpers.Debug;
|
||||
|
||||
/**
|
||||
* <p>A simple command-line XML parsing application.</p>
|
||||
*
|
||||
* <p>This class implements a simple command-line XML Parser. It's
|
||||
* just a little wrapper around the JAXP Parser with support for
|
||||
* catalogs.
|
||||
* </p>
|
||||
*
|
||||
* <p>Usage: xparse [options] document.xml</p>
|
||||
*
|
||||
* <p>Where:</p>
|
||||
*
|
||||
* <dl>
|
||||
* <dt><code>-c</code> <em>catalogfile</em></dt>
|
||||
* <dd>Load a particular catalog file.</dd>
|
||||
* <dt><code>-w</code></dt>
|
||||
* <dd>Perform a well-formed parse, not a validating parse.</dd>
|
||||
* <dt><code>-v</code> (the default)</dt>
|
||||
* <dd>Perform a validating parse.</dd>
|
||||
* <dt><code>-n</code></dt>
|
||||
* <dd>Perform a namespace-ignorant parse.</dd>
|
||||
* <dt><code>-N</code> (the default)</dt>
|
||||
* <dd>Perform a namespace-aware parse.</dd>
|
||||
* <dt><code>-d</code> <em>integer</em></dt>
|
||||
* <dd>Set the debug level. Warnings are shown if the debug level
|
||||
* is > 2.</dd>
|
||||
* <dt><code>-E</code> <em>integer</em></dt>
|
||||
* <dd>Set the maximum number of errors to display.</dd>
|
||||
* </dl>
|
||||
*
|
||||
* <p>The process ends with error-level 1, if there errors.</p>
|
||||
*
|
||||
* @deprecated This interface has been replaced by the
|
||||
* {@link org.apache.xml.resolver.tools.ResolvingXMLReader} for SAX2.
|
||||
*
|
||||
* @see org.apache.xml.resolver.tools.ResolvingParser
|
||||
*
|
||||
* @author Norman Walsh
|
||||
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
|
||||
*
|
||||
* @version 1.0
|
||||
*/
|
||||
public class xparse
|
||||
{
|
||||
/** The main entry point */
|
||||
public static void main (String[] args)
|
||||
throws FileNotFoundException, IOException {
|
||||
|
||||
String xmlfile = null;
|
||||
int debug = 0;
|
||||
int maxErrs = 10;
|
||||
boolean nsAware = true;
|
||||
boolean validating = true;
|
||||
boolean showWarnings = (debug > 2);
|
||||
boolean showErrors = true;
|
||||
Vector catalogFiles = new Vector();
|
||||
|
||||
for (int i=0; i<args.length; i++) {
|
||||
if (args[i].equals("-c")) {
|
||||
++i;
|
||||
catalogFiles.add(args[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-w")) {
|
||||
validating = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-v")) {
|
||||
validating = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-n")) {
|
||||
nsAware = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-N")) {
|
||||
nsAware = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-d")) {
|
||||
++i;
|
||||
String debugstr = args[i];
|
||||
try {
|
||||
debug = Integer.parseInt(debugstr);
|
||||
if (debug >= 0) {
|
||||
Debug.setDebug(debug);
|
||||
showWarnings = (debug > 2);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// nop
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-E")) {
|
||||
++i;
|
||||
String errstr = args[i];
|
||||
try {
|
||||
int errs = Integer.parseInt(errstr);
|
||||
if (errs >= 0) {
|
||||
maxErrs = errs;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// nop
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
xmlfile = args[i];
|
||||
}
|
||||
|
||||
if (xmlfile == null) {
|
||||
System.out.println("Usage: org.apache.xml.resolver.apps.xparse [opts] xmlfile");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
ResolvingParser.validating = validating;
|
||||
ResolvingParser.namespaceAware = nsAware;
|
||||
ResolvingParser reader = new ResolvingParser();
|
||||
Catalog catalog = reader.getCatalog();
|
||||
|
||||
for (int count = 0; count < catalogFiles.size(); count++) {
|
||||
String file = (String) catalogFiles.elementAt(count);
|
||||
catalog.parseCatalog(file);
|
||||
}
|
||||
|
||||
XParseError xpe = new XParseError(showErrors, showWarnings);
|
||||
xpe.setMaxMessages(maxErrs);
|
||||
reader.setErrorHandler(xpe);
|
||||
|
||||
String parseType = validating ? "validating" : "well-formed";
|
||||
String nsType = nsAware ? "namespace-aware" : "namespace-ignorant";
|
||||
if (maxErrs > 0) {
|
||||
System.out.println("Attempting "
|
||||
+ parseType
|
||||
+ ", "
|
||||
+ nsType
|
||||
+ " parse");
|
||||
}
|
||||
|
||||
Date startTime = new Date();
|
||||
|
||||
try {
|
||||
reader.parse(xmlfile);
|
||||
} catch (SAXException sx) {
|
||||
System.out.println("SAX Exception: " + sx);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Date endTime = new Date();
|
||||
|
||||
long millisec = endTime.getTime() - startTime.getTime();
|
||||
long secs = 0;
|
||||
long mins = 0;
|
||||
long hours = 0;
|
||||
|
||||
if (millisec > 1000) {
|
||||
secs = millisec / 1000;
|
||||
millisec = millisec % 1000;
|
||||
}
|
||||
|
||||
if (secs > 60) {
|
||||
mins = secs / 60;
|
||||
secs = secs % 60;
|
||||
}
|
||||
|
||||
if (mins > 60) {
|
||||
hours = mins / 60;
|
||||
mins = mins % 60;
|
||||
}
|
||||
|
||||
if (maxErrs > 0) {
|
||||
System.out.print("Parse ");
|
||||
if (xpe.getFatalCount() > 0) {
|
||||
System.out.print("failed ");
|
||||
} else {
|
||||
System.out.print("succeeded ");
|
||||
System.out.print("(");
|
||||
if (hours > 0) {
|
||||
System.out.print(hours + ":");
|
||||
}
|
||||
if (hours > 0 || mins > 0) {
|
||||
System.out.print(mins + ":");
|
||||
}
|
||||
System.out.print(secs + "." + millisec);
|
||||
System.out.print(") ");
|
||||
}
|
||||
System.out.print("with ");
|
||||
|
||||
int errCount = xpe.getErrorCount();
|
||||
int warnCount = xpe.getWarningCount();
|
||||
|
||||
if (errCount > 0) {
|
||||
System.out.print(errCount + " error");
|
||||
System.out.print(errCount > 1 ? "s" : "");
|
||||
System.out.print(" and ");
|
||||
} else {
|
||||
System.out.print("no errors and ");
|
||||
}
|
||||
|
||||
if (warnCount > 0) {
|
||||
System.out.print(warnCount + " warning");
|
||||
System.out.print(warnCount > 1 ? "s" : "");
|
||||
System.out.print(".");
|
||||
} else {
|
||||
System.out.print("no warnings.");
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
if (xpe.getErrorCount() > 0) {
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
// xread.java - A simple command-line XML parser
|
||||
|
||||
/*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2001 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 "Xalan" 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 and was
|
||||
* originally based on software copyright (c) 2001, International
|
||||
* Business Machines Corporation., http://www.ibm.com. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.xml.resolver.apps;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Vector;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.xml.sax.*;
|
||||
import org.xml.sax.helpers.*;
|
||||
|
||||
import javax.xml.parsers.*;
|
||||
|
||||
import org.apache.xml.resolver.tools.ResolvingXMLReader;
|
||||
import org.apache.xml.resolver.Catalog;
|
||||
import org.apache.xml.resolver.helpers.Debug;
|
||||
|
||||
/**
|
||||
* <p>A simple command-line XML parsing application.</p>
|
||||
*
|
||||
* <p>This class implements a simple command-line XML Parser. It's
|
||||
* just a little wrapper around the JAXP XMLReader with support for
|
||||
* catalogs.
|
||||
* </p>
|
||||
*
|
||||
* <p>Usage: xread [options] document.xml</p>
|
||||
*
|
||||
* <p>Where:</p>
|
||||
*
|
||||
* <dl>
|
||||
* <dt><code>-c</code> <em>catalogfile</em></dt>
|
||||
* <dd>Load a particular catalog file.</dd>
|
||||
* <dt><code>-w</code></dt>
|
||||
* <dd>Perform a well-formed parse, not a validating parse.</dd>
|
||||
* <dt><code>-v</code> (the default)</dt>
|
||||
* <dd>Perform a validating parse.</dd>
|
||||
* <dt><code>-n</code></dt>
|
||||
* <dd>Perform a namespace-ignorant parse.</dd>
|
||||
* <dt><code>-N</code> (the default)</dt>
|
||||
* <dd>Perform a namespace-aware parse.</dd>
|
||||
* <dt><code>-d</code> <em>integer</em></dt>
|
||||
* <dd>Set the debug level. Warnings are shown if the debug level
|
||||
* is > 2.</dd>
|
||||
* <dt><code>-E</code> <em>integer</em></dt>
|
||||
* <dd>Set the maximum number of errors to display.</dd>
|
||||
* </dl>
|
||||
*
|
||||
* <p>The process ends with error-level 1, if there errors.</p>
|
||||
*
|
||||
* @see org.apache.xml.resolver.tools.ResolvingXMLReader
|
||||
*
|
||||
* @author Norman Walsh
|
||||
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
|
||||
*
|
||||
* @version 1.0
|
||||
*/
|
||||
public class xread
|
||||
{
|
||||
/** The main entry point */
|
||||
public static void main (String[] args)
|
||||
throws FileNotFoundException, IOException {
|
||||
|
||||
String xmlfile = null;
|
||||
int debug = 0;
|
||||
int maxErrs = 10;
|
||||
boolean nsAware = true;
|
||||
boolean validating = true;
|
||||
boolean showWarnings = (debug > 2);
|
||||
boolean showErrors = true;
|
||||
Vector catalogFiles = new Vector();
|
||||
|
||||
for (int i=0; i<args.length; i++) {
|
||||
if (args[i].equals("-c")) {
|
||||
++i;
|
||||
catalogFiles.add(args[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-w")) {
|
||||
validating = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-v")) {
|
||||
validating = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-n")) {
|
||||
nsAware = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-N")) {
|
||||
nsAware = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-d")) {
|
||||
++i;
|
||||
String debugstr = args[i];
|
||||
try {
|
||||
debug = Integer.parseInt(debugstr);
|
||||
if (debug >= 0) {
|
||||
Debug.setDebug(debug);
|
||||
showWarnings = (debug > 2);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// nop
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (args[i].equals("-E")) {
|
||||
++i;
|
||||
String errstr = args[i];
|
||||
try {
|
||||
int errs = Integer.parseInt(errstr);
|
||||
if (errs >= 0) {
|
||||
maxErrs = errs;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// nop
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
xmlfile = args[i];
|
||||
}
|
||||
|
||||
if (xmlfile == null) {
|
||||
System.out.println("Usage: org.apache.xml.resolver.apps.xread [opts] xmlfile");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
ResolvingXMLReader reader = new ResolvingXMLReader();
|
||||
|
||||
try {
|
||||
reader.setFeature("http://xml.org/sax/features/namespaces", nsAware);
|
||||
reader.setFeature("http://xml.org/sax/features/validation", validating);
|
||||
} catch (SAXException e) {
|
||||
// nop;
|
||||
}
|
||||
|
||||
Catalog catalog = reader.getCatalog();
|
||||
|
||||
for (int count = 0; count < catalogFiles.size(); count++) {
|
||||
String file = (String) catalogFiles.elementAt(count);
|
||||
catalog.parseCatalog(file);
|
||||
}
|
||||
|
||||
XParseError xpe = new XParseError(showErrors, showWarnings);
|
||||
xpe.setMaxMessages(maxErrs);
|
||||
reader.setErrorHandler(xpe);
|
||||
|
||||
String parseType = validating ? "validating" : "well-formed";
|
||||
String nsType = nsAware ? "namespace-aware" : "namespace-ignorant";
|
||||
if (maxErrs > 0) {
|
||||
System.out.println("Attempting "
|
||||
+ parseType
|
||||
+ ", "
|
||||
+ nsType
|
||||
+ " parse");
|
||||
}
|
||||
|
||||
Date startTime = new Date();
|
||||
|
||||
try {
|
||||
reader.parse(xmlfile);
|
||||
} catch (SAXException sx) {
|
||||
System.out.println("SAX Exception: " + sx);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Date endTime = new Date();
|
||||
|
||||
long millisec = endTime.getTime() - startTime.getTime();
|
||||
long secs = 0;
|
||||
long mins = 0;
|
||||
long hours = 0;
|
||||
|
||||
if (millisec > 1000) {
|
||||
secs = millisec / 1000;
|
||||
millisec = millisec % 1000;
|
||||
}
|
||||
|
||||
if (secs > 60) {
|
||||
mins = secs / 60;
|
||||
secs = secs % 60;
|
||||
}
|
||||
|
||||
if (mins > 60) {
|
||||
hours = mins / 60;
|
||||
mins = mins % 60;
|
||||
}
|
||||
|
||||
if (maxErrs > 0) {
|
||||
System.out.print("Parse ");
|
||||
if (xpe.getFatalCount() > 0) {
|
||||
System.out.print("failed ");
|
||||
} else {
|
||||
System.out.print("succeeded ");
|
||||
System.out.print("(");
|
||||
if (hours > 0) {
|
||||
System.out.print(hours + ":");
|
||||
}
|
||||
if (hours > 0 || mins > 0) {
|
||||
System.out.print(mins + ":");
|
||||
}
|
||||
System.out.print(secs + "." + millisec);
|
||||
System.out.print(") ");
|
||||
}
|
||||
System.out.print("with ");
|
||||
|
||||
int errCount = xpe.getErrorCount();
|
||||
int warnCount = xpe.getWarningCount();
|
||||
|
||||
if (errCount > 0) {
|
||||
System.out.print(errCount + " error");
|
||||
System.out.print(errCount > 1 ? "s" : "");
|
||||
System.out.print(" and ");
|
||||
} else {
|
||||
System.out.print("no errors and ");
|
||||
}
|
||||
|
||||
if (warnCount > 0) {
|
||||
System.out.print(warnCount + " warning");
|
||||
System.out.print(warnCount > 1 ? "s" : "");
|
||||
System.out.print(".");
|
||||
} else {
|
||||
System.out.print("no warnings.");
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
if (xpe.getErrorCount() > 0) {
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user