jaxp/src/java.xml/share/classes/javax/xml/catalog/Util.java
changeset 43121 e73af7b6ce47
parent 40582 1dddef49982c
child 43358 9e2d943b5b66
--- a/jaxp/src/java.xml/share/classes/javax/xml/catalog/Util.java	Wed Jul 05 22:40:29 2017 +0200
+++ b/jaxp/src/java.xml/share/classes/javax/xml/catalog/Util.java	Wed Jan 11 13:06:04 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, 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,13 +25,20 @@
 package javax.xml.catalog;
 
 import java.io.File;
+import java.io.IOException;
 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 java.util.Iterator;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import static javax.xml.catalog.CatalogFeatures.DEFER_FALSE;
+import static javax.xml.catalog.CatalogFeatures.DEFER_TRUE;
+import javax.xml.catalog.CatalogFeatures.Feature;
+import static javax.xml.catalog.CatalogFeatures.PREFER_PUBLIC;
+import static javax.xml.catalog.CatalogFeatures.PREFER_SYSTEM;
+import static javax.xml.catalog.CatalogFeatures.RESOLVE_CONTINUE;
+import static javax.xml.catalog.CatalogFeatures.RESOLVE_IGNORE;
+import static javax.xml.catalog.CatalogFeatures.RESOLVE_STRICT;
 import jdk.xml.internal.SecuritySupport;
 
 /**
@@ -39,22 +46,25 @@
  * @since 9
  */
 class Util {
+
     final static String URN = "urn:publicid:";
     final static String PUBLICID_PREFIX = "-//";
     final static String PUBLICID_PREFIX_ALT = "+//";
+    final static String SCHEME_FILE = "file";
+    final static String SCHEME_JAR = "jar";
+    final static String SCHEME_JARFILE = "jar:file:";
 
     /**
      * Finds an entry in the catalog that matches with the publicId or systemId.
      *
-     * The resolution follows the following rules determined by the prefer setting:
+     * The resolution follows the following rules determined by the prefer
+     * setting:
      *
-     * prefer "system": attempts to resolve with a system entry;
-     *                  attempts to resolve with a public entry when only
-     *                  publicId is specified.
+     * prefer "system": attempts to resolve with a system entry; attempts to
+     * resolve with a public entry when only publicId is specified.
      *
-     * prefer "public": attempts to resolve with a system entry;
-     *                  attempts to resolve with a public entry if no matching
-     *                  system entry is found.
+     * prefer "public": attempts to resolve with a system entry; attempts to
+     * resolve with a public entry if no matching system entry is found.
      *
      * If no match is found, continue searching uri entries
      *
@@ -70,9 +80,9 @@
         catalog.reset();
         if (systemId != null) {
             /*
-               If a system identifier is specified, it is used no matter how
-            prefer is set.
-            */
+             If a system identifier is specified, it is used no matter how
+             prefer is set.
+             */
             resolvedSystemId = catalog.matchSystem(systemId);
         }
 
@@ -91,7 +101,7 @@
         if (resolvedSystemId == null) {
             Iterator<Catalog> iter = catalog.catalogs().iterator();
             while (iter.hasNext()) {
-                resolvedSystemId = resolve((CatalogImpl)iter.next(), publicId, systemId);
+                resolvedSystemId = resolve((CatalogImpl) iter.next(), publicId, systemId);
                 if (resolvedSystemId != null) {
                     break;
                 }
@@ -102,73 +112,112 @@
         return resolvedSystemId;
     }
 
+    static void validateUrisSyntax(URI... uris) {
+        for (URI uri : uris) {
+            validateUriSyntax(uri);
+        }
+    }
+
+    static void validateUrisSyntax(String... uris) {
+        for (String uri : uris) {
+            validateUriSyntax(URI.create(uri));
+        }
+    }
+
     /**
-     * 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.
+     * Validate that the URI must be absolute and a valid URL.
      *
-     * @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
+     * Note that this method does not verify the existence of the resource. The
+     * Catalog standard requires that such resources be ignored.
+     *
+     * @param uri
+     * @throws IllegalArgumentException if the uri is not absolute and a valid
+     * URL
      */
-    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);
+    static void validateUriSyntax(URI uri) {
+        CatalogMessages.reportNPEOnNull("URI input", uri);
+
+        if (!uri.isAbsolute()) {
+            CatalogMessages.reportIAE(CatalogMessages.ERR_URI_NOTABSOLUTE,
+                    new Object[]{uri}, null);
+        }
 
-            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;
+        try {
+            // check if the scheme was valid
+            uri.toURL();
+        } catch (MalformedURLException ex) {
+            CatalogMessages.reportIAE(CatalogMessages.ERR_URI_NOTVALIDURL,
+                    new Object[]{uri}, null);
+        }
+
+        // verify the resource exists where possible
+        if (isFileUri(uri)) {
+            if (!isFileUriExist(uri, false)) {
+                CatalogMessages.reportIAE(CatalogMessages.ERR_URI_NOTVALIDURL,
+                        new Object[]{uri}, null);
             }
         }
-        return null;
     }
 
     /**
-     * Resolves the specified uri. If the uri is relative, makes it absolute by
-     * the user.dir directory.
+     * Checks whether the URI is a file URI, including JAR file.
      *
-     * @param uri The specified URI.
-     * @return The resolved URI
+     * @param uri the specified URI.
+     * @return true if it is a file or JAR file URI, false otherwise
      */
