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.File;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.net.URL;
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.NoSuchElementException;
38 import java.util.Spliterator;
39 import java.util.Spliterators;
40 import java.util.stream.Stream;
41 import java.util.stream.StreamSupport;
42 import static javax.xml.catalog.BaseEntry.CatalogEntryType;
43 import static javax.xml.catalog.CatalogFeatures.DEFER_TRUE;
44 import javax.xml.catalog.CatalogFeatures.Feature;
45 import static javax.xml.catalog.CatalogMessages.formatMessage;
46 import javax.xml.parsers.ParserConfigurationException;
47 import javax.xml.parsers.SAXParser;
48 import javax.xml.parsers.SAXParserFactory;
124 String[] catalogFile = file;
125 if (level == 0 && file.length == 0) {
126 String files = features.get(Feature.FILES);
127 if (files != null) {
128 catalogFile = files.split(";[ ]*");
129 }
130 }
131
132 /*
133 In accordance with 8. Resource Failures of the Catalog spec, missing
134 Catalog entry files are to be ignored.
135 */
136 if ((catalogFile != null && catalogFile.length > 0)) {
137 int start = 0;
138 URI uri = null;
139 for (String temp : catalogFile) {
140 uri = getSystemId(temp);
141 start++;
142 if (verifyCatalogFile(uri)) {
143 systemId = uri.toASCIIString();
144 break;
145 }
146 }
147
148 //Save the rest of input files as alternative catalogs
149 if (level == 0 && catalogFile.length > start) {
150 inputFiles = new ArrayList<>();
151 for (int i = start; i < catalogFile.length; i++) {
152 if (catalogFile[i] != null) {
153 inputFiles.add(catalogFile[i]);
154 }
155 }
156 }
157
158 if (systemId != null) {
159 parse(systemId);
160 }
161 }
162 }
163
274
275 try {
276 CatalogReader reader = new CatalogReader(this, parser);
277 parser.parse(systemId, reader);
278 } catch (SAXException | IOException ex) {
279 CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSING_FAILED, ex);
280 }
281 }
282
283 /**
284 * Resolves the specified file path to an absolute systemId. If it is
285 * relative, it shall be resolved using the base or user.dir property if
286 * base is not specified.
287 *
288 * @param file The specified file path
289 * @return The systemId of the file
290 * @throws CatalogException if the specified file path can not be converted
291 * to a system id
292 */
293 private URI getSystemId(String file) {
294 URL filepath;
295 if (file != null && file.length() > 0) {
296 try {
297 File f = new File(file);
298 if (baseURI != null && !f.isAbsolute()) {
299 filepath = new URL(baseURI, fixSlashes(file));
300 return filepath.toURI();
301 } else {
302 return resolveURI(file);
303 }
304 } catch (MalformedURLException | URISyntaxException e) {
305 CatalogMessages.reportRunTimeError(CatalogMessages.ERR_INVALID_PATH,
306 new Object[]{file}, e);
307 }
308 }
309 return null;
310 }
311
312 /**
313 * Resolves the specified uri. If the uri is relative, makes it absolute by
314 * the user.dir directory.
315 *
316 * @param uri The specified URI.
317 * @return The resolved URI
318 */
319 private URI resolveURI(String uri) throws MalformedURLException {
320 if (uri == null) {
321 uri = "";
322 }
323
324 URI temp = toURI(uri);
325 String str = temp.toASCIIString();
326 String base = str.substring(0, str.lastIndexOf('/') + 1);
327 baseURI = new URL(str);
328
329 return temp;
330 }
331
332 /**
333 * Converts an URI string or file path to URI.
334 *
335 * @param uri an URI string or file path
336 * @return an URI
337 */
338 private URI toURI(String uri) {
339 URI temp = null;
340 try {
341 URL url = new URL(uri);
342 temp = url.toURI();
343 } catch (MalformedURLException | URISyntaxException mue) {
344 File file = new File(uri);
345 temp = file.toURI();
346 }
347 return temp;
348 }
349
350 /**
351 * Returns a SAXParser instance
352 * @return a SAXParser instance
353 * @throws CatalogException if constructing a SAXParser failed
354 */
355 private SAXParser getParser() {
356 SAXParser p = null;
357 try {
358 SAXParserFactory spf = new SAXParserFactoryImpl();
359 spf.setNamespaceAware(true);
360 spf.setValidating(false);
361 spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
362 p = spf.newSAXParser();
363 } catch (ParserConfigurationException | SAXException e) {
364 CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSING_FAILED, e);
365 }
366 return p;
|
1 /*
2 * Copyright (c) 2015, 2016, 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.IOException;
29 import java.net.MalformedURLException;
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import java.net.URL;
33 import java.util.ArrayList;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.NoSuchElementException;
37 import java.util.Spliterator;
38 import java.util.Spliterators;
39 import java.util.stream.Stream;
40 import java.util.stream.StreamSupport;
41 import static javax.xml.catalog.BaseEntry.CatalogEntryType;
42 import static javax.xml.catalog.CatalogFeatures.DEFER_TRUE;
43 import javax.xml.catalog.CatalogFeatures.Feature;
44 import static javax.xml.catalog.CatalogMessages.formatMessage;
45 import javax.xml.parsers.ParserConfigurationException;
46 import javax.xml.parsers.SAXParser;
47 import javax.xml.parsers.SAXParserFactory;
123 String[] catalogFile = file;
124 if (level == 0 && file.length == 0) {
125 String files = features.get(Feature.FILES);
126 if (files != null) {
127 catalogFile = files.split(";[ ]*");
128 }
129 }
130
131 /*
132 In accordance with 8. Resource Failures of the Catalog spec, missing
133 Catalog entry files are to be ignored.
134 */
135 if ((catalogFile != null && catalogFile.length > 0)) {
136 int start = 0;
137 URI uri = null;
138 for (String temp : catalogFile) {
139 uri = getSystemId(temp);
140 start++;
141 if (verifyCatalogFile(uri)) {
142 systemId = uri.toASCIIString();
143 try {
144 baseURI = new URL(systemId);
145 } catch (MalformedURLException e) {
146 CatalogMessages.reportRunTimeError(CatalogMessages.ERR_INVALID_PATH,
147 new Object[]{temp}, e);
148 }
149 break;
150 }
151 }
152
153 //Save the rest of input files as alternative catalogs
154 if (level == 0 && catalogFile.length > start) {
155 inputFiles = new ArrayList<>();
156 for (int i = start; i < catalogFile.length; i++) {
157 if (catalogFile[i] != null) {
158 inputFiles.add(catalogFile[i]);
159 }
160 }
161 }
162
163 if (systemId != null) {
164 parse(systemId);
165 }
166 }
167 }
168
279
280 try {
281 CatalogReader reader = new CatalogReader(this, parser);
282 parser.parse(systemId, reader);
283 } catch (SAXException | IOException ex) {
284 CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSING_FAILED, ex);
285 }
286 }
287
288 /**
289 * Resolves the specified file path to an absolute systemId. If it is
290 * relative, it shall be resolved using the base or user.dir property if
291 * base is not specified.
292 *
293 * @param file The specified file path
294 * @return The systemId of the file
295 * @throws CatalogException if the specified file path can not be converted
296 * to a system id
297 */
298 private URI getSystemId(String file) {
299 URI temp = null;
300
301 try {
302 temp = Util.verifyAndGetURI(file, baseURI);
303 } catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) {
304 CatalogMessages.reportRunTimeError(CatalogMessages.ERR_INVALID_PATH,
305 new Object[]{file}, e);
306 }
307
308 return temp;
309 }
310
311 /**
312 * Returns a SAXParser instance
313 * @return a SAXParser instance
314 * @throws CatalogException if constructing a SAXParser failed
315 */
316 private SAXParser getParser() {
317 SAXParser p = null;
318 try {
319 SAXParserFactory spf = new SAXParserFactoryImpl();
320 spf.setNamespaceAware(true);
321 spf.setValidating(false);
322 spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
323 p = spf.newSAXParser();
324 } catch (ParserConfigurationException | SAXException e) {
325 CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSING_FAILED, e);
326 }
327 return p;
|