Return-Path: Delivered-To: apmail-xml-commons-cvs-archive@xml.apache.org Received: (qmail 61897 invoked by uid 500); 13 Nov 2002 20:49:36 -0000 Mailing-List: contact commons-cvs-help@xml.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: list-post: Delivered-To: mailing list commons-cvs@xml.apache.org Received: (qmail 61888 invoked from network); 13 Nov 2002 20:49:36 -0000 Received: from icarus.apache.org (63.251.56.143) by daedalus.apache.org with SMTP; 13 Nov 2002 20:49:36 -0000 Received: (qmail 83529 invoked by uid 1420); 13 Nov 2002 20:49:34 -0000 Date: 13 Nov 2002 20:49:33 -0000 Message-ID: <20021113204933.83519.qmail@icarus.apache.org> From: ndw@apache.org To: xml-commons-cvs@apache.org Subject: cvs commit: xml-commons/java/src/org/apache/xml/resolver Catalog.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N ndw 2002/11/13 12:49:31 Modified: java/src/org/apache/xml/resolver Catalog.java Log: 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. Revision Changes Path 1.3 +164 -73 xml-commons/java/src/org/apache/xml/resolver/Catalog.java Index: Catalog.java =================================================================== RCS file: /home/cvs/xml-commons/java/src/org/apache/xml/resolver/Catalog.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- Catalog.java 14 Jun 2002 14:37:20 -0000 1.2 +++ Catalog.java 13 Nov 2002 20:49:31 -0000 1.3 @@ -72,7 +72,6 @@ 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 @@ /** The default initial override setting. */ protected boolean default_override = true; + /** The catalog manager in use for this instance */ + protected CatalogManager catalogManager = CatalogManager.getStaticManager(); + /** *

A vector of catalog files to be loaded.

* @@ -380,6 +382,44 @@ protected Vector readerArr = new Vector(); /** + *

Constructs an empty Catalog.

+ * + *

The constructor interrogates the relevant system properties + * using the default (static) CatalogManager + * and initializes the catalog data structures.

+ */ + public Catalog() { + // nop; + } + + /** + *

Constructs an empty Catalog with a specific CatalogManager.

+ * + *

The constructor interrogates the relevant system properties + * using the specified Catalog Manager + * and initializes the catalog data structures.

+ */ + 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; + } + + /** *

Setup readers.

*/ public void setupReaders() { @@ -472,6 +512,7 @@ * *

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. *

* *

N.B. All Catalog subtypes should call newCatalog() to construct @@ -483,36 +524,28 @@ 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; } /** - *

Constructs an empty Catalog.

- * - *

The constructor interrogates the relevant system properties - * and initializes the catalog data structures.

- */ - public Catalog() { - default_override = CatalogManager.preferPublic(); - } - - /** *

Returns the current base URI.

*/ public String getCurrentBase() { @@ -547,7 +580,7 @@ */ 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 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 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 @@ 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); } @@ -634,6 +669,62 @@ } /** + *

Parse a catalog document, augmenting internal data structures

+ * + *

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.

+ * + * @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(); + } + + /** *

Parse all of the pending catalogs.

* *

Catalogs may refer to other catalogs, this method parses @@ -752,7 +843,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 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 @@ 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 @@ 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 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 @@ 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 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 @@ 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 @@ 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 @@ 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 @@ 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; }