8177561: Remove deprecated for removal portions of com.sun.org.apache

Reviewed-by: lancea, rriggs
This commit is contained in:
Joe Wang 2017-06-20 14:29:52 -07:00
parent 87cf68f361
commit a14dd28e6b
26 changed files with 0 additions and 9153 deletions

View File

@ -1,596 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xerces.internal.util;
import java.io.IOException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.ext.EntityResolver2;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
import javax.xml.parsers.SAXParserFactory;
import com.sun.org.apache.xerces.internal.dom.DOMInputImpl;
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
import com.sun.org.apache.xerces.internal.xni.XNIException;
import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.CatalogManager;
import com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader;
import com.sun.org.apache.xml.internal.resolver.readers.SAXCatalogReader;
/**
* <p>The catalog resolver handles the resolution of external
* identifiers and URI references through XML catalogs. This
* component supports XML catalogs defined by the
* <a href="http://www.oasis-open.org/committees/entity/spec.html">
* OASIS XML Catalogs Specification</a>. It encapsulates the
* <a href="http://xml.apache.org/commons/">XML Commons</a> resolver.
* An instance of this class may be registered on the parser
* as a SAX entity resolver, as a DOM LSResourceResolver or
* as an XNI entity resolver by setting the property
* (http://apache.org/xml/properties/internal/entity-resolver).</p>
*
* <p>It is intended that this class may be used standalone to perform
* catalog resolution outside of a parsing context. It may be shared
* between several parsers and the application.</p>
*
* @deprecated This class and the JDK internal Catalog API in package
* {@code com.sun.org.apache.xml.internal.resolver}
* is encapsulated in JDK 9. The entire implementation under the package is now
* deprecated and subject to removal in a future release. Users of the API should
* migrate to the {@linkplain javax.xml.catalog new public API}.
* <p>
* The new Catalog API is supported throughout the JDK XML Processors, which allows
* the use of Catalog by simply setting a path to a Catalog file as a property.
*
* @author Michael Glavassevich, IBM
*
*/
@Deprecated(since="9", forRemoval=true)
public class XMLCatalogResolver
implements XMLEntityResolver, EntityResolver2, LSResourceResolver {
/** Internal catalog manager for Apache catalogs. **/
private CatalogManager fResolverCatalogManager = null;
/** Internal catalog structure. **/
private Catalog fCatalog = null;
/** An array of catalog URIs. **/
private String [] fCatalogsList = null;
/**
* Indicates whether the list of catalogs has
* changed since it was processed.
*/
private boolean fCatalogsChanged = true;
/** Application specified prefer public setting. **/
private boolean fPreferPublic = true;
/**
* Indicates whether the application desires that
* the parser or some other component performing catalog
* resolution should use the literal system identifier
* instead of the expanded system identifier.
*/
private boolean fUseLiteralSystemId = true;
/**
* <p>Constructs a catalog resolver with a default configuration.</p>
*/
public XMLCatalogResolver () {
this(null, true);
}
/**
* <p>Constructs a catalog resolver with the given
* list of entry files.</p>
*
* @param catalogs an ordered array list of absolute URIs
*/
public XMLCatalogResolver (String [] catalogs) {
this(catalogs, true);
}
/**
* <p>Constructs a catalog resolver with the given
* list of entry files and the preference for whether
* system or public matches are preferred.</p>
*
* @param catalogs an ordered array list of absolute URIs
* @param preferPublic the prefer public setting
*/
public XMLCatalogResolver (String [] catalogs, boolean preferPublic) {
init(catalogs, preferPublic);
}
/**
* <p>Returns the initial list of catalog entry files.</p>
*
* @return the initial list of catalog entry files
*/
public final synchronized String [] getCatalogList () {
return (fCatalogsList != null)
? (String[]) fCatalogsList.clone() : null;
}
/**
* <p>Sets the initial list of catalog entry files.
* If there were any catalog mappings cached from
* the previous list they will be replaced by catalog
* mappings from the new list the next time the catalog
* is queried.</p>
*
* @param catalogs an ordered array list of absolute URIs
*/
public final synchronized void setCatalogList (String [] catalogs) {
fCatalogsChanged = true;
fCatalogsList = (catalogs != null)
? (String[]) catalogs.clone() : null;
}
/**
* <p>Forces the cache of catalog mappings to be cleared.</p>
*/
public final synchronized void clear () {
fCatalog = null;
}
/**
* <p>Returns the preference for whether system or public
* matches are preferred. This is used in the absence
* of any occurence of the <code>prefer</code> attribute
* on the <code>catalog</code> entry of a catalog. If this
* property has not yet been explicitly set its value is
* <code>true</code>.</p>
*
* @return the prefer public setting
*/
public final boolean getPreferPublic () {
return fPreferPublic;
}
/**
* <p>Sets the preference for whether system or public
* matches are preferred. This is used in the absence
* of any occurence of the <code>prefer</code> attribute
* on the <code>catalog</code> entry of a catalog.</p>
*
* @param preferPublic the prefer public setting
*/
public final void setPreferPublic (boolean preferPublic) {
fPreferPublic = preferPublic;
fResolverCatalogManager.setPreferPublic(preferPublic);
}
/**
* <p>Returns the preference for whether the literal system
* identifier should be used when resolving system
* identifiers when both it and the expanded system
* identifier are available. If this property has not yet
* been explicitly set its value is <code>true</code>.</p>
*
* @return the preference for using literal system identifers
* for catalog resolution
*
* @see #setUseLiteralSystemId
*/
public final boolean getUseLiteralSystemId () {
return fUseLiteralSystemId;
}
/**
* <p>Sets the preference for whether the literal system
* identifier should be used when resolving system
* identifiers when both it and the expanded system
* identifier are available.</p>
*
* <p>The literal system identifier is the URI as it was
* provided before absolutization. It may be embedded within
* an entity. It may be provided externally or it may be the
* result of redirection. For example, redirection may
* have come from the protocol level through HTTP or from
* an application's entity resolver.</p>
*
* <p>The expanded system identifier is an absolute URI
* which is the result of resolving the literal system
* identifier against a base URI.</p>
*
* @param useLiteralSystemId the preference for using
* literal system identifers for catalog resolution
*/
public final void setUseLiteralSystemId (boolean useLiteralSystemId) {
fUseLiteralSystemId = useLiteralSystemId;
}
/**
* <p>Resolves an external entity. If the entity cannot be
* resolved, this method should return <code>null</code>. This
* method returns an input source if an entry was found in the
* catalog for the given external identifier. It should be
* overrided if other behaviour is required.</p>
*
* @param publicId the public identifier, or <code>null</code> if none was supplied
* @param systemId the system identifier
*
* @throws SAXException any SAX exception, possibly wrapping another exception
* @throws IOException thrown if some i/o error occurs
*/
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
String resolvedId = null;
if (publicId != null && systemId != null) {
resolvedId = resolvePublic(publicId, systemId);
}
else if (systemId != null) {
resolvedId = resolveSystem(systemId);
}
if (resolvedId != null) {
InputSource source = new InputSource(resolvedId);
source.setPublicId(publicId);
return source;
}
return null;
}
/**
* <p>Resolves an external entity. If the entity cannot be
* resolved, this method should return <code>null</code>. This
* method returns an input source if an entry was found in the
* catalog for the given external identifier. It should be
* overrided if other behaviour is required.</p>
*
* @param name the identifier of the external entity
* @param publicId the public identifier, or <code>null</code> if none was supplied
* @param baseURI the URI with respect to which relative systemIDs are interpreted.
* @param systemId the system identifier
*
* @throws SAXException any SAX exception, possibly wrapping another exception
* @throws IOException thrown if some i/o error occurs
*/
public InputSource resolveEntity(String name, String publicId,
String baseURI, String systemId) throws SAXException, IOException {
String resolvedId = null;
if (!getUseLiteralSystemId() && baseURI != null) {
// Attempt to resolve the system identifier against the base URI.
try {
URI uri = new URI(new URI(baseURI), systemId);
systemId = uri.toString();
}
// Ignore the exception. Fallback to the literal system identifier.
catch (URI.MalformedURIException ex) {}
}
if (publicId != null && systemId != null) {
resolvedId = resolvePublic(publicId, systemId);
}
else if (systemId != null) {
resolvedId = resolveSystem(systemId);
}
if (resolvedId != null) {
InputSource source = new InputSource(resolvedId);
source.setPublicId(publicId);
return source;
}
return null;
}
/**
* <p>Locates an external subset for documents which do not explicitly
* provide one. This method always returns <code>null</code>. It
* should be overrided if other behaviour is required.</p>
*
* @param name the identifier of the document root element
* @param baseURI the document's base URI
*
* @throws SAXException any SAX exception, possibly wrapping another exception
* @throws IOException thrown if some i/o error occurs
*/
public InputSource getExternalSubset(String name, String baseURI)
throws SAXException, IOException {
return null;
}
/**
* <p>Resolves a resource using the catalog. This method interprets that
* the namespace URI corresponds to uri entries in the catalog.
* Where both a namespace and an external identifier exist, the namespace
* takes precedence.</p>
*
* @param type the type of the resource being resolved
* @param namespaceURI the namespace of the resource being resolved,
* or <code>null</code> if none was supplied
* @param publicId the public identifier of the resource being resolved,
* or <code>null</code> if none was supplied
* @param systemId the system identifier of the resource being resolved,
* or <code>null</code> if none was supplied
* @param baseURI the absolute base URI of the resource being parsed,
* or <code>null</code> if there is no base URI
*/
public LSInput resolveResource(String type, String namespaceURI,
String publicId, String systemId, String baseURI) {
String resolvedId = null;
try {
// The namespace is useful for resolving namespace aware
// grammars such as XML schema. Let it take precedence over
// the external identifier if one exists.
if (namespaceURI != null) {
resolvedId = resolveURI(namespaceURI);
}
if (!getUseLiteralSystemId() && baseURI != null) {
// Attempt to resolve the system identifier against the base URI.
try {
URI uri = new URI(new URI(baseURI), systemId);
systemId = uri.toString();
}
// Ignore the exception. Fallback to the literal system identifier.
catch (URI.MalformedURIException ex) {}
}
// Resolve against an external identifier if one exists. This
// is useful for resolving DTD external subsets and other
// external entities. For XML schemas if there was no namespace
// mapping we might be able to resolve a system identifier
// specified as a location hint.
if (resolvedId == null) {
if (publicId != null && systemId != null) {
resolvedId = resolvePublic(publicId, systemId);
}
else if (systemId != null) {
resolvedId = resolveSystem(systemId);
}
}
}
// Ignore IOException. It cannot be thrown from this method.
catch (IOException ex) {}
if (resolvedId != null) {
return new DOMInputImpl(publicId, resolvedId, baseURI);
}
return null;
}
/**
* <p>Resolves an external entity. If the entity cannot be
* resolved, this method should return <code>null</code>. This
* method only calls <code>resolveIdentifier</code> and returns
* an input source if an entry was found in the catalog. It
* should be overrided if other behaviour is required.</p>
*
* @param resourceIdentifier location of the XML resource to resolve
*
* @throws XNIException thrown on general error
* @throws IOException thrown if some i/o error occurs
*/
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
throws XNIException, IOException {
String resolvedId = resolveIdentifier(resourceIdentifier);
if (resolvedId != null) {
return new XMLInputSource(resourceIdentifier.getPublicId(),
resolvedId, resourceIdentifier.getBaseSystemId(), false);
}
return null;
}
/**
* <p>Resolves an identifier using the catalog. This method interprets that
* the namespace of the identifier corresponds to uri entries in the catalog.
* Where both a namespace and an external identifier exist, the namespace
* takes precedence.</p>
*
* @param resourceIdentifier the identifier to resolve
*
* @throws XNIException thrown on general error
* @throws IOException thrown if some i/o error occurs
*/
public String resolveIdentifier(XMLResourceIdentifier resourceIdentifier)
throws IOException, XNIException {
String resolvedId = null;
// The namespace is useful for resolving namespace aware
// grammars such as XML schema. Let it take precedence over
// the external identifier if one exists.
String namespace = resourceIdentifier.getNamespace();
if (namespace != null) {
resolvedId = resolveURI(namespace);
}
// Resolve against an external identifier if one exists. This
// is useful for resolving DTD external subsets and other
// external entities. For XML schemas if there was no namespace
// mapping we might be able to resolve a system identifier
// specified as a location hint.
if (resolvedId == null) {
String publicId = resourceIdentifier.getPublicId();
String systemId = getUseLiteralSystemId()
? resourceIdentifier.getLiteralSystemId()
: resourceIdentifier.getExpandedSystemId();
if (publicId != null && systemId != null) {
resolvedId = resolvePublic(publicId, systemId);
}
else if (systemId != null) {
resolvedId = resolveSystem(systemId);
}
}
return resolvedId;
}
/**
* <p>Returns the URI mapping in the catalog for the given
* external identifier or <code>null</code> if no mapping
* exists. If the system identifier is an URN in the
* <code>publicid</code> namespace it is converted into
* a public identifier by URN "unwrapping" as specified
* in the XML Catalogs specification.</p>
*
* @param systemId the system identifier to locate in the catalog
*
* @return the mapped URI or <code>null</code> if no mapping
* was found in the catalog
*
* @throws IOException if an i/o error occurred while reading
* the catalog
*/
public final synchronized String resolveSystem (String systemId)
throws IOException {
if (fCatalogsChanged) {
parseCatalogs();
fCatalogsChanged = false;
}
return (fCatalog != null)
? fCatalog.resolveSystem(systemId) : null;
}
/**
* <p>Returns the URI mapping in the catalog for the given
* external identifier or <code>null</code> if no mapping
* exists. Public identifiers are normalized before
* comparison.</p>
*
* @param publicId the public identifier to locate in the catalog
* @param systemId the system identifier to locate in the catalog
*
* @return the mapped URI or <code>null</code> if no mapping
* was found in the catalog
*
* @throws IOException if an i/o error occurred while reading
* the catalog
*/
public final synchronized String resolvePublic (String publicId, String systemId)
throws IOException {
if (fCatalogsChanged) {
parseCatalogs();
fCatalogsChanged = false;
}
return (fCatalog != null)
? fCatalog.resolvePublic(publicId, systemId) : null;
}
/**
* <p>Returns the URI mapping in the catalog for the given URI
* reference or <code>null</code> if no mapping exists.
* URI comparison is case sensitive. If the URI reference
* is an URN in the <code>publicid</code> namespace
* it is converted into a public identifier by URN "unwrapping"
* as specified in the XML Catalogs specification and then
* resolution is performed following the semantics of
* external identifier resolution.</p>
*
* @param uri the URI to locate in the catalog
*
* @return the mapped URI or <code>null</code> if no mapping
* was found in the catalog
*
* @throws IOException if an i/o error occurred while reading
* the catalog
*/
public final synchronized String resolveURI (String uri)
throws IOException {
if (fCatalogsChanged) {
parseCatalogs();
fCatalogsChanged = false;
}
return (fCatalog != null)
? fCatalog.resolveURI(uri) : null;
}
/**
* Initialization. Create a CatalogManager and set all
* the properties upfront. This prevents JVM wide system properties
* or a property file somewhere in the environment from affecting
* the behaviour of this catalog resolver.
*/
private void init (String [] catalogs, boolean preferPublic) {
fCatalogsList = (catalogs != null) ? (String[]) catalogs.clone() : null;
fPreferPublic = preferPublic;
fResolverCatalogManager = new CatalogManager();
fResolverCatalogManager.setAllowOasisXMLCatalogPI(false);
fResolverCatalogManager.setCatalogClassName("com.sun.org.apache.xml.internal.resolver.Catalog");
fResolverCatalogManager.setCatalogFiles("");
fResolverCatalogManager.setIgnoreMissingProperties(true);
fResolverCatalogManager.setPreferPublic(fPreferPublic);
fResolverCatalogManager.setRelativeCatalogs(false);
fResolverCatalogManager.setUseStaticCatalog(false);
fResolverCatalogManager.setVerbosity(0);
}
/**
* Instruct the <code>Catalog</code> to parse each of the
* catalogs in the list. Only the first catalog will actually be
* parsed immediately. The others will be queued and read if
* they are needed later.
*/
private void parseCatalogs () throws IOException {
if (fCatalogsList != null) {
fCatalog = new Catalog(fResolverCatalogManager);
attachReaderToCatalog(fCatalog);
for (int i = 0; i < fCatalogsList.length; ++i) {
String catalog = fCatalogsList[i];
if (catalog != null && catalog.length() > 0) {
fCatalog.parseCatalog(catalog);
}
}
}
else {
fCatalog = null;
}
}
/**
* Attaches the reader to the catalog.
*/
private void attachReaderToCatalog (Catalog catalog) {
SAXParserFactory spf = new SAXParserFactoryImpl();
spf.setNamespaceAware(true);
spf.setValidating(false);
SAXCatalogReader saxReader = new SAXCatalogReader(spf);
saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, "catalog",
"com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader");
catalog.addReader("application/xml", saxReader);
}
}

View File

