8151162: Public entries not searched when prefer='system'

Reviewed-by: lancea
This commit is contained in:
Joe Wang 2016-04-12 14:44:23 -07:00
parent 70dbeeb6df
commit 470dc96724
8 changed files with 165 additions and 8 deletions

View file

@ -52,8 +52,8 @@ final class CatalogResolverImpl implements CatalogResolver {
@Override
public InputSource resolveEntity(String publicId, String systemId) {
//Normalize publicId and systemId
systemId = Normalizer.normalizeURI(systemId);
publicId = Normalizer.normalizePublicId(Normalizer.decodeURN(publicId));
systemId = Normalizer.normalizeURI(Util.getNotNullOrEmpty(systemId));
publicId = Normalizer.normalizePublicId(Normalizer.decodeURN(Util.getNotNullOrEmpty(publicId)));
//check whether systemId is an urn
if (systemId != null && systemId.startsWith("urn:publicid:")) {
@ -87,7 +87,17 @@ final class CatalogResolverImpl implements CatalogResolver {
}
/**
* Resolves the publicId or systemId to one specified in the catalog.
* Resolves the publicId or systemId using public or system entries in the catalog.
*
* The resolution follows the following rules determined by the prefer setting:
*
* prefer "system": attempts to resolve with a system entry;
* attempts to resolve with a public entry when only
* publicId is specified.
*
* prefer "public": attempts to resolve with a system entry;
* attempts to resolve with a public entry if no matching
* system entry is found.
* @param catalog the catalog
* @param publicId the publicId
* @param systemId the systemId
@ -99,9 +109,14 @@ final class CatalogResolverImpl implements CatalogResolver {
//search the current catalog
catalog.reset();
if (systemId != null) {
/*
If a system identifier is specified, it is used no matter how
prefer is set.
*/
resolvedSystemId = catalog.matchSystem(systemId);
}
if (resolvedSystemId == null) {
if (resolvedSystemId == null && publicId != null) {
resolvedSystemId = catalog.matchPublic(publicId);
}

View file

@ -60,6 +60,9 @@ final class CatalogUriResolverImpl implements CatalogUriResolver {
@Override
public Source resolve(String href, String base) {
href = Util.getNotNullOrEmpty(href);
base = Util.getNotNullOrEmpty(base);
if (href == null) return null;
CatalogImpl c = (CatalogImpl)catalog;

View file

@ -82,6 +82,9 @@ class GroupEntry extends BaseEntry {
//The length of the longest match of a suffix type
int longestSuffixMatch = 0;
//Indicate whether a system entry has been searched
boolean systemEntrySearched = false;
/**
* PreferType represents possible values of the prefer property
*/
@ -156,6 +159,7 @@ class GroupEntry extends BaseEntry {
longestRewriteMatch = 0;
suffixMatch = null;
longestSuffixMatch = 0;
systemEntrySearched = false;
}
/**
* Constructs a group entry.
@ -212,6 +216,7 @@ class GroupEntry extends BaseEntry {
* @return An URI string if a mapping is found, or null otherwise.
*/
public String matchSystem(String systemId) {
systemEntrySearched = true;
String match = null;
for (BaseEntry entry : entries) {
switch (entry.type) {
@ -277,11 +282,13 @@ class GroupEntry extends BaseEntry {
* @return An URI string if a mapping is found, or null otherwise.
*/
public String matchPublic(String publicId) {
//as the specification required
if (!isPreferPublic) {
/*
When both public and system identifiers are specified, and prefer is
not public (that is, system), only system entry will be used.
*/
if (!isPreferPublic && systemEntrySearched) {
return null;
}
//match public entries
String match = null;
for (BaseEntry entry : entries) {

View file

@ -122,4 +122,25 @@ class Util {
}
return null;
}
/**
* Checks whether the specified string is null or empty, returns the original
* string with leading and trailing spaces removed if not.
* @param test the string to be tested
* @return the original string with leading and trailing spaces removed,
* or null if it is null or empty
*
*/
static String getNotNullOrEmpty(String test) {
if (test == null) {
return test;
} else {
String temp = test.trim();
if (temp.length() == 0) {
return null;
} else {
return temp;
}
}
}
}