Support uriSuffix and systemIdSuffix per XML Catalogs V1.1

git-svn-id: https://svn.apache.org/repos/asf/xml/commons/trunk@226200 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
ndw committed 2005-04-13 14:16:13 +00:00
1 parent 743cf92ca8
commit 8b61e857cb
5 files changed
+139 -5

No files matched your search

+82 -1
View File
@@ -130,6 +130,11 @@ import org.apache.xml.resolver.helpers.FileURL;
* specifies alternate prefix for a system identifier.</li>
* <li><b>REWRITE_URI</b>
* specifies alternate prefix for a URI.</li>
* <li><b>SYSTEM_SUFFIX</b>
* maps any system identifier that ends with a particular suffix to another
* system identifier.</li>
* <li><b>URI_SUFFIX</b>
* maps any URI that ends with a particular suffix to another URI.</li>
* <li><b>DOCTYPE</b>
* associates the names of root elements with URIs. (In other words, an XML
* processor might infer the doctype of an XML document that does not include
@@ -275,6 +280,10 @@ public class Catalog {
/** The REWRITE_URI Catalog Entry type. */
public static final int REWRITE_URI = CatalogEntry.addEntryType("REWRITE_URI", 2);
/** The SYSTEM_SUFFIX Catalog Entry type. */
public static final int SYSTEM_SUFFIX = CatalogEntry.addEntryType("SYSTEM_SUFFIX", 2);
/** The URI_SUFFIX Catalog Entry type. */
public static final int URI_SUFFIX = CatalogEntry.addEntryType("URI_SUFFIX", 2);
/**
* The base URI for relative system identifiers in the catalog.
@@ -1054,6 +1063,26 @@ public class Catalog {
catalogManager.debug.message(4, "REWRITE_URI", pui, upx);
catalogEntries.addElement(entry);
} else if (type == SYSTEM_SUFFIX) {
String pui = normalizeURI(entry.getEntryArg(0));
String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
entry.setEntryArg(0, pui);
entry.setEntryArg(1, upx);
catalogManager.debug.message(4, "SYSTEM_SUFFIX", pui, upx);
catalogEntries.addElement(entry);
} else if (type == URI_SUFFIX) {
String pui = normalizeURI(entry.getEntryArg(0));
String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
entry.setEntryArg(0, pui);
entry.setEntryArg(1, upx);
catalogManager.debug.message(4, "URI_SUFFIX", pui, upx);
catalogEntries.addElement(entry);
} else if (type == DOCTYPE) {
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
@@ -1758,6 +1787,32 @@ public class Catalog {
return prefix + systemId.substring(startString.length());
}
// If there's a SYSTEM_SUFFIX entry in this catalog, use it
en = catalogEntries.elements();
String suffixString = null;
String suffixURI = null;
while (en.hasMoreElements()) {
CatalogEntry e = (CatalogEntry) en.nextElement();
if (e.getEntryType() == SYSTEM_SUFFIX) {
String p = (String) e.getEntryArg(0);
if (p.length() <= systemId.length()
&& systemId.endsWith(p)) {
// Is this the longest prefix?
if (suffixString == null
|| p.length() > suffixString.length()) {
suffixString = p;
suffixURI = e.getEntryArg(1);
}
}
}
}
if (suffixURI != null) {
// return the systemId for the suffix
return suffixURI;
}
// If there's a DELEGATE_SYSTEM entry in this catalog, use it
en = catalogEntries.elements();
Vector delCats = new Vector();
@@ -1886,10 +1941,36 @@ public class Catalog {
}
if (prefix != null) {
// return the systemId with the new prefix
// return the uri with the new prefix
return prefix + uri.substring(startString.length());
}
// If there's a URI_SUFFIX entry in this catalog, use it
en = catalogEntries.elements();
String suffixString = null;
String suffixURI = null;
while (en.hasMoreElements()) {
CatalogEntry e = (CatalogEntry) en.nextElement();
if (e.getEntryType() == URI_SUFFIX) {
String p = (String) e.getEntryArg(0);
if (p.length() <= uri.length()
&& uri.endsWith(p)) {
// Is this the longest prefix?
if (suffixString == null
|| p.length() > suffixString.length()) {
suffixString = p;
suffixURI = e.getEntryArg(1);
}
}
}
}
if (suffixURI != null) {
// return the uri for the suffix
return suffixURI;
}
// If there's a DELEGATE_URI entry in this catalog, use it
en = catalogEntries.elements();
Vector delCats = new Vector();
@@ -193,7 +193,7 @@ public class CatalogManager {
/** Current catalog files list. */
private String catalogFiles = null;
/** Did the catalgoFiles come from the properties file? */
/** Did the catalogFiles come from the properties file? */
private boolean fromPropertiesFile = false;
/** Default verbosity level if there is no property setting for it. */
@@ -284,6 +284,16 @@ public class OASISXMLCatalogReader extends SAXCatalogReader implements SAXCatalo
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;
@@ -294,6 +304,16 @@ public class OASISXMLCatalogReader extends SAXCatalogReader implements SAXCatalo
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;
@@ -122,4 +122,31 @@ public class BasicResolverTests extends TestCase {
assertTrue(local.equals(resolved));
}
public void testSystemSuffix() {
String resolved = null;
String local = "file:/usr/local/xml/docbook/4.4/docbookx.dtd";
try {
resolved = catalog.resolveSystem("http://any/random/path/to/docbookx.dtd");
} catch (Exception e) {
e.printStackTrace();
}
assertTrue(local.equals(resolved));
}
public void testUriSuffix() {
String resolved = null;
String local = "file:/usr/local/xsl/docbook/html/docbook.xsl";
try {
resolved = catalog.resolveURI("http://any/path/to/html/docbook.xsl");
} catch (Exception e) {
e.printStackTrace();
}
assertTrue(local.equals(resolved));
}
}
@@ -8,13 +8,13 @@
uri="file:///local/system-resolver.dtd"/>
<rewriteSystem systemIdStartString="http://rewrite.example.org/"
rewritePrefix="/short/"/>
rewritePrefix="file:///short/"/>
<rewriteSystem systemIdStartString="http://rewrite.example.org/longest/"
rewritePrefix="/longest/"/>
rewritePrefix="file:///longest/"/>
<rewriteSystem systemIdStartString="http://rewrite.example.org/long"
rewritePrefix="/long/"/>
rewritePrefix="file:///long/"/>
<uri name="http://example.org/someURI"
uri="file:///local/uri"/>
@@ -29,4 +29,10 @@
<nextCatalog catalog="tr9401.xml"/>
<systemSuffix systemIdSuffix="/docbookx.dtd"
uri="file:///usr/local/xml/docbook/4.4/docbookx.dtd"/>
<uriSuffix uriSuffix="/html/docbook.xsl"
uri="file:///usr/local/xsl/docbook/html/docbook.xsl"/>
</catalog>