Add interfaces for constructing a filter with a particular CatalogManager

git-svn-id: https://svn.apache.org/repos/asf/xml/commons/trunk@226010 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
ndw 2002-11-13 20:54:30 +00:00
parent ec123dbb6d
commit 4f7be003f3
4 changed files with 91 additions and 65 deletions

View File

@ -104,9 +104,11 @@ public class CatalogResolver implements EntityResolver, URIResolver {
public boolean validating = false; public boolean validating = false;
/** The underlying catalog */ /** The underlying catalog */
private static Catalog staticCatalog = null;
private Catalog catalog = null; private Catalog catalog = null;
/** The catalog manager */
private CatalogManager catalogManager = CatalogManager.getStaticManager();
/** Constructor */ /** Constructor */
public CatalogResolver() { public CatalogResolver() {
initializeCatalogs(false); initializeCatalogs(false);
@ -117,44 +119,15 @@ public class CatalogResolver implements EntityResolver, URIResolver {
initializeCatalogs(privateCatalog); initializeCatalogs(privateCatalog);
} }
/** Constructor */
public CatalogResolver(CatalogManager manager) {
catalogManager = manager;
initializeCatalogs(!catalogManager.getUseStaticCatalog());
}
/** Initialize catalog */ /** Initialize catalog */
private void initializeCatalogs(boolean privateCatalog) { private void initializeCatalogs(boolean privateCatalog) {
catalog = staticCatalog; catalog = catalogManager.getCatalog();
if (privateCatalog || catalog == null) {
try {
String catalogClassName = CatalogManager.catalogClassName();
if (catalogClassName == null) {
catalog = new Catalog();
} else {
try {
catalog = (Catalog) Class.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.setupReaders();
if (!privateCatalog) {
catalog.loadSystemCatalogs();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (!privateCatalog && catalog != null && CatalogManager.staticCatalog()) {
staticCatalog = catalog;
}
} }
/** Return the underlying catalog */ /** Return the underlying catalog */
@ -186,7 +159,7 @@ public class CatalogResolver implements EntityResolver, URIResolver {
String resolved = null; String resolved = null;
if (catalog == null) { if (catalog == null) {
Debug.message(1, "Catalog resolution attempted with null catalog; ignored"); catalogManager.debug.message(1, "Catalog resolution attempted with null catalog; ignored");
return null; return null;
} }
@ -194,11 +167,11 @@ public class CatalogResolver implements EntityResolver, URIResolver {
try { try {
resolved = catalog.resolveSystem(systemId); resolved = catalog.resolveSystem(systemId);
} catch (MalformedURLException me) { } catch (MalformedURLException me) {
Debug.message(1, "Malformed URL exception trying to resolve", catalogManager.debug.message(1, "Malformed URL exception trying to resolve",
publicId); publicId);
resolved = null; resolved = null;
} catch (IOException ie) { } catch (IOException ie) {
Debug.message(1, "I/O exception trying to resolve", publicId); catalogManager.debug.message(1, "I/O exception trying to resolve", publicId);
resolved = null; resolved = null;
} }
} }
@ -208,18 +181,18 @@ public class CatalogResolver implements EntityResolver, URIResolver {
try { try {
resolved = catalog.resolvePublic(publicId, systemId); resolved = catalog.resolvePublic(publicId, systemId);
} catch (MalformedURLException me) { } catch (MalformedURLException me) {
Debug.message(1, "Malformed URL exception trying to resolve", catalogManager.debug.message(1, "Malformed URL exception trying to resolve",
publicId); publicId);
} catch (IOException ie) { } catch (IOException ie) {
Debug.message(1, "I/O exception trying to resolve", publicId); catalogManager.debug.message(1, "I/O exception trying to resolve", publicId);
} }
} }
if (resolved != null) { if (resolved != null) {
Debug.message(2, "Resolved public", publicId, resolved); catalogManager.debug.message(2, "Resolved public", publicId, resolved);
} }
} else { } else {
Debug.message(2, "Resolved system", systemId, resolved); catalogManager.debug.message(2, "Resolved system", systemId, resolved);
} }
return resolved; return resolved;
@ -277,7 +250,7 @@ public class CatalogResolver implements EntityResolver, URIResolver {
return iSource; return iSource;
} catch (Exception e) { } catch (Exception e) {
Debug.message(1, "Failed to create InputSource", resolved); catalogManager.debug.message(1, "Failed to create InputSource", resolved);
return null; return null;
} }
} }
@ -332,7 +305,7 @@ public class CatalogResolver implements EntityResolver, URIResolver {
} }
// if (!href.equals(result)) { // if (!href.equals(result)) {
Debug.message(2, "Resolved URI", href, result); catalogManager.debug.message(2, "Resolved URI", href, result);
// } // }
SAXSource source = new SAXSource(); SAXSource source = new SAXSource();

