mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 11:04:34 +02:00
8163232: Catalog API: Consolidating CatalogResolver to support all XML Resolvers
Reviewed-by: dfuchs, lancea
This commit is contained in:
parent
08c97e3e67
commit
cdcc5575ac
65 changed files with 1197 additions and 499 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -42,9 +42,9 @@ import java.util.stream.Stream;
|
|||
* <p>
|
||||
* A catalog can be used in two situations:
|
||||
* <ul>
|
||||
* <li>Locate the replacement text for an external entity;
|
||||
* <li>Locate the external resources with a public or system identifier;
|
||||
* </li>
|
||||
* <li>Locate an alternate URI reference for a resource.
|
||||
* <li>Locate an alternate URI reference with an URI.
|
||||
* </li>
|
||||
* </ul>
|
||||
* <p>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -76,17 +76,6 @@ public final class CatalogManager {
|
|||
return new CatalogResolverImpl(catalog);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of a {@code CatalogUriResolver} using the specified catalog.
|
||||
*
|
||||
* @param catalog the catalog instance
|
||||
* @return an instance of a {@code CatalogResolver}
|
||||
*/
|
||||
public static CatalogUriResolver catalogUriResolver(Catalog catalog) {
|
||||
if (catalog == null) CatalogMessages.reportNPEOnNull("catalog", null);
|
||||
return new CatalogUriResolverImpl(catalog);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of a {@code CatalogResolver} using the specified feature
|
||||
* settings and path to one or more catalog files.
|
||||
|
@ -115,33 +104,4 @@ public final class CatalogManager {
|
|||
Catalog catalog = catalog(features, paths);
|
||||
return new CatalogResolverImpl(catalog);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of a {@code CatalogUriResolver} using the specified
|
||||
* feature settings and path to one or more catalog files.
|
||||
* <p>
|
||||
* If {@code paths} is empty, system property {@code javax.xml.catalog.files}
|
||||
* will be read to locate the initial list of catalog files.
|
||||
* <p>
|
||||
* If more than one catalog files are specified through the paths argument or
|
||||
* {@code javax.xml.catalog.files} property, the first entry is considered
|
||||
* the main catalog, while others are treated as alternative catalogs after
|
||||
* those referenced by the {@code nextCatalog} elements in the main catalog.
|
||||
* <p>
|
||||
* As specified in
|
||||
* <a href="https://www.oasis-open.org/committees/download.php/14809/xml-catalogs.html#s.res.fail">
|
||||
* XML Catalogs, OASIS Standard V1.1</a>, invalid path entries will be ignored.
|
||||
* No error will be reported. In case all entries are invalid, the resolver
|
||||
* will return as no mapping is found.
|
||||
*
|
||||
* @param features the catalog features
|
||||
* @param paths the path(s) to one or more catalogs
|
||||
*
|
||||
* @return an instance of a {@code CatalogUriResolver}
|
||||
* @throws CatalogException If an error occurs while parsing the catalog
|
||||
*/
|
||||
public static CatalogUriResolver catalogUriResolver(CatalogFeatures features, String... paths) {
|
||||
Catalog catalog = catalog(features, paths);
|
||||
return new CatalogUriResolverImpl(catalog);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -24,32 +24,90 @@
|
|||
*/
|
||||
package javax.xml.catalog;
|
||||
|
||||
import java.io.InputStream;
|
||||
import javax.xml.stream.XMLResolver;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.URIResolver;
|
||||
import org.w3c.dom.ls.LSInput;
|
||||
import org.w3c.dom.ls.LSResourceResolver;
|
||||
import org.xml.sax.EntityResolver;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
/**
|
||||
* A SAX EntityResolver that uses catalogs to resolve references.
|
||||
* A Catalog Resolver that implements SAX {@link org.xml.sax.EntityResolver},
|
||||
* StAX {@link javax.xml.stream.XMLResolver},
|
||||
* DOM LS {@link org.w3c.dom.ls.LSResourceResolver} used by Schema Validation, and
|
||||
* Transform {@link javax.xml.transform.URIResolver}, and resolves
|
||||
* external references using catalogs.
|
||||
* <p>
|
||||
* The <a href="https://www.oasis-open.org/committees/download.php/14809/xml-catalogs.html">
|
||||
* Catalog Standard</a> distinguished {@code external identifiers} from {@code uri entries}
|
||||
* as being used to solely identify DTDs, while {@code uri entries} for
|
||||
* other resources such as stylesheets and schema. The Java APIs, such as
|
||||
* {@link javax.xml.stream.XMLResolver} and {@link org.w3c.dom.ls.LSResourceResolver}
|
||||
* however, make no such distinction.
|
||||
* In consistent with the existing Java API, this CatalogResolver recognizes a
|
||||
* system identifier as an URI and will search both {@code system} and {@code uri}
|
||||
* entries in a catalog in order to find a matching entry.
|
||||
* <p>
|
||||
* The search is started in the current catalog. If a match is found,
|
||||
* no further attempt will be made. Only if there is no match in the current
|
||||
* catalog, will alternate catalogs including delegate and next catalogs be considered.
|
||||
* <p>
|
||||
* <h3>Search Order</h3>
|
||||
* The resolver will first search the system-type of entries with the specified
|
||||
* {@code systemId}. The system entries include {@code system},
|
||||
* {@code rewriteSystem} and {@code systemSuffix} entries.
|
||||
* <p>
|
||||
* If no match is found, {@code public} entries may be searched in accordance with
|
||||
* the {@code prefer} attribute.
|
||||
* <p>
|
||||
* <b>The {@code prefer} attribute</b>: if the {@code prefer} is public,
|
||||
* and there is no match found through the system entries, {@code public} entries
|
||||
* will be considered. If it is not specified, the {@code prefer} is public
|
||||
* by default (Note that by the OASIS standard, system entries will always
|
||||
* be considered before public entries. Prefer public means that public entries
|
||||
* will be matched when both system and public identifiers are specified.
|
||||
* In general therefore, prefer public is recommended.)
|
||||
* <p>
|
||||
* If no match is found with the {@code systemId} and {@code public} identifier,
|
||||
* the resolver will continue searching {@code uri} entries
|
||||
* with the specified {@code systemId} or {@code href}. The {@code uri} entries
|
||||
* include {@code uri}, {@code rewriteURI}, and {@code uriSuffix} entries.
|
||||
*
|
||||
* <p>
|
||||
* <h3>Error Handling</h3>
|
||||
* The interfaces that the CatalogResolver extend specified checked exceptions, including:
|
||||
* <ul>
|
||||
* <li>
|
||||
* {@link org.xml.sax.SAXException} and {@link java.io.IOException} by
|
||||
* {@link org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)}
|
||||
* </li>
|
||||
* <li>
|
||||
* {@link javax.xml.stream.XMLStreamException} by
|
||||
* {@link javax.xml.stream.XMLResolver#resolveEntity(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}
|
||||
* </li>
|
||||
* <li>
|
||||
* {@link javax.xml.transform.TransformerException} by
|
||||
* {@link javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String)}
|
||||
* </li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* The CatalogResolver however, will throw {@link javax.xml.catalog.CatalogException}
|
||||
* only when {@code javax.xml.catalog.resolve} is specified as {@code strict}.
|
||||
* For applications that expect to handle the checked Exceptions, it may be
|
||||
* necessary to use a custom resolver to wrap the CatalogResolver or implement it
|
||||
* with a {@link javax.xml.catalog.Catalog} object.
|
||||
*
|
||||
* @since 9
|
||||
*/
|
||||
public interface CatalogResolver extends EntityResolver {
|
||||
public interface CatalogResolver extends EntityResolver, XMLResolver,
|
||||
URIResolver, LSResourceResolver {
|
||||
|
||||
/**
|
||||
* The method searches through the catalog entries in the main and
|
||||
* alternative catalogs to attempt to find a match with the specified publicId
|
||||
* or systemId.
|
||||
* <p>
|
||||
* For resolving external entities, system entries will be matched before
|
||||
* the public entries.
|
||||
* <p>
|
||||
* <b>The {@code prefer} attribute</b>: if the {@code prefer} is public,
|
||||
* and there is no match found through the system entries, public entries
|
||||
* will be considered. If it is not specified, the {@code prefer} is public
|
||||
* by default (Note that by the OASIS standard, system entries will always
|
||||
* be considered first when the external system identifier is specified.
|
||||
* Prefer public means that public entries will be matched when both system
|
||||
* and public identifiers are specified. In general therefore, prefer
|
||||
* public is recommended.)
|
||||
* Implements {@link org.xml.sax.EntityResolver}. The method searches through
|
||||
* the catalog entries in the main and alternative catalogs to attempt to find
|
||||
* a match with the specified {@code publicId} or systemId.
|
||||
*
|
||||
* @param publicId the public identifier of the external entity being
|
||||
* referenced, or null if none was supplied
|
||||
|
@ -59,15 +117,123 @@ public interface CatalogResolver extends EntityResolver {
|
|||
* requires a system identifier on all external entities, so this value is
|
||||
* always specified.
|
||||
*
|
||||
* @return a {@link org.xml.sax.InputSource} object if a mapping is found. If no mapping is
|
||||
* found, returns a {@link org.xml.sax.InputSource} object containing an empty
|
||||
* {@link java.io.Reader} if the {@code javax.xml.catalog.resolve} property
|
||||
* is set to {@code ignore}; returns null if the
|
||||
* @return a {@link org.xml.sax.InputSource} object if a mapping is found.
|
||||
* If no mapping is found, returns a {@link org.xml.sax.InputSource} object
|
||||
* containing an empty {@link java.io.Reader} if the
|
||||
* {@code javax.xml.catalog.resolve} property is set to {@code ignore};
|
||||
* returns null if the
|
||||
* {@code javax.xml.catalog.resolve} property is set to {@code continue}.
|
||||
*
|
||||
* @throws CatalogException if no mapping is found and
|
||||
* {@code javax.xml.catalog.resolve} is specified as strict
|
||||
* {@code javax.xml.catalog.resolve} is specified as {@code strict}
|
||||
*/
|
||||
@Override
|
||||
public InputSource resolveEntity(String publicId, String systemId);
|
||||
|
||||
|
||||
/**
|
||||
* Implements URIResolver. The method searches through the catalog entries
|
||||
* in the main and alternative catalogs to attempt to find a match
|
||||
* with the specified {@code href} attribute. The {@code href} attribute will
|
||||
* be used literally, with no attempt to be made absolute to the {@code base}.
|
||||
* <p>
|
||||
* If the value is an URN, the {@code href} attribute is recognized as a
|
||||
* {@code publicId}, and used to search {@code public} entries.
|
||||
* If the value is an URI, it is taken as a {@code systemId}, and used to
|
||||
* search both {@code system} and {@code uri} entries.
|
||||
*
|
||||
*
|
||||
* @param href the href attribute that specifies the URI of a style sheet,
|
||||
* which may be relative or absolute
|
||||
* @param base The base URI against which the href attribute will be made
|
||||
* absolute if the absolute URI is required
|
||||
*
|
||||
* @return a {@link javax.xml.transform.Source} object if a mapping is found.
|
||||
* If no mapping is found, returns an empty {@link javax.xml.transform.Source}
|
||||
* object if the {@code javax.xml.catalog.resolve} property is set to
|
||||
* {@code ignore};
|
||||
* returns a {@link javax.xml.transform.Source} object with the original URI
|
||||
* (href, or href resolved with base if base is not null) if the
|
||||
* {@code javax.xml.catalog.resolve} property is set to {@code continue}.
|
||||
*
|
||||
* @throws CatalogException if no mapping is found and
|
||||
* {@code javax.xml.catalog.resolve} is specified as {@code strict}
|
||||
*/
|
||||
@Override
|
||||
public Source resolve(String href, String base);
|
||||
|
||||
/**
|
||||
* Implements {@link javax.xml.stream.XMLResolver}. For the purpose of resolving
|
||||
* {@code publicId} and {@code systemId}, this method is equivalent to
|
||||
* {@link #resolveEntity(java.lang.String, java.lang.String) }.
|
||||
* <p>
|
||||
* The {@code systemId} will be used literally, with no attempt to be made
|
||||
* absolute to the {@code baseUri}. The {@code baseUri} and {@code namespace}
|
||||
* are not used in the search for a match in a catalog. However, a relative
|
||||
* {@code systemId} in an xml source may have been made absolute by the parser
|
||||
* with the {@code baseURI}, thus making it unable to find a {@code system} entry.
|
||||
* In such a case, a {@code systemSuffix} entry is recommended over a
|
||||
* {@code system} entry.
|
||||
*
|
||||
* @param publicId the public identifier of the external entity being
|
||||
* referenced, or null if none was supplied
|
||||
*
|
||||
* @param systemId the system identifier of the external entity being
|
||||
* referenced. A system identifier is required on all external entities. XML
|
||||
* requires a system identifier on all external entities, so this value is
|
||||
* always specified.
|
||||
* @param baseUri the absolute base URI, not used by the CatalogResolver
|
||||
* @param namespace the namespace of the entity to resolve, not used by the
|
||||
* CatalogResolver.
|
||||
*
|
||||
* @return an {@link java.io.InputStream} object if a mapping is found; null
|
||||
* if no mapping is found and the {@code javax.xml.catalog.resolve} property
|
||||
* is set to {@code continue} or {@code ignore}. Note that for XMLResolver,
|
||||
* it is not possible to ignore a reference, {@code ignore} is therefore
|
||||
* treated the same as {@code continue}.
|
||||
*
|
||||
* @throws CatalogException if no mapping is found and
|
||||
* {@code javax.xml.catalog.resolve} is specified as {@code strict}
|
||||
*/
|
||||
@Override
|
||||
public InputStream resolveEntity(String publicId, String systemId,
|
||||
String baseUri, String namespace);
|
||||
|
||||
/**
|
||||
* Implements {@link org.w3c.dom.ls.LSResourceResolver}. For the purpose of
|
||||
* resolving {@code publicId} and {@code systemId}, this method is equivalent
|
||||
* to {@link #resolveEntity(java.lang.String, java.lang.String) }.
|
||||
* <p>
|
||||
* The {@code systemId} will be used literally, with no attempt to be made
|
||||
* absolute to the {@code baseUri}. The {@code baseUri}, {@code namespaceUri}
|
||||
* and {@code type} are not used in the search for a match in a catalog.
|
||||
* However, a relative {@code systemId} in a source may have been made absolute
|
||||
* by the parser with the {@code baseURI}, thus making it unable to find a
|
||||
* {@code system} entry. In such a case, a {@code systemSuffix} entry is
|
||||
* recommended over a {@code system} entry.
|
||||
*
|
||||
* @param type the type of the resource being resolved,
|
||||
* not used by the CatalogResolver
|
||||
* @param namespaceUri the namespace of the resource being resolved,
|
||||
* not used by the CatalogResolver
|
||||
* @param publicId the public identifier of the external entity being
|
||||
* referenced, or {@code null} if no public identifier was
|
||||
* supplied or if the resource is not an entity.
|
||||
* @param systemId the system identifier, an URI reference of the
|
||||
* external resource being referenced
|
||||
* @param baseUri the absolute base URI, not used by the CatalogResolver
|
||||
*
|
||||
* @return a {@link org.w3c.dom.ls.LSInput} object if a mapping is found; null
|
||||
* if no mapping is found and the {@code javax.xml.catalog.resolve} property
|
||||
* is set to {@code continue} or {@code ignore}. Note that for
|
||||
* {@link org.w3c.dom.ls.LSResourceResolver}, it is not possible to ignore a
|
||||
* reference, {@code ignore} is therefore treated the same as {@code continue}.
|
||||
*
|
||||
* @throws CatalogException if no mapping is found and
|
||||
* {@code javax.xml.catalog.resolve} is specified as {@code strict}
|
||||
*/
|
||||
@Override
|
||||
public LSInput resolveResource(String type, String namespaceUri,
|
||||
String publicId, String systemId, String baseUri);
|
||||
|
||||
}
|
||||
|
|
|
@ -24,15 +24,27 @@
|
|||
*/
|
||||
package javax.xml.catalog;
|
||||
|
||||
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.Iterator;
|
||||
import java.net.URL;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import org.w3c.dom.ls.LSInput;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
/**
|
||||
* A SAX EntityResolver/JAXP URIResolver that uses catalogs.
|
||||
* Implements CatalogResolver.
|
||||
*
|
||||
* <p>
|
||||
* This class implements both a SAX EntityResolver and a JAXP URIResolver.
|
||||
* This class implements a SAX EntityResolver, StAX XMLResolver,
|
||||
* Schema Validation LSResourceResolver and Transform URIResolver.
|
||||
*
|
||||
*
|
||||
* @since 9
|
||||
|
@ -49,9 +61,14 @@ final class CatalogResolverImpl implements CatalogResolver {
|
|||
this.catalog = catalog;
|
||||
}
|
||||
|
||||
/*
|
||||
Implements the EntityResolver interface
|
||||
*/
|
||||
@Override
|
||||
public InputSource resolveEntity(String publicId, String systemId) {
|
||||
//8150187: NPE expected if the system identifier is null for CatalogResolver
|
||||
CatalogMessages.reportNPEOnNull("systemId", systemId);
|
||||
|
||||
//Normalize publicId and systemId
|
||||
systemId = Normalizer.normalizeURI(Util.getNotNullOrEmpty(systemId));
|
||||
publicId = Normalizer.normalizePublicId(Normalizer.decodeURN(Util.getNotNullOrEmpty(publicId)));
|
||||
|
@ -87,4 +104,242 @@ final class CatalogResolverImpl implements CatalogResolver {
|
|||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
Implements the URIResolver interface
|
||||
*/
|
||||
CatalogResolverImpl entityResolver;
|
||||
|
||||
@Override
|
||||
public Source resolve(String href, String base) {
|
||||
CatalogMessages.reportNPEOnNull("href", href);
|
||||
|
||||
href = Util.getNotNullOrEmpty(href);
|
||||
base = Util.getNotNullOrEmpty(base);
|
||||
|
||||
String result = null;
|
||||
CatalogImpl c = (CatalogImpl)catalog;
|
||||
String uri = Normalizer.normalizeURI(href);
|
||||
//check whether uri is an urn
|
||||
if (uri != null && uri.startsWith(Util.URN)) {
|
||||
String publicId = Normalizer.decodeURN(uri);
|
||||
if (publicId != null) {
|
||||
result = Util.resolve(c, publicId, null);
|
||||
}
|
||||
}
|
||||
|
||||
//if no match with a public id, continue search for an URI
|
||||
if (result == null) {
|
||||
//remove fragment if any.
|
||||
int hashPos = uri.indexOf("#");
|
||||
if (hashPos >= 0) {
|
||||
uri = uri.substring(0, hashPos);
|
||||
}
|
||||
|
||||
//search the current catalog
|
||||
result = Util.resolve(c, null, uri);
|
||||
}
|
||||
|
||||
//Report error or return the URI as is when no match is found
|
||||
if (result == null) {
|
||||
GroupEntry.ResolveType resolveType = c.getResolve();
|
||||
switch (resolveType) {
|
||||
case IGNORE:
|
||||
return new SAXSource(new InputSource(new StringReader("")));
|
||||
case STRICT:
|
||||
CatalogMessages.reportError(CatalogMessages.ERR_NO_URI_MATCH,
|
||||
new Object[]{href, base});
|
||||
}
|
||||
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) {
|
||||
CatalogMessages.reportError(CatalogMessages.ERR_CREATING_URI,
|
||||
new Object[]{href, base});
|
||||
}
|
||||
}
|
||||
|
||||
SAXSource source = new SAXSource();
|
||||
source.setInputSource(new InputSource(result));
|
||||
setEntityResolver(source);
|
||||
return source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Establish an entityResolver for newly resolved URIs.
|
||||
* <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). Quote from JAXP specification on Class
|
||||
* SAXTransformerFactory:
|
||||
* <p>
|
||||
* {@code 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}
|
||||
*
|
||||
*/
|
||||
private void setEntityResolver(SAXSource source) {
|
||||
XMLReader reader = source.getXMLReader();
|
||||
if (reader == null) {
|
||||
SAXParserFactory spFactory = new SAXParserFactoryImpl();
|
||||
spFactory.setNamespaceAware(true);
|
||||
try {
|
||||
reader = spFactory.newSAXParser().getXMLReader();
|
||||
} catch (ParserConfigurationException | SAXException ex) {
|
||||
CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSER_CONF, ex);
|
||||
}
|
||||
}
|
||||
if (entityResolver != null) {
|
||||
entityResolver = new CatalogResolverImpl(catalog);
|
||||
}
|
||||
reader.setEntityResolver(entityResolver);
|
||||
source.setXMLReader(reader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream resolveEntity(String publicId, String systemId, String baseUri, String namespace) {
|
||||
InputSource is = resolveEntity(publicId, systemId);
|
||||
|
||||
if (is != null && !is.isEmpty()) {
|
||||
|
||||
try {
|
||||
return new URL(is.getSystemId()).openStream();
|
||||
} catch (IOException ex) {
|
||||
//considered as no mapping.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
GroupEntry.ResolveType resolveType = ((CatalogImpl) catalog).getResolve();
|
||||
switch (resolveType) {
|
||||
case IGNORE:
|
||||
return null;
|
||||
case STRICT:
|
||||
CatalogMessages.reportError(CatalogMessages.ERR_NO_MATCH,
|
||||
new Object[]{publicId, systemId});
|
||||
}
|
||||
|
||||
//no action, allow the parser to continue
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
|
||||
InputSource is = resolveEntity(publicId, systemId);
|
||||
|
||||
if (is != null && !is.isEmpty()) {
|
||||
return new LSInputImpl(is.getSystemId());
|
||||
}
|
||||
|
||||
GroupEntry.ResolveType resolveType = ((CatalogImpl) catalog).getResolve();
|
||||
switch (resolveType) {
|
||||
case IGNORE:
|
||||
return null;
|
||||
case STRICT:
|
||||
CatalogMessages.reportError(CatalogMessages.ERR_NO_MATCH,
|
||||
new Object[]{publicId, systemId});
|
||||
}
|
||||
|
||||
//no action, allow the parser to continue
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements LSInput. All that we need is the systemId since the Catalog
|
||||
* has already resolved it.
|
||||
*/
|
||||
class LSInputImpl implements LSInput {
|
||||
|
||||
private String systemId;
|
||||
|
||||
public LSInputImpl(String systemId) {
|
||||
this.systemId = systemId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader getCharacterStream() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCharacterStream(Reader characterStream) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getByteStream() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setByteStream(InputStream byteStream) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStringData() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStringData(String stringData) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSystemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSystemId(String systemId) {
|
||||
this.systemId = systemId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPublicId() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPublicId(String publicId) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBaseURI() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBaseURI(String baseURI) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEncoding() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEncoding(String encoding) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getCertifiedText() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCertifiedText(boolean certifiedText) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package javax.xml.catalog;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.URIResolver;
|
||||
|
||||
/**
|
||||
* A JAXP URIResolver that uses catalogs to resolve references.
|
||||
*
|
||||
* @since 9
|
||||
*/
|
||||
public interface CatalogUriResolver extends URIResolver {
|
||||
|
||||
/**
|
||||
* The method searches through the catalog entries in the main and
|
||||
* alternative catalogs to attempt to find a match with the specified URI.
|
||||
*
|
||||
* @param href an href attribute, which may be relative or absolute
|
||||
* @param base The base URI against which the href attribute will be made
|
||||
* absolute if the absolute URI is required
|
||||
*
|
||||
* @return a {@link javax.xml.transform.Source} object if a mapping is found.
|
||||
* If no mapping is found, returns an empty {@link javax.xml.transform.Source}
|
||||
* object if the {@code javax.xml.catalog.resolve} property is set to
|
||||
* {@code ignore};
|
||||
* returns a {@link javax.xml.transform.Source} object with the original URI
|
||||
* (href, or href resolved with base if base is not null) if the
|
||||
* {@code javax.xml.catalog.resolve} property is set to {@code continue}.
|
||||
*
|
||||
* @throws CatalogException if no mapping is found and
|
||||
* {@code javax.xml.catalog.resolve} is specified as strict
|
||||
*/
|
||||
@Override
|
||||
public Source resolve(String href, String base);
|
||||
}
|
|
@ -1,191 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package javax.xml.catalog;
|
||||
|
||||
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
|
||||
import java.io.StringReader;
|
||||
import java.net.URL;
|
||||
import java.util.Iterator;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
/**
|
||||
* A SAX EntityResolver/JAXP URIResolver that uses catalogs.
|
||||
* <p>
|
||||
* This class implements both a SAX EntityResolver and a JAXP URIResolver.
|
||||
*
|
||||
*
|
||||
* @since 9
|
||||
*/
|
||||
final class CatalogUriResolverImpl implements CatalogUriResolver {
|
||||
|
||||
Catalog catalog;
|
||||
CatalogResolverImpl entityResolver;
|
||||
|
||||
/**
|
||||
* Construct an instance of the CatalogResolver from a Catalog.
|
||||
*
|
||||
* @param catalog A Catalog.
|
||||
*/
|
||||
public CatalogUriResolverImpl(Catalog catalog) {
|
||||
this.catalog = catalog;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Source resolve(String href, String base) {
|
||||
href = Util.getNotNullOrEmpty(href);
|
||||
base = Util.getNotNullOrEmpty(base);
|
||||
|
||||
if (href == null) return null;
|
||||
|
||||
String result = null;
|
||||
CatalogImpl c = (CatalogImpl)catalog;
|
||||
String uri = Normalizer.normalizeURI(href);
|
||||
//check whether uri is an urn
|
||||
if (uri != null && uri.startsWith(Util.URN)) {
|
||||
String publicId = Normalizer.decodeURN(uri);
|
||||
if (publicId != null) {
|
||||
result = Util.resolve(c, publicId, null);
|
||||
}
|
||||
}
|
||||
|
||||
//if no match with a public id, continue search for an URI
|
||||
if (result == null) {
|
||||
//remove fragment if any.
|
||||
int hashPos = uri.indexOf("#");
|
||||
if (hashPos >= 0) {
|
||||
uri = uri.substring(0, hashPos);
|
||||
}
|
||||
|
||||
//search the current catalog
|
||||
result = resolve(c, uri);
|
||||
}
|
||||
|
||||
//Report error or return the URI as is when no match is found
|
||||
if (result == null) {
|
||||
GroupEntry.ResolveType resolveType = c.getResolve();
|
||||
switch (resolveType) {
|
||||
case IGNORE:
|
||||
return new SAXSource(new InputSource(new StringReader("")));
|
||||
case STRICT:
|
||||
CatalogMessages.reportError(CatalogMessages.ERR_NO_URI_MATCH,
|
||||
new Object[]{href, base});
|
||||
}
|
||||
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) {
|
||||
CatalogMessages.reportError(CatalogMessages.ERR_CREATING_URI,
|
||||
new Object[]{href, base});
|
||||
}
|
||||
}
|
||||
|
||||
SAXSource source = new SAXSource();
|
||||
source.setInputSource(new InputSource(result));
|
||||
setEntityResolver(source);
|
||||
return source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the publicId or systemId to one specified in the catalog.
|
||||
* @param catalog the catalog
|
||||
* @param href an href attribute, which may be relative or absolute
|
||||
* @return the resolved systemId if a match is found, null otherwise
|
||||
*/
|
||||
String resolve(CatalogImpl catalog, String href) {
|
||||
String result = null;
|
||||
|
||||
//search the current catalog
|
||||
catalog.reset();
|
||||
if (href != null) {
|
||||
result = catalog.matchURI(href);
|
||||
}
|
||||
|
||||
//mark the catalog as having been searched before trying alternatives
|
||||
catalog.markAsSearched();
|
||||
|
||||
//search alternative catalogs
|
||||
if (result == null) {
|
||||
Iterator<Catalog> iter = catalog.catalogs().iterator();
|
||||
while (iter.hasNext()) {
|
||||
result = resolve((CatalogImpl)iter.next(), href);
|
||||
if (result != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Establish an entityResolver for newly resolved URIs.
|
||||
* <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). Quote from JAXP specification on Class
|
||||
* SAXTransformerFactory:
|
||||
* <p>
|
||||
* {@code 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}
|
||||
*
|
||||
*/
|
||||
private void setEntityResolver(SAXSource source) {
|
||||
XMLReader reader = source.getXMLReader();
|
||||
if (reader == null) {
|
||||
SAXParserFactory spFactory = new SAXParserFactoryImpl();
|
||||
spFactory.setNamespaceAware(true);
|
||||
try {
|
||||
reader = spFactory.newSAXParser().getXMLReader();
|
||||
} catch (ParserConfigurationException | SAXException ex) {
|
||||
CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSER_CONF, ex);
|
||||
}
|
||||
}
|
||||
if (entityResolver != null) {
|
||||
entityResolver = new CatalogResolverImpl(catalog);
|
||||
}
|
||||
reader.setEntityResolver(entityResolver);
|
||||
source.setXMLReader(reader);
|
||||
}
|
||||
}
|
|
@ -55,6 +55,9 @@ class Util {
|
|||
* prefer "public": attempts to resolve with a system entry;
|
||||
* attempts to resolve with a public entry if no matching
|
||||
* system entry is found.
|
||||
*
|
||||
* If no match is found, continue searching uri entries
|
||||
*
|
||||
* @param catalog the catalog
|
||||
* @param publicId the publicId
|
||||
* @param systemId the systemId
|
||||
|
@ -77,6 +80,10 @@ class Util {
|
|||
resolvedSystemId = catalog.matchPublic(publicId);
|
||||
}
|
||||
|
||||
if (resolvedSystemId == null && systemId != null) {
|
||||
resolvedSystemId = catalog.matchURI(systemId);
|
||||
}
|
||||
|
||||
//mark the catalog as having been searched before trying alternatives
|
||||
catalog.markAsSearched();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue