Catalogs are now governed by their CatalogManager instance (of which there is a single static instance for simplicity and backwards compatibility). Stefan Wachter provided a parse method that takes a URL; this allows catalogs to occur inside jar files.

git-svn-id: https://svn.apache.org/repos/asf/xml/commons/trunk@226008 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
ndw committed 2002-11-13 20:49:31 +00:00
1 parent dcec3ff81e
commit 60a2439163
1 file changed
+164 -73
+164 -73
View File
@@ -72,7 +72,6 @@ import java.util.Vector;
import java.net.URL;
import java.net.MalformedURLException;
import org.apache.xml.resolver.CatalogManager;
import org.apache.xml.resolver.helpers.Debug;
import org.apache.xml.resolver.helpers.PublicId;
import org.apache.xml.resolver.readers.CatalogReader;
import org.apache.xml.resolver.readers.SAXCatalogReader;
@@ -292,6 +291,9 @@ public class Catalog {
/** The default initial override setting. */
protected boolean default_override = true;
/** The catalog manager in use for this instance */
protected CatalogManager catalogManager = CatalogManager.getStaticManager();
/**
* <p>A vector of catalog files to be loaded.</p>
*
@@ -379,6 +381,44 @@ public class Catalog {
*/
protected Vector readerArr = new Vector();
/**
* <p>Constructs an empty Catalog.</p>
*
* <p>The constructor interrogates the relevant system properties
* using the default (static) CatalogManager
* and initializes the catalog data structures.</p>
*/
public Catalog() {
// nop;
}
/**
* <p>Constructs an empty Catalog with a specific CatalogManager.</p>
*
* <p>The constructor interrogates the relevant system properties
* using the specified Catalog Manager
* and initializes the catalog data structures.</p>
*/
public Catalog(CatalogManager manager) {
catalogManager = manager;
}
/**
* Return the CatalogManager used by this catalog.
*
*/
public CatalogManager getCatalogManager() {
return catalogManager;
}
/**
* Establish the CatalogManager used by this catalog.
*
*/
public void setCatalogManager(CatalogManager manager) {
catalogManager = manager;
}
/**
* <p>Setup readers.</p>
*/
@@ -472,6 +512,7 @@ public class Catalog {
*
* <p>This method constructs a new instance of the running Catalog
* class (which might be a subtype of org.apache.xml.resolver.Catalog).
* All new catalogs are managed by the same CatalogManager.
* </p>
*
* <p>N.B. All Catalog subtypes should call newCatalog() to construct
@@ -483,35 +524,27 @@ public class Catalog {
try {
Catalog c = (Catalog) (Class.forName(catalogClass).newInstance());
c.setCatalogManager(catalogManager);
copyReaders(c);
return c;
} catch (ClassNotFoundException cnfe) {
Debug.message(1, "Class Not Found Exception: " + catalogClass);
catalogManager.debug.message(1, "Class Not Found Exception: " + catalogClass);
} catch (IllegalAccessException iae) {
Debug.message(1, "Illegal Access Exception: " + catalogClass);
catalogManager.debug.message(1, "Illegal Access Exception: " + catalogClass);
} catch (InstantiationException ie) {
Debug.message(1, "Instantiation Exception: " + catalogClass);
catalogManager.debug.message(1, "Instantiation Exception: " + catalogClass);
} catch (ClassCastException cce) {
Debug.message(1, "Class Cast Exception: " + catalogClass);
catalogManager.debug.message(1, "Class Cast Exception: " + catalogClass);
} catch (Exception e) {
Debug.message(1, "Other Exception: " + catalogClass);
catalogManager.debug.message(1, "Other Exception: " + catalogClass);
}
Catalog c = new Catalog();
c.setCatalogManager(catalogManager);
copyReaders(c);
return c;
}
/**
* <p>Constructs an empty Catalog.</p>
*
* <p>The constructor interrogates the relevant system properties
* and initializes the catalog data structures.</p>
*/
public Catalog() {
default_override = CatalogManager.preferPublic();
}
/**
* <p>Returns the current base URI.</p>
*/
@@ -547,7 +580,7 @@ public class Catalog {
*/
public void loadSystemCatalogs()
throws MalformedURLException, IOException {
Vector catalogs = CatalogManager.catalogFiles();
Vector catalogs = catalogManager.getCatalogFiles();
if (catalogs != null) {
for (int count = 0; count < catalogs.size(); count++) {
catalogFiles.addElement(catalogs.elementAt(count));
@@ -585,7 +618,8 @@ public class Catalog {
public synchronized void parseCatalog(String fileName)
throws MalformedURLException, IOException {
Debug.message(4, "Parse catalog: " + fileName);
default_override = catalogManager.getPreferPublic();
catalogManager.debug.message(4, "Parse catalog: " + fileName);
// Put the file into the list of catalogs to process...
// In all cases except the case when initCatalog() is the
@@ -612,7 +646,8 @@ public class Catalog {
public synchronized void parseCatalog(String mimeType, InputStream is)
throws IOException, CatalogException {
Debug.message(4, "Parse " + mimeType + " catalog on input stream");
default_override = catalogManager.getPreferPublic();
catalogManager.debug.message(4, "Parse " + mimeType + " catalog on input stream");
CatalogReader reader = null;
@@ -623,7 +658,7 @@ public class Catalog {
if (reader == null) {
String msg = "No CatalogReader for MIME type: " + mimeType;
Debug.message(2, msg);
catalogManager.debug.message(2, msg);
throw new CatalogException(CatalogException.UNPARSEABLE, msg);
}
@@ -633,6 +668,62 @@ public class Catalog {
parsePendingCatalogs();
}
/**
* <p>Parse a catalog document, augmenting internal data structures</p>
*
* <p>This method supports catalog files stored in jar files: e.g.,
* jar:file:///path/to/filename.jar!/path/to/catalog.xml". That URI
* doesn't survive transmogrification through the URI processing that
* the parseCatalog(String) performs and passing it as an input stream
* doesn't set the base URI appropriately.</p>
*
* @param aUrl The URL of the catalog document to process
*
* @throws IOException Error reading catalog file.
* @author Stefan Wachter (2002-09-26)
*/
public synchronized void parseCatalog(URL aUrl) throws IOException {
catalogCwd = aUrl;
base = aUrl;
default_override = catalogManager.getPreferPublic();
catalogManager.debug.message(4, "Parse catalog: " + aUrl.toString());
DataInputStream inStream = null;
boolean parsed = false;
for (int count = 0; !parsed && count < readerArr.size(); count++) {
CatalogReader reader = (CatalogReader) readerArr.get(count);
try {
inStream = new DataInputStream(aUrl.openStream());
} catch (FileNotFoundException fnfe) {
// No catalog; give up!
break;
}
try {
reader.readCatalog(this, inStream);
parsed=true;
} catch (CatalogException ce) {
if (ce.getExceptionType() == CatalogException.PARSE_FAILED) {
// give up!
break;
} else {
// try again!
}
}
try {
inStream.close();
} catch (IOException e) {
//nop
}
}
if (parsed) parsePendingCatalogs();
}
/**
* <p>Parse all of the pending catalogs.</p>
*
@@ -752,7 +843,7 @@ public class Catalog {
catalogCwd = new URL("file:" + userdir + "/basename");
} catch (MalformedURLException e) {
String userdir = fixSlashes(System.getProperty("user.dir"));
Debug.message(1, "Malformed URL on cwd", userdir);
catalogManager.debug.message(1, "Malformed URL on cwd", userdir);
catalogCwd = null;
}
@@ -763,14 +854,14 @@ public class Catalog {
try {
base = new URL("file:" + fixSlashes(fileName));
} catch (MalformedURLException e2) {
Debug.message(1, "Malformed URL on catalog filename",
catalogManager.debug.message(1, "Malformed URL on catalog filename",
fixSlashes(fileName));
base = null;
}
}
Debug.message(2, "Loading catalog", fileName);
Debug.message(4, "Default BASE", base.toString());
catalogManager.debug.message(2, "Loading catalog", fileName);
catalogManager.debug.message(4, "Default BASE", base.toString());
fileName = base.toString();
@@ -811,9 +902,9 @@ public class Catalog {
if (!parsed) {
if (notFound) {
Debug.message(3, "Catalog does not exist", fileName);
catalogManager.debug.message(3, "Catalog does not exist", fileName);
} else {
Debug.message(1, "Failed to parse catalog", fileName);
catalogManager.debug.message(1, "Failed to parse catalog", fileName);
}
}
}
@@ -835,8 +926,8 @@ public class Catalog {
String value = entry.getEntryArg(0);
URL newbase = null;
Debug.message(5, "BASE CUR", base.toString());
Debug.message(4, "BASE STR", value);
catalogManager.debug.message(5, "BASE CUR", base.toString());
catalogManager.debug.message(4, "BASE STR", value);
try {
value = fixSlashes(value);
@@ -845,7 +936,7 @@ public class Catalog {
try {
newbase = new URL("file:" + value);
} catch (MalformedURLException e2) {
Debug.message(1, "Malformed URL on base", value);
catalogManager.debug.message(1, "Malformed URL on base", value);
newbase = null;
}
}
@@ -854,11 +945,11 @@ public class Catalog {
base = newbase;
}
Debug.message(5, "BASE NEW", base.toString());
catalogManager.debug.message(5, "BASE NEW", base.toString());
} else if (type == CATALOG) {
String fsi = makeAbsolute(entry.getEntryArg(0));
Debug.message(4, "CATALOG", fsi);
catalogManager.debug.message(4, "CATALOG", fsi);
localCatalogFiles.addElement(fsi);
} else if (type == PUBLIC) {
@@ -868,7 +959,7 @@ public class Catalog {
entry.setEntryArg(0, publicid);
entry.setEntryArg(1, systemid);
Debug.message(4, "PUBLIC", publicid, systemid);
catalogManager.debug.message(4, "PUBLIC", publicid, systemid);
catalogEntries.addElement(entry);
} else if (type == SYSTEM) {
@@ -877,7 +968,7 @@ public class Catalog {
entry.setEntryArg(1, fsi);
Debug.message(4, "SYSTEM", systemid, fsi);
catalogManager.debug.message(4, "SYSTEM", systemid, fsi);
catalogEntries.addElement(entry);
} else if (type == URI) {
@@ -886,18 +977,18 @@ public class Catalog {
entry.setEntryArg(1, altURI);
Debug.message(4, "URI", uri, altURI);
catalogManager.debug.message(4, "URI", uri, altURI);
catalogEntries.addElement(entry);
} else if (type == DOCUMENT) {
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0)));
entry.setEntryArg(0, fsi);
Debug.message(4, "DOCUMENT", fsi);
catalogManager.debug.message(4, "DOCUMENT", fsi);
catalogEntries.addElement(entry);
} else if (type == OVERRIDE) {
Debug.message(4, "OVERRIDE", entry.getEntryArg(0));
catalogManager.debug.message(4, "OVERRIDE", entry.getEntryArg(0));
catalogEntries.addElement(entry);
} else if (type == SGMLDECL) {
@@ -905,7 +996,7 @@ public class Catalog {
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0)));
entry.setEntryArg(0, fsi);
Debug.message(4, "SGMLDECL", fsi);
catalogManager.debug.message(4, "SGMLDECL", fsi);
catalogEntries.addElement(entry);
} else if (type == DELEGATE_PUBLIC) {
@@ -915,7 +1006,7 @@ public class Catalog {
entry.setEntryArg(0, ppi);
entry.setEntryArg(1, fsi);
Debug.message(4, "DELEGATE_PUBLIC", ppi, fsi);
catalogManager.debug.message(4, "DELEGATE_PUBLIC", ppi, fsi);
addDelegate(entry);
} else if (type == DELEGATE_SYSTEM) {
@@ -925,7 +1016,7 @@ public class Catalog {
entry.setEntryArg(0, psi);
entry.setEntryArg(1, fsi);
Debug.message(4, "DELEGATE_SYSTEM", psi, fsi);
catalogManager.debug.message(4, "DELEGATE_SYSTEM", psi, fsi);
addDelegate(entry);
} else if (type == DELEGATE_URI) {
@@ -935,7 +1026,7 @@ public class Catalog {
entry.setEntryArg(0, pui);
entry.setEntryArg(1, fsi);
Debug.message(4, "DELEGATE_URI", pui, fsi);
catalogManager.debug.message(4, "DELEGATE_URI", pui, fsi);
addDelegate(entry);
} else if (type == REWRITE_SYSTEM) {
@@ -945,7 +1036,7 @@ public class Catalog {
entry.setEntryArg(0, psi);
entry.setEntryArg(1, rpx);
Debug.message(4, "REWRITE_SYSTEM", psi, rpx);
catalogManager.debug.message(4, "REWRITE_SYSTEM", psi, rpx);
catalogEntries.addElement(entry);
} else if (type == REWRITE_URI) {
@@ -955,14 +1046,14 @@ public class Catalog {
entry.setEntryArg(0, pui);
entry.setEntryArg(1, upx);
Debug.message(4, "REWRITE_URI", pui, upx);
catalogManager.debug.message(4, "REWRITE_URI", pui, upx);
catalogEntries.addElement(entry);
} else if (type == DOCTYPE) {
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
entry.setEntryArg(1, fsi);
Debug.message(4, "DOCTYPE", entry.getEntryArg(0), fsi);
catalogManager.debug.message(4, "DOCTYPE", entry.getEntryArg(0), fsi);
catalogEntries.addElement(entry);
} else if (type == DTDDECL) {
@@ -972,14 +1063,14 @@ public class Catalog {
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
entry.setEntryArg(1, fsi);
Debug.message(4, "DTDDECL", fpi, fsi);
catalogManager.debug.message(4, "DTDDECL", fpi, fsi);
catalogEntries.addElement(entry);
} else if (type == ENTITY) {
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
entry.setEntryArg(1, fsi);
Debug.message(4, "ENTITY", entry.getEntryArg(0), fsi);
catalogManager.debug.message(4, "ENTITY", entry.getEntryArg(0), fsi);
catalogEntries.addElement(entry);
} else if (type == LINKTYPE) {
@@ -987,14 +1078,14 @@ public class Catalog {
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
entry.setEntryArg(1, fsi);
Debug.message(4, "LINKTYPE", entry.getEntryArg(0), fsi);
catalogManager.debug.message(4, "LINKTYPE", entry.getEntryArg(0), fsi);
catalogEntries.addElement(entry);
} else if (type == NOTATION) {
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
entry.setEntryArg(1, fsi);
Debug.message(4, "NOTATION", entry.getEntryArg(0), fsi);
catalogManager.debug.message(4, "NOTATION", entry.getEntryArg(0), fsi);
catalogEntries.addElement(entry);
} else {
@@ -1011,7 +1102,7 @@ public class Catalog {
public void unknownEntry(Vector strings) {
if (strings != null && strings.size() > 0) {
String keyword = (String) strings.elementAt(0);
Debug.message(2, "Unrecognized token parsing catalog", keyword);
catalogManager.debug.message(2, "Unrecognized token parsing catalog", keyword);
}
}
@@ -1099,7 +1190,7 @@ public class Catalog {
throws MalformedURLException, IOException {
String resolved = null;
Debug.message(3, "resolveDoctype("
catalogManager.debug.message(3, "resolveDoctype("
+entityName+","+publicId+","+systemId+")");
systemId = normalizeURI(systemId);
@@ -1111,7 +1202,7 @@ public class Catalog {
if (systemId != null && systemId.startsWith("urn:publicid:")) {
systemId = PublicId.decodeURN(systemId);
if (publicId != null && !publicId.equals(systemId)) {
Debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
systemId = null;
} else {
publicId = systemId;
@@ -1176,7 +1267,7 @@ public class Catalog {
throws MalformedURLException, IOException {
// If there's a DOCUMENT entry, return it
Debug.message(3, "resolveDocument");
catalogManager.debug.message(3, "resolveDocument");
Enumeration enum = catalogEntries.elements();
while (enum.hasMoreElements()) {
@@ -1212,7 +1303,7 @@ public class Catalog {
throws MalformedURLException, IOException {
String resolved = null;
Debug.message(3, "resolveEntity("
catalogManager.debug.message(3, "resolveEntity("
+entityName+","+publicId+","+systemId+")");
systemId = normalizeURI(systemId);
@@ -1224,7 +1315,7 @@ public class Catalog {
if (systemId != null && systemId.startsWith("urn:publicid:")) {
systemId = PublicId.decodeURN(systemId);
if (publicId != null && !publicId.equals(systemId)) {
Debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
systemId = null;
} else {
publicId = systemId;
@@ -1298,7 +1389,7 @@ public class Catalog {
throws MalformedURLException, IOException {
String resolved = null;
Debug.message(3, "resolveNotation("
catalogManager.debug.message(3, "resolveNotation("
+notationName+","+publicId+","+systemId+")");
systemId = normalizeURI(systemId);
@@ -1310,7 +1401,7 @@ public class Catalog {
if (systemId != null && systemId.startsWith("urn:publicid:")) {
systemId = PublicId.decodeURN(systemId);
if (publicId != null && !publicId.equals(systemId)) {
Debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
systemId = null;
} else {
publicId = systemId;
@@ -1388,7 +1479,7 @@ public class Catalog {
public String resolvePublic(String publicId, String systemId)
throws MalformedURLException, IOException {
Debug.message(3, "resolvePublic("+publicId+","+systemId+")");
catalogManager.debug.message(3, "resolvePublic("+publicId+","+systemId+")");
systemId = normalizeURI(systemId);
@@ -1399,7 +1490,7 @@ public class Catalog {
if (systemId != null && systemId.startsWith("urn:publicid:")) {
systemId = PublicId.decodeURN(systemId);
if (publicId != null && !publicId.equals(systemId)) {
Debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
systemId = null;
} else {
publicId = systemId;
@@ -1540,11 +1631,11 @@ public class Catalog {
if (delCats.size() > 0) {
Enumeration enumCats = delCats.elements();
if (Debug.getDebug() > 1) {
Debug.message(2, "Switching to delegated catalog(s):");
if (catalogManager.debug.getDebug() > 1) {
catalogManager.debug.message(2, "Switching to delegated catalog(s):");
while (enumCats.hasMoreElements()) {
String delegatedCatalog = (String) enumCats.nextElement();
Debug.message(2, "\t" + delegatedCatalog);
catalogManager.debug.message(2, "\t" + delegatedCatalog);
}
}
@@ -1584,7 +1675,7 @@ public class Catalog {
public String resolveSystem(String systemId)
throws MalformedURLException, IOException {
Debug.message(3, "resolveSystem("+systemId+")");
catalogManager.debug.message(3, "resolveSystem("+systemId+")");
systemId = normalizeURI(systemId);
@@ -1681,11 +1772,11 @@ public class Catalog {
if (delCats.size() > 0) {
Enumeration enumCats = delCats.elements();
if (Debug.getDebug() > 1) {
Debug.message(2, "Switching to delegated catalog(s):");
if (catalogManager.debug.getDebug() > 1) {
catalogManager.debug.message(2, "Switching to delegated catalog(s):");
while (enumCats.hasMoreElements()) {
String delegatedCatalog = (String) enumCats.nextElement();
Debug.message(2, "\t" + delegatedCatalog);
catalogManager.debug.message(2, "\t" + delegatedCatalog);
}
}
@@ -1722,7 +1813,7 @@ public class Catalog {
public String resolveURI(String uri)
throws MalformedURLException, IOException {
Debug.message(3, "resolveURI("+uri+")");
catalogManager.debug.message(3, "resolveURI("+uri+")");
uri = normalizeURI(uri);
@@ -1814,11 +1905,11 @@ public class Catalog {
if (delCats.size() > 0) {
Enumeration enumCats = delCats.elements();
if (Debug.getDebug() > 1) {
Debug.message(2, "Switching to delegated catalog(s):");
if (catalogManager.debug.getDebug() > 1) {
catalogManager.debug.message(2, "Switching to delegated catalog(s):");
while (enumCats.hasMoreElements()) {
String delegatedCatalog = (String) enumCats.nextElement();
Debug.message(2, "\t" + delegatedCatalog);
catalogManager.debug.message(2, "\t" + delegatedCatalog);
}
}
@@ -1883,12 +1974,12 @@ public class Catalog {
try {
c.parseCatalog(catfile);
} catch (MalformedURLException mue) {
Debug.message(1, "Malformed Catalog URL", catfile);
catalogManager.debug.message(1, "Malformed Catalog URL", catfile);
} catch (FileNotFoundException fnfe) {
Debug.message(1, "Failed to load catalog, file not found",
catalogManager.debug.message(1, "Failed to load catalog, file not found",
catfile);
} catch (IOException ioe) {
Debug.message(1, "Failed to load catalog, I/O error", catfile);
catalogManager.debug.message(1, "Failed to load catalog, I/O error", catfile);
}
catalogs.setElementAt(c, catPos);
@@ -1957,7 +2048,7 @@ public class Catalog {
try {
local = new URL(base, sysid);
} catch (MalformedURLException e) {
Debug.message(1, "Malformed URL on system identifier", sysid);
catalogManager.debug.message(1, "Malformed URL on system identifier", sysid);
}
if (local != null) {
@@ -1985,7 +2076,7 @@ public class Catalog {
bytes = uriref.getBytes("UTF-8");
} catch (UnsupportedEncodingException uee) {
// this can't happen
Debug.message(1, "UTF-8 is an unsupported encoding!?");
catalogManager.debug.message(1, "UTF-8 is an unsupported encoding!?");
return uriref;
}