View File

@ -117,8 +117,11 @@ public class ResolvingParser
/** The underlying DTDHandler */ /** The underlying DTDHandler */
private DTDHandler dtdHandler = null; private DTDHandler dtdHandler = null;
/** The manager for the underlying resolver */
private CatalogManager catalogManager = CatalogManager.getStaticManager();
/** The underlying catalog resolver */ /** The underlying catalog resolver */
private CatalogResolver catalogResolver = new CatalogResolver(); private CatalogResolver catalogResolver = null;
/** A separate resolver for oasis-xml-pi catalogs */ /** A separate resolver for oasis-xml-pi catalogs */
private CatalogResolver piCatalogResolver = null; private CatalogResolver piCatalogResolver = null;
@ -134,6 +137,19 @@ public class ResolvingParser
/** Constructor */ /** Constructor */
public ResolvingParser() { public ResolvingParser() {
initParser();
}
/** Constructor */
public ResolvingParser(CatalogManager manager) {
catalogManager = manager;
initParser();
}
/** Initialize the parser */
private void initParser() {
catalogResolver = new CatalogResolver(catalogManager);
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(namespaceAware); spf.setNamespaceAware(namespaceAware);
spf.setValidating(validating); spf.setValidating(validating);
@ -293,12 +309,12 @@ public class ResolvingParser
} }
if (allowXMLCatalogPI) { if (allowXMLCatalogPI) {
if (CatalogManager.allowOasisXMLCatalogPI()) { if (catalogManager.allowOasisXMLCatalogPI()) {
Debug.message(4,"oasis-xml-catalog PI", pidata); catalogManager.debug.message(4,"oasis-xml-catalog PI", pidata);
if (catalog != null) { if (catalog != null) {
try { try {
Debug.message(4,"oasis-xml-catalog", catalog.toString()); catalogManager.debug.message(4,"oasis-xml-catalog", catalog.toString());
oasisXMLCatalogPI = true; oasisXMLCatalogPI = true;
if (piCatalogResolver == null) { if (piCatalogResolver == null) {
@ -307,17 +323,17 @@ public class ResolvingParser
piCatalogResolver.getCatalog().parseCatalog(catalog.toString()); piCatalogResolver.getCatalog().parseCatalog(catalog.toString());
} catch (Exception e) { } catch (Exception e) {
Debug.message(3, "Exception parsing oasis-xml-catalog: " catalogManager.debug.message(3, "Exception parsing oasis-xml-catalog: "
+ catalog.toString()); + catalog.toString());
} }
} else { } else {
Debug.message(3, "PI oasis-xml-catalog unparseable: " + pidata); catalogManager.debug.message(3, "PI oasis-xml-catalog unparseable: " + pidata);
} }
} else { } else {
Debug.message(4,"PI oasis-xml-catalog ignored: " + pidata); catalogManager.debug.message(4,"PI oasis-xml-catalog ignored: " + pidata);
} }
} else { } else {
Debug.message(3, "PI oasis-xml-catalog occurred in an invalid place: " catalogManager.debug.message(3, "PI oasis-xml-catalog occurred in an invalid place: "
+ pidata); + pidata);
} }
} else { } else {
@ -406,7 +422,7 @@ public class ResolvingParser
return iSource; return iSource;
} catch (Exception e) { } catch (Exception e) {
Debug.message(1, "Failed to create InputSource", resolved); catalogManager.debug.message(1, "Failed to create InputSource", resolved);
return null; return null;
} }
} else { } else {

View File

@ -96,8 +96,11 @@ public class ResolvingXMLFilter extends XMLFilterImpl {
*/ */
public static boolean suppressExplanation = false; public static boolean suppressExplanation = false;
/** The manager for the underlying resolver */
private CatalogManager catalogManager = CatalogManager.getStaticManager();
/** The underlying catalog resolver */ /** The underlying catalog resolver */
private CatalogResolver catalogResolver = new CatalogResolver(); private CatalogResolver catalogResolver = null;
/** A separate resolver for oasis-xml-pi catalogs */ /** A separate resolver for oasis-xml-pi catalogs */
private CatalogResolver piCatalogResolver = null; private CatalogResolver piCatalogResolver = null;
@ -114,11 +117,27 @@ public class ResolvingXMLFilter extends XMLFilterImpl {
/** Construct an empty XML Filter with no parent */ /** Construct an empty XML Filter with no parent */
public ResolvingXMLFilter() { public ResolvingXMLFilter() {
super(); super();
catalogResolver = new CatalogResolver(catalogManager);
} }
/** Construct an XML filter with the specified parent */ /** Construct an XML filter with the specified parent */
public ResolvingXMLFilter(XMLReader parent) { public ResolvingXMLFilter(XMLReader parent) {
super(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);
} }
/** /**
@ -216,7 +235,7 @@ public class ResolvingXMLFilter extends XMLFilterImpl {
return iSource; return iSource;
} catch (Exception e) { } catch (Exception e) {
Debug.message(1, "Failed to create InputSource", resolved); catalogManager.debug.message(1, "Failed to create InputSource", resolved);
return null; return null;
} }
} else { } else {
@ -297,12 +316,12 @@ public class ResolvingXMLFilter extends XMLFilterImpl {
} }
if (allowXMLCatalogPI) { if (allowXMLCatalogPI) {
if (CatalogManager.allowOasisXMLCatalogPI()) { if (catalogManager.allowOasisXMLCatalogPI()) {
Debug.message(4,"oasis-xml-catalog PI", pidata); catalogManager.debug.message(4,"oasis-xml-catalog PI", pidata);
if (catalog != null) { if (catalog != null) {
try { try {
Debug.message(4,"oasis-xml-catalog", catalog.toString()); catalogManager.debug.message(4,"oasis-xml-catalog", catalog.toString());
oasisXMLCatalogPI = true; oasisXMLCatalogPI = true;
if (piCatalogResolver == null) { if (piCatalogResolver == null) {
@ -311,17 +330,17 @@ public class ResolvingXMLFilter extends XMLFilterImpl {
piCatalogResolver.getCatalog().parseCatalog(catalog.toString()); piCatalogResolver.getCatalog().parseCatalog(catalog.toString());
} catch (Exception e) { } catch (Exception e) {
Debug.message(3, "Exception parsing oasis-xml-catalog: " catalogManager.debug.message(3, "Exception parsing oasis-xml-catalog: "
+ catalog.toString()); + catalog.toString());
} }
} else { } else {
Debug.message(3, "PI oasis-xml-catalog unparseable: " + pidata); catalogManager.debug.message(3, "PI oasis-xml-catalog unparseable: " + pidata);
} }
} else { } else {
Debug.message(4,"PI oasis-xml-catalog ignored: " + pidata); catalogManager.debug.message(4,"PI oasis-xml-catalog ignored: " + pidata);
} }
} else { } else {
Debug.message(3, "PI oasis-xml-catalog occurred in an invalid place: " catalogManager.debug.message(3, "PI oasis-xml-catalog occurred in an invalid place: "
+ pidata); + pidata);
} }
} else { } else {

View File

@ -91,7 +91,7 @@ public class ResolvingXMLReader extends ResolvingXMLFilter {
* *
* <p>In order to do its job, a ResolvingXMLReader must in fact be * <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 * a filter. So the only difference between this code and the filter
* code is that a zero-argument constructor builds a new reader.</p> * code is that the constructor builds a new reader.</p>
*/ */
public ResolvingXMLReader() { public ResolvingXMLReader() {
super(); super();
@ -103,5 +103,23 @@ public class ResolvingXMLReader extends ResolvingXMLFilter {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
/**
* <p>Construct a new reader from the JAXP factory</p>
*
* <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 = SAXParserFactory.newInstance();
try {
SAXParser parser = spf.newSAXParser();
setParent(parser.getXMLReader());
} catch (Exception ex) {
ex.printStackTrace();
}
}
} }