@ -1,251 +0,0 @@
/*
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Represents a Catalog entry.
*
* <p>Instances of this class represent individual entries
* in a Catalog.</p>
*
* <p>Each catalog entry has a unique name and is associated with
* an arbitrary number of arguments (all strings). For example, the
* TR9401 catalog entry "PUBLIC" has two arguments, a public identifier
* and a system identifier. Each entry has a unique numeric type,
* assigned automatically when the entry type is created.</p>
*
* <p>The number and type of catalog entries is maintained
* <em>statically</em>. Catalog classes, or their subclasses, can add
* new entry types, but all Catalog objects share the same global pool
* of types.</p>
*
* <p>Initially there are no valid entries.</p>
*
* @see Catalog
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class CatalogEntry {
/** The nextEntry is the ordinal number of the next entry type. */
static AtomicInteger nextEntry = new AtomicInteger(0);
/**
* The entryTypes vector maps catalog entry names
* (e.g., 'BASE' or 'SYSTEM') to their type (1, 2, etc.).
* Names are case sensitive.
*/
static final Map<String, Integer> entryTypes = new ConcurrentHashMap<>();
/** The entryTypes vector maps catalog entry types to the
number of arguments they're required to have. */
static final Vector entryArgs = new Vector();
/**
* Adds a new catalog entry type.
*
* @param name The name of the catalog entry type. This must be
* unique among all types and is case-sensitive. (Adding a duplicate
* name effectively replaces the old type with the new type.)
* @param numArgs The number of arguments that this entry type
* is required to have. There is no provision for variable numbers
* of arguments.
* @return The type for the new entry.
*/
static int addEntryType(String name, int numArgs) {
final int index = nextEntry.getAndIncrement();
entryTypes.put(name, index);
entryArgs.add(index, numArgs);
return index;
}
/**
* Lookup an entry type
*
* @param name The name of the catalog entry type.
* @return The type of the catalog entry with the specified name.
* @throws InvalidCatalogEntryTypeException if no entry has the
* specified name.
*/
public static int getEntryType(String name)
throws CatalogException {
if (!entryTypes.containsKey(name)) {
throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
}
Integer iType = entryTypes.get(name);
if (iType == null) {
throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
}
return iType;
}
/**
* Find out how many arguments an entry is required to have.
*
* @param name The name of the catalog entry type.
* @return The number of arguments that entry type is required to have.
* @throws InvalidCatalogEntryTypeException if no entry has the
* specified name.
*/
public static int getEntryArgCount(String name)
throws CatalogException {
return getEntryArgCount(getEntryType(name));
}
/**
* Find out how many arguments an entry is required to have.
*
* @param type A valid catalog entry type.
* @return The number of arguments that entry type is required to have.
* @throws InvalidCatalogEntryTypeException if the type is invalid.
*/
public static int getEntryArgCount(int type)
throws CatalogException {
try {
Integer iArgs = (Integer) entryArgs.get(type);
return iArgs.intValue();
} catch (ArrayIndexOutOfBoundsException e) {
throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
}
}
/** The entry type of this entry */
protected int entryType = 0;
/** The arguments associated with this entry */
protected Vector args = null;
/**
* Null constructor; something for subclasses to call.
*/
public CatalogEntry() {}
/**
* Construct a catalog entry of the specified type.
*
* @param name The name of the entry type
* @param args A String Vector of arguments
* @throws InvalidCatalogEntryTypeException if no such entry type
* exists.
* @throws InvalidCatalogEntryException if the wrong number of arguments
* is passed.
*/
public CatalogEntry(String name, Vector args)
throws CatalogException {
Integer iType = entryTypes.get(name);
if (iType == null) {
throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
}
int type = iType;
try {
Integer iArgs = (Integer) entryArgs.get(type);
if (iArgs.intValue() != args.size()) {
throw new CatalogException(CatalogException.INVALID_ENTRY);
}
} catch (ArrayIndexOutOfBoundsException e) {
throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
}
entryType = type;
this.args = args;
}
/**
* Construct a catalog entry of the specified type.
*
* @param type The entry type
* @param args A String Vector of arguments
* @throws InvalidCatalogEntryTypeException if no such entry type
* exists.
* @throws InvalidCatalogEntryException if the wrong number of arguments
* is passed.
*/
public CatalogEntry(int type, Vector args)
throws CatalogException {
try {
Integer iArgs = (Integer) entryArgs.get(type);
if (iArgs.intValue() != args.size()) {
throw new CatalogException(CatalogException.INVALID_ENTRY);
}
} catch (ArrayIndexOutOfBoundsException e) {
throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
}
entryType = type;
this.args = args;
}
/**
* Get the entry type.
*
* @return The entry type of the CatalogEntry
*/
public int getEntryType() {
return entryType;
}
/**
* Get an entry argument.
*
* @param argNum The argument number (arguments are numbered from 0).
* @return The specified argument or null if an invalid argNum is
* provided.
*/
public String getEntryArg(int argNum) {
try {
String arg = (String) args.get(argNum);
return arg;
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Set an entry argument.
*
* <p>Catalogs sometimes need to adjust the catlog entry parameters,
* for example to make a relative URI absolute with respect to the
* current base URI. But in general, this function should only be
* called shortly after object creation to do some sort of cleanup.
* Catalog entries should not mutate over time.</p>
*
* @param argNum The argument number (arguments are numbered from 0).
* @throws ArrayIndexOutOfBoundsException if an invalid argument
* number is provided.
*/
public void setEntryArg(int argNum, String newspec)
throws ArrayIndexOutOfBoundsException {
args.set(argNum, newspec);
}
}

View File

@ -1,165 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver;
/**
* Signal Catalog exception.
*
* <p>This exception is thrown if an error occurs loading a
* catalog file.</p>
*
* @see Catalog
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class CatalogException extends Exception {
private static final long serialVersionUID = 4007157171817798450L;
/** A wrapper around another exception */
public static final int WRAPPER = 1;
/** An invalid entry */
public static final int INVALID_ENTRY = 2;
/** An invalid entry type */
public static final int INVALID_ENTRY_TYPE = 3;
/** Could not instantiate an XML parser */
public static final int NO_XML_PARSER = 4;
/** Unknown XML format */
public static final int UNKNOWN_FORMAT = 5;
/** Unparseable XML catalog (not XML)*/
public static final int UNPARSEABLE = 6;
/** XML but parse failed */
public static final int PARSE_FAILED = 7;
/** Text catalog ended in mid-comment */
public static final int UNENDED_COMMENT = 8;
/**
* The embedded exception if tunnelling, or null.
*/
private final Exception exception;
private final int exceptionType;
/**
* Create a new CatalogException.
*
* @param type The exception type
* @param message The error or warning message.
*/
public CatalogException (int type, String message) {
super(message);
this.exceptionType = type;
this.exception = null;
}
/**
* Create a new CatalogException.
*
* @param type The exception type
*/
public CatalogException (int type) {
super("Catalog Exception " + type);
this.exceptionType = type;
this.exception = null;
}
/**
* Create a new CatalogException wrapping an existing exception.
*
* <p>The existing exception will be embedded in the new
* one, and its message will become the default message for
* the CatalogException.</p>
*
* @param e The exception to be wrapped in a CatalogException.
*/
public CatalogException (Exception e) {
super();
this.exceptionType = WRAPPER;
this.exception = e;
}
/**
* Create a new CatalogException from an existing exception.
*
* <p>The existing exception will be embedded in the new
* one, but the new exception will have its own message.</p>
*
* @param message The detail message.
* @param e The exception to be wrapped in a CatalogException.
*/
public CatalogException (String message, Exception e) {
super(message);
this.exceptionType = WRAPPER;
this.exception = e;
}
/**
* Return a detail message for this exception.
*
* <p>If there is an embedded exception, and if the CatalogException
* has no detail message of its own, this method will return
* the detail message from the embedded exception.</p>
*
* @return The error or warning message.
*/
public String getMessage ()
{
String message = super.getMessage();
if (message == null && exception != null) {
return exception.getMessage();
} else {
return message;
}
}
/**
* Return the embedded exception, if any.
*
* @return The embedded exception, or null if there is none.
*/
public Exception getException ()
{
return exception;
}
/**
* Return the exception type
*
* @return The exception type
*/
public int getExceptionType ()
{
return exceptionType;
}
/**
* Override toString to pick up any embedded exception.
*
* @return A string representation of this exception.
*/
public String toString ()
{
if (exception != null) {
return exception.toString();
} else {
return super.toString();
}
}
}

View File

@ -1,870 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver;
import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
import com.sun.org.apache.xml.internal.resolver.helpers.BootstrapResolver;
import com.sun.org.apache.xml.internal.resolver.helpers.Debug;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.Vector;
import sun.reflect.misc.ReflectUtil;
/**
* CatalogManager provides an interface to the catalog properties.
*
* <p>Properties can come from two places: from system properties or
* from a <i>CatalogManager.properties</i> file. This class provides a transparent
* interface to both, with system properties preferred over property file values.</p>
*
* <p>The following table summarizes the properties:</p>
*
* <table border="1">
* <thead>
* <tr>
* <td>System Property</td>
* <td>CatalogManager.properties<br/>Property</td>
* <td>Description</td>
* </tr>
* </thead>
* <tbody>
* <tr>
* <td>xml.catalog.ignoreMissing</td>
* <td>&#160;</td>
* <td>If true, a missing <i>CatalogManager.properties</i> file or missing properties
* within that file will not generate warning messages. See also the
* <i>ignoreMissingProperties</i> method.</td>
* </tr>
*
* <tr>
* <td>xml.catalog.files</td>
* <td>catalogs</td>
* <td>The <emph>semicolon-delimited</emph> list of catalog files.</td>
* </tr>
*
* <tr>
* <td>&#160;</td>
* <td>relative-catalogs</td>
* <td>If false, relative catalog URIs are made absolute with respect to the base URI of
* the <i>CatalogManager.properties</i> file. This setting only applies to catalog
* URIs obtained from the <i>catalogs</i> property <emph>in the</emph>
* <i>CatalogManager.properties</i> file</td>
* </tr>
*
* <tr>
* <td>xml.catalog.verbosity</td>
* <td>verbosity</td>
* <td>If non-zero, the Catalog classes will print informative and debugging messages.
* The higher the number, the more messages.</td>
* </tr>
*
* <tr>
* <td>xml.catalog.prefer</td>
* <td>prefer</td>
* <td>Which identifier is preferred, "public" or "system"?</td>
* </tr>
*
* <tr>
* <td>xml.catalog.staticCatalog</td>
* <td>static-catalog</td>
* <td>Should a single catalog be constructed for all parsing, or should a different
* catalog be created for each parser?</td>
* </tr>
*
* <tr>
* <td>xml.catalog.allowPI</td>
* <td>allow-oasis-xml-catalog-pi</td>
* <td>If the source document contains "oasis-xml-catalog" processing instructions,
* should they be used?</td>
* </tr>
*
* <tr>
* <td>xml.catalog.className</td>
* <td>catalog-class-name</td>
* <td>If you're using the convenience classes
* <tt>com.sun.org.apache.xml.internal.resolver.tools.*</tt>), this setting
* allows you to specify an alternate class name to use for the underlying
* catalog.</td>
* </tr>
* </tbody>
* </table>
*
* @see Catalog
* @deprecated The JDK internal Catalog API in package
* {@code com.sun.org.apache.xml.internal.resolver}
* is encapsulated in JDK 9. The entire implementation under the package is now
* deprecated and subject to removal in a future release. Users of the API
* should migrate to the {@linkplain javax.xml.catalog new public API}.
* <p>
* The new Catalog API is supported throughout the JDK XML Processors, which allows
* the use of Catalog by simply setting a path to a Catalog file as a property.
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
* @version 1.0
*/
@Deprecated(since="9", forRemoval=true)
public class CatalogManager {
private static final String pFiles = "xml.catalog.files";
private static final String pVerbosity = "xml.catalog.verbosity";
private static final String pPrefer = "xml.catalog.prefer";
private static final String pStatic = "xml.catalog.staticCatalog";
private static final String pAllowPI = "xml.catalog.allowPI";
private static final String pClassname = "xml.catalog.className";
private static final String pIgnoreMissing = "xml.catalog.ignoreMissing";
/** A static CatalogManager instance for sharing */
private static final CatalogManager staticManager = new CatalogManager();
/** The bootstrap resolver to use when loading XML Catalogs. */
private BootstrapResolver bResolver = new BootstrapResolver();
/** Flag to ignore missing property files and/or properties */
private boolean ignoreMissingProperties
= (SecuritySupport.getSystemProperty(pIgnoreMissing) != null
|| SecuritySupport.getSystemProperty(pFiles) != null);
/** Holds the resources after they are loaded from the file. */
private ResourceBundle resources;
/** The name of the CatalogManager properties file. */
private String propertyFile = "CatalogManager.properties";
/** The location of the propertyFile */
private URL propertyFileURI = null;
/** Default catalog files list. */
private String defaultCatalogFiles = "./xcatalog";
/** Current catalog files list. */
private String catalogFiles = null;
/** Did the catalogFiles come from the properties file? */
private boolean fromPropertiesFile = false;
/** Default verbosity level if there is no property setting for it. */
private int defaultVerbosity = 1;
/** Current verbosity level. */
private Integer verbosity = null;
/** Default preference setting. */
private boolean defaultPreferPublic = true;
/** Current preference setting. */
private Boolean preferPublic = null;
/** Default setting of the static catalog flag. */
private boolean defaultUseStaticCatalog = true;
/** Current setting of the static catalog flag. */
private Boolean useStaticCatalog = null;
/** The static catalog used by this manager. */
private static volatile Catalog staticCatalog = null;
/** Default setting of the oasisXMLCatalogPI flag. */
private boolean defaultOasisXMLCatalogPI = true;
/** Current setting of the oasisXMLCatalogPI flag. */
private Boolean oasisXMLCatalogPI = null;
/** Default setting of the relativeCatalogs flag. */
private boolean defaultRelativeCatalogs = true;
/** Current setting of the relativeCatalogs flag. */
private Boolean relativeCatalogs = null;
/** Current catalog class name. */
private String catalogClassName = null;
/**
* Indicates whether implementation parts should use
* service loader (or similar).
* Note the default value (false) is the safe option..
*/
private boolean useServicesMechanism;
/** The manager's debug object. Used for printing debugging messages.
*
* <p>This field is public so that objects that have access to this
* CatalogManager can use this debug object.</p>
*/
public Debug debug = null;
/** Constructor. */
public CatalogManager() {
init();
}
/** Constructor that specifies an explicit property file. */
public CatalogManager(String propertyFile) {
this.propertyFile = propertyFile;
init();
}
private void init() {
debug = new Debug();
// Note that we don't setDebug() here; we do that lazily. Either the
// user will set it explicitly, or we'll do it automagically if they
// read from the propertyFile for some other reason. That way, there's
// no attempt to read from the file before the caller has had a chance
// to avoid it.
if (System.getSecurityManager() == null) {
useServicesMechanism = true;
}
// Make sure verbosity is set by xml.catalog.verbosity sysprop
// setting, if defined.
queryVerbosityFromSysProp();
}
/** Set the bootstrap resolver
* @param resolver the bootstrap resolver
*/
public void setBootstrapResolver(BootstrapResolver resolver) {
bResolver = resolver;
}
/** Get the bootstrap resolver
* @return the bootstrap resolver
*/
public BootstrapResolver getBootstrapResolver() {
return bResolver;
}
/** Query system property for verbosity level. */
private void queryVerbosityFromSysProp() {
String verbStr = SecuritySupport.getSystemProperty(pVerbosity);
if (verbStr != null) {
try {
int verb = Integer.parseInt(verbStr.trim());
verbosity = new Integer(verb);
debug.setDebug(verb);
} catch (Exception e) {
System.err.println("Cannot parse verbosity: \"" + verbStr + "\"");
}
}
}
/**
* Load the properties from the propertyFile and build the
* resources from it.
*/
private synchronized void readProperties() {
try {
propertyFileURI = CatalogManager.class.getResource("/"+propertyFile);
InputStream in =
CatalogManager.class.getResourceAsStream("/"+propertyFile);
if (in==null) {
if (!ignoreMissingProperties) {
System.err.println("Cannot find "+propertyFile);
// there's no reason to give this warning more than once
ignoreMissingProperties = true;
}
return;
}
resources = new PropertyResourceBundle(in);
} catch (MissingResourceException mre) {
if (!ignoreMissingProperties) {
System.err.println("Cannot read "+propertyFile);
}
} catch (java.io.IOException e) {
if (!ignoreMissingProperties) {
System.err.println("Failure trying to read "+propertyFile);
}
}
// This is a bit of a hack. After we've successfully read the properties,
// use them to set the default debug level, if the user hasn't already set
// the default debug level.
if (verbosity == null) {
try {
String verbStr = resources.getString("verbosity");
int verb = Integer.parseInt(verbStr.trim());
debug.setDebug(verb);
verbosity = new Integer(verb);
} catch (Exception e) {
// nop
}
}
}
/**
* Allow access to the static CatalogManager
*/
public static CatalogManager getStaticManager() {
return staticManager;
}
/**
* How are missing properties handled?
*
* <p>If true, missing or unreadable property files will
* not be reported. Otherwise, a message will be sent to System.err.
* </p>
*/
public boolean getIgnoreMissingProperties() {
return ignoreMissingProperties;
}
/**
* How should missing properties be handled?
*
* <p>If ignore is true, missing or unreadable property files will
* not be reported. Otherwise, a message will be sent to System.err.
* </p>
*/
public void setIgnoreMissingProperties(boolean ignore) {
ignoreMissingProperties = ignore;
}
/**
* How are missing properties handled?
*
* <p>If ignore is true, missing or unreadable property files will
* not be reported. Otherwise, a message will be sent to System.err.
* </p>
*
* @deprecated No longer static; use get/set methods.
*/
public void ignoreMissingProperties(boolean ignore) {
setIgnoreMissingProperties(ignore);
}
/**
* Obtain the verbosity setting from the properties.
*
* @return The verbosity level from the propertyFile or the
* defaultVerbosity.
*/
private int queryVerbosity () {
String defaultVerbStr = Integer.toString(defaultVerbosity);
String verbStr = SecuritySupport.getSystemProperty(pVerbosity);
if (verbStr == null) {
if (resources==null) readProperties();
if (resources != null) {
try {
verbStr = resources.getString("verbosity");
} catch (MissingResourceException e) {
verbStr = defaultVerbStr;
}
} else {
verbStr = defaultVerbStr;
}
}
int verb = defaultVerbosity;
try {
verb = Integer.parseInt(verbStr.trim());
} catch (Exception e) {
System.err.println("Cannot parse verbosity: \"" + verbStr + "\"");
}
// 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;
}
/**
* What is the current verbosity?
*/
public int getVerbosity() {
if (verbosity == null) {
verbosity = new Integer(queryVerbosity());
}
return verbosity.intValue();
}
/**
* Set the current verbosity.
*/
public void setVerbosity (int verbosity) {
this.verbosity = new Integer(verbosity);
debug.setDebug(verbosity);
}
/**
* What is the current verbosity?
*
* @deprecated No longer static; use get/set methods.
*/
public int verbosity () {
return getVerbosity();
}
/**
* Obtain the relativeCatalogs setting from the properties.
*
* @return The relativeCatalogs setting from the propertyFile or the
* defaultRelativeCatalogs.
*/
private boolean queryRelativeCatalogs () {
if (resources==null) readProperties();
if (resources==null) return defaultRelativeCatalogs;
try {
String allow = resources.getString("relative-catalogs");
return (allow.equalsIgnoreCase("true")
|| allow.equalsIgnoreCase("yes")
|| allow.equalsIgnoreCase("1"));
} catch (MissingResourceException e) {
return defaultRelativeCatalogs;
}
}
/**
* Get the relativeCatalogs setting.
*
* <p>This property is used when the catalogFiles property is
* interrogated. If true, then relative catalog entry file names
* are returned. If false, relative catalog entry file names are
* made absolute with respect to the properties file before returning
* them.</p>
*
* <p>This property <emph>only applies</emph> when the catalog files
* come from a properties file. If they come from a system property or
* the default list, they are never considered relative. (What would
* they be relative to?)</p>
*
* <p>In the properties, a value of 'yes', 'true', or '1' is considered
* true, anything else is false.</p>
*
* @return The relativeCatalogs setting from the propertyFile or the
* defaultRelativeCatalogs.
*/
public boolean getRelativeCatalogs () {
if (relativeCatalogs == null) {
relativeCatalogs = queryRelativeCatalogs() ? Boolean.TRUE : Boolean.FALSE;
}
return relativeCatalogs.booleanValue();
}
/**
* Set the relativeCatalogs setting.
*
* @see #getRelativeCatalogs()
*/
public void setRelativeCatalogs (boolean relative) {
relativeCatalogs = relative ? Boolean.TRUE : Boolean.FALSE;
}
/**
* Get the relativeCatalogs setting.
*
* @deprecated No longer static; use get/set methods.
*/
public boolean relativeCatalogs () {
return getRelativeCatalogs();
}
/**
* Obtain the list of catalog files from the properties.
*
* @return A semicolon delimited list of catlog file URIs
*/
private String queryCatalogFiles () {
String catalogList = SecuritySupport.getSystemProperty(pFiles);
fromPropertiesFile = false;
if (catalogList == null) {
if (resources == null) readProperties();
if (resources != null) {
try {
catalogList = resources.getString("catalogs");
fromPropertiesFile = true;
} catch (MissingResourceException e) {
System.err.println(propertyFile + ": catalogs not found.");
catalogList = null;
}
}
}
if (catalogList == null) {
catalogList = defaultCatalogFiles;
}
return catalogList;
}
/**
* Return the current list of catalog files.
*
* @return A vector of the catalog file names or null if no catalogs
* are available in the properties.
*/
public Vector getCatalogFiles() {
if (catalogFiles == null) {
catalogFiles = queryCatalogFiles();
}
StringTokenizer files = new StringTokenizer(catalogFiles, ";");
Vector catalogs = new Vector();
while (files.hasMoreTokens()) {
String catalogFile = files.nextToken();
URL absURI = null;
if (fromPropertiesFile && !relativeCatalogs()) {
try {
absURI = new URL(propertyFileURI, catalogFile);
catalogFile = absURI.toString();
} catch (MalformedURLException mue) {
absURI = null;
}
}
catalogs.add(catalogFile);
}
return catalogs;
}
/**
* Set the list of catalog files.
*/
public void setCatalogFiles(String fileList) {
catalogFiles = fileList;
fromPropertiesFile = false;
}
/**
* Return the current list of catalog files.
*
* @return A vector of the catalog file names or null if no catalogs
* are available in the properties.
*
* @deprecated No longer static; use get/set methods.
*/
public Vector catalogFiles() {
return getCatalogFiles();
}
/**
* Obtain the preferPublic setting from the properties.
*
* <p>In the properties, a value of 'public' is true,
* anything else is false.</p>
*
* @return True if prefer is public or the
* defaultPreferSetting.
*/
private boolean queryPreferPublic () {
String prefer = SecuritySupport.getSystemProperty(pPrefer);
if (prefer == null) {
if (resources==null) readProperties();
if (resources==null) return defaultPreferPublic;
try {
prefer = resources.getString("prefer");
} catch (MissingResourceException e) {
return defaultPreferPublic;
}
}
if (prefer == null) {
return defaultPreferPublic;
}
return (prefer.equalsIgnoreCase("public"));
}
/**
* Return the current prefer public setting.
*
* @return True if public identifiers are preferred.
*/
public boolean getPreferPublic () {
if (preferPublic == null) {
preferPublic = queryPreferPublic() ? Boolean.TRUE : Boolean.FALSE;
}
return preferPublic.booleanValue();
}
/**
* Set the prefer public setting.
*/
public void setPreferPublic (boolean preferPublic) {
this.preferPublic = preferPublic ? Boolean.TRUE : Boolean.FALSE;
}
/**
* Return the current prefer public setting.
*
* @return True if public identifiers are preferred.
*
* @deprecated No longer static; use get/set methods.
*/
public boolean preferPublic () {
return getPreferPublic();
}
/**
* Obtain the static-catalog setting from the properties.
*
* <p>In the properties, a value of 'yes', 'true', or '1' is considered
* true, anything else is false.</p>
*
* @return The static-catalog setting from the propertyFile or the
* defaultUseStaticCatalog.
*/
private boolean queryUseStaticCatalog () {
String staticCatalog = SecuritySupport.getSystemProperty(pStatic);
if (staticCatalog == null) {
if (resources==null) readProperties();
if (resources==null) return defaultUseStaticCatalog;
try {
staticCatalog = resources.getString("static-catalog");
} catch (MissingResourceException e) {
return defaultUseStaticCatalog;
}
}
if (staticCatalog == null) {
return defaultUseStaticCatalog;
}
return (staticCatalog.equalsIgnoreCase("true")
|| staticCatalog.equalsIgnoreCase("yes")
|| staticCatalog.equalsIgnoreCase("1"));
}
/**
* Get the current use static catalog setting.
*/
public boolean getUseStaticCatalog() {
if (useStaticCatalog == null) {
useStaticCatalog = queryUseStaticCatalog() ? Boolean.TRUE : Boolean.FALSE;
}
return useStaticCatalog.booleanValue();
}
/**
* Set the use static catalog setting.
*/
public void setUseStaticCatalog(boolean useStatic) {
useStaticCatalog = useStatic ? Boolean.TRUE : Boolean.FALSE;
}
/**
* Get the current use static catalog setting.
*
* @deprecated No longer static; use get/set methods.
*/
public boolean staticCatalog() {
return getUseStaticCatalog();
}
/**
* Get a new catalog instance.
*
* This method always returns a new instance of the underlying catalog class.
*/
public Catalog getPrivateCatalog() {
Catalog catalog = staticCatalog;
if (useStaticCatalog == null) {
useStaticCatalog = getUseStaticCatalog() ? Boolean.TRUE : Boolean.FALSE;
}
if (catalog == null || !useStaticCatalog.booleanValue()) {
try {
String catalogClassName = getCatalogClassName();
if (catalogClassName == null) {
catalog = new Catalog();
} else {
try {
catalog = (Catalog) ReflectUtil.forName(catalogClassName).newInstance();
} catch (ClassNotFoundException cnfe) {
debug.message(1,"Catalog class named '"
+ catalogClassName
+ "' could not be found. Using default.");
catalog = new Catalog();
} catch (ClassCastException cnfe) {
debug.message(1,"Class named '"
+ catalogClassName
+ "' is not a Catalog. Using default.");
catalog = new Catalog();
}
}
catalog.setCatalogManager(this);
catalog.setupReaders();
catalog.loadSystemCatalogs();
} catch (Exception ex) {
ex.printStackTrace();
}
if (useStaticCatalog.booleanValue()) {
staticCatalog = catalog;
}
}
return catalog;
}
/**
* Get a catalog instance.
*
* If this manager uses static catalogs, the same static catalog will
* always be returned. Otherwise a new catalog will be returned.
*/
public Catalog getCatalog() {
Catalog catalog = staticCatalog;
if (useStaticCatalog == null) {
useStaticCatalog = getUseStaticCatalog() ? Boolean.TRUE : Boolean.FALSE;
}
if (catalog == null || !useStaticCatalog.booleanValue()) {
catalog = getPrivateCatalog();
if (useStaticCatalog.booleanValue()) {
staticCatalog = catalog;
}
}
return catalog;
}
/**
* <p>Obtain the oasisXMLCatalogPI setting from the properties.</p>
*
* <p>In the properties, a value of 'yes', 'true', or '1' is considered
* true, anything else is false.</p>
*
* @return The oasisXMLCatalogPI setting from the propertyFile or the
* defaultOasisXMLCatalogPI.
*/
public boolean queryAllowOasisXMLCatalogPI () {
String allow = SecuritySupport.getSystemProperty(pAllowPI);
if (allow == null) {
if (resources==null) readProperties();
if (resources==null) return defaultOasisXMLCatalogPI;
try {
allow = resources.getString("allow-oasis-xml-catalog-pi");
} catch (MissingResourceException e) {
return defaultOasisXMLCatalogPI;
}
}
if (allow == null) {
return defaultOasisXMLCatalogPI;
}
return (allow.equalsIgnoreCase("true")
|| allow.equalsIgnoreCase("yes")
|| allow.equalsIgnoreCase("1"));
}
/**
* Get the current XML Catalog PI setting.
*/
public boolean getAllowOasisXMLCatalogPI () {
if (oasisXMLCatalogPI == null) {
oasisXMLCatalogPI = queryAllowOasisXMLCatalogPI() ? Boolean.TRUE : Boolean.FALSE;
}
return oasisXMLCatalogPI.booleanValue();
}
public boolean useServicesMechanism() {
return useServicesMechanism;
}
/**
* Set the XML Catalog PI setting
*/
public void setAllowOasisXMLCatalogPI(boolean allowPI) {
oasisXMLCatalogPI = allowPI ? Boolean.TRUE : Boolean.FALSE;
}
/**
* Get the current XML Catalog PI setting.
*
* @deprecated No longer static; use get/set methods.
*/
public boolean allowOasisXMLCatalogPI() {
return getAllowOasisXMLCatalogPI();
}
/**
* Obtain the Catalog class name setting from the properties.
*
*/
public String queryCatalogClassName () {
String className = SecuritySupport.getSystemProperty(pClassname);
if (className == null) {
if (resources==null) readProperties();
if (resources==null) return null;
try {
return resources.getString("catalog-class-name");
} catch (MissingResourceException e) {
return null;
}
}
return className;
}
/**
* Get the current Catalog class name.
*/
public String getCatalogClassName() {
if (catalogClassName == null) {
catalogClassName = queryCatalogClassName();
}
return catalogClassName;
}
/**
* Set the Catalog class name.
*/
public void setCatalogClassName(String className) {
catalogClassName = className;
}
/**
* Get the current Catalog class name.
*
* @deprecated No longer static; use get/set methods.
*/
public String catalogClassName() {
return getCatalogClassName();
}
}

