8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
Reviewed-by: lancea
--- a/jaxp/src/java.xml/share/classes/javax/xml/catalog/BaseEntry.java Wed Mar 23 19:33:37 2016 -0700
+++ b/jaxp/src/java.xml/share/classes/javax/xml/catalog/BaseEntry.java Thu Mar 24 15:34:50 2016 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,13 +24,9 @@
*/
package javax.xml.catalog;
-import java.io.File;
import java.net.MalformedURLException;
-import java.net.URI;
-import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;
-import jdk.xml.internal.SecuritySupport;
/**
* Represents a general Catalog entry.
@@ -238,18 +234,6 @@
}
/**
- * Replace backslashes with forward slashes. (URLs always use forward
- * slashes.)
- *
- * @param sysid The input system identifier.
- * @return The same system identifier with backslashes turned into forward
- * slashes.
- */
- protected String fixSlashes(String sysid) {
- return sysid.replace('\\', '/');
- }
-
- /**
* Construct an absolute URI from a relative one, using the current base
* URI.
*
@@ -260,7 +244,7 @@
protected String makeAbsolute(String sysid) {
URL local = null;
- sysid = fixSlashes(sysid);
+ sysid = Util.fixSlashes(sysid);
/**
* try { local = new URL(base, sysid); } catch (MalformedURLException e)
* { catalogManager.debug.message(1, "Malformed URL on system
--- a/jaxp/src/java.xml/share/classes/javax/xml/catalog/CatalogFeatures.java Wed Mar 23 19:33:37 2016 -0700
+++ b/jaxp/src/java.xml/share/classes/javax/xml/catalog/CatalogFeatures.java Thu Mar 24 15:34:50 2016 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,10 @@
*/
package javax.xml.catalog;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.Map;
import jdk.xml.internal.SecuritySupport;
/**
@@ -380,10 +384,7 @@
*/
CatalogFeatures(Builder builder) {
init();
- setProperty(Feature.FILES.ordinal(), State.APIPROPERTY, builder.files);
- setProperty(Feature.PREFER.ordinal(), State.APIPROPERTY, builder.prefer);
- setProperty(Feature.DEFER.ordinal(), State.APIPROPERTY, builder.defer);
- setProperty(Feature.RESOLVE.ordinal(), State.APIPROPERTY, builder.resolve);
+ setProperties(builder);
}
/**
@@ -410,6 +411,15 @@
}
/**
+ * Sets properties by the Builder.
+ * @param builder the CatalogFeatures builder
+ */
+ private void setProperties(Builder builder) {
+ builder.values.entrySet().stream().forEach((entry) -> {
+ setProperty(entry.getKey().ordinal(), State.APIPROPERTY, entry.getValue());
+ });
+ }
+ /**
* Sets the value of a property by its index, updates only if it shall override.
*
* @param index the index of the property
@@ -432,11 +442,24 @@
&& !value.equals(RESOLVE_IGNORE)) {
CatalogMessages.reportIAE(new Object[]{value, Feature.RESOLVE.name()}, null);
}
+ } else if (index == Feature.FILES.ordinal()) {
+ try {
+ if (Util.verifyAndGetURI(value, null) == null) {
+ CatalogMessages.reportIAE(new Object[]{value, Feature.FILES.name()}, null);
+ }
+ }catch (MalformedURLException | URISyntaxException | IllegalArgumentException ex) {
+ CatalogMessages.reportIAE(new Object[]{value, Feature.FILES.name()}, ex);
+ }
+
}
if (states[index] == null || state.compareTo(states[index]) >= 0) {
values[index] = value;
states[index] = state;
}
+ } else {
+ if (state == State.SYSTEMPROPERTY || state == State.JAXPDOTPROPERTIES) {
+ CatalogMessages.reportIAE(new Object[]{value, Feature.values()[index].name()}, null);
+ }
}
}
@@ -486,9 +509,9 @@
*/
public static class Builder {
/**
- * Variables for the features supported by CatalogFeatures.
+ * Values of the features supported by CatalogFeatures.
*/
- String files, prefer, defer, resolve;
+ Map<Feature, String> values = new HashMap<>();
/**
* Instantiation of Builder is not allowed.
@@ -505,20 +528,10 @@
* property
*/
public Builder with(Feature feature, String value) {
- switch (feature) {
- case FILES :
- files = value;
- break;
- case PREFER :
- prefer = value;
- break;
- case DEFER :
- defer = value;
- break;
- case RESOLVE :
- resolve = value;
- break;
+ if (value == null || value.length() == 0) {
+ CatalogMessages.reportIAE(new Object[]{value, feature.name()}, null);
}
+ values.put(feature, value);
return this;
}
--- a/jaxp/src/java.xml/share/classes/javax/xml/catalog/CatalogImpl.java Wed Mar 23 19:33:37 2016 -0700
+++ b/jaxp/src/java.xml/share/classes/javax/xml/catalog/CatalogImpl.java Thu Mar 24 15:34:50 2016 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,6 @@
package javax.xml.catalog;
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
-import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
@@ -141,6 +140,12 @@
start++;
if (verifyCatalogFile(uri)) {
systemId = uri.toASCIIString();
+ try {
+ baseURI = new URL(systemId);
+ } catch (MalformedURLException e) {
+ CatalogMessages.reportRunTimeError(CatalogMessages.ERR_INVALID_PATH,
+ new Object[]{temp}, e);
+ }
break;
}
}
@@ -291,59 +296,15 @@
* to a system id
*/
private URI getSystemId(String file) {
- URL filepath;
- if (file != null && file.length() > 0) {
- try {
- File f = new File(file);
- if (baseURI != null && !f.isAbsolute()) {
- filepath = new URL(baseURI, fixSlashes(file));
- return filepath.toURI();
- } else {
- return resolveURI(file);
- }
- } catch (MalformedURLException | URISyntaxException e) {
- CatalogMessages.reportRunTimeError(CatalogMessages.ERR_INVALID_PATH,
- new Object[]{file}, e);
- }
- }
- return null;
- }
+ URI temp = null;
- /**
- * Resolves the specified uri. If the uri is relative, makes it absolute by
- * the user.dir directory.
- *
- * @param uri The specified URI.
- * @return The resolved URI
- */
- private URI resolveURI(String uri) throws MalformedURLException {
- if (uri == null) {
- uri = "";
+ try {
+ temp = Util.verifyAndGetURI(file, baseURI);
+ } catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) {
+ CatalogMessages.reportRunTimeError(CatalogMessages.ERR_INVALID_PATH,
+ new Object[]{file}, e);
}
- URI temp = toURI(uri);
- String str = temp.toASCIIString();
- String base = str.substring(0, str.lastIndexOf('/') + 1);
- baseURI = new URL(str);
-
- return temp;
- }
-
- /**
- * Converts an URI string or file path to URI.
- *
- * @param uri an URI string or file path
- * @return an URI
- */
- private URI toURI(String uri) {
- URI temp = null;
- try {
- URL url = new URL(uri);
- temp = url.toURI();
- } catch (MalformedURLException | URISyntaxException mue) {
- File file = new File(uri);
- temp = file.toURI();
- }
return temp;
}
--- a/jaxp/src/java.xml/share/classes/javax/xml/catalog/Util.java Wed Mar 23 19:33:37 2016 -0700
+++ b/jaxp/src/java.xml/share/classes/javax/xml/catalog/Util.java Thu Mar 24 15:34:50 2016 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,13 @@
*/
package javax.xml.catalog;
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import jdk.xml.internal.SecuritySupport;
/**
@@ -31,6 +38,76 @@
* @since 9
*/
class Util {
+
+ /**
+ * Resolves the specified file path to an absolute systemId. If it is
+ * relative, it shall be resolved using the base or user.dir property if
+ * base is not specified.
+ *
+ * @param file The specified file path
+ * @param baseURI the base URI
+ * @return The URI
+ * @throws CatalogException if the specified file path can not be converted
+ * to a system id
+ */
+ static URI verifyAndGetURI(String file, URL baseURI)
+ throws MalformedURLException, URISyntaxException, IllegalArgumentException {
+ URL filepath;
+ URI temp;
+ if (file != null && file.length() > 0) {
+ File f = new File(file);
+
+ if (baseURI != null && !f.isAbsolute()) {
+ filepath = new URL(baseURI, fixSlashes(file));
+ temp = filepath.toURI();
+ } else {
+ temp = resolveURI(file);
+ }
+ //Paths.get may throw IllegalArgumentException
+ Path path = Paths.get(temp);
+ if (path.toFile().isFile()) {
+ return temp;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Resolves the specified uri. If the uri is relative, makes it absolute by
+ * the user.dir directory.
+ *
+ * @param uri The specified URI.
+ * @return The resolved URI
+ */
+ static URI resolveURI(String uri) throws MalformedURLException {
+ if (uri == null) {
+ uri = "";
+ }
+
+ URI temp = null;
+ try {
+ URL url = new URL(uri);
+ temp = url.toURI();
+ } catch (MalformedURLException | URISyntaxException mue) {
+ File file = new File(uri);
+ temp = file.toURI();
+ }
+
+ return temp;
+ }
+
+ /**
+ * Replace backslashes with forward slashes. (URLs always use forward
+ * slashes.)
+ *
+ * @param sysid The input system identifier.
+ * @return The same system identifier with backslashes turned into forward
+ * slashes.
+ */
+ static String fixSlashes(String sysid) {
+ return sysid.replace('\\', '/');
+ }
+
/**
* Find catalog file paths by reading the system property, and then
* jaxp.properties if the system property is not specified.
@@ -38,7 +115,7 @@
* @param sysPropertyName the name of system property
* @return the catalog file paths, or null if not found.
*/
- static public String[] getCatalogFiles(String sysPropertyName) {
+ static String[] getCatalogFiles(String sysPropertyName) {
String value = SecuritySupport.getJAXPSystemProperty(sysPropertyName);
if (value != null && !value.equals("")) {
return value.split(";");
--- a/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogTest.java Wed Mar 23 19:33:37 2016 -0700
+++ b/jaxp/test/javax/xml/jaxp/unittest/catalog/CatalogTest.java Thu Mar 24 15:34:50 2016 -0700
@@ -42,11 +42,23 @@
import org.xml.sax.ext.DefaultHandler2;
/*
- * @bug 8081248, 8144966, 8146606, 8146237
+ * @bug 8081248, 8144966, 8146606, 8146237, 8151154
* @summary Tests basic Catalog functions.
*/
+public class CatalogTest {
+ /**
+ * @bug 8151154
+ * Verifies that the CatalogFeatures' builder throws IllegalArgumentException
+ * on invalid file inputs.
+ * @param file the file path
+ */
+ @Test(dataProvider = "invalidPaths", expectedExceptions = IllegalArgumentException.class)
+ public void testFileInput(String file) {
+ CatalogFeatures features = CatalogFeatures.builder()
+ .with(CatalogFeatures.Feature.FILES, file)
+ .build();
+ }
-public class CatalogTest {
/**
* @bug 8146237
* PREFER from Features API taking precedence over catalog file
@@ -201,6 +213,24 @@
}
}
+ /*
+ DataProvider: for testing the verification of file paths by
+ the CatalogFeatures builder
+ */
+ @DataProvider(name = "invalidPaths")
+ Object[][] getFiles() {
+ return new Object[][]{
+ {null},
+ {""},
+ {"file:a/b\\c"},
+ {"file:/../../.."},
+ {"c:/te:t"},
+ {"c:/te?t"},
+ {"c/te*t"},
+ {"in|valid.txt"},
+ {"shema:invalid.txt"},
+ };
+ }
/*
DataProvider: provides test name, expected string, the catalog, and XML