-    static URI resolveURI(String uri) throws MalformedURLException {
-        if (uri == null) {
-            uri = "";
+    static boolean isFileUri(URI uri) {
+        if (SCHEME_FILE.equals(uri.getScheme())
+                || SCHEME_JAR.equals(uri.getScheme())) {
+            return true;
         }
-
-        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;
+        return false;
     }
 
     /**
-     * Replace backslashes with forward slashes. (URLs always use forward
-     * slashes.)
+     * Verifies whether the file resource exists.
      *
-     * @param sysid The input system identifier.
-     * @return The same system identifier with backslashes turned into forward
-     * slashes.
+     * @param uri the URI to locate the resource
+     * @param openJarFile a flag to indicate whether a JAR file should be
+     * opened. This operation may be expensive.
+     * @return true if the resource exists, false otherwise.
      */
-    static String fixSlashes(String sysid) {
-        return sysid.replace('\\', '/');
+    static boolean isFileUriExist(URI uri, boolean openJarFile) {
+        if (uri != null && uri.isAbsolute()) {
+            if (null != uri.getScheme()) {
+                switch (uri.getScheme()) {
+                    case SCHEME_FILE:
+                        String path = uri.getPath();
+                        File f1 = new File(path);
+                        if (f1.isFile()) {
+                            return true;
+                        }
+                        break;
+                    case SCHEME_JAR:
+                        String tempUri = uri.toString();
+                        int pos = tempUri.indexOf("!");
+                        if (pos < 0) {
+                            return false;
+                        }
+                        if (openJarFile) {
+                            String jarFile = tempUri.substring(SCHEME_JARFILE.length(), pos);
+                            String entryName = tempUri.substring(pos + 2);
+                            try {
+                                JarFile jf = new JarFile(jarFile);
+                                JarEntry je = jf.getJarEntry(entryName);
+                                if (je != null) {
+                                    return true;
+                                }
+                            } catch (IOException ex) {
+                                return false;
+                            }
+                        } else {
+                            return true;
+                        }
+                        break;
+                }
+            }
+        }
+        return false;
     }
 
     /**
@@ -187,11 +236,12 @@
     }
 
     /**
-     * Checks whether the specified string is null or empty, returns the original
-     * string with leading and trailing spaces removed if not.
+     * Checks whether the specified string is null or empty, returns the
+     * original string with leading and trailing spaces removed if not.
+     *
      * @param test the string to be tested
-     * @return the original string with leading and trailing spaces removed,
-     * or null if it is null or empty
+     * @return the original string with leading and trailing spaces removed, or
+     * null if it is null or empty
      *
      */
     static String getNotNullOrEmpty(String test) {
@@ -206,4 +256,39 @@
             }
         }
     }
+
+    /**
+     * Validates the input for features.
+     *
+     * @param f the feature
+     * @param value the value
+     * @throws IllegalArgumentException if the value is invalid for the feature
+     */
+    static void validateFeatureInput(Feature f, String value) {
+        CatalogMessages.reportNPEOnNull(f.name(), value);
+        if (value.length() == 0) {
+            CatalogMessages.reportIAE(CatalogMessages.ERR_INVALID_ARGUMENT,
+                    new Object[]{value, f.name()}, null);
+        }
+
+        if (f == Feature.PREFER) {
+            if (!value.equals(PREFER_SYSTEM) && !value.equals(PREFER_PUBLIC)) {
+                CatalogMessages.reportIAE(CatalogMessages.ERR_INVALID_ARGUMENT,
+                        new Object[]{value, Feature.PREFER.name()}, null);
+            }
+        } else if (f == Feature.DEFER) {
+            if (!value.equals(DEFER_TRUE) && !value.equals(DEFER_FALSE)) {
+                CatalogMessages.reportIAE(CatalogMessages.ERR_INVALID_ARGUMENT,
+                        new Object[]{value, Feature.DEFER.name()}, null);
+            }
+        } else if (f == Feature.RESOLVE) {
+            if (!value.equals(RESOLVE_STRICT) && !value.equals(RESOLVE_CONTINUE)
+                    && !value.equals(RESOLVE_IGNORE)) {
+                CatalogMessages.reportIAE(CatalogMessages.ERR_INVALID_ARGUMENT,
+                        new Object[]{value, Feature.RESOLVE.name()}, null);
+            }
+        } else if (f == Feature.FILES) {
+            Util.validateUrisSyntax(value.split(";"));
+        }
+    }
 }