View File

@ -1,697 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.util.Enumeration;
import java.util.Vector;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import javax.xml.parsers.SAXParserFactory;
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
import com.sun.org.apache.xml.internal.resolver.readers.SAXCatalogReader;
import com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader;
import com.sun.org.apache.xml.internal.resolver.readers.TR9401CatalogReader;
/**
* An extension to OASIS Open Catalog files, this class supports
* suffix-based matching and an external RFC2483 resolver.
*
* @see Catalog
* @deprecated The JDK internal Catalog API in package
* {@code com.sun.org.apache.xml.internal.resolver}
* is encapsulated in JDK 9. The entire implementation under the package is now
* deprecated and subject to removal in a future release. Users of the API
* should migrate to the {@linkplain javax.xml.catalog new public API}.
* <p>
* The new Catalog API is supported throughout the JDK XML Processors, which allows
* the use of Catalog by simply setting a path to a Catalog file as a property.
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
* @version 1.0
*/
@Deprecated(since="9", forRemoval=true)
public class Resolver extends Catalog {
/**
* The URISUFFIX Catalog Entry type.
*
* <p>URI suffix entries match URIs that end in a specified suffix.</p>
*/
public static final int URISUFFIX = CatalogEntry.addEntryType("URISUFFIX", 2);
/**
* The SYSTEMSUFFIX Catalog Entry type.
*
* <p>System suffix entries match system identifiers that end in a
* specified suffix.</p>
*/
public static final int SYSTEMSUFFIX = CatalogEntry.addEntryType("SYSTEMSUFFIX", 2);
/**
* The RESOLVER Catalog Entry type.
*
* <p>A hook for providing support for web-based backup resolvers.</p>
*/
public static final int RESOLVER = CatalogEntry.addEntryType("RESOLVER", 1);
/**
* The SYSTEMREVERSE Catalog Entry type.
*
* <p>This is a bit of a hack. There's no actual SYSTEMREVERSE entry,
* but this entry type is used to indicate that a reverse lookup is
* being performed. (This allows the Resolver to implement
* RFC2483 I2N and I2NS.)
*/
public static final int SYSTEMREVERSE
= CatalogEntry.addEntryType("SYSTEMREVERSE", 1);
/**
* Setup readers.
*/
public void setupReaders() {
SAXParserFactory spf = catalogManager.useServicesMechanism() ?
SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
spf.setNamespaceAware(true);
spf.setValidating(false);
SAXCatalogReader saxReader = new SAXCatalogReader(spf);
saxReader.setCatalogParser(null, "XCatalog",
"com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader");
saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName,
"catalog",
"com.sun.org.apache.xml.internal.resolver.readers.ExtendedXMLCatalogReader");
addReader("application/xml", saxReader);
TR9401CatalogReader textReader = new TR9401CatalogReader();
addReader("text/plain", textReader);
}
/**
* Cleanup and process a Catalog entry.
*
* <p>This method processes each Catalog entry, changing mapped
* relative system identifiers into absolute ones (based on the current
* base URI), and maintaining other information about the current
* catalog.</p>
*
* @param entry The CatalogEntry to process.
*/
public void addEntry(CatalogEntry entry) {
int type = entry.getEntryType();
if (type == URISUFFIX) {
String suffix = normalizeURI(entry.getEntryArg(0));
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
entry.setEntryArg(1, fsi);
catalogManager.debug.message(4, "URISUFFIX", suffix, fsi);
} else if (type == SYSTEMSUFFIX) {
String suffix = normalizeURI(entry.getEntryArg(0));
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
entry.setEntryArg(1, fsi);
catalogManager.debug.message(4, "SYSTEMSUFFIX", suffix, fsi);
}
super.addEntry(entry);
}
/**
* Return the applicable URI.
*
* <p>If a URI entry exists in the Catalog
* for the URI specified, return the mapped value.</p>
*
* <p>In the Resolver (as opposed to the Catalog) class, if the
* URI isn't found by the usual algorithm, URISUFFIX entries are
* considered.</p>
*
* <p>URI comparison is case sensitive.</p>
*
* @param uri The URI to locate in the catalog.
*
* @return The resolved URI.
*
* @throws MalformedURLException The system identifier of a
* subordinate catalog cannot be turned into a valid URL.
* @throws IOException Error reading subordinate catalog file.
*/
public String resolveURI(String uri)
throws MalformedURLException, IOException {
String resolved = super.resolveURI(uri);
if (resolved != null) {
return resolved;
}
Enumeration en = catalogEntries.elements();
while (en.hasMoreElements()) {
CatalogEntry e = (CatalogEntry) en.nextElement();
if (e.getEntryType() == RESOLVER) {
resolved = resolveExternalSystem(uri, e.getEntryArg(0));
if (resolved != null) {
return resolved;
}
} else if (e.getEntryType() == URISUFFIX) {
String suffix = e.getEntryArg(0);
String result = e.getEntryArg(1);
if (suffix.length() <= uri.length()
&& uri.substring(uri.length()-suffix.length()).equals(suffix)) {
return result;
}
}
}
// Otherwise, look in the subordinate catalogs
return resolveSubordinateCatalogs(Catalog.URI,
null,
null,
uri);
}
/**
* Return the applicable SYSTEM system identifier, resorting
* to external RESOLVERs if necessary.
*
* <p>If a SYSTEM entry exists in the Catalog
* for the system ID specified, return the mapped value.</p>
*
* <p>In the Resolver (as opposed to the Catalog) class, if the
* URI isn't found by the usual algorithm, SYSTEMSUFFIX entries are
* considered.</p>
*
* <p>On Windows-based operating systems, the comparison between
* the system identifier provided and the SYSTEM entries in the
* Catalog is case-insensitive.</p>
*
* @param systemId The system ID to locate in the catalog.
*
* @return The system identifier to use for systemId.
*
* @throws MalformedURLException The formal system identifier of a
* subordinate catalog cannot be turned into a valid URL.
* @throws IOException Error reading subordinate catalog file.
*/
public String resolveSystem(String systemId)
throws MalformedURLException, IOException {
String resolved = super.resolveSystem(systemId);
if (resolved != null) {
return resolved;
}
Enumeration en = catalogEntries.elements();
while (en.hasMoreElements()) {
CatalogEntry e = (CatalogEntry) en.nextElement();
if (e.getEntryType() == RESOLVER) {
resolved = resolveExternalSystem(systemId, e.getEntryArg(0));
if (resolved != null) {
return resolved;
}
} else if (e.getEntryType() == SYSTEMSUFFIX) {
String suffix = e.getEntryArg(0);
String result = e.getEntryArg(1);
if (suffix.length() <= systemId.length()
&& systemId.substring(systemId.length()-suffix.length()).equals(suffix)) {
return result;
}
}
}
return resolveSubordinateCatalogs(Catalog.SYSTEM,
null,
null,
systemId);
}
/**
* Return the applicable PUBLIC or SYSTEM identifier, resorting
* to external resolvers if necessary.
*
* <p>This method searches the Catalog and returns the system
* identifier specified for the given system or
* public identifiers. If
* no appropriate PUBLIC or SYSTEM entry is found in the Catalog,
* null is returned.</p>
*
* <p>Note that a system or public identifier in the current catalog
* (or subordinate catalogs) will be used in preference to an
* external resolver. Further, if a systemId is present, the external
* resolver(s) will be queried for that before the publicId.</p>
*
* @param publicId The public identifier to locate in the catalog.
* Public identifiers are normalized before comparison.
* @param systemId The nominal system identifier for the entity
* in question (as provided in the source document).
*
* @throws MalformedURLException The formal system identifier of a
* subordinate catalog cannot be turned into a valid URL.
* @throws IOException Error reading subordinate catalog file.
*
* @return The system identifier to use.
* Note that the nominal system identifier is not returned if a
* match is not found in the catalog, instead null is returned
* to indicate that no match was found.
*/
public String resolvePublic(String publicId, String systemId)
throws MalformedURLException, IOException {
String resolved = super.resolvePublic(publicId, systemId);
if (resolved != null) {
return resolved;
}
Enumeration en = catalogEntries.elements();
while (en.hasMoreElements()) {
CatalogEntry e = (CatalogEntry) en.nextElement();
if (e.getEntryType() == RESOLVER) {
if (systemId != null) {
resolved = resolveExternalSystem(systemId,
e.getEntryArg(0));
if (resolved != null) {
return resolved;
}
}
resolved = resolveExternalPublic(publicId, e.getEntryArg(0));
if (resolved != null) {
return resolved;
}
}
}
return resolveSubordinateCatalogs(Catalog.PUBLIC,
null,
publicId,
systemId);
}
/**
* Query an external RFC2483 resolver for a system identifier.
*
* @param systemId The system ID to locate.
* @param resolver The name of the resolver to use.
*
* @return The system identifier to use for the systemId.
*/
protected String resolveExternalSystem(String systemId, String resolver)
throws MalformedURLException, IOException {
Resolver r = queryResolver(resolver, "i2l", systemId, null);
if (r != null) {
return r.resolveSystem(systemId);
} else {
return null;
}
}
/**
* Query an external RFC2483 resolver for a public identifier.
*
* @param publicId The system ID to locate.
* @param resolver The name of the resolver to use.
*
* @return The system identifier to use for the systemId.
*/
protected String resolveExternalPublic(String publicId, String resolver)
throws MalformedURLException, IOException {
Resolver r = queryResolver(resolver, "fpi2l", publicId, null);
if (r != null) {
return r.resolvePublic(publicId, null);
} else {
return null;
}
}
/**
* Query an external RFC2483 resolver.
*
* @param resolver The URL of the RFC2483 resolver.
* @param command The command to send the resolver.
* @param arg1 The first argument to the resolver.
* @param arg2 The second argument to the resolver, usually null.
*
* @return The Resolver constructed.
*/
protected Resolver queryResolver(String resolver,
String command,
String arg1,
String arg2) {
InputStream iStream = null;
String RFC2483 = resolver + "?command=" + command
+ "&format=tr9401&uri=" + arg1
+ "&uri2=" + arg2;
String line = null;
try {
URL url = new URL(RFC2483);
URLConnection urlCon = url.openConnection();
urlCon.setUseCaches(false);
Resolver r = (Resolver) newCatalog();
String cType = urlCon.getContentType();
// I don't care about the character set or subtype
if (cType.indexOf(";") > 0) {
cType = cType.substring(0, cType.indexOf(";"));
}
r.parseCatalog(cType, urlCon.getInputStream());
return r;
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.UNPARSEABLE) {
catalogManager.debug.message(1, "Unparseable catalog: " + RFC2483);
} else if (cex.getExceptionType()
== CatalogException.UNKNOWN_FORMAT) {
catalogManager.debug.message(1, "Unknown catalog format: " + RFC2483);
}
return null;
} catch (MalformedURLException mue) {
catalogManager.debug.message(1, "Malformed resolver URL: " + RFC2483);
return null;
} catch (IOException ie) {
catalogManager.debug.message(1, "I/O Exception opening resolver: " + RFC2483);
return null;
}
}
/**
* Append two vectors, returning the result.
*
* @param vec The first vector
* @param appvec The vector to be appended
* @return The vector vec, with appvec's elements appended to it
*/
private Vector appendVector(Vector vec, Vector appvec) {
if (appvec != null) {
for (int count = 0; count < appvec.size(); count++) {
vec.addElement(appvec.elementAt(count));
}
}
return vec;
}
/**
* Find the URNs for a given system identifier in all catalogs.
*
* @param systemId The system ID to locate.
*
* @return A vector of URNs that map to the systemId.
*/
public Vector resolveAllSystemReverse(String systemId)
throws MalformedURLException, IOException {
Vector resolved = new Vector();
// If there's a SYSTEM entry in this catalog, use it
if (systemId != null) {
Vector localResolved = resolveLocalSystemReverse(systemId);
resolved = appendVector(resolved, localResolved);
}
// Otherwise, look in the subordinate catalogs
Vector subResolved = resolveAllSubordinateCatalogs(SYSTEMREVERSE,
null,
null,
systemId);
return appendVector(resolved, subResolved);
}
/**
* Find the URN for a given system identifier.
*
* @param systemId The system ID to locate.
*
* @return A (single) URN that maps to the systemId.
*/
public String resolveSystemReverse(String systemId)
throws MalformedURLException, IOException {
Vector resolved = resolveAllSystemReverse(systemId);
if (resolved != null && resolved.size() > 0) {
return (String) resolved.elementAt(0);
} else {
return null;
}
}
/**
* Return the applicable SYSTEM system identifiers.
*
* <p>If one or more SYSTEM entries exists in the Catalog
* for the system ID specified, return the mapped values.</p>
*
* <p>The caller is responsible for doing any necessary
* normalization of the system identifier before calling
* this method. For example, a relative system identifier in
* a document might be converted to an absolute system identifier
* before attempting to resolve it.</p>
*
* <p>Note that this function will force all subordinate catalogs
* to be loaded.</p>
*
* <p>On Windows-based operating systems, the comparison between
* the system identifier provided and the SYSTEM entries in the
* Catalog is case-insensitive.</p>
*
* @param systemId The system ID to locate in the catalog.
*
* @return The system identifier to use for the notation.
*
* @throws MalformedURLException The formal system identifier of a
* subordinate catalog cannot be turned into a valid URL.
* @throws IOException Error reading subordinate catalog file.
*/
public Vector resolveAllSystem(String systemId)
throws MalformedURLException, IOException {
Vector resolutions = new Vector();
// If there are SYSTEM entries in this catalog, start with them
if (systemId != null) {
Vector localResolutions = resolveAllLocalSystem(systemId);
resolutions = appendVector(resolutions, localResolutions);
}
// Then look in the subordinate catalogs
Vector subResolutions = resolveAllSubordinateCatalogs(SYSTEM,
null,
null,
systemId);
resolutions = appendVector(resolutions, subResolutions);
if (resolutions.size() > 0) {
return resolutions;
} else {
return null;
}
}
/**
* Return all applicable SYSTEM system identifiers in this
* catalog.
*
* <p>If one or more SYSTEM entries exists in the catalog file
* for the system ID specified, return the mapped values.</p>
*
* @param systemId The system ID to locate in the catalog
*
* @return A vector of the mapped system identifiers or null
*/
private Vector resolveAllLocalSystem(String systemId) {
Vector map = new Vector();
String osname = SecuritySupport.getSystemProperty("os.name");
boolean windows = (osname.indexOf("Windows") >= 0);
Enumeration en = catalogEntries.elements();
while (en.hasMoreElements()) {
CatalogEntry e = (CatalogEntry) en.nextElement();
if (e.getEntryType() == SYSTEM
&& (e.getEntryArg(0).equals(systemId)
|| (windows
&& e.getEntryArg(0).equalsIgnoreCase(systemId)))) {
map.addElement(e.getEntryArg(1));
}
}
if (map.size() == 0) {
return null;
} else {
return map;
}
}
/**
* Find the URNs for a given system identifier in the current catalog.
*
* @param systemId The system ID to locate.
*
* @return A vector of URNs that map to the systemId.
*/
private Vector resolveLocalSystemReverse(String systemId) {
Vector map = new Vector();
String osname = SecuritySupport.getSystemProperty("os.name");
boolean windows = (osname.indexOf("Windows") >= 0);
Enumeration en = catalogEntries.elements();
while (en.hasMoreElements()) {
CatalogEntry e = (CatalogEntry) en.nextElement();
if (e.getEntryType() == SYSTEM
&& (e.getEntryArg(1).equals(systemId)
|| (windows
&& e.getEntryArg(1).equalsIgnoreCase(systemId)))) {
map.addElement(e.getEntryArg(0));
}
}
if (map.size() == 0) {
return null;
} else {
return map;
}
}
/**
* Search the subordinate catalogs, in order, looking for all
* match.
*
* <p>This method searches the Catalog and returns all of the system
* identifiers specified for the given entity type with the given
* name, public, and system identifiers. In some contexts, these
* may be null.</p>
*
* @param entityType The CatalogEntry type for which this query is
* being conducted. This is necessary in order to do the approprate
* query on a subordinate catalog.
* @param entityName The name of the entity being searched for, if
* appropriate.
* @param publicId The public identifier of the entity in question
* (as provided in the source document).
* @param systemId The nominal system identifier for the entity
* in question (as provided in the source document).
*
* @throws MalformedURLException The formal system identifier of a
* delegated catalog cannot be turned into a valid URL.
* @throws IOException Error reading delegated catalog file.
*
* @return The system identifier to use.
* Note that the nominal system identifier is not returned if a
* match is not found in the catalog, instead null is returned
* to indicate that no match was found.
*/
private synchronized Vector resolveAllSubordinateCatalogs(int entityType,
String entityName,
String publicId,
String systemId)
throws MalformedURLException, IOException {
Vector resolutions = new Vector();
for (int catPos = 0; catPos < catalogs.size(); catPos++) {
Resolver c = null;
try {
c = (Resolver) catalogs.elementAt(catPos);
} catch (ClassCastException e) {
String catfile = (String) catalogs.elementAt(catPos);
c = (Resolver) newCatalog();
try {
c.parseCatalog(catfile);
} catch (MalformedURLException mue) {
catalogManager.debug.message(1, "Malformed Catalog URL", catfile);
} catch (FileNotFoundException fnfe) {
catalogManager.debug.message(1, "Failed to load catalog, file not found",
catfile);
} catch (IOException ioe) {
catalogManager.debug.message(1, "Failed to load catalog, I/O error", catfile);
}
catalogs.setElementAt(c, catPos);
}
String resolved = null;
// Ok, now what are we supposed to call here?
if (entityType == DOCTYPE) {
resolved = c.resolveDoctype(entityName,
publicId,
systemId);
if (resolved != null) {
// Only find one DOCTYPE resolution
resolutions.addElement(resolved);
return resolutions;
}
} else if (entityType == DOCUMENT) {
resolved = c.resolveDocument();
if (resolved != null) {
// Only find one DOCUMENT resolution
resolutions.addElement(resolved);
return resolutions;
}
} else if (entityType == ENTITY) {
resolved = c.resolveEntity(entityName,
publicId,
systemId);
if (resolved != null) {
// Only find one ENTITY resolution
resolutions.addElement(resolved);
return resolutions;
}
} else if (entityType == NOTATION) {
resolved = c.resolveNotation(entityName,
publicId,
systemId);
if (resolved != null) {
// Only find one NOTATION resolution
resolutions.addElement(resolved);
return resolutions;
}
} else if (entityType == PUBLIC) {
resolved = c.resolvePublic(publicId, systemId);
if (resolved != null) {
// Only find one PUBLIC resolution
resolutions.addElement(resolved);
return resolutions;
}
} else if (entityType == SYSTEM) {
Vector localResolutions = c.resolveAllSystem(systemId);
resolutions = appendVector(resolutions, localResolutions);
break;
} else if (entityType == SYSTEMREVERSE) {
Vector localResolutions = c.resolveAllSystemReverse(systemId);
resolutions = appendVector(resolutions, localResolutions);
}
}
if (resolutions != null) {
return resolutions;
} else {
return null;
}
}
}

