8156845: Uri is getting incorrectly unwrapped

Reviewed-by: lancea
This commit is contained in:
Joe Wang 2016-05-17 14:14:15 -07:00
parent a3cdceaa83
commit ec0e48670f
8 changed files with 131 additions and 68 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -56,7 +56,7 @@ final class CatalogResolverImpl implements CatalogResolver {
publicId = Normalizer.normalizePublicId(Normalizer.decodeURN(Util.getNotNullOrEmpty(publicId)));
//check whether systemId is an urn
if (systemId != null && systemId.startsWith("urn:publicid:")) {
if (systemId != null && systemId.startsWith(Util.URN)) {
systemId = Normalizer.decodeURN(systemId);
if (publicId != null && !publicId.equals(systemId)) {
systemId = null;
@ -67,7 +67,7 @@ final class CatalogResolverImpl implements CatalogResolver {
}
CatalogImpl c = (CatalogImpl)catalog;
String resolvedSystemId = resolve(c, publicId, systemId);
String resolvedSystemId = Util.resolve(c, publicId, systemId);
if (resolvedSystemId != null) {
return new InputSource(resolvedSystemId);
@ -86,55 +86,4 @@ final class CatalogResolverImpl implements CatalogResolver {
return null;
}
/**
* 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
* @return the resolved systemId if a match is found, null otherwise
*/
String resolve(CatalogImpl catalog, String publicId, String systemId) {
String resolvedSystemId = null;
//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 && publicId != null) {
resolvedSystemId = catalog.matchPublic(publicId);
}
//mark the catalog as having been searched before trying alternatives
catalog.markAsSearched();
//search alternative catalogs
if (resolvedSystemId == null) {
Iterator<Catalog> iter = catalog.catalogs().iterator();
while (iter.hasNext()) {
resolvedSystemId = resolve((CatalogImpl)iter.next(), publicId, systemId);
if (resolvedSystemId != null) {
break;
}
}
}
return resolvedSystemId;
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -65,18 +65,30 @@ final class CatalogUriResolverImpl implements CatalogUriResolver {
if (href == null) return null;
String result = null;
CatalogImpl c = (CatalogImpl)catalog;
String uri = Normalizer.normalizeURI(href);
String result;
//remove fragment if any.
int hashPos = uri.indexOf("#");
if (hashPos >= 0) {
uri = uri.substring(0, hashPos);
//check whether uri is an urn
if (uri != null && uri.startsWith(Util.URN)) {
String publicId = Normalizer.decodeURN(uri);
if (publicId != null) {
result = Util.resolve(c, publicId, null);
}
}
//search the current catalog
result = resolve(c, uri);
//if no match with a public id, continue search for an URI
if (result == null) {
//remove fragment if any.
int hashPos = uri.indexOf("#");
if (hashPos >= 0) {
uri = uri.substring(0, hashPos);
}
//search the current catalog
result = resolve(c, uri);
}
//Report error or return the URI as is when no match is found
if (result == null) {
GroupEntry.ResolveType resolveType = c.getResolve();
switch (resolveType) {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -298,6 +298,9 @@ class GroupEntry extends BaseEntry {
case PUBLIC:
match = ((PublicEntry) entry).match(publicId);
break;
case URI:
match = ((UriEntry) entry).match(publicId);
break;
case GROUP:
match = ((GroupEntry) entry).matchPublic(publicId);
break;

View file

@ -100,7 +100,7 @@ class Normalizer {
} catch (UnsupportedEncodingException ex) {
CatalogMessages.reportRunTimeError(CatalogMessages.ERR_OTHER, ex);
}
return "urn:publicid:" + urn;
return Util.URN + urn;
}
/**
@ -114,7 +114,7 @@ class Normalizer {
static String decodeURN(String urn) {
String publicId;
if (urn != null && urn.startsWith("urn:publicid:")) {
if (urn != null && urn.startsWith(Util.URN)) {
publicId = urn.substring(13);
} else {
return urn;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -52,7 +52,11 @@ final class UriEntry extends BaseEntry {
*/
public void setName(String name) {
CatalogMessages.reportNPEOnNull("name", name);
this.name = Normalizer.normalizeURI(name);
if (name.startsWith(Util.PUBLICID_PREFIX) || name.startsWith(Util.PUBLICID_PREFIX_ALT)) {
this.name = Normalizer.normalizePublicId(name);
} else {
this.name = Normalizer.normalizeURI(name);
}
}
/**
@ -72,6 +76,7 @@ final class UriEntry extends BaseEntry {
public String getName() {
return name;
}
/**
* Get the uri attribute.
* @return The uri attribute value.

View file

@ -31,6 +31,7 @@ import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import jdk.xml.internal.SecuritySupport;
/**
@ -38,6 +39,61 @@ import jdk.xml.internal.SecuritySupport;
* @since 9
*/
class Util {
final static String URN = "urn:publicid:";
final static String PUBLICID_PREFIX = "-//";
final static String PUBLICID_PREFIX_ALT = "+//";
/**
* Finds an entry in the catalog that matches with the publicId or systemId.
*
* 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
* @return the resolved systemId if a match is found, null otherwise
*/
static String resolve(CatalogImpl catalog, String publicId, String systemId) {
String resolvedSystemId = null;
//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 && publicId != null) {
resolvedSystemId = catalog.matchPublic(publicId);
}
//mark the catalog as having been searched before trying alternatives
catalog.markAsSearched();
//search alternative catalogs
if (resolvedSystemId == null) {
Iterator<Catalog> iter = catalog.catalogs().iterator();
while (iter.hasNext()) {
resolvedSystemId = resolve((CatalogImpl)iter.next(), publicId, systemId);
if (resolvedSystemId != null) {
break;
}
}
}
return resolvedSystemId;
}
/**
* Resolves the specified file path to an absolute systemId. If it is