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) {
|
|
63 |
if (href == null) return null;
|
|
64 |
|
|
65 |
CatalogImpl c = (CatalogImpl)catalog;
|
|
66 |
String uri = Normalizer.normalizeURI(href);
|
|
67 |
String result;
|
|
68 |
|
|
69 |
//remove fragment if any.
|
|
70 |
int hashPos = uri.indexOf("#");
|
|
71 |
if (hashPos >= 0) {
|
|
72 |
uri = uri.substring(0, hashPos);
|
|
73 |
}
|
|
74 |
|
|
75 |
//search the current catalog
|
|
76 |
result = resolve(c, uri);
|
|
77 |
if (result == null) {
|
|
78 |
GroupEntry.ResolveType resolveType = c.getResolve();
|
|
79 |
switch (resolveType) {
|
|
80 |
case IGNORE:
|
|
81 |
return new SAXSource(new InputSource(new StringReader("")));
|
|
82 |
case STRICT:
|
|
83 |
CatalogMessages.reportError(CatalogMessages.ERR_NO_URI_MATCH,
|
|
84 |
new Object[]{href, base});
|
|
85 |
}
|
|
86 |
try {
|
|
87 |
URL url = null;
|
|
88 |
|
|
89 |
if (base == null) {
|
|
90 |
url = new URL(uri);
|
|
91 |
result = url.toString();
|
|
92 |
} else {
|
|
93 |
URL baseURL = new URL(base);
|
|
94 |
url = (href.length() == 0 ? baseURL : new URL(baseURL, uri));
|
|
95 |
result = url.toString();
|
|
96 |
}
|
|
97 |
} catch (java.net.MalformedURLException mue) {
|
|
98 |
CatalogMessages.reportError(CatalogMessages.ERR_CREATING_URI,
|
|
99 |
new Object[]{href, base});
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
SAXSource source = new SAXSource();
|
|
104 |
source.setInputSource(new InputSource(result));
|
|
105 |
setEntityResolver(source);
|
|
106 |
return source;
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Resolves the publicId or systemId to one specified in the catalog.
|
|
111 |
* @param catalog the catalog
|
|
112 |
* @param href an href attribute, which may be relative or absolute
|
|
113 |
* @return the resolved systemId if a match is found, null otherwise
|
|
114 |
*/
|
|
115 |
String resolve(CatalogImpl catalog, String href) {
|
|
116 |
String result = null;
|
|
117 |
|
|
118 |
//search the current catalog
|
|
119 |
catalog.reset();
|
|
120 |
if (href != null) {
|
|
121 |
result = catalog.matchURI(href);
|
|
122 |
}
|
|
123 |
|
|
124 |
//mark the catalog as having been searched before trying alternatives
|
|
125 |
catalog.markAsSearched();
|
|
126 |
|
|
127 |
//search alternative catalogs
|
|
128 |
if (result == null) {
|
|
129 |
Iterator<Catalog> iter = catalog.catalogs().iterator();
|
|
130 |
while (iter.hasNext()) {
|
|
131 |
result = resolve((CatalogImpl)iter.next(), href);
|
|
132 |
if (result != null) {
|
|
133 |
break;
|
|
134 |
}
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
return result;
|
|
139 |
}
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Establish an entityResolver for newly resolved URIs.
|
|
143 |
* <p>
|
|
144 |
* This is called from the URIResolver to set an EntityResolver on the SAX
|
|
145 |
* parser to be used for new XML documents that are encountered as a result
|
|
146 |
* of the document() function, xsl:import, or xsl:include. This is done
|
|
147 |
* because the XSLT processor calls out to the SAXParserFactory itself to
|
|
148 |
* create a new SAXParser to parse the new document. The new parser does not
|
|
149 |
* automatically inherit the EntityResolver of the original (although
|
|
150 |
* arguably it should). Quote from JAXP specification on Class
|
|
151 |
* SAXTransformerFactory:
|
|
152 |
* <p>
|
|
153 |
* {@code If an application wants to set the ErrorHandler or EntityResolver
|
|
154 |
* for an XMLReader used during a transformation, it should use a URIResolver
|
|
155 |
* to return the SAXSource which provides (with getXMLReader) a reference to
|
|
156 |
* the XMLReader}
|
|
157 |
*
|
|
158 |
*/
|
|
159 |
private void setEntityResolver(SAXSource source) {
|
|
160 |
XMLReader reader = source.getXMLReader();
|
|
161 |
if (reader == null) {
|
|
162 |
SAXParserFactory spFactory = new SAXParserFactoryImpl();
|
|
163 |
spFactory.setNamespaceAware(true);
|
|
164 |
try {
|
|
165 |
reader = spFactory.newSAXParser().getXMLReader();
|
|
166 |
} catch (ParserConfigurationException | SAXException ex) {
|
|
167 |
CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSER_CONF, ex);
|
|
168 |
}
|
|
169 |
}
|
|
170 |
if (entityResolver != null) {
|
|
171 |
entityResolver = new CatalogResolverImpl(catalog);
|
|
172 |
}
|
|
173 |
reader.setEntityResolver(entityResolver);
|
|
174 |
source.setXMLReader(reader);
|
|
175 |
}
|
|
176 |
}
|