View File

@ -1,207 +0,0 @@
/*
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.helpers;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
/**
* A simple bootstrapping resolver.
*
* <p>This class is used as the entity resolver when reading XML Catalogs.
* It searches for the OASIS XML Catalog DTD, Relax NG Grammar and W3C XML Schema
* as resources (e.g., in the resolver jar file).</p>
*
* <p>If you have your own DTDs or schemas, you can extend this class and
* set the BootstrapResolver in your CatalogManager.</p>
*
* @see com.sun.org.apache.xml.internal.resolver.CatalogManager
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class BootstrapResolver implements EntityResolver, URIResolver {
/** URI of the W3C XML Schema for OASIS XML Catalog files. */
public static final String xmlCatalogXSD = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.xsd";
/** URI of the RELAX NG Grammar for OASIS XML Catalog files. */
public static final String xmlCatalogRNG = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.rng";
/** Public identifier for OASIS XML Catalog files. */
public static final String xmlCatalogPubId = "-//OASIS//DTD XML Catalogs V1.0//EN";
/** System identifier for OASIS XML Catalog files. */
public static final String xmlCatalogSysId = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd";
/** Public identifier for legacy Apache XCatalog files. There is no official system identifier for XCatalog files. */
public static final String xCatalogPubId = "-//DTD XCatalog//EN";
/** Private hash used for public identifiers. */
private final Map<String, String> publicMap = new HashMap<>();
/** Private hash used for system identifiers. */
private final Map<String, String> systemMap = new HashMap<>();
/** Private hash used for URIs. */
private final Map<String, String> uriMap = new HashMap<>();
/** Constructor. */
public BootstrapResolver() {
URL url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.dtd");
if (url != null) {
publicMap.put(xmlCatalogPubId, url.toString());
systemMap.put(xmlCatalogSysId, url.toString());
}
url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.rng");
if (url != null) {
uriMap.put(xmlCatalogRNG, url.toString());
}
url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.xsd");
if (url != null) {
uriMap.put(xmlCatalogXSD, url.toString());
}
url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/xcatalog.dtd");
if (url != null) {
publicMap.put(xCatalogPubId, url.toString());
}
}
/** SAX resolveEntity API. */
public InputSource resolveEntity (String publicId, String systemId) {
String resolved = null;
if (systemId != null && systemMap.containsKey(systemId)) {
resolved = systemMap.get(systemId);
} else if (publicId != null && publicMap.containsKey(publicId)) {
resolved = publicMap.get(publicId);
}
if (resolved != null) {
try {
InputSource iSource = new InputSource(resolved);
iSource.setPublicId(publicId);
// Ideally this method would not attempt to open the
// InputStream, but there is a bug (in Xerces, at least)
// that causes the parser to mistakenly open the wrong
// system identifier if the returned InputSource does
// not have a byteStream.
//
// It could be argued that we still shouldn't do this here,
// but since the purpose of calling the entityResolver is
// almost certainly to open the input stream, it seems to
// do little harm.
//
URL url = new URL(resolved);
InputStream iStream = url.openStream();
iSource.setByteStream(iStream);
return iSource;
} catch (Exception e) {
// FIXME: silently fail?
return null;
}
}
return null;
}
/** Transformer resolve API. */
public Source resolve(String href, String base)
throws TransformerException {
String uri = href;
String fragment = null;
int hashPos = href.indexOf("#");
if (hashPos >= 0) {
uri = href.substring(0, hashPos);
fragment = href.substring(hashPos+1);
}
String result = null;
if (href != null && uriMap.containsKey(href)) {
result = uriMap.get(href);
}
if (result == null) {
try {
URL url = null;
if (base==null) {
url = new URL(uri);
result = url.toString();
} else {
URL baseURL = new URL(base);
url = (href.length()==0 ? baseURL : new URL(baseURL, uri));
result = url.toString();
}
} catch (java.net.MalformedURLException mue) {
// try to make an absolute URI from the current base
String absBase = makeAbsolute(base);
if (!absBase.equals(base)) {
// don't bother if the absBase isn't different!
return resolve(href, absBase);
} else {
throw new TransformerException("Malformed URL "
+ href + "(base " + base + ")",
mue);
}
}
}
SAXSource source = new SAXSource();
source.setInputSource(new InputSource(result));
return source;
}
/** Attempt to construct an absolute URI */
private String makeAbsolute(String uri) {
if (uri == null) {
uri = "";
}
try {
URL url = new URL(uri);
return url.toString();
} catch (MalformedURLException mue) {
try {
URL fileURL = FileURL.makeURL(uri);
return fileURL.toString();
} catch (MalformedURLException mue2) {
// bail
return uri;
}
}
}
}

View File

@ -1,105 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.helpers;
/**
* Static debugging/messaging class for Catalogs.
*
* <p>This class defines a set of static methods that can be called
* to produce debugging messages. Messages have an associated "debug
* level" and messages below the current setting are not displayed.</p>
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class Debug {
/** The internal debug level. */
protected int debug = 0;
/** Constructor */
public Debug() {
// nop
}
/** Set the debug level for future messages. */
public void setDebug(int newDebug) {
debug = newDebug;
}
/** Get the current debug level. */
public int getDebug() {
return debug;
}
/**
* Print debug message (if the debug level is high enough).
*
* <p>Prints "the message"</p>
*
* @param level The debug level of this message. This message
* will only be
* displayed if the current debug level is at least equal to this
* value.
* @param message The text of the message.
*/
public void message(int level, String message) {
if (debug >= level) {
System.out.println(message);
}
}
/**
* Print debug message (if the debug level is high enough).
*
* <p>Prints "the message: spec"</p>
*
* @param level The debug level of this message. This message
* will only be
* displayed if the current debug level is at least equal to this
* value.
* @param message The text of the message.
* @param spec An argument to the message.
*/
public void message(int level, String message, String spec) {
if (debug >= level) {
System.out.println(message + ": " + spec);
}
}
/**
* Print debug message (if the debug level is high enough).
*
* <p>Prints "the message: spec1" and "spec2" indented on the next line.</p>
*
* @param level The debug level of this message. This message
* will only be
* displayed if the current debug level is at least equal to this
* value.
* @param message The text of the message.
* @param spec1 An argument to the message.
* @param spec2 Another argument to the message.
*/
public void message(int level, String message,
String spec1, String spec2) {
if (debug >= level) {
System.out.println(message + ": " + spec1);
System.out.println("\t" + spec2);
}
}
}

View File

@ -1,87 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.helpers;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.File;
/**
* Static method for dealing with file: URLs.
*
* <p>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.</p>
*
* <p>(Yes, I'd rather have called this class FileUR<b>I</b>, but
* given that a jave.net.URL is returned, it seemed...even more
* confusing.)</p>
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
* @version 1.0
*/
public abstract class FileURL {
protected FileURL() { }
/**
* Construct a file: URL for a path name.
*
* <p>URLs in the file: scheme can be constructed for paths on
* the local file system. Several possibilities need to be considered:
* </p>
*
* <ul>
* <li>If the path does not begin with a slash, then it is assumed
* to reside in the users current working directory
* (System.getProperty("user.dir")).</li>
* <li>On Windows machines, the current working directory uses
* backslashes (\\, instead of /).</li>
* <li>If the current working directory is "/", don't add an extra
* slash before the base name.</li>
* </ul>
*
* <p>This method is declared static so that other classes
* can use it directly.</p>
*
* @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);
}
*/
File file = new File(pathname);
return file.toURI().toURL();
}
}

View File

@ -1,110 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.helpers;
import org.w3c.dom.*;
/**
* Static Namespace query methods.
*
* <p>This class defines a set of static methods that can be called
* to analyze the namespace properties of DOM nodes.</p>
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class Namespaces {
/**
* Returns the "prefix" part of a QName or the empty string (not
* null) if the name has no prefix.
*
* @param element The QName of an element.
* @return The prefix part of the element name.
*/
public static String getPrefix(Element element) {
String name = element.getTagName();
String prefix = "";
final int indexOfColon = name.indexOf(':');
if (indexOfColon > 0) {
prefix = name.substring(0, indexOfColon);
}
return prefix;
}
/**
* Returns the "localname" part of a QName, which is the whole
* name if it has no prefix.
*
* @param element The QName of an element.
* @return The local part of a QName.
*/
public static String getLocalName(Element element) {
String name = element.getTagName();
final int indexOfColon = name.indexOf(':');
if (indexOfColon > 0) {
name = name.substring(indexOfColon + 1);
}
return name;
}
/**
* Returns the namespace URI for the specified prefix at the
* specified context node.
*
* @param node The context node.
* @param prefix The prefix.
* @return The namespace URI associated with the prefix, or
* null if no namespace declaration exists for the prefix.
*/
public static String getNamespaceURI(Node node, String prefix) {
if (node == null || node.getNodeType() != Node.ELEMENT_NODE) {
return null;
}
if (prefix.length() == 0) {
if (((Element) node).hasAttribute("xmlns")) {
return ((Element) node).getAttribute("xmlns");
}
} else {
String nsattr = "xmlns:" + prefix;
if (((Element) node).hasAttribute(nsattr)) {
return ((Element) node).getAttribute(nsattr);
}
}
return getNamespaceURI(node.getParentNode(), prefix);
}
/**
* Returns the namespace URI for the namespace to which the
* element belongs.
*
* @param element The element.
* @return The namespace URI associated with the namespace of the
* element, or null if no namespace declaration exists for it.
*/
public static String getNamespaceURI(Element element) {
String prefix = getPrefix(element);
return getNamespaceURI(element, prefix);
}
}

View File

@ -1,169 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.helpers;
/**
* Static methods for dealing with public identifiers.
*
* <p>This class defines a set of static methods that can be called
* to handle public identifiers.</p>
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public abstract class PublicId {
protected PublicId() {}
/**
* Normalize a public identifier.
*
* <p>Public identifiers must be normalized according to the following
* rules before comparisons between them can be made:</p>
*
* <ul>
* <li>Whitespace characters are normalized to spaces (e.g., line feeds,
* tabs, etc. become spaces).</li>
* <li>Leading and trailing whitespace is removed.</li>
* <li>Multiple internal whitespaces are normalized to a single
* space.</li>
* </ul>
*
* <p>This method is declared static so that other classes
* can use it directly.</p>
*
* @param publicId The unnormalized public identifier.
*
* @return The normalized identifier.
*/
public static String normalize(String publicId) {
String normal = publicId.replace('\t', ' ');
normal = normal.replace('\r', ' ');
normal = normal.replace('\n', ' ');
normal = normal.trim();
int pos;
while ((pos = normal.indexOf(" ")) >= 0) {
normal = normal.substring(0, pos) + normal.substring(pos+1);
}
return normal;
}
/**
* Encode a public identifier as a "publicid" URN.
*
* <p>This method is declared static so that other classes
* can use it directly.</p>
*
* @param publicId The unnormalized public identifier.
*
* @return The normalized identifier.
*/
public static String encodeURN(String publicId) {
String urn = PublicId.normalize(publicId);
urn = PublicId.stringReplace(urn, "%", "%25");
urn = PublicId.stringReplace(urn, ";", "%3B");
urn = PublicId.stringReplace(urn, "'", "%27");
urn = PublicId.stringReplace(urn, "?", "%3F");
urn = PublicId.stringReplace(urn, "#", "%23");
urn = PublicId.stringReplace(urn, "+", "%2B");
urn = PublicId.stringReplace(urn, " ", "+");
urn = PublicId.stringReplace(urn, "::", ";");
urn = PublicId.stringReplace(urn, ":", "%3A");
urn = PublicId.stringReplace(urn, "//", ":");
urn = PublicId.stringReplace(urn, "/", "%2F");
StringBuilder buffer = new StringBuilder(13 + urn.length());
buffer.append("urn:publicid:");
buffer.append(urn);
return buffer.toString();
}
/**
* Decode a "publicid" URN into a public identifier.
*
* <p>This method is declared static so that other classes
* can use it directly.</p>
*
* @param urn The urn:publicid: URN
*
* @return The normalized identifier.
*/
public static String decodeURN(String urn) {
String publicId;
if (urn.startsWith("urn:publicid:")) {
publicId = urn.substring(13);
}
else {
return urn;
}
final boolean hasEscape = (publicId.indexOf('%') >= 0);
if (hasEscape) {
publicId = PublicId.stringReplace(publicId, "%2F", "/");
}
publicId = PublicId.stringReplace(publicId, ":", "//");
if (hasEscape) {
publicId = PublicId.stringReplace(publicId, "%3A", ":");
}
publicId = PublicId.stringReplace(publicId, ";", "::");
publicId = PublicId.stringReplace(publicId, "+", " ");
if (hasEscape) {
publicId = PublicId.stringReplace(publicId, "%2B", "+");
publicId = PublicId.stringReplace(publicId, "%23", "#");
publicId = PublicId.stringReplace(publicId, "%3F", "?");
publicId = PublicId.stringReplace(publicId, "%27", "'");
publicId = PublicId.stringReplace(publicId, "%3B", ";");
publicId = PublicId.stringReplace(publicId, "%25", "%");
}
return publicId;
}
/**
* Replace one string with another.
*/
private static String stringReplace(String str,
String oldStr,
String newStr) {
int pos = str.indexOf(oldStr);
if (pos >= 0) {
final StringBuilder buffer = new StringBuilder();
final int oldStrLength = oldStr.length();
int start = 0;
do {
for (int i = start; i < pos; ++i) {
buffer.append(str.charAt(i));
}
buffer.append(newStr);
start = pos + oldStrLength;
pos = str.indexOf(oldStr, start);
}
while (pos >= 0);
final int strLength = str.length();
for (int i = start; i < strLength; ++i) {
buffer.append(str.charAt(i));
}
return buffer.toString();
}
return str;
}
}

View File

@ -1,75 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.readers;
import java.io.IOException;
import java.net.MalformedURLException;
import com.sun.org.apache.xml.internal.resolver.CatalogException;
import java.io.InputStream;
import com.sun.org.apache.xml.internal.resolver.Catalog;
/**
* The CatalogReader interface.
*
* <p>The Catalog class requires that classes implement this interface
* in order to be used to read catalogs. Examples of CatalogReaders
* include the TextCatalogReader, the SAXCatalogReader, and the
* DOMCatalogReader.</p>
*
* @see Catalog
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public interface CatalogReader {
/**
* Read a catalog from a file.
*
* <p>This class reads a catalog from a URL.</p>
*
* @param catalog The catalog for which this reader is called.
* @param fileUrl The URL of a document to be read.
* @throws MalformedURLException if the specified URL cannot be
* turned into a URL object.
* @throws IOException if the URL cannot be read.
* @throws UnknownCatalogFormatException if the catalog format is
* not recognized.
* @throws UnparseableCatalogException if the catalog cannot be parsed.
* (For example, if it is supposed to be XML and isn't well-formed.)
*/
public void readCatalog(Catalog catalog, String fileUrl)
throws MalformedURLException, IOException, CatalogException;
/**
* Read a catalog from an input stream.
*
* <p>This class reads a catalog from an input stream.</p>
*
* @param catalog The catalog for which this reader is called.
* @param is The input stream that is to be read.
* @throws IOException if the URL cannot be read.
* @throws UnknownCatalogFormatException if the catalog format is
* not recognized.
* @throws UnparseableCatalogException if the catalog cannot be parsed.
* (For example, if it is supposed to be XML and isn't well-formed.)
*/
public void readCatalog(Catalog catalog, InputStream is)
throws IOException, CatalogException;
}

