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 com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl; |
|
28 |
import java.io.StringReader; |
|
29 |
import java.net.URL; |
|
30 |
import java.util.Iterator; |
|
31 |
import javax.xml.parsers.ParserConfigurationException; |
|
32 |
import javax.xml.parsers.SAXParserFactory; |
|
33 |
import javax.xml.transform.Source; |
|
34 |
import javax.xml.transform.sax.SAXSource; |
|
35 |
import org.xml.sax.InputSource; |
|
36 |
import org.xml.sax.SAXException; |
|
37 |
import org.xml.sax.XMLReader; |
|
38 |
||
39 |
/** |
|
40 |
* A SAX EntityResolver/JAXP URIResolver that uses catalogs. |
|
41 |
* <p> |
|
42 |
* This class implements both a SAX EntityResolver and a JAXP URIResolver. |
|
43 |
* |
|
44 |
* |
|
45 |
* @since 9 |
|
46 |
*/ |
|
47 |
final class CatalogUriResolverImpl implements CatalogUriResolver { |
|
48 |
||
49 |
Catalog catalog; |
|
50 |
CatalogResolverImpl entityResolver; |
|
51 |
||
52 |
/** |
|
53 |
* Construct an instance of the CatalogResolver from a Catalog. |
|
54 |
* |
|
55 |
* @param catalog A Catalog. |
|
56 |
*/ |
|
57 |
public CatalogUriResolverImpl(Catalog catalog) { |
|
58 |
this.catalog = catalog; |
|
59 |
} |
|
60 |
||
61 |
@Override |
|
62 |
public Source resolve(String href, String base) { |
|
37382
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
63 |
href = Util.getNotNullOrEmpty(href); |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
64 |
base = Util.getNotNullOrEmpty(base); |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
33542
diff
changeset
|
65 |
|
33542 | 66 |
if (href == null) return null; |
67 |
||
68 |
CatalogImpl c = (CatalogImpl)catalog; |
|
69 |
String uri = Normalizer.normalizeURI(href); |
|
70 |
String result; |
|
71 |
||
72 |
//remove fragment if any. |
|
73 |
int hashPos = uri.indexOf("#"); |
|
74 |
if (hashPos >= 0) { |
|
75 |
uri = uri.substring(0, hashPos); |
|
76 |
} |
|
77 |
||
78 |
//search the current catalog |
|
79 |
result = resolve(c, uri); |
|
80 |
if (result == null) { |
|
81 |
GroupEntry.ResolveType resolveType = c.getResolve(); |
|
82 |
switch (resolveType) { |
|
83 |
case IGNORE: |
|
84 |
return new SAXSource(new InputSource(new StringReader(""))); |
|
85 |
case STRICT: |
|
86 |
CatalogMessages.reportError(CatalogMessages.ERR_NO_URI_MATCH, |
|
87 |
new Object[]{href, base}); |
|
88 |
} |
|
89 |
try { |
|
90 |
URL url = null; |
|
91 |
||
92 |
if (base == null) { |
|
93 |
url = new URL(uri); |
|
94 |
result = url.toString(); |
|
95 |
} else { |
|
96 |
URL baseURL = new URL(base); |
|
97 |
url = (href.length() == 0 ? baseURL : new URL(baseURL, uri)); |
|
98 |
result = url.toString(); |
|
99 |
} |
|
100 |
} catch (java.net.MalformedURLException mue) { |
|
101 |
CatalogMessages.reportError(CatalogMessages.ERR_CREATING_URI, |
|
102 |
new Object[]{href, base}); |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
SAXSource source = new SAXSource(); |
|
107 |
source.setInputSource(new InputSource(result)); |
|
108 |
setEntityResolver(source); |
|
109 |
return source; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Resolves the publicId or systemId to one specified in the catalog. |
|
114 |
* @param catalog the catalog |
|
115 |
* @param href an href attribute, which may be relative or absolute |
|
116 |
* @return the resolved systemId if a match is found, null otherwise |
|
117 |
*/ |
|
118 |
String resolve(CatalogImpl catalog, String href) { |
|
119 |
String result = null; |
|
120 |
||
121 |
//search the current catalog |
|
122 |
catalog.reset(); |
|
123 |
if (href != null) { |
|
124 |
result = catalog.matchURI(href); |
|
125 |
} |
|
126 |
||
127 |
//mark the catalog as having been searched before trying alternatives |
|
128 |
catalog.markAsSearched(); |
|
129 |
||
130 |
//search alternative catalogs |
|
131 |
if (result == null) { |
|
132 |
Iterator<Catalog> iter = catalog.catalogs().iterator(); |
|
133 |
while (iter.hasNext()) { |
|
134 |
result = resolve((CatalogImpl)iter.next(), href); |
|
135 |
if (result != null) { |
|
136 |
break; |
|
137 |
} |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
return result; |
|
142 |
} |
|
143 |
||
144 |
/** |
|
145 |
* Establish an entityResolver for newly resolved URIs. |
|
146 |
* <p> |
|
147 |
* This is called from the URIResolver to set an EntityResolver on the SAX |
|
148 |
* parser to be used for new XML documents that are encountered as a result |
|
149 |
* of the document() function, xsl:import, or xsl:include. This is done |
|
150 |
* because the XSLT processor calls out to the SAXParserFactory itself to |
|
151 |
* create a new SAXParser to parse the new document. The new parser does not |
|
152 |
* automatically inherit the EntityResolver of the original (although |
|
153 |
* arguably it should). Quote from JAXP specification on Class |
|
154 |
* SAXTransformerFactory: |
|
155 |
* <p> |
|
156 |
* {@code If an application wants to set the ErrorHandler or EntityResolver |
|
157 |
* for an XMLReader used during a transformation, it should use a URIResolver |
|
158 |
* to return the SAXSource which provides (with getXMLReader) a reference to |
|
159 |
* the XMLReader} |
|
160 |
* |
|
161 |
*/ |
|
162 |
private void setEntityResolver(SAXSource source) { |
|
163 |
XMLReader reader = source.getXMLReader(); |
|
164 |
if (reader == null) { |
|
165 |
SAXParserFactory spFactory = new SAXParserFactoryImpl(); |
|
166 |
spFactory.setNamespaceAware(true); |
|
167 |
try { |
|
168 |
reader = spFactory.newSAXParser().getXMLReader(); |
|
169 |
} catch (ParserConfigurationException | SAXException ex) { |
|
170 |
CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSER_CONF, ex); |
|
171 |
} |
|
172 |
} |
|
173 |
if (entityResolver != null) { |
|
174 |
entityResolver = new CatalogResolverImpl(catalog); |
|
175 |
} |
|
176 |
reader.setEntityResolver(entityResolver); |
|
177 |
source.setXMLReader(reader); |
|
178 |
} |
|
179 |
} |