author | joehw |
Wed, 06 Apr 2016 14:31:38 -0700 | |
changeset 36984 | 4d76e25cb8d3 |
parent 36029 | a847c5a7e22d |
child 40753 | 7fdd41fa7d26 |
permissions | -rw-r--r-- |
33542 | 1 |
/* |
36029
a847c5a7e22d
8146237: PREFER from Features API taking precedence over catalog file
joehw
parents:
33542
diff
changeset
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33542 | 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.net.URI; |
|
29 |
import java.net.URISyntaxException; |
|
30 |
import java.nio.file.Path; |
|
31 |
import java.nio.file.Paths; |
|
32 |
import javax.xml.catalog.BaseEntry.CatalogEntryType; |
|
33 |
import javax.xml.parsers.SAXParser; |
|
34 |
import javax.xml.transform.Source; |
|
35 |
import javax.xml.transform.TransformerException; |
|
36 |
import javax.xml.transform.URIResolver; |
|
37 |
import javax.xml.transform.sax.SAXSource; |
|
38 |
import org.xml.sax.Attributes; |
|
39 |
import org.xml.sax.EntityResolver; |
|
40 |
import org.xml.sax.InputSource; |
|
41 |
import org.xml.sax.SAXException; |
|
42 |
import org.xml.sax.helpers.DefaultHandler; |
|
43 |
||
44 |
/** |
|
45 |
* CatalogReader handles SAX events while parsing through a catalog file to |
|
46 |
* create a catalog object. |
|
47 |
* |
|
48 |
* @since 9 |
|
49 |
*/ |
|
50 |
class CatalogReader extends DefaultHandler implements EntityResolver, URIResolver { |
|
51 |
/** URI of the W3C XML Schema for OASIS XML Catalog files. */ |
|
52 |
public static final String xmlCatalogXSD = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.xsd"; |
|
53 |
||
54 |
/** Public identifier for OASIS XML Catalog files. */ |
|
55 |
public static final String xmlCatalogPubId = "-//OASIS//DTD XML Catalogs V1.0//EN"; |
|
56 |
||
57 |
/** |
|
58 |
* The namespace name defined by the OASIS Standard |
|
59 |
*/ |
|
60 |
public static final String NAMESPACE_OASIS = "urn:oasis:names:tc:entity:xmlns:xml:catalog"; |
|
61 |
||
62 |
//Indicate whether the root element has been found |
|
63 |
boolean seenRoot; |
|
64 |
||
65 |
//Indicate that the parser is in a group entry |
|
66 |
boolean inGroup; |
|
67 |
||
68 |
//The Catalog instance |
|
69 |
CatalogImpl catalog; |
|
70 |
||
71 |
//The parser for reading the catalog |
|
72 |
SAXParser parser; |
|
73 |
||
74 |
//The current catalog entry |
|
75 |
CatalogEntry catalogEntry; |
|
76 |
||
77 |
//The current group |
|
78 |
GroupEntry group; |
|
79 |
||
80 |
//The current entry |
|
81 |
BaseEntry entry; |
|
82 |
||
83 |
//remove this variable once 8136778 is committed |
|
84 |
boolean ignoreTheCatalog = false; |
|
85 |
||
86 |
/** |
|
87 |
* Constructs an instance with a Catalog object and parser. |
|
88 |
* |
|
89 |
* @param catalog The Catalog object that represents a catalog |
|
90 |
*/ |
|
91 |
@SuppressWarnings("unchecked") |
|
92 |
public CatalogReader(Catalog catalog, SAXParser parser) { |
|
93 |
this.catalog = (CatalogImpl) catalog; |
|
94 |
this.parser = parser; |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Returns when the specified path is valid. |
|
99 |
* @param path a path |
|
100 |
* @return true if the path is valid, false otherwise |
|
101 |
*/ |
|
102 |
boolean isValidPath(String path) { |
|
103 |
boolean valid = true; |
|
104 |
try { |
|
105 |
Path p = Paths.get(new URI(path)); |
|
106 |
if (!p.toFile().exists()) { |
|
107 |
valid = false; |
|
108 |
} |
|
109 |
} catch (URISyntaxException ex) { |
|
110 |
valid = false; |
|
111 |
} |
|
112 |
||
113 |
return valid; |
|
114 |
} |
|
115 |
||
116 |
@Override |
|
117 |
public void startElement(String namespaceURI, |
|
118 |
String localName, |
|
119 |
String qName, |
|
120 |
Attributes atts) |
|
121 |
throws SAXException { |
|
122 |
||
123 |
//ignore the catalog if it's not compliant. See section 8, item 3 of the spec. |
|
124 |
if (ignoreTheCatalog) return; |
|
125 |
if (!NAMESPACE_OASIS.equals(namespaceURI)) { |
|
126 |
//wait till 8136778 is committed |
|
127 |
// parser.stop(); |
|
128 |
ignoreTheCatalog = true; |
|
129 |
return; |
|
130 |
} |
|
131 |
||
132 |
||
133 |
CatalogEntryType type = CatalogEntryType.getType(localName); |
|
134 |
if (type == null) { |
|
135 |
CatalogMessages.reportError(CatalogMessages.ERR_INVALID_ENTRY_TYPE, |
|
136 |
new Object[]{localName}); |
|
137 |
} |
|
138 |
if (type != CatalogEntryType.CATALOGENTRY) { |
|
139 |
if (!seenRoot) { |
|
140 |
CatalogMessages.reportError(CatalogMessages.ERR_INVALID_CATALOG); |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
String base = atts.getValue("xml:base"); |
|
145 |
if (base == null) { |
|
146 |
if (inGroup) { |
|
147 |
base = group.getBaseURI().toString(); |
|
148 |
} else { |
|
149 |
if (type == CatalogEntryType.CATALOGENTRY) { |
|
150 |
base = catalog.getBaseURI().toString(); |
|
151 |
} else { |
|
152 |
base = catalogEntry.getBaseURI().toString(); |
|
153 |
} |
|
154 |
} |
|
155 |
} else { |
|
156 |
base = Normalizer.normalizeURI(base); |
|
157 |
} |
|
158 |
||
159 |
//parse the catalog and group entries |
|
160 |
if (type == CatalogEntryType.CATALOGENTRY |
|
161 |
|| type == CatalogEntryType.GROUP) { |
|
162 |
String prefer = atts.getValue("prefer"); |
|
163 |
if (prefer == null) { |
|
164 |
if (type == CatalogEntryType.CATALOGENTRY) { |
|
165 |
//use the general setting |
|
166 |
prefer = catalog.isPreferPublic() ? |
|
167 |
CatalogFeatures.PREFER_PUBLIC : CatalogFeatures.PREFER_SYSTEM; |
|
168 |
} else { |
|
169 |
//Group inherit from the catalog entry |
|
170 |
prefer = catalogEntry.isPreferPublic() ? |
|
171 |
CatalogFeatures.PREFER_PUBLIC : CatalogFeatures.PREFER_SYSTEM; |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
if (type == CatalogEntryType.CATALOGENTRY) { |
|
176 |
seenRoot = true; |
|
177 |
if (catalog.isTop()) { |
|
178 |
String defer = atts.getValue("defer"); |
|
179 |
String resolve = atts.getValue("resolve"); |
|
180 |
if (defer == null) { |
|
181 |
defer = catalog.isDeferred() ? |
|
182 |
CatalogFeatures.DEFER_TRUE : CatalogFeatures.DEFER_FALSE; |
|
183 |
} |
|
184 |
if (resolve == null) { |
|
185 |
resolve = catalog.getResolve().literal; |
|
186 |
} |
|
36029
a847c5a7e22d
8146237: PREFER from Features API taking precedence over catalog file
joehw
parents:
33542
diff
changeset
|
187 |
//override property settings with those from the catalog file |
33542 | 188 |
catalog.setResolve(resolve); |
36984
4d76e25cb8d3
8150969: DEFER from Features API is taking precedence over defer preference in catalog file
joehw
parents:
36029
diff
changeset
|
189 |
catalog.setDeferred(defer); |
33542 | 190 |
catalogEntry = new CatalogEntry(base, prefer, defer, resolve); |
191 |
} else { |
|
192 |
catalogEntry = new CatalogEntry(base, prefer); |
|
193 |
} |
|
36029
a847c5a7e22d
8146237: PREFER from Features API taking precedence over catalog file
joehw
parents:
33542
diff
changeset
|
194 |
catalog.setPrefer(prefer); |
33542 | 195 |
return; |
196 |
} else { |
|
197 |
inGroup = true; |
|
198 |
group = new GroupEntry(catalog, base, prefer); |
|
199 |
catalog.addEntry(group); |
|
200 |
return; |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
//parse entries other than the catalog and group entries |
|
205 |
switch (type) { |
|
206 |
case PUBLIC: |
|
207 |
entry = new PublicEntry(base, atts.getValue("publicId"), atts.getValue("uri")); |
|
208 |
break; |
|
209 |
case SYSTEM: |
|
210 |
entry = new SystemEntry(base, atts.getValue("systemId"), atts.getValue("uri")); |
|
211 |
break; |
|
212 |
case REWRITESYSTEM: |
|
213 |
entry = new RewriteSystem(base, atts.getValue("systemIdStartString"), atts.getValue("rewritePrefix")); |
|
214 |
break; |
|
215 |
case SYSTEMSUFFIX: |
|
216 |
entry = new SystemSuffix(base, atts.getValue("systemIdSuffix"), atts.getValue("uri")); |
|
217 |
break; |
|
218 |
case DELEGATEPUBLIC: |
|
219 |
entry = new DelegatePublic(base, atts.getValue("publicIdStartString"), atts.getValue("catalog")); |
|
220 |
break; |
|
221 |
case DELEGATESYSTEM: |
|
222 |
entry = new DelegateSystem(base, atts.getValue("systemIdStartString"), atts.getValue("catalog")); |
|
223 |
break; |
|
224 |
case URI: |
|
225 |
entry = new UriEntry(base, atts.getValue("name"), atts.getValue("uri")); |
|
226 |
break; |
|
227 |
case REWRITEURI: |
|
228 |
entry = new RewriteUri(base, atts.getValue("uriStartString"), atts.getValue("rewritePrefix")); |
|
229 |
break; |
|
230 |
case URISUFFIX: |
|
231 |
entry = new UriSuffix(base, atts.getValue("uriSuffix"), atts.getValue("uri")); |
|
232 |
break; |
|
233 |
case DELEGATEURI: |
|
234 |
entry = new DelegateUri(base, atts.getValue("uriStartString"), atts.getValue("catalog")); |
|
235 |
break; |
|
236 |
case NEXTCATALOG: |
|
237 |
entry = new NextCatalog(base, atts.getValue("catalog")); |
|
238 |
break; |
|
239 |
} |
|
240 |
||
241 |
if (type == CatalogEntryType.NEXTCATALOG) { |
|
242 |
catalog.addNextCatalog((NextCatalog) entry); |
|
243 |
} else if (inGroup) { |
|
244 |
group.addEntry(entry); |
|
245 |
} else { |
|
246 |
catalog.addEntry(entry); |
|
247 |
} |
|
248 |
||
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Handles endElement event |
|
253 |
*/ |
|
254 |
@Override |
|
255 |
public void endElement(String namespaceURI, String localName, String qName) |
|
256 |
throws SAXException { |
|
257 |
if (ignoreTheCatalog) return; |
|
258 |
||
259 |
CatalogEntryType type = CatalogEntryType.getType(localName); |
|
260 |
if (type == CatalogEntryType.GROUP) { |
|
261 |
inGroup = false; |
|
262 |
} else if (type == CatalogEntryType.CATALOGENTRY) { |
|
263 |
/* |
|
264 |
Done reading the catalog file. |
|
265 |
Load delegate and alternative catalogs if defer is false. |
|
266 |
*/ |
|
267 |
if (!catalog.isDeferred()) { |
|
268 |
catalog.loadDelegateCatalogs(); |
|
269 |
catalog.loadNextCatalogs(); |
|
270 |
} |
|
271 |
} |
|
272 |
} |
|
273 |
||
274 |
||
275 |
/** |
|
276 |
* Skips external DTD since resolving external DTD is not required |
|
277 |
* by the specification. |
|
278 |
*/ |
|
279 |
@Override |
|
280 |
public InputSource resolveEntity(String publicId, String systemId) { |
|
281 |
return new InputSource(new StringReader("")); |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* Skips external references since resolution is not required |
|
286 |
* by the specification. |
|
287 |
*/ |
|
288 |
@Override |
|
289 |
public Source resolve(String href, String base) |
|
290 |
throws TransformerException { |
|
291 |
return new SAXSource(new InputSource(new StringReader(""))); |
|
292 |
} |
|
293 |
||
294 |
} |