View File

@ -1,47 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.readers;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import org.w3c.dom.Node;
/**
* The DOMCatalogParser interface.
*
* <p>This interface must be implemented in order for a class to
* participate as a parser for the DOMCatalogReader.
*
* @see Catalog
* @see DOMCatalogReader
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public interface DOMCatalogParser {
/**
* Parse a DOM node as a catalog entry.
*
* <p>This method is expected to analyze the specified node and
* construct appropriate catalog entry(ies) from it.</p>
*
* @param catalog The catalog for which this node is being considered.
* @param node The DOM Node from the catalog.
*/
public void parseCatalogEntry(Catalog catalog, Node node);
}

View File

@ -1,240 +0,0 @@
/*
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.readers;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.CatalogException;
import com.sun.org.apache.xml.internal.resolver.helpers.Namespaces;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import sun.reflect.misc.ReflectUtil;
/**
* A DOM-based CatalogReader.
*
* <p>This class is used to read XML Catalogs using the DOM. This reader
* has an advantage over the SAX-based reader that it can analyze the
* DOM tree rather than simply a series of SAX events. It has the disadvantage
* that it requires all of the code necessary to build and walk a DOM
* tree.</p>
*
* <p>Since the choice of CatalogReaders (in the InputStream case) can only
* be made on the basis of MIME type, the following problem occurs: only
* one CatalogReader can exist for all XML mime types. In order to get
* around this problem, the DOMCatalogReader relies on a set of external
* CatalogParsers to actually build the catalog.</p>
*
* <p>The selection of CatalogParsers is made on the basis of the QName
* of the root element of the document.</p>
*
*
* @see Catalog
* @see CatalogReader
* @see SAXCatalogReader
* @see TextCatalogReader
* @see DOMCatalogParser
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class DOMCatalogReader implements CatalogReader {
/**
* Mapping table from QNames to CatalogParser classes.
*
* <p>Each key in this hash table has the form "elementname"
* or "{namespaceuri}elementname". The former is used if the
* namespace URI is null.</p>
*/
protected Map<String, String> namespaceMap = new HashMap<>();
/**
* Add a new parser to the reader.
*
* <p>This method associates the specified parserClass with the
* namespaceURI/rootElement names specified.</p>
*
* @param namespaceURI The namespace URI. <em>Not</em> the prefix.
* @param rootElement The name of the root element.
* @param parserClass The name of the parserClass to instantiate
* for this kind of catalog.
*/
public void setCatalogParser(String namespaceURI,
String rootElement,
String parserClass) {
if (namespaceURI == null) {
namespaceMap.put(rootElement, parserClass);
} else {
namespaceMap.put("{"+namespaceURI+"}"+rootElement, parserClass);
}
}
/**
* Get the name of the parser class for a given catalog type.
*
* <p>This method returns the parserClass associated with the
* namespaceURI/rootElement names specified.</p>
*
* @param namespaceURI The namespace URI. <em>Not</em> the prefix.
* @param rootElement The name of the root element.
* @return The parser class.
*/
public String getCatalogParser(String namespaceURI,
String rootElement) {
if (namespaceURI == null) {
return namespaceMap.get(rootElement);
} else {
return namespaceMap.get("{"+namespaceURI+"}"+rootElement);
}
}
/**
* Null constructor; something for subclasses to call.
*/
public DOMCatalogReader() { }
/**
* Read a catalog from an input stream.
*
* <p>This class reads a catalog from an input stream:</p>
*
* <ul>
* <li>Based on the QName of the root element, it determines which
* parser to instantiate for this catalog.</li>
* <li>It constructs a DOM Document from the catalog and</li>
* <li>For each child of the root node, it calls the parser's
* parseCatalogEntry method. This method is expected to make
* appropriate calls back into the catalog to add entries for the
* entries in the catalog. It is free to do this in whatever manner
* is appropriate (perhaps using just the node passed in, perhaps
* wandering arbitrarily throughout the tree).</li>
* </ul>
*
* @param catalog The catalog for which this reader is called.
* @param is The input stream that is to be read.
* @throws IOException if the URL cannot be read.
* @throws UnknownCatalogFormatException if the catalog format is
* not recognized.
* @throws UnparseableCatalogException if the catalog cannot be parsed.
* (For example, if it is supposed to be XML and isn't well-formed or
* if the parser class cannot be instantiated.)
*/
public void readCatalog(Catalog catalog, InputStream is)
throws IOException, CatalogException {
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
throw new CatalogException(CatalogException.UNPARSEABLE);
}
Document doc = null;
try {
doc = builder.parse(is);
} catch (SAXException se) {
throw new CatalogException(CatalogException.UNKNOWN_FORMAT);
}
Element root = doc.getDocumentElement();
String namespaceURI = Namespaces.getNamespaceURI(root);
String localName = Namespaces.getLocalName(root);
String domParserClass = getCatalogParser(namespaceURI,
localName);
if (domParserClass == null) {
if (namespaceURI == null) {
catalog.getCatalogManager().debug.message(1, "No Catalog parser for "
+ localName);
} else {
catalog.getCatalogManager().debug.message(1, "No Catalog parser for "
+ "{" + namespaceURI + "}"
+ localName);
}
return;
}
DOMCatalogParser domParser = null;
try {
domParser = (DOMCatalogParser) ReflectUtil.forName(domParserClass).newInstance();
} catch (ClassNotFoundException cnfe) {
catalog.getCatalogManager().debug.message(1, "Cannot load XML Catalog Parser class", domParserClass);
throw new CatalogException(CatalogException.UNPARSEABLE);
} catch (InstantiationException ie) {
catalog.getCatalogManager().debug.message(1, "Cannot instantiate XML Catalog Parser class", domParserClass);
throw new CatalogException(CatalogException.UNPARSEABLE);
} catch (IllegalAccessException iae) {
catalog.getCatalogManager().debug.message(1, "Cannot access XML Catalog Parser class", domParserClass);
throw new CatalogException(CatalogException.UNPARSEABLE);
} catch (ClassCastException cce ) {
catalog.getCatalogManager().debug.message(1, "Cannot cast XML Catalog Parser class", domParserClass);
throw new CatalogException(CatalogException.UNPARSEABLE);
}
Node node = root.getFirstChild();
while (node != null) {
domParser.parseCatalogEntry(catalog, node);
node = node.getNextSibling();
}
}
/**
* Read the catalog behind the specified URL.
*
* @see #readCatalog(Catalog, InputStream)
*
* @param catalog The catalog for which we are reading.
* @param fileUrl The URL of the document that should be read.
*
* @throws MalformedURLException if the specified URL cannot be
* turned into a URL object.
* @throws IOException if the URL cannot be read.
* @throws UnknownCatalogFormatException if the catalog format is
* not recognized.
* @throws UnparseableCatalogException if the catalog cannot be parsed.
* (For example, if it is supposed to be XML and isn't well-formed.)
*/
public void readCatalog(Catalog catalog, String fileUrl)
throws MalformedURLException, IOException, CatalogException {
URL url = new URL(fileUrl);
URLConnection urlCon = url.openConnection();
readCatalog(catalog, urlCon.getInputStream());
}
}

View File

@ -1,181 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.readers;
import java.util.Vector;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.Resolver;
import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
import com.sun.org.apache.xml.internal.resolver.CatalogException;
import org.xml.sax.*;
import org.w3c.dom.*;
/**
* Parse Extended OASIS Entity Resolution Technical Committee
* XML Catalog files.
*
* @see Catalog
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class ExtendedXMLCatalogReader extends OASISXMLCatalogReader {
/** The namespace name of extended catalog elements */
public static final String extendedNamespaceName = "http://nwalsh.com/xcatalog/1.0";
/**
* The SAX <code>startElement</code> method recognizes elements
* from the plain catalog format and instantiates CatalogEntry
* objects for them.
*
* @param namespaceURI The namespace name of the element.
* @param localName The local name of the element.
* @param qName The QName of the element.
* @param atts The list of attributes on the element.
*
* @see CatalogEntry
*/
public void startElement (String namespaceURI,
String localName,
String qName,
Attributes atts)
throws SAXException {
// Check before calling the super because super will report our
// namespace as an extension namespace, but that doesn't count
// for this element.
boolean inExtension = inExtensionNamespace();
super.startElement(namespaceURI, localName, qName, atts);
int entryType = -1;
Vector entryArgs = new Vector();
if (namespaceURI != null && extendedNamespaceName.equals(namespaceURI)
&& !inExtension) {
// This is an Extended XML Catalog entry
if (atts.getValue("xml:base") != null) {
String baseURI = atts.getValue("xml:base");
entryType = Catalog.BASE;
entryArgs.add(baseURI);
baseURIStack.push(baseURI);
debug.message(4, "xml:base", baseURI);
try {
CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
catalog.addEntry(ce);
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
debug.message(1, "Invalid catalog entry type", localName);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
debug.message(1, "Invalid catalog entry (base)", localName);
}
}
entryType = -1;
entryArgs = new Vector();
} else {
baseURIStack.push(baseURIStack.peek());
}
if (localName.equals("uriSuffix")) {
if (checkAttributes(atts, "suffix", "uri")) {
entryType = Resolver.URISUFFIX;
entryArgs.add(atts.getValue("suffix"));
entryArgs.add(atts.getValue("uri"));
debug.message(4, "uriSuffix",
atts.getValue("suffix"),
atts.getValue("uri"));
}
} else if (localName.equals("systemSuffix")) {
if (checkAttributes(atts, "suffix", "uri")) {
entryType = Resolver.SYSTEMSUFFIX;
entryArgs.add(atts.getValue("suffix"));
entryArgs.add(atts.getValue("uri"));
debug.message(4, "systemSuffix",
atts.getValue("suffix"),
atts.getValue("uri"));
}
} else {
// This is equivalent to an invalid catalog entry type
debug.message(1, "Invalid catalog entry type", localName);
}
if (entryType >= 0) {
try {
CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
catalog.addEntry(ce);
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
debug.message(1, "Invalid catalog entry type", localName);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
debug.message(1, "Invalid catalog entry", localName);
}
}
}
}
}
/** The SAX <code>endElement</code> method does nothing. */
public void endElement (String namespaceURI,
String localName,
String qName)
throws SAXException {
super.endElement(namespaceURI, localName, qName);
// Check after popping the stack so we don't erroneously think we
// are our own extension namespace...
boolean inExtension = inExtensionNamespace();
int entryType = -1;
Vector entryArgs = new Vector();
if (namespaceURI != null
&& (extendedNamespaceName.equals(namespaceURI))
&& !inExtension) {
String popURI = (String) baseURIStack.pop();
String baseURI = (String) baseURIStack.peek();
if (!baseURI.equals(popURI)) {
entryType = Catalog.BASE;
entryArgs.add(baseURI);
debug.message(4, "(reset) xml:base", baseURI);
try {
CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
catalog.addEntry(ce);
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
debug.message(1, "Invalid catalog entry type", localName);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
debug.message(1, "Invalid catalog entry (rbase)", localName);
}
}
}
}
}
}

View File

@ -1,546 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.readers;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
import com.sun.org.apache.xml.internal.resolver.CatalogException;
import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
import java.util.Enumeration;
import java.util.Stack;
import java.util.Vector;
import javax.xml.parsers.SAXParserFactory;
import org.w3c.dom.*;
import org.xml.sax.*;
/**
* Parse OASIS Entity Resolution Technical Committee
* XML Catalog files.
*
* @see Catalog
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class OASISXMLCatalogReader extends SAXCatalogReader implements SAXCatalogParser {
/** The catalog object needs to be stored by the object so that
* SAX callbacks can use it.
*/
protected Catalog catalog = null;
/** The namespace name of OASIS ERTC catalogs */
public static final String namespaceName = "urn:oasis:names:tc:entity:xmlns:xml:catalog";
/** The namespace name of OASIS ERTC TR9401 catalog extension */
public static final String tr9401NamespaceName = "urn:oasis:names:tc:entity:xmlns:tr9401:catalog";
protected Stack baseURIStack = new Stack();
protected Stack overrideStack = new Stack();
protected Stack namespaceStack = new Stack();
/** Set the current catalog. */
public void setCatalog (Catalog catalog) {
this.catalog = catalog;
debug = catalog.getCatalogManager().debug;
}
/** Get the current catalog. */
public Catalog getCatalog () {
return catalog;
}
/** Default constructor */
public OASISXMLCatalogReader() {
super();
}
/** Constructor allowing for providing custom SAX parser factory */
public OASISXMLCatalogReader(SAXParserFactory parserFactory, Catalog catalog) {
super(parserFactory);
setCatalog(catalog);
}
/**
* Are we in an extension namespace?
*
* @return true if the current stack of open namespaces includes
* an extension namespace.
*/
protected boolean inExtensionNamespace() {
boolean inExtension = false;
Enumeration elements = namespaceStack.elements();
while (!inExtension && elements.hasMoreElements()) {
String ns = (String) elements.nextElement();
if (ns == null) {
inExtension = true;
} else {
inExtension = (!ns.equals(tr9401NamespaceName)
&& !ns.equals(namespaceName));
}
}
return inExtension;
}
// ----------------------------------------------------------------------
// Implement the SAX ContentHandler interface
/** The SAX <code>setDocumentLocator</code> method does nothing. */
public void setDocumentLocator (Locator locator) {
return;
}
/** The SAX <code>startDocument</code> */
public void startDocument ()
throws SAXException {
baseURIStack.push(catalog.getCurrentBase());
overrideStack.push(catalog.getDefaultOverride());
return;
}
/** The SAX <code>endDocument</code> method does nothing. */
public void endDocument ()
throws SAXException {
return;
}
/**
* The SAX <code>startElement</code> method recognizes elements
* from the plain catalog format and instantiates CatalogEntry
* objects for them.
*
* @param namespaceURI The namespace name of the element.
* @param localName The local name of the element.
* @param qName The QName of the element.
* @param atts The list of attributes on the element.
*
* @see CatalogEntry
*/
public void startElement (String namespaceURI,
String localName,
String qName,
Attributes atts)
throws SAXException {
int entryType = -1;
Vector entryArgs = new Vector();
namespaceStack.push(namespaceURI);
boolean inExtension = inExtensionNamespace();
if (namespaceURI != null && namespaceName.equals(namespaceURI)
&& !inExtension) {
// This is an XML Catalog entry
if (atts.getValue("xml:base") != null) {
String baseURI = atts.getValue("xml:base");
entryType = Catalog.BASE;
entryArgs.add(baseURI);
baseURIStack.push(baseURI);
debug.message(4, "xml:base", baseURI);
try {
CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
catalog.addEntry(ce);
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
debug.message(1, "Invalid catalog entry type", localName);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
debug.message(1, "Invalid catalog entry (base)", localName);
}
}
entryType = -1;
entryArgs = new Vector();
} else {
baseURIStack.push(baseURIStack.peek());
}
if ((localName.equals("catalog") || localName.equals("group"))
&& atts.getValue("prefer") != null) {
String override = atts.getValue("prefer");
if (override.equals("public")) {
override = "yes";
} else if (override.equals("system")) {
override = "no";
} else {
debug.message(1,
"Invalid prefer: must be 'system' or 'public'",
localName);
override = catalog.getDefaultOverride();
}
entryType = Catalog.OVERRIDE;
entryArgs.add(override);
overrideStack.push(override);
debug.message(4, "override", override);
try {
CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
catalog.addEntry(ce);
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
debug.message(1, "Invalid catalog entry type", localName);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
debug.message(1, "Invalid catalog entry (override)", localName);
}
}
entryType = -1;
entryArgs = new Vector();
} else {
overrideStack.push(overrideStack.peek());
}
if (localName.equals("delegatePublic")) {
if (checkAttributes(atts, "publicIdStartString", "catalog")) {
entryType = Catalog.DELEGATE_PUBLIC;
entryArgs.add(atts.getValue("publicIdStartString"));
entryArgs.add(atts.getValue("catalog"));
debug.message(4, "delegatePublic",
PublicId.normalize(atts.getValue("publicIdStartString")),
atts.getValue("catalog"));
}
} else if (localName.equals("delegateSystem")) {
if (checkAttributes(atts, "systemIdStartString", "catalog")) {
entryType = Catalog.DELEGATE_SYSTEM;
entryArgs.add(atts.getValue("systemIdStartString"));
entryArgs.add(atts.getValue("catalog"));
debug.message(4, "delegateSystem",
atts.getValue("systemIdStartString"),
atts.getValue("catalog"));
}
} else if (localName.equals("delegateURI")) {
if (checkAttributes(atts, "uriStartString", "catalog")) {
entryType = Catalog.DELEGATE_URI;
entryArgs.add(atts.getValue("uriStartString"));
entryArgs.add(atts.getValue("catalog"));
debug.message(4, "delegateURI",
atts.getValue("uriStartString"),
atts.getValue("catalog"));
}
} else if (localName.equals("rewriteSystem")) {
if (checkAttributes(atts, "systemIdStartString", "rewritePrefix")) {
entryType = Catalog.REWRITE_SYSTEM;
entryArgs.add(atts.getValue("systemIdStartString"));
entryArgs.add(atts.getValue("rewritePrefix"));
debug.message(4, "rewriteSystem",
atts.getValue("systemIdStartString"),
atts.getValue("rewritePrefix"));
}
} else if (localName.equals("systemSuffix")) {
if (checkAttributes(atts, "systemIdSuffix", "uri")) {
entryType = Catalog.SYSTEM_SUFFIX;
entryArgs.add(atts.getValue("systemIdSuffix"));
entryArgs.add(atts.getValue("uri"));
debug.message(4, "systemSuffix",
atts.getValue("systemIdSuffix"),
atts.getValue("uri"));
}
} else if (localName.equals("rewriteURI")) {
if (checkAttributes(atts, "uriStartString", "rewritePrefix")) {
entryType = Catalog.REWRITE_URI;
entryArgs.add(atts.getValue("uriStartString"));
entryArgs.add(atts.getValue("rewritePrefix"));
debug.message(4, "rewriteURI",
atts.getValue("uriStartString"),
atts.getValue("rewritePrefix"));
}
} else if (localName.equals("uriSuffix")) {
if (checkAttributes(atts, "uriSuffix", "uri")) {
entryType = Catalog.URI_SUFFIX;
entryArgs.add(atts.getValue("uriSuffix"));
entryArgs.add(atts.getValue("uri"));
debug.message(4, "uriSuffix",
atts.getValue("uriSuffix"),
atts.getValue("uri"));
}
} else if (localName.equals("nextCatalog")) {
if (checkAttributes(atts, "catalog")) {
entryType = Catalog.CATALOG;
entryArgs.add(atts.getValue("catalog"));
debug.message(4, "nextCatalog", atts.getValue("catalog"));
}
} else if (localName.equals("public")) {
if (checkAttributes(atts, "publicId", "uri")) {
entryType = Catalog.PUBLIC;
entryArgs.add(atts.getValue("publicId"));
entryArgs.add(atts.getValue("uri"));
debug.message(4, "public",
PublicId.normalize(atts.getValue("publicId")),
atts.getValue("uri"));
}
} else if (localName.equals("system")) {
if (checkAttributes(atts, "systemId", "uri")) {
entryType = Catalog.SYSTEM;
entryArgs.add(atts.getValue("systemId"));
entryArgs.add(atts.getValue("uri"));
debug.message(4, "system",
atts.getValue("systemId"),
atts.getValue("uri"));
}
} else if (localName.equals("uri")) {
if (checkAttributes(atts, "name", "uri")) {
entryType = Catalog.URI;
entryArgs.add(atts.getValue("name"));
entryArgs.add(atts.getValue("uri"));
debug.message(4, "uri",
atts.getValue("name"),
atts.getValue("uri"));
}
} else if (localName.equals("catalog")) {
// nop, start of catalog
} else if (localName.equals("group")) {
// nop, a group
} else {
// This is equivalent to an invalid catalog entry type
debug.message(1, "Invalid catalog entry type", localName);
}
if (entryType >= 0) {
try {
CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
catalog.addEntry(ce);
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
debug.message(1, "Invalid catalog entry type", localName);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
debug.message(1, "Invalid catalog entry", localName);
}
}
}
}
if (namespaceURI != null && tr9401NamespaceName.equals(namespaceURI)
&& !inExtension) {
// This is a TR9401 Catalog entry
if (atts.getValue("xml:base") != null) {
String baseURI = atts.getValue("xml:base");
entryType = Catalog.BASE;
entryArgs.add(baseURI);
baseURIStack.push(baseURI);
debug.message(4, "xml:base", baseURI);
try {
CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
catalog.addEntry(ce);
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
debug.message(1, "Invalid catalog entry type", localName);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
debug.message(1, "Invalid catalog entry (base)", localName);
}
}
entryType = -1;
entryArgs = new Vector();
} else {
baseURIStack.push(baseURIStack.peek());
}
if (localName.equals("doctype")) {
entryType = Catalog.DOCTYPE;
entryArgs.add(atts.getValue("name"));
entryArgs.add(atts.getValue("uri"));
} else if (localName.equals("document")) {
entryType = Catalog.DOCUMENT;
entryArgs.add(atts.getValue("uri"));
} else if (localName.equals("dtddecl")) {
entryType = Catalog.DTDDECL;
entryArgs.add(atts.getValue("publicId"));
entryArgs.add(atts.getValue("uri"));
} else if (localName.equals("entity")) {
entryType = Catalog.ENTITY;
entryArgs.add(atts.getValue("name"));
entryArgs.add(atts.getValue("uri"));
} else if (localName.equals("linktype")) {
entryType = Catalog.LINKTYPE;
entryArgs.add(atts.getValue("name"));
entryArgs.add(atts.getValue("uri"));
} else if (localName.equals("notation")) {
entryType = Catalog.NOTATION;
entryArgs.add(atts.getValue("name"));
entryArgs.add(atts.getValue("uri"));
} else if (localName.equals("sgmldecl")) {
entryType = Catalog.SGMLDECL;
entryArgs.add(atts.getValue("uri"));
} else {
// This is equivalent to an invalid catalog entry type
debug.message(1, "Invalid catalog entry type", localName);
}
if (entryType >= 0) {
try {
CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
catalog.addEntry(ce);
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
debug.message(1, "Invalid catalog entry type", localName);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
debug.message(1, "Invalid catalog entry", localName);
}
}
}
}
}
public boolean checkAttributes (Attributes atts, String attName) {
if (atts.getValue(attName) == null) {
debug.message(1, "Error: required attribute " + attName + " missing.");
return false;
} else {
return true;
}
}
public boolean checkAttributes (Attributes atts,
String attName1,
String attName2) {
return checkAttributes(atts, attName1)
&& checkAttributes(atts, attName2);
}
/** The SAX <code>endElement</code> */
public void endElement (String namespaceURI,
String localName,
String qName)
throws SAXException {
int entryType = -1;
Vector entryArgs = new Vector();
boolean inExtension = inExtensionNamespace();
if (namespaceURI != null
&& !inExtension
&& (namespaceName.equals(namespaceURI)
|| tr9401NamespaceName.equals(namespaceURI))) {
String popURI = (String) baseURIStack.pop();
String baseURI = (String) baseURIStack.peek();
if (!baseURI.equals(popURI)) {
entryType = Catalog.BASE;
entryArgs.add(baseURI);
debug.message(4, "(reset) xml:base", baseURI);
try {
CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
catalog.addEntry(ce);
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
debug.message(1, "Invalid catalog entry type", localName);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
debug.message(1, "Invalid catalog entry (rbase)", localName);
}
}
}
}
if (namespaceURI != null && namespaceName.equals(namespaceURI)
&& !inExtension) {
if (localName.equals("catalog") || localName.equals("group")) {
String popOverride = (String) overrideStack.pop();
String override = (String) overrideStack.peek();
if (!override.equals(popOverride)) {
entryType = Catalog.OVERRIDE;
entryArgs.add(override);
overrideStack.push(override);
debug.message(4, "(reset) override", override);
try {
CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
catalog.addEntry(ce);
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
debug.message(1, "Invalid catalog entry type", localName);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
debug.message(1, "Invalid catalog entry (roverride)", localName);
}
}
}
}
}
namespaceStack.pop();
return;
}
/** The SAX <code>characters</code> method does nothing. */
public void characters (char ch[], int start, int length)
throws SAXException {
return;
}
/** The SAX <code>ignorableWhitespace</code> method does nothing. */
public void ignorableWhitespace (char ch[], int start, int length)
throws SAXException {
return;
}
/** The SAX <code>processingInstruction</code> method does nothing. */
public void processingInstruction (String target, String data)
throws SAXException {
return;
}
/** The SAX <code>skippedEntity</code> method does nothing. */
public void skippedEntity (String name)
throws SAXException {
return;
}
/** The SAX <code>startPrefixMapping</code> method does nothing. */
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
return;
}
/** The SAX <code>endPrefixMapping</code> method does nothing. */
public void endPrefixMapping(String prefix)
throws SAXException {
return;
}
}

