author | joehw |
Tue, 12 Apr 2016 14:44:23 -0700 | |
changeset 37382 | c7d898d8da12 |
parent 33542 | 9f0eef87e8c1 |
child 38497 | 06b1977d0f4f |
permissions | -rw-r--r-- |
33542 | 1 |
/* |
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package javax.xml.catalog; |
|
26 |
||
27 |
import java.io.StringReader; |
|
28 |
import java.util.Iterator; |
|
29 |
import org.xml.sax.InputSource; |
|
30 |
||
31 |
/** |
|
32 |
* A SAX EntityResolver/JAXP URIResolver that uses catalogs. |
|
33 |
* |
|
34 |
* <p> |
|
35 |
* This class implements both a SAX EntityResolver and a JAXP URIResolver. |
|
36 |
* |
|
37 |
* |
|
38 |
* @since 9 |
|
39 |
*/ |
|
40 |
final class CatalogResolverImpl implements CatalogResolver { |
|
41 |
Catalog catalog; |
|
42 |
||
43 |
/** |
|
44 |
* Construct an instance of the CatalogResolver from a Catalog. |
|
45 |
* |
|
46 |
* @param catalog A Catalog. |
|
47 |
*/ |
|
48 |
public CatalogResolverImpl(Catalog catalog) { |
|
49 |
this.catalog = catalog; |
|
50 |
} |
|
51 |
||
52 |
@Override |
|
53 |
public InputSource resolveEntity(String publicId, String systemId) { |
|
54 |
//Normalize publicId and systemId |
|
37382
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
55 |
systemId = Normalizer.normalizeURI(Util.getNotNullOrEmpty(systemId)); |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
56 |
publicId = Normalizer.normalizePublicId(Normalizer.decodeURN(Util.getNotNullOrEmpty(publicId))); |
33542 | 57 |
|
58 |
//check whether systemId is an urn |
|
59 |
if (systemId != null && systemId.startsWith("urn:publicid:")) { |
|
60 |
systemId = Normalizer.decodeURN(systemId); |
|
61 |
if (publicId != null && !publicId.equals(systemId)) { |
|
62 |
systemId = null; |
|
63 |
} else { |
|
64 |
publicId = systemId; |
|
65 |
systemId = null; |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
CatalogImpl c = (CatalogImpl)catalog; |
|
70 |
String resolvedSystemId = resolve(c, publicId, systemId); |
|
71 |
||
72 |
if (resolvedSystemId != null) { |
|
73 |
return new InputSource(resolvedSystemId); |
|
74 |
} |
|
75 |
||
76 |
GroupEntry.ResolveType resolveType = ((CatalogImpl) catalog).getResolve(); |
|
77 |
switch (resolveType) { |
|
78 |
case IGNORE: |
|
79 |
return new InputSource(new StringReader("")); |
|
80 |
case STRICT: |
|
81 |
CatalogMessages.reportError(CatalogMessages.ERR_NO_MATCH, |
|
82 |
new Object[]{publicId, systemId}); |
|
83 |
} |
|
84 |
||
85 |
//no action, allow the parser to continue |
|
86 |
return null; |
|
87 |
} |
|
88 |
||
89 |
/** |
|
37382
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
90 |
* Resolves the publicId or systemId using public or system entries in the catalog. |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
91 |
* |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
92 |
* The resolution follows the following rules determined by the prefer setting: |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
93 |
* |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
94 |
* prefer "system": attempts to resolve with a system entry; |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
95 |
* attempts to resolve with a public entry when only |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
96 |
* publicId is specified. |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
97 |
* |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
98 |
* prefer "public": attempts to resolve with a system entry; |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
99 |
* attempts to resolve with a public entry if no matching |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
100 |
* system entry is found. |
33542 | 101 |
* @param catalog the catalog |
102 |
* @param publicId the publicId |
|
103 |
* @param systemId the systemId |
|
104 |
* @return the resolved systemId if a match is found, null otherwise |
|
105 |
*/ |
|
106 |
String resolve(CatalogImpl catalog, String publicId, String systemId) { |
|
107 |
String resolvedSystemId = null; |
|
108 |
||
109 |
//search the current catalog |
|
110 |
catalog.reset(); |
|
111 |
if (systemId != null) { |
|
37382
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
112 |
/* |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
113 |
If a system identifier is specified, it is used no matter how |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
114 |
prefer is set. |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
115 |
*/ |
33542 | 116 |
resolvedSystemId = catalog.matchSystem(systemId); |
117 |
} |
|
37382
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
118 |
|
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
119 |
if (resolvedSystemId == null && publicId != null) { |
33542 | 120 |
resolvedSystemId = catalog.matchPublic(publicId); |
121 |
} |
|
122 |
||
123 |
//mark the catalog as having been searched before trying alternatives |
|
124 |
catalog.markAsSearched(); |
|
125 |
||
126 |
//search alternative catalogs |
|
127 |
if (resolvedSystemId == null) { |
|
128 |
Iterator<Catalog> iter = catalog.catalogs().iterator(); |
|
129 |
while (iter.hasNext()) { |
|
130 |
resolvedSystemId = resolve((CatalogImpl)iter.next(), publicId, systemId); |
|
131 |
if (resolvedSystemId != null) { |
|
132 |
break; |
|
133 |
} |
|
134 |
||
135 |
} |
|
136 |
} |
|
137 |
||
138 |
return resolvedSystemId; |
|
139 |
} |
|
140 |
} |