View File

@ -1,39 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.readers;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import org.xml.sax.*;
/**
* The SAXCatalogParser interface.
*
* <p>This interface must be implemented in order for a class to
* participate as a parser for the SAXCatalogReader.
*
* @see Catalog
* @see SAXCatalogReader
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public interface SAXCatalogParser extends ContentHandler, DocumentHandler {
/** Set the Catalog for which parsing is being performed. */
public void setCatalog(Catalog catalog);
}

View File

@ -1,503 +0,0 @@
/*
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.readers;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.CatalogException;
import com.sun.org.apache.xml.internal.resolver.CatalogManager;
import com.sun.org.apache.xml.internal.resolver.helpers.Debug;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.AttributeList;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.DocumentHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;
import sun.reflect.misc.ReflectUtil;
/**
* A SAX-based CatalogReader.
*
* <p>This class is used to read XML Catalogs using the SAX. This reader
* has an advantage over the DOM-based reader in that it functions on
* the stream of SAX events. It has the disadvantage
* that it cannot look around in the tree.</p>
*
* <p>Since the choice of CatalogReaders (in the InputStream case) can only
* be made on the basis of MIME type, the following problem occurs: only
* one CatalogReader can exist for all XML mime types. In order to get
* around this problem, the SAXCatalogReader relies on a set of external
* CatalogParsers to actually build the catalog.</p>
*
* <p>The selection of CatalogParsers is made on the basis of the QName
* of the root element of the document.</p>
*
* @see Catalog
* @see CatalogReader
* @see SAXCatalogReader
* @see TextCatalogReader
* @see DOMCatalogParser
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class SAXCatalogReader implements CatalogReader, ContentHandler, DocumentHandler {
/** The SAX Parser Factory */
protected SAXParserFactory parserFactory = null;
/** The SAX Parser Class */
protected String parserClass = null;
/**
* Mapping table from QNames to CatalogParser classes.
*
* <p>Each key in this hash table has the form "elementname"
* or "{namespaceuri}elementname". The former is used if the
* namespace URI is null.</p>
*/
protected Map<String, String> namespaceMap = new HashMap<>();
/** The parser in use for the current catalog. */
private SAXCatalogParser saxParser = null;
/** Set if something goes horribly wrong. It allows the class to
* ignore the rest of the events that are received.
*/
private boolean abandonHope = false;
/** The Catalog that we're working for. */
private Catalog catalog;
/** Set the XML SAX Parser Factory.
*/
public void setParserFactory(SAXParserFactory parserFactory) {
this.parserFactory = parserFactory;
}
/** Set the XML SAX Parser Class
*/
public void setParserClass(String parserClass) {
this.parserClass = parserClass;
}
/** Get the parser factory currently in use. */
public SAXParserFactory getParserFactory() {
return parserFactory;
}
/** Get the parser class currently in use. */
public String getParserClass() {
return parserClass;
}
/** The debug class to use for this reader.
*
* This is a bit of a hack. Anyway, whenever we read for a catalog,
* we extract the debug object
* from the catalog's manager so that we can use it to print messages.
*
* In production, we don't really expect any messages so it doesn't
* really matter. But it's still a bit of a hack.
*/
protected Debug debug = CatalogManager.getStaticManager().debug;
/** The constructor */
public SAXCatalogReader() {
parserFactory = null;
parserClass = null;
}
/** The constructor */
public SAXCatalogReader(SAXParserFactory parserFactory) {
this.parserFactory = parserFactory;
}
/** The constructor */
public SAXCatalogReader(String parserClass) {
this.parserClass = parserClass;
}
/**
* Set the SAXCatalogParser class for the given namespace/root
* element type.
*/
public void setCatalogParser(String namespaceURI,
String rootElement,
String parserClass) {
namespaceURI = namespaceURI != null ? namespaceURI.trim() : "";
namespaceMap.put("{"+namespaceURI+"}"+rootElement, parserClass);
}
/**
* Get the SAXCatalogParser class for the given namespace/root
* element type.
*/
public String getCatalogParser(String namespaceURI,
String rootElement) {
namespaceURI = namespaceURI != null ? namespaceURI.trim() : "";
return namespaceMap.get("{"+namespaceURI+"}"+rootElement);
}
/**
* Parse an XML Catalog file.
*
* @param catalog The catalog to which this catalog file belongs
* @param fileUrl The URL or filename of the catalog file to process
*
* @throws MalformedURLException Improper fileUrl
* @throws IOException Error reading catalog file
*/
public void readCatalog(Catalog catalog, String fileUrl)
throws MalformedURLException, IOException,
CatalogException {
URL url = null;
try {
url = new URL(fileUrl);
} catch (MalformedURLException e) {
url = new URL("file:///" + fileUrl);
}
debug = catalog.getCatalogManager().debug;
try {
URLConnection urlCon = url.openConnection();
readCatalog(catalog, urlCon.getInputStream());
} catch (FileNotFoundException e) {
catalog.getCatalogManager().debug.message(1, "Failed to load catalog, file not found",
url.toString());
}
}
/**
* Parse an XML Catalog stream.
*
* @param catalog The catalog to which this catalog file belongs
* @param is The input stream from which the catalog will be read
*
* @throws MalformedURLException Improper fileUrl
* @throws IOException Error reading catalog file
* @throws CatalogException A Catalog exception
*/
public void readCatalog(Catalog catalog, InputStream is)
throws IOException, CatalogException {
// Create an instance of the parser
if (parserFactory == null && parserClass == null) {
debug.message(1, "Cannot read SAX catalog without a parser");
throw new CatalogException(CatalogException.UNPARSEABLE);
}
debug = catalog.getCatalogManager().debug;
EntityResolver bResolver = catalog.getCatalogManager().getBootstrapResolver();
this.catalog = catalog;
try {
if (parserFactory != null) {
SAXParser parser = parserFactory.newSAXParser();
SAXParserHandler spHandler = new SAXParserHandler();
spHandler.setContentHandler(this);
if (bResolver != null) {
spHandler.setEntityResolver(bResolver);
}
parser.parse(new InputSource(is), spHandler);
} else {
Class<?> c = ReflectUtil.forName(parserClass);
if (!Parser.class.isAssignableFrom(c)) {
throw new ClassCastException(parserClass
+ " cannot be cast to "
+ Parser.class.getName());
}
Parser parser = (Parser) c.newInstance();
parser.setDocumentHandler(this);
if (bResolver != null) {
parser.setEntityResolver(bResolver);
}
parser.parse(new InputSource(is));
}
} catch (ClassNotFoundException cnfe) {
throw new CatalogException(CatalogException.UNPARSEABLE);
} catch (IllegalAccessException iae) {
throw new CatalogException(CatalogException.UNPARSEABLE);
} catch (InstantiationException ie) {
throw new CatalogException(CatalogException.UNPARSEABLE);
} catch (ParserConfigurationException pce) {
throw new CatalogException(CatalogException.UNKNOWN_FORMAT);
} catch (SAXException se) {
Exception e = se.getException();
// FIXME: there must be a better way
UnknownHostException uhe = new UnknownHostException();
FileNotFoundException fnfe = new FileNotFoundException();
if (e != null) {
if (e.getClass() == uhe.getClass()) {
throw new CatalogException(CatalogException.PARSE_FAILED,
e.toString());
} else if (e.getClass() == fnfe.getClass()) {
throw new CatalogException(CatalogException.PARSE_FAILED,
e.toString());
}
}
throw new CatalogException(se);
}
}
// ----------------------------------------------------------------------
// Implement the SAX ContentHandler interface
/** The SAX <code>setDocumentLocator</code> method. Does nothing. */
public void setDocumentLocator (Locator locator) {
if (saxParser != null) {
saxParser.setDocumentLocator(locator);
}
}
/** The SAX <code>startDocument</code> method. Does nothing. */
public void startDocument () throws SAXException {
saxParser = null;
abandonHope = false;
return;
}
/** The SAX <code>endDocument</code> method. Does nothing. */
public void endDocument ()throws SAXException {
if (saxParser != null) {
saxParser.endDocument();
}
}
/**
* The SAX <code>startElement</code> method.
*
* <p>The catalog parser is selected based on the namespace of the
* first element encountered in the catalog.</p>
*/
public void startElement (String name,
AttributeList atts)
throws SAXException {
if (abandonHope) {
return;
}
if (saxParser == null) {
String prefix = "";
if (name.indexOf(':') > 0) {
prefix = name.substring(0, name.indexOf(':'));
}
String localName = name;
if (localName.indexOf(':') > 0) {
localName = localName.substring(localName.indexOf(':')+1);
}
String namespaceURI = null;
if (prefix.length() == 0) {
namespaceURI = atts.getValue("xmlns");
} else {
namespaceURI = atts.getValue("xmlns:" + prefix);
}
String saxParserClass = getCatalogParser(namespaceURI,
localName);
if (saxParserClass == null) {
abandonHope = true;
if (namespaceURI == null) {
debug.message(2, "No Catalog parser for " + name);
} else {
debug.message(2, "No Catalog parser for "
+ "{" + namespaceURI + "}"
+ name);
}
return;
}
try {
saxParser = (SAXCatalogParser)
ReflectUtil.forName(saxParserClass).newInstance();
saxParser.setCatalog(catalog);
saxParser.startDocument();
saxParser.startElement(name, atts);
} catch (ClassNotFoundException cnfe) {
saxParser = null;
abandonHope = true;
debug.message(2, cnfe.toString());
} catch (InstantiationException ie) {
saxParser = null;
abandonHope = true;
debug.message(2, ie.toString());
} catch (IllegalAccessException iae) {
saxParser = null;
abandonHope = true;
debug.message(2, iae.toString());
} catch (ClassCastException cce ) {
saxParser = null;
abandonHope = true;
debug.message(2, cce.toString());
}
} else {
saxParser.startElement(name, atts);
}
}
/**
* The SAX2 <code>startElement</code> method.
*
* <p>The catalog parser is selected based on the namespace of the
* first element encountered in the catalog.</p>
*/
public void startElement (String namespaceURI,
String localName,
String qName,
Attributes atts)
throws SAXException {
if (abandonHope) {
return;
}
if (saxParser == null) {
String saxParserClass = getCatalogParser(namespaceURI,
localName);
if (saxParserClass == null) {
abandonHope = true;
if (namespaceURI == null) {
debug.message(2, "No Catalog parser for " + localName);
} else {
debug.message(2, "No Catalog parser for "
+ "{" + namespaceURI + "}"
+ localName);
}
return;
}
try {
saxParser = (SAXCatalogParser)
ReflectUtil.forName(saxParserClass).newInstance();
saxParser.setCatalog(catalog);
saxParser.startDocument();
saxParser.startElement(namespaceURI, localName, qName, atts);
} catch (ClassNotFoundException cnfe) {
saxParser = null;
abandonHope = true;
debug.message(2, cnfe.toString());
} catch (InstantiationException ie) {
saxParser = null;
abandonHope = true;
debug.message(2, ie.toString());
} catch (IllegalAccessException iae) {
saxParser = null;
abandonHope = true;
debug.message(2, iae.toString());
} catch (ClassCastException cce ) {
saxParser = null;
abandonHope = true;
debug.message(2, cce.toString());
}
} else {
saxParser.startElement(namespaceURI, localName, qName, atts);
}
}
/** The SAX <code>endElement</code> method. Does nothing. */
public void endElement (String name) throws SAXException {
if (saxParser != null) {
saxParser.endElement(name);
}
}
/** The SAX2 <code>endElement</code> method. Does nothing. */
public void endElement (String namespaceURI,
String localName,
String qName) throws SAXException {
if (saxParser != null) {
saxParser.endElement(namespaceURI, localName, qName);
}
}
/** The SAX <code>characters</code> method. Does nothing. */
public void characters (char ch[], int start, int length)
throws SAXException {
if (saxParser != null) {
saxParser.characters(ch, start, length);
}
}
/** The SAX <code>ignorableWhitespace</code> method. Does nothing. */
public void ignorableWhitespace (char ch[], int start, int length)
throws SAXException {
if (saxParser != null) {
saxParser.ignorableWhitespace(ch, start, length);
}
}
/** The SAX <code>processingInstruction</code> method. Does nothing. */
public void processingInstruction (String target, String data)
throws SAXException {
if (saxParser != null) {
saxParser.processingInstruction(target, data);
}
}
/** The SAX <code>startPrefixMapping</code> method. Does nothing. */
public void startPrefixMapping (String prefix, String uri)
throws SAXException {
if (saxParser != null) {
saxParser.startPrefixMapping (prefix, uri);
}
}
/** The SAX <code>endPrefixMapping</code> method. Does nothing. */
public void endPrefixMapping (String prefix)
throws SAXException {
if (saxParser != null) {
saxParser.endPrefixMapping (prefix);
}
}
/** The SAX <code>skippedentity</code> method. Does nothing. */
public void skippedEntity (String name)
throws SAXException {
if (saxParser != null) {
saxParser.skippedEntity(name);
}
}
}

View File

@ -1,144 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.readers;
import java.io.IOException;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
/**
* An entity-resolving DefaultHandler.
*
* <p>This class provides a SAXParser DefaultHandler that performs
* entity resolution.
* </p>
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*/
public class SAXParserHandler extends DefaultHandler {
private EntityResolver er = null;
private ContentHandler ch = null;
public SAXParserHandler() {
super();
}
public void setEntityResolver(EntityResolver er) {
this.er = er;
}
public void setContentHandler(ContentHandler ch) {
this.ch = ch;
}
// Entity Resolver
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException {
if (er != null) {
try {
return er.resolveEntity(publicId, systemId);
} catch (IOException e) {
System.out.println("resolveEntity threw IOException!");
return null;
}
} else {
return null;
}
}
// Content Handler
public void characters(char[] ch, int start, int length)
throws SAXException {
if (this.ch != null) {
this.ch.characters(ch, start, length);
}
}
public void endDocument()
throws SAXException {
if (ch != null) {
ch.endDocument();
}
}
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (ch != null) {
ch.endElement(namespaceURI, localName, qName);
}
}
public void endPrefixMapping(String prefix)
throws SAXException {
if (ch != null) {
ch.endPrefixMapping(prefix);
}
}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
if (this.ch != null) {
this.ch.ignorableWhitespace(ch, start, length);
}
}
public void processingInstruction(String target, String data)
throws SAXException {
if (ch != null) {
ch.processingInstruction(target, data);
}
}
public void setDocumentLocator(Locator locator) {
if (ch != null) {
ch.setDocumentLocator(locator);
}
}
public void skippedEntity(String name)
throws SAXException {
if (ch != null) {
ch.skippedEntity(name);
}
}
public void startDocument()
throws SAXException {
if (ch != null) {
ch.startDocument();
}
}
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts)
throws SAXException {
if (ch != null) {
ch.startElement(namespaceURI, localName, qName, atts);
}
}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
if (ch != null) {
ch.startPrefixMapping(prefix, uri);
}
}
}

View File

@ -1,138 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.readers;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
import com.sun.org.apache.xml.internal.resolver.CatalogException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Locale;
import java.util.Vector;
/**
* Parses OASIS Open Catalog files.
*
* <p>This class reads OASIS Open Catalog files, returning a stream
* of tokens.</p>
*
* <p>This code interrogates the following non-standard system properties:</p>
*
* <dl>
* <dt><b>xml.catalog.debug</b></dt>
* <dd><p>Sets the debug level. A value of 0 is assumed if the
* property is not set or is not a number.</p></dd>
* </dl>
*
* @see Catalog
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class TR9401CatalogReader extends TextCatalogReader {
/**
* Start parsing an OASIS TR9401 Open Catalog file. The file is
* actually read and parsed
* as needed by <code>nextEntry</code>.
*
* <p>In a TR9401 Catalog the 'DELEGATE' entry delegates public
* identifiers. There is no delegate entry for system identifiers
* or URIs.</p>
*
* @param catalog The Catalog to populate
* @param is The input stream from which to read the TR9401 Catalog
*
* @throws MalformedURLException Improper fileUrl
* @throws IOException Error reading catalog file
*/
public void readCatalog(Catalog catalog, InputStream is)
throws MalformedURLException, IOException {
catfile = is;
if (catfile == null) {
return;
}
Vector unknownEntry = null;
try {
while (true) {
String token = nextToken();
if (token == null) {
if (unknownEntry != null) {
catalog.unknownEntry(unknownEntry);
unknownEntry = null;
}
catfile.close();
catfile = null;
return;
}
String entryToken = null;
if (caseSensitive) {
entryToken = token;
} else {
entryToken = token.toUpperCase(Locale.ENGLISH);
}
if (entryToken.equals("DELEGATE")) {
entryToken = "DELEGATE_PUBLIC";
}
try {
int type = CatalogEntry.getEntryType(entryToken);
int numArgs = CatalogEntry.getEntryArgCount(type);
Vector args = new Vector();
if (unknownEntry != null) {
catalog.unknownEntry(unknownEntry);
unknownEntry = null;
}
for (int count = 0; count < numArgs; count++) {
args.addElement(nextToken());
}
catalog.addEntry(new CatalogEntry(entryToken, args));
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
if (unknownEntry == null) {
unknownEntry = new Vector();
}
unknownEntry.addElement(token);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
catalog.getCatalogManager().debug.message(1, "Invalid catalog entry", token);
unknownEntry = null;
} else if (cex.getExceptionType() == CatalogException.UNENDED_COMMENT) {
catalog.getCatalogManager().debug.message(1, cex.getMessage());
}
}
}
} catch (CatalogException cex2) {
if (cex2.getExceptionType() == CatalogException.UNENDED_COMMENT) {
catalog.getCatalogManager().debug.message(1, cex2.getMessage());
}
}
}
}

View File

@ -1,298 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.readers;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
import com.sun.org.apache.xml.internal.resolver.CatalogException;
import com.sun.org.apache.xml.internal.resolver.readers.CatalogReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;
import java.util.Stack;
import java.util.Vector;
/**
* Parses plain text Catalog files.
*
* <p>This class reads plain text Open Catalog files.</p>
*
* @see Catalog
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class TextCatalogReader implements CatalogReader {
/** The input stream used to read the catalog */
protected InputStream catfile = null;
/**
* Character lookahead stack. Reading a catalog sometimes requires
* up to two characters of lookahead.
*/
protected int[] stack = new int[3];
/**
* Token stack. Recognizing an unexpected catalog entry requires
* the ability to "push back" a token.
*/
protected Stack tokenStack = new Stack();
/** The current position on the lookahead stack */
protected int top = -1;
/** Are keywords in the catalog case sensitive? */
protected boolean caseSensitive = false;
/**
* Construct a CatalogReader object.
*/
public TextCatalogReader() { }
public void setCaseSensitive(boolean isCaseSensitive) {
caseSensitive = isCaseSensitive;
}
public boolean getCaseSensitive() {
return caseSensitive;
}
/**
* Start parsing a text catalog file. The file is
* actually read and parsed
* as needed by <code>nextEntry</code>.</p>
*
* @param fileUrl The URL or filename of the catalog file to process
*
* @throws MalformedURLException Improper fileUrl
* @throws IOException Error reading catalog file
*/
public void readCatalog(Catalog catalog, String fileUrl)
throws MalformedURLException, IOException {
URL catURL = null;
try {
catURL = new URL(fileUrl);
} catch (MalformedURLException e) {
catURL = new URL("file:///" + fileUrl);
}
URLConnection urlCon = catURL.openConnection();
try {
readCatalog(catalog, urlCon.getInputStream());
} catch (FileNotFoundException e) {
catalog.getCatalogManager().debug.message(1, "Failed to load catalog, file not found",
catURL.toString());
}
}
public void readCatalog(Catalog catalog, InputStream is)
throws MalformedURLException, IOException {
catfile = is;
if (catfile == null) {
return;
}
Vector unknownEntry = null;
try {
while (true) {
String token = nextToken();
if (token == null) {
if (unknownEntry != null) {
catalog.unknownEntry(unknownEntry);
unknownEntry = null;
}
catfile.close();
catfile = null;
return;
}
String entryToken = null;
if (caseSensitive) {
entryToken = token;
} else {
entryToken = token.toUpperCase(Locale.ENGLISH);
}
try {
int type = CatalogEntry.getEntryType(entryToken);
int numArgs = CatalogEntry.getEntryArgCount(type);
Vector args = new Vector();
if (unknownEntry != null) {
catalog.unknownEntry(unknownEntry);
unknownEntry = null;
}
for (int count = 0; count < numArgs; count++) {
args.addElement(nextToken());
}
catalog.addEntry(new CatalogEntry(entryToken, args));
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
if (unknownEntry == null) {
unknownEntry = new Vector();
}
unknownEntry.addElement(token);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
catalog.getCatalogManager().debug.message(1, "Invalid catalog entry", token);
unknownEntry = null;
} else if (cex.getExceptionType() == CatalogException.UNENDED_COMMENT) {
catalog.getCatalogManager().debug.message(1, cex.getMessage());
}
}
}
} catch (CatalogException cex2) {
if (cex2.getExceptionType() == CatalogException.UNENDED_COMMENT) {
catalog.getCatalogManager().debug.message(1, cex2.getMessage());
}
}
}
/**
* The destructor.
*
* <p>Makes sure the catalog file is closed.</p>
*/
protected void finalize() {
if (catfile != null) {
try {
catfile.close();
} catch (IOException e) {
// whatever...
}
}
catfile = null;
}
// -----------------------------------------------------------------
/**
* Return the next token in the catalog file.
*
* <p>FYI: This code does not throw any sort of exception for
* a file that contains an n
*
* @return The Catalog file token from the input stream.
* @throws IOException If an error occurs reading from the stream.
*/
protected String nextToken() throws IOException, CatalogException {
String token = "";
int ch, nextch;
if (!tokenStack.empty()) {
return (String) tokenStack.pop();
}
// Skip over leading whitespace and comments
while (true) {
// skip leading whitespace
ch = catfile.read();
while (ch <= ' ') { // all ctrls are whitespace
ch = catfile.read();
if (ch < 0) {
return null;
}
}
// now 'ch' is the current char from the file
nextch = catfile.read();
if (nextch < 0) {
return null;
}
if (ch == '-' && nextch == '-') {
// we've found a comment, skip it...
ch = ' ';
nextch = nextChar();
while ((ch != '-' || nextch != '-') && nextch > 0) {
ch = nextch;
nextch = nextChar();
}
if (nextch < 0) {
throw new CatalogException(CatalogException.UNENDED_COMMENT,
"Unterminated comment in catalog file; EOF treated as end-of-comment.");
}
// Ok, we've found the end of the comment,
// loop back to the top and start again...
} else {
stack[++top] = nextch;
stack[++top] = ch;
break;
}
}
ch = nextChar();
if (ch == '"' || ch == '\'') {
int quote = ch;
while ((ch = nextChar()) != quote) {
char[] chararr = new char[1];
chararr[0] = (char) ch;
String s = new String(chararr);
token = token.concat(s);
}
return token;
} else {
// return the next whitespace or comment delimited
// string
while (ch > ' ') {
nextch = nextChar();
if (ch == '-' && nextch == '-') {
stack[++top] = ch;
stack[++top] = nextch;
return token;
} else {
char[] chararr = new char[1];
chararr[0] = (char) ch;
String s = new String(chararr);
token = token.concat(s);
ch = nextch;
}
}
return token;
}
}
/**
* Return the next logical character from the input stream.
*
* @return The next (logical) character from the input stream. The
* character may be buffered from a previous lookahead.
*
* @throws IOException If an error occurs reading from the stream.
*/
protected int nextChar() throws IOException {
if (top < 0) {
return catfile.read();
} else {
return stack[top--];
}
}
}

View File

@ -1,208 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.readers;
import java.util.Vector;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
import com.sun.org.apache.xml.internal.resolver.CatalogException;
import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
import org.xml.sax.*;
import javax.xml.parsers.*;
/**
* Parse "XCatalog" XML Catalog files, this is the XML Catalog format
* developed by John Cowan and supported by Apache.
*
* @see Catalog
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
*/
public class XCatalogReader extends SAXCatalogReader implements SAXCatalogParser {
/** The catalog object needs to be stored by the object so that
* SAX callbacks can use it.
*/
protected Catalog catalog = null;
/** Set the current catalog. */
public void setCatalog (Catalog catalog) {
this.catalog = catalog;
debug = catalog.getCatalogManager().debug;
}
/** Get the current catalog. */
public Catalog getCatalog () {
return catalog;
}
/** Default constructor */
public XCatalogReader() {
super();
}
/** Constructor allowing for providing custom SAX parser factory */
public XCatalogReader(SAXParserFactory parserFactory, Catalog catalog) {
super(parserFactory);
setCatalog(catalog);
}
// ----------------------------------------------------------------------
// Implement the SAX ContentHandler interface
/** The SAX <code>setDocumentLocator</code> method does nothing. */
public void setDocumentLocator (Locator locator) {
return;
}
/** The SAX <code>startDocument</code> method does nothing. */
public void startDocument ()
throws SAXException {
return;
}
/** The SAX <code>endDocument</code> method does nothing. */
public void endDocument ()
throws SAXException {
return;
}
/**
* The SAX <code>startElement</code> method recognizes elements
* from the plain catalog format and instantiates CatalogEntry
* objects for them.
*
* @param namespaceURI The namespace name of the element.
* @param localName The local name of the element.
* @param qName The QName of the element.
* @param atts The list of attributes on the element.
*
* @see CatalogEntry
*/
public void startElement (String namespaceURI,
String localName,
String qName,
Attributes atts)
throws SAXException {
int entryType = -1;
Vector entryArgs = new Vector();
if (localName.equals("Base")) {
entryType = Catalog.BASE;
entryArgs.add(atts.getValue("HRef"));
debug.message(4, "Base", atts.getValue("HRef"));
} else if (localName.equals("Delegate")) {
entryType = Catalog.DELEGATE_PUBLIC;
entryArgs.add(atts.getValue("PublicID"));
entryArgs.add(atts.getValue("HRef"));
debug.message(4, "Delegate",
PublicId.normalize(atts.getValue("PublicID")),
atts.getValue("HRef"));
} else if (localName.equals("Extend")) {
entryType = Catalog.CATALOG;
entryArgs.add(atts.getValue("HRef"));
debug.message(4, "Extend", atts.getValue("HRef"));
} else if (localName.equals("Map")) {
entryType = Catalog.PUBLIC;
entryArgs.add(atts.getValue("PublicID"));
entryArgs.add(atts.getValue("HRef"));
debug.message(4, "Map",
PublicId.normalize(atts.getValue("PublicID")),
atts.getValue("HRef"));
} else if (localName.equals("Remap")) {
entryType = Catalog.SYSTEM;
entryArgs.add(atts.getValue("SystemID"));
entryArgs.add(atts.getValue("HRef"));
debug.message(4, "Remap",
atts.getValue("SystemID"),
atts.getValue("HRef"));
} else if (localName.equals("XCatalog")) {
// nop, start of catalog
} else {
// This is equivalent to an invalid catalog entry type
debug.message(1, "Invalid catalog entry type", localName);
}
if (entryType >= 0) {
try {
CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
catalog.addEntry(ce);
} catch (CatalogException cex) {
if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
debug.message(1, "Invalid catalog entry type", localName);
} else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
debug.message(1, "Invalid catalog entry", localName);
}
}
}
}
/** The SAX <code>endElement</code> method does nothing. */
public void endElement (String namespaceURI,
String localName,
String qName)
throws SAXException {
return;
}
/** The SAX <code>characters</code> method does nothing. */
public void characters (char ch[], int start, int length)
throws SAXException {
return;
}
/** The SAX <code>ignorableWhitespace</code> method does nothing. */
public void ignorableWhitespace (char ch[], int start, int length)
throws SAXException {
return;
}
/** The SAX <code>processingInstruction</code> method does nothing. */
public void processingInstruction (String target, String data)
throws SAXException {
return;
}
/** The SAX <code>skippedEntity</code> method does nothing. */
public void skippedEntity (String name)
throws SAXException {
return;
}
/** The SAX <code>startPrefixMapping</code> method does nothing. */
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
return;
}
/** The SAX <code>endPrefixMapping</code> method does nothing. */
public void endPrefixMapping(String prefix)
throws SAXException {
return;
}
}

View File

@ -1,349 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.tools;
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.MalformedURLException;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.EntityResolver;
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 com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.CatalogManager;
import com.sun.org.apache.xml.internal.resolver.helpers.FileURL;
/**
* A SAX EntityResolver/JAXP URIResolver that uses catalogs.
*
* <p>This class implements both a SAX EntityResolver and a JAXP URIResolver.
* </p>
*
* <p>This resolver understands OASIS TR9401 catalogs, XCatalogs, and the
* current working draft of the OASIS Entity Resolution Technical
* Committee specification.</p>
*
* @see Catalog
* @see org.xml.sax.EntityResolver
* @see javax.xml.transform.URIResolver
* @deprecated The JDK internal Catalog API in package
* {@code com.sun.org.apache.xml.internal.resolver}
* is encapsulated in JDK 9. The entire implementation under the package is now
* deprecated and subject to removal in a future release. Users of the API
* should migrate to the {@linkplain javax.xml.catalog new public API}.
* <p>
* The new Catalog API is supported throughout the JDK XML Processors, which allows
* the use of Catalog by simply setting a path to a Catalog file as a property.
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
* @version 1.0
*/
@Deprecated(since="9", forRemoval=true)
public class CatalogResolver implements EntityResolver, URIResolver {
/** Make the parser Namespace aware? */
public boolean namespaceAware = true;
/** Make the parser validating? */
public boolean validating = false;
/** The underlying catalog */
private Catalog catalog = null;
/** The catalog manager */
private CatalogManager catalogManager = CatalogManager.getStaticManager();
/** Constructor */
public CatalogResolver() {
initializeCatalogs(false);
}
/** Constructor */
public CatalogResolver(boolean privateCatalog) {
initializeCatalogs(privateCatalog);
}
/** Constructor */
public CatalogResolver(CatalogManager manager) {
catalogManager = manager;
initializeCatalogs(!catalogManager.getUseStaticCatalog());
}
/** Initialize catalog */
private void initializeCatalogs(boolean privateCatalog) {
catalog = catalogManager.getCatalog();
}
/** Return the underlying catalog */
public Catalog getCatalog() {
return catalog;
}
/**
* Implements the guts of the <code>resolveEntity</code> method
* for the SAX interface.
*
* <p>Presented with an optional public identifier and a system
* identifier, this function attempts to locate a mapping in the
* catalogs.</p>
*
* <p>If such a mapping is found, it is returned. If no mapping is
* found, null is returned.</p>
*
* @param publicId The public identifier for the entity in question.
* This may be null.
*
* @param systemId The system identifier for the entity in question.
* XML requires a system identifier on all external entities, so this
* value is always specified.
*
* @return The resolved identifier (a URI reference).
*/
public String getResolvedEntity (String publicId, String systemId) {
String resolved = null;
if (catalog == null) {
catalogManager.debug.message(1, "Catalog resolution attempted with null catalog; ignored");
return null;
}
if (systemId != null) {
try {
resolved = catalog.resolveSystem(systemId);
} catch (MalformedURLException me) {
catalogManager.debug.message(1, "Malformed URL exception trying to resolve",
publicId);
resolved = null;
} catch (IOException ie) {
catalogManager.debug.message(1, "I/O exception trying to resolve", publicId);
resolved = null;
}
}
if (resolved == null) {
if (publicId != null) {
try {
resolved = catalog.resolvePublic(publicId, systemId);
} catch (MalformedURLException me) {
catalogManager.debug.message(1, "Malformed URL exception trying to resolve",
publicId);
} catch (IOException ie) {
catalogManager.debug.message(1, "I/O exception trying to resolve", publicId);
}
}
if (resolved != null) {
catalogManager.debug.message(2, "Resolved public", publicId, resolved);
}
} else {
catalogManager.debug.message(2, "Resolved system", systemId, resolved);
}
return resolved;
}
/**
* Implements the <code>resolveEntity</code> method
* for the SAX interface.
*
* <p>Presented with an optional public identifier and a system
* identifier, this function attempts to locate a mapping in the
* catalogs.</p>
*
* <p>If such a mapping is found, the resolver attempts to open
* the mapped value as an InputSource and return it. Exceptions are
* ignored and null is returned if the mapped value cannot be opened
* as an input source.</p>
*
* <p>If no mapping is found (or an error occurs attempting to open
* the mapped value as an input source), null is returned and the system
* will use the specified system identifier as if no entityResolver
* was specified.</p>
*
* @param publicId The public identifier for the entity in question.
* This may be null.
*
* @param systemId The system identifier for the entity in question.
* XML requires a system identifier on all external entities, so this
* value is always specified.
*
* @return An InputSource for the mapped identifier, or null.
*/
public InputSource resolveEntity (String publicId, String systemId) {
String resolved = getResolvedEntity(publicId, systemId);
if (resolved != null) {
try {
InputSource iSource = new InputSource(resolved);
iSource.setPublicId(publicId);
// Ideally this method would not attempt to open the
// InputStream, but there is a bug (in Xerces, at least)
// that causes the parser to mistakenly open the wrong
// system identifier if the returned InputSource does
// not have a byteStream.
//
// It could be argued that we still shouldn't do this here,
// but since the purpose of calling the entityResolver is
// almost certainly to open the input stream, it seems to
// do little harm.
//
URL url = new URL(resolved);
InputStream iStream = url.openStream();
iSource.setByteStream(iStream);
return iSource;
} catch (Exception e) {
catalogManager.debug.message(1,
"Failed to create InputSource ("
+ e.toString()
+ ")", resolved);
return null;
}
}
return null;
}
/** JAXP URIResolver API */
public Source resolve(String href, String base)
throws TransformerException {
String uri = href;
String fragment = null;
int hashPos = href.indexOf("#");
if (hashPos >= 0) {
uri = href.substring(0, hashPos);
fragment = href.substring(hashPos+1);
}
String result = null;
try {
result = catalog.resolveURI(href);
} catch (Exception e) {
// nop;
}
if (result == null) {
try {
URL url = null;
if (base==null) {
url = new URL(uri);
result = url.toString();
} else {
URL baseURL = new URL(base);
url = (href.length()==0 ? baseURL : new URL(baseURL, uri));
result = url.toString();
}
} catch (java.net.MalformedURLException mue) {
// try to make an absolute URI from the current base
String absBase = makeAbsolute(base);
if (!absBase.equals(base)) {
// don't bother if the absBase isn't different!
return resolve(href, absBase);
} else {
throw new TransformerException("Malformed URL "
+ href + "(base " + base + ")",
mue);
}
}
}
catalogManager.debug.message(2, "Resolved URI", href, result);
SAXSource source = new SAXSource();
source.setInputSource(new InputSource(result));
setEntityResolver(source);
return source;
}
/**
* <p>Establish an entityResolver for newly resolved URIs.</p>
*
* <p>This is called from the URIResolver to set an EntityResolver
* on the SAX parser to be used for new XML documents that are
* encountered as a result of the document() function, xsl:import,
* or xsl:include. This is done because the XSLT processor calls
* out to the SAXParserFactory itself to create a new SAXParser to
* parse the new document. The new parser does not automatically
* inherit the EntityResolver of the original (although arguably
* it should). See below:</p>
*
* <tt>"If an application wants to set the ErrorHandler or
* EntityResolver for an XMLReader used during a transformation,
* it should use a URIResolver to return the SAXSource which
* provides (with getXMLReader) a reference to the XMLReader"</tt>
*
* <p>...quoted from page 118 of the Java API for XML
* Processing 1.1 specification</p>
*
*/
private void setEntityResolver(SAXSource source) throws TransformerException {
XMLReader reader = source.getXMLReader();
if (reader == null) {
SAXParserFactory spFactory = catalogManager.useServicesMechanism() ?
SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
spFactory.setNamespaceAware(true);
try {
reader = spFactory.newSAXParser().getXMLReader();
}
catch (ParserConfigurationException ex) {
throw new TransformerException(ex);
}
catch (SAXException ex) {
throw new TransformerException(ex);
}
}
reader.setEntityResolver(this);
source.setXMLReader(reader);
}
/** Attempt to construct an absolute URI */
private String makeAbsolute(String uri) {
if (uri == null) {
uri = "";
}
try {
URL url = new URL(uri);
return url.toString();
} catch (MalformedURLException mue) {
try {
URL fileURL = FileURL.makeURL(uri);
return fileURL.toString();
} catch (MalformedURLException mue2) {
// bail
return uri;
}
}
}
}

View File

@ -1,434 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.tools;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Locale;
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.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.CatalogManager;
import com.sun.org.apache.xml.internal.resolver.helpers.FileURL;
/**
* A SAX Parser that performs catalog-based entity resolution.
*
* <p>This class implements a SAX Parser that performs entity resolution
* using the CatalogResolver. The actual, underlying parser is obtained
* from a SAXParserFactory.</p>
* </p>
*
* @deprecated This interface has been replaced by the
* {@link com.sun.org.apache.xml.internal.resolver.tools.ResolvingXMLReader} for SAX2.
* @see CatalogResolver
* @see org.xml.sax.Parser
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
* @version 1.0
*/
public class ResolvingParser
implements Parser, DTDHandler, DocumentHandler, EntityResolver {
/** Suppress explanatory message?
*
* @see #parse(InputSource)
*/
private static final boolean suppressExplanation = false;
/** The underlying parser. */
private SAXParser saxParser = null;
/** The underlying reader. */
private Parser parser = null;
/** The underlying DocumentHandler. */
private DocumentHandler documentHandler = null;
/** The underlying DTDHandler. */
private DTDHandler dtdHandler = null;
/** The manager for the underlying resolver. */
private CatalogManager catalogManager = CatalogManager.getStaticManager();
/** The underlying catalog resolver. */
private CatalogResolver catalogResolver = null;
/** A separate resolver for oasis-xml-pi catalogs. */
private CatalogResolver piCatalogResolver = null;
/** Are we in the prolog? Is an oasis-xml-catalog PI valid now? */
private boolean allowXMLCatalogPI = false;
/** The base URI of the input document, if known. */
private URL baseURL = null;
/** Constructor. */
public ResolvingParser() {
initParser();
}
/** Constructor. */
public ResolvingParser(CatalogManager manager) {
catalogManager = manager;
initParser();
}
/** Initialize the parser. */
private void initParser() {
catalogResolver = new CatalogResolver(catalogManager);
SAXParserFactory spf = catalogManager.useServicesMechanism() ?
SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
spf.setNamespaceAware(true);
spf.setValidating(false);
try {
saxParser = spf.newSAXParser();
parser = saxParser.getParser();
documentHandler = null;
dtdHandler = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** Return the Catalog being used. */
public Catalog getCatalog() {
return catalogResolver.getCatalog();
}
/**
* SAX Parser API.
*
* <p>Note that the JAXP 1.1ea2 parser crashes with an InternalError if
* it encounters a system identifier that appears to be a relative URI
* that begins with a slash. For example, the declaration:</p>
*
* <pre>
* &lt;!DOCTYPE book SYSTEM "/path/to/dtd/on/my/system/docbookx.dtd">
* </pre>
*
* <p>would cause such an error. As a convenience, this method catches
* that error and prints an explanation. (Unfortunately, it's not possible
* to identify the particular system identifier that causes the problem.)
* </p>
*
* <p>The underlying error is forwarded after printing the explanatory
* message. The message is only every printed once and if
* <code>suppressExplanation</code> is set to <code>false</code> before
* parsing, it will never be printed.</p>
*/
public void parse(InputSource input)
throws IOException,
SAXException {
setupParse(input.getSystemId());
try {
parser.parse(input);
} catch (InternalError ie) {
explain(input.getSystemId());
throw ie;
}
}
/** SAX Parser API.
*
* @see #parse(InputSource)
*/
public void parse(String systemId)
throws IOException,
SAXException {
setupParse(systemId);
try {
parser.parse(systemId);
} catch (InternalError ie) {
explain(systemId);
throw ie;
}
}
/** SAX Parser API. */
public void setDocumentHandler(DocumentHandler handler) {
documentHandler = handler;
}
/** SAX Parser API. */
public void setDTDHandler(DTDHandler handler) {
dtdHandler = handler;
}
/**
* SAX Parser API.
*
* <p>The purpose of this class is to implement an entity resolver.
* Attempting to set a different one is pointless (and ignored).</p>
*/
public void setEntityResolver(EntityResolver resolver) {
// nop
}
/** SAX Parser API. */
public void setErrorHandler(ErrorHandler handler) {
parser.setErrorHandler(handler);
}
/** SAX Parser API. */
public void setLocale(Locale locale) throws SAXException {
parser.setLocale(locale);
}
/** SAX DocumentHandler API. */
public void characters(char[] ch, int start, int length)
throws SAXException {
if (documentHandler != null) {
documentHandler.characters(ch,start,length);
}
}
/** SAX DocumentHandler API. */
public void endDocument() throws SAXException {
if (documentHandler != null) {
documentHandler.endDocument();
}
}
/** SAX DocumentHandler API. */
public void endElement(String name) throws SAXException {
if (documentHandler != null) {
documentHandler.endElement(name);
}
}
/** SAX DocumentHandler API. */
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
if (documentHandler != null) {
documentHandler.ignorableWhitespace(ch,start,length);
}
}
/** SAX DocumentHandler API. */
public void processingInstruction(String target, String pidata)
throws SAXException {
if (target.equals("oasis-xml-catalog")) {
URL catalog = null;
String data = pidata;
int pos = data.indexOf("catalog=");
if (pos >= 0) {
data = data.substring(pos+8);
if (data.length() > 1) {
String quote = data.substring(0,1);
data = data.substring(1);
pos = data.indexOf(quote);
if (pos >= 0) {
data = data.substring(0, pos);
try {
if (baseURL != null) {
catalog = new URL(baseURL, data);
} else {
catalog = new URL(data);
}
} catch (MalformedURLException mue) {
// nevermind
}
}
}
}
if (allowXMLCatalogPI) {
if (catalogManager.getAllowOasisXMLCatalogPI()) {
catalogManager.debug.message(4,"oasis-xml-catalog PI", pidata);
if (catalog != null) {
try {
catalogManager.debug.message(4,"oasis-xml-catalog", catalog.toString());
if (piCatalogResolver == null) {
piCatalogResolver = new CatalogResolver(true);
}
piCatalogResolver.getCatalog().parseCatalog(catalog.toString());
} catch (Exception e) {
catalogManager.debug.message(3, "Exception parsing oasis-xml-catalog: "
+ catalog.toString());
}
} else {
catalogManager.debug.message(3, "PI oasis-xml-catalog unparseable: " + pidata);
}
} else {
catalogManager.debug.message(4,"PI oasis-xml-catalog ignored: " + pidata);
}
} else {
catalogManager.debug.message(3, "PI oasis-xml-catalog occurred in an invalid place: "
+ pidata);
}
} else {
if (documentHandler != null) {
documentHandler.processingInstruction(target, pidata);
}
}
}
/** SAX DocumentHandler API. */
public void setDocumentLocator(Locator locator) {
if (documentHandler != null) {
documentHandler.setDocumentLocator(locator);
}
}
/** SAX DocumentHandler API. */
public void startDocument() throws SAXException {
if (documentHandler != null) {
documentHandler.startDocument();
}
}
/** SAX DocumentHandler API. */
public void startElement(String name, AttributeList atts)
throws SAXException {
allowXMLCatalogPI = false;
if (documentHandler != null) {
documentHandler.startElement(name,atts);
}
}
/** SAX DTDHandler API. */
public void notationDecl (String name, String publicId, String systemId)
throws SAXException {
allowXMLCatalogPI = false;
if (dtdHandler != null) {
dtdHandler.notationDecl(name,publicId,systemId);
}
}
/** SAX DTDHandler API. */
public void unparsedEntityDecl (String name,
String publicId,
String systemId,
String notationName)
throws SAXException {
allowXMLCatalogPI = false;
if (dtdHandler != null) {
dtdHandler.unparsedEntityDecl (name, publicId, systemId, notationName);
}
}
/**
* Implements the <code>resolveEntity</code> method
* for the SAX interface, using an underlying CatalogResolver
* to do the real work.
*/
public InputSource resolveEntity (String publicId, String systemId) {
allowXMLCatalogPI = false;
String resolved = catalogResolver.getResolvedEntity(publicId, systemId);
if (resolved == null && piCatalogResolver != null) {
resolved = piCatalogResolver.getResolvedEntity(publicId, systemId);
}
if (resolved != null) {
try {
InputSource iSource = new InputSource(resolved);
iSource.setPublicId(publicId);
// Ideally this method would not attempt to open the
// InputStream, but there is a bug (in Xerces, at least)
// that causes the parser to mistakenly open the wrong
// system identifier if the returned InputSource does
// not have a byteStream.
//
// It could be argued that we still shouldn't do this here,
// but since the purpose of calling the entityResolver is
// almost certainly to open the input stream, it seems to
// do little harm.
//
URL url = new URL(resolved);
InputStream iStream = url.openStream();
iSource.setByteStream(iStream);
return iSource;
} catch (Exception e) {
catalogManager.debug.message(1,
"Failed to create InputSource ("
+ e.toString()
+ ")", resolved);
return null;
}
} else {
return null;
}
}
/** Setup for parsing. */
private void setupParse(String systemId) {
allowXMLCatalogPI = true;
parser.setEntityResolver(this);
parser.setDocumentHandler(this);
parser.setDTDHandler(this);
URL cwd = null;
try {
cwd = FileURL.makeURL("basename");
} catch (MalformedURLException mue) {
cwd = null;
}
try {
baseURL = new URL(systemId);
} catch (MalformedURLException mue) {
if (cwd != null) {
try {
baseURL = new URL(cwd, systemId);
} catch (MalformedURLException mue2) {
// give up
baseURL = null;
}
} else {
// give up
baseURL = null;
}
}
}
/** Provide one possible explanation for an InternalError. */
private void explain(String systemId) {
if (!suppressExplanation) {
System.out.println("Parser probably encountered bad URI in " + systemId);
System.out.println("For example, replace '/some/uri' with 'file:/some/uri'.");
}
}
}

View File

@ -1,346 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.tools;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
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 com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.CatalogManager;
import com.sun.org.apache.xml.internal.resolver.helpers.FileURL;
/**
* A SAX XMLFilter that performs catalog-based entity resolution.
*
* <p>This class implements a SAX XMLFilter that performs entity resolution
* using the CatalogResolver. The actual, underlying parser is obtained
* from a SAXParserFactory.</p>
* </p>
*
* @see CatalogResolver
* @see org.xml.sax.XMLFilter
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
* @version 1.0
*/
public class ResolvingXMLFilter extends XMLFilterImpl {
/**
* Suppress explanatory message?
*
* @see #parse(InputSource)
*/
private static boolean suppressExplanation = false;
/** The manager for the underlying resolver. */
CatalogManager catalogManager = CatalogManager.getStaticManager();
/** The underlying catalog resolver. */
private CatalogResolver catalogResolver = null;
/** A separate resolver for oasis-xml-pi catalogs. */
private CatalogResolver piCatalogResolver = null;
/** Are we in the prolog? Is an oasis-xml-catalog PI valid now? */
private boolean allowXMLCatalogPI = false;
/** The base URI of the input document, if known. */
private URL baseURL = null;
/** Construct an empty XML Filter with no parent. */
public ResolvingXMLFilter() {
super();
catalogResolver = new CatalogResolver(catalogManager);
}
/** Construct an XML filter with the specified parent. */
public ResolvingXMLFilter(XMLReader parent) {
super(parent);
catalogResolver = new CatalogResolver(catalogManager);
}
/** Construct an XML filter with the specified parent. */
public ResolvingXMLFilter(CatalogManager manager) {
super();
catalogManager = manager;
catalogResolver = new CatalogResolver(catalogManager);
}
/** Construct an XML filter with the specified parent. */
public ResolvingXMLFilter(XMLReader parent, CatalogManager manager) {
super(parent);
catalogManager = manager;
catalogResolver = new CatalogResolver(catalogManager);
}
/**
* Provide accessto the underlying Catalog.
*/
public Catalog getCatalog() {
return catalogResolver.getCatalog();
}
/**
* SAX XMLReader API.
*
* <p>Note that the JAXP 1.1ea2 parser crashes with an InternalError if
* it encounters a system identifier that appears to be a relative URI
* that begins with a slash. For example, the declaration:</p>
*
* <pre>
* &lt;!DOCTYPE book SYSTEM "/path/to/dtd/on/my/system/docbookx.dtd">
* </pre>
*
* <p>would cause such an error. As a convenience, this method catches
* that error and prints an explanation. (Unfortunately, it's not possible
* to identify the particular system identifier that causes the problem.)
* </p>
*
* <p>The underlying error is forwarded after printing the explanatory
* message. The message is only every printed once and if
* <code>suppressExplanation</code> is set to <code>false</code> before
* parsing, it will never be printed.</p>
*/
public void parse(InputSource input)
throws IOException, SAXException {
allowXMLCatalogPI = true;
setupBaseURI(input.getSystemId());
try {
super.parse(input);
} catch (InternalError ie) {
explain(input.getSystemId());
throw ie;
}
}
/** SAX XMLReader API.
*
* @see #parse(InputSource)
*/
public void parse(String systemId)
throws IOException, SAXException {
allowXMLCatalogPI = true;
setupBaseURI(systemId);
try {
super.parse(systemId);
} catch (InternalError ie) {
explain(systemId);
throw ie;
}
}
/**
* Implements the <code>resolveEntity</code> method
* for the SAX interface, using an underlying CatalogResolver
* to do the real work.
*/
public InputSource resolveEntity (String publicId, String systemId) {
allowXMLCatalogPI = false;
String resolved = catalogResolver.getResolvedEntity(publicId, systemId);
if (resolved == null && piCatalogResolver != null) {
resolved = piCatalogResolver.getResolvedEntity(publicId, systemId);
}
if (resolved != null) {
try {
InputSource iSource = new InputSource(resolved);
iSource.setPublicId(publicId);
// Ideally this method would not attempt to open the
// InputStream, but there is a bug (in Xerces, at least)
// that causes the parser to mistakenly open the wrong
// system identifier if the returned InputSource does
// not have a byteStream.
//
// It could be argued that we still shouldn't do this here,
// but since the purpose of calling the entityResolver is
// almost certainly to open the input stream, it seems to
// do little harm.
//
URL url = new URL(resolved);
InputStream iStream = url.openStream();
iSource.setByteStream(iStream);
return iSource;
} catch (Exception e) {
catalogManager.debug.message(1,
"Failed to create InputSource ("
+ e.toString()
+ ")", resolved);
return null;
}
} else {
return null;
}
}
/** SAX DTDHandler API.
*
* <p>Captured here only to detect the end of the prolog so that
* we can ignore subsequent oasis-xml-catalog PIs. Otherwise
* the events are just passed through.</p>
*/
public void notationDecl (String name, String publicId, String systemId)
throws SAXException {
allowXMLCatalogPI = false;
super.notationDecl(name,publicId,systemId);
}
/** SAX DTDHandler API.
*
* <p>Captured here only to detect the end of the prolog so that
* we can ignore subsequent oasis-xml-catalog PIs. Otherwise
* the events are just passed through.</p>
*/
public void unparsedEntityDecl (String name,
String publicId,
String systemId,
String notationName)
throws SAXException {
allowXMLCatalogPI = false;
super.unparsedEntityDecl (name, publicId, systemId, notationName);
}
/** SAX ContentHandler API.
*
* <p>Captured here only to detect the end of the prolog so that
* we can ignore subsequent oasis-xml-catalog PIs. Otherwise
* the events are just passed through.</p>
*/
public void startElement (String uri, String localName, String qName,
Attributes atts)
throws SAXException {
allowXMLCatalogPI = false;
super.startElement(uri,localName,qName,atts);
}
/** SAX ContentHandler API.
*
* <p>Detect and use the oasis-xml-catalog PI if it occurs.</p>
*/
public void processingInstruction(String target, String pidata)
throws SAXException {
if (target.equals("oasis-xml-catalog")) {
URL catalog = null;
String data = pidata;
int pos = data.indexOf("catalog=");
if (pos >= 0) {
data = data.substring(pos+8);
if (data.length() > 1) {
String quote = data.substring(0,1);
data = data.substring(1);
pos = data.indexOf(quote);
if (pos >= 0) {
data = data.substring(0, pos);
try {
if (baseURL != null) {
catalog = new URL(baseURL, data);
} else {
catalog = new URL(data);
}
} catch (MalformedURLException mue) {
// nevermind
}
}
}
}
if (allowXMLCatalogPI) {
if (catalogManager.getAllowOasisXMLCatalogPI()) {
catalogManager.debug.message(4,"oasis-xml-catalog PI", pidata);
if (catalog != null) {
try {
catalogManager.debug.message(4,"oasis-xml-catalog", catalog.toString());
if (piCatalogResolver == null) {
piCatalogResolver = new CatalogResolver(true);
}
piCatalogResolver.getCatalog().parseCatalog(catalog.toString());
} catch (Exception e) {
catalogManager.debug.message(3, "Exception parsing oasis-xml-catalog: "
+ catalog.toString());
}
} else {
catalogManager.debug.message(3, "PI oasis-xml-catalog unparseable: " + pidata);
}
} else {
catalogManager.debug.message(4,"PI oasis-xml-catalog ignored: " + pidata);
}
} else {
catalogManager.debug.message(3, "PI oasis-xml-catalog occurred in an invalid place: "
+ pidata);
}
} else {
super.processingInstruction(target, pidata);
}
}
/** Save the base URI of the document being parsed. */
private void setupBaseURI(String systemId) {
URL cwd = null;
try {
cwd = FileURL.makeURL("basename");
} catch (MalformedURLException mue) {
cwd = null;
}
try {
baseURL = new URL(systemId);
} catch (MalformedURLException mue) {
if (cwd != null) {
try {
baseURL = new URL(cwd, systemId);
} catch (MalformedURLException mue2) {
// give up
baseURL = null;
}
} else {
// give up
baseURL = null;
}
}
}
/** Provide one possible explanation for an InternalError. */
private void explain(String systemId) {
if (!suppressExplanation) {
System.out.println("XMLReader probably encountered bad URI in " + systemId);
System.out.println("For example, replace '/some/uri' with 'file:/some/uri'.");
}
suppressExplanation = true;
}
}

View File

@ -1,90 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xml.internal.resolver.tools;
import javax.xml.parsers.*;
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
import com.sun.org.apache.xml.internal.resolver.*;
/**
* A SAX XMLReader that performs catalog-based entity resolution.
*
* <p>This class implements a SAX XMLReader that performs entity resolution
* using the CatalogResolver. The actual, underlying parser is obtained
* from a SAXParserFactory.</p>
* </p>
*
* @see CatalogResolver
* @see org.xml.sax.XMLReader
*
* @author Norman Walsh
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
*
* @version 1.0
*/
public class ResolvingXMLReader extends ResolvingXMLFilter {
/** Make the parser Namespace aware? */
private static final boolean namespaceAware = true;
/** Make the parser validating? */
private static final boolean validating = false;
/**
* Construct a new reader from the JAXP factory.
*
* <p>In order to do its job, a ResolvingXMLReader must in fact be
* a filter. So the only difference between this code and the filter
* code is that the constructor builds a new reader.</p>
*/
public ResolvingXMLReader() {
super();
SAXParserFactory spf = catalogManager.useServicesMechanism() ?
SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
spf.setNamespaceAware(namespaceAware);
spf.setValidating(validating);
try {
SAXParser parser = spf.newSAXParser();
setParent(parser.getXMLReader());
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Construct a new reader from the JAXP factory.
*
* <p>In order to do its job, a ResolvingXMLReader must in fact be
* a filter. So the only difference between this code and the filter
* code is that the constructor builds a new reader.</p>
*/
public ResolvingXMLReader(CatalogManager manager) {
super(manager);
SAXParserFactory spf = catalogManager.useServicesMechanism() ?
SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
spf.setNamespaceAware(namespaceAware);
spf.setValidating(validating);
try {
SAXParser parser = spf.newSAXParser();
setParent(parser.getXMLReader());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}