< prev index next >

src/java.base/share/classes/jdk/internal/module/Resources.java

Print this page




   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 jdk.internal.loader;
  26 
  27 import java.io.File;



  28 import java.nio.file.Path;
  29 import java.nio.file.Paths;
  30 
  31 import jdk.internal.module.Checks;
  32 
  33 /**
  34  * Helper class for Class#getResource, Module#getResourceAsStream, and other
  35  * methods that locate a resource in a module.
  36  */
  37 public final class ResourceHelper {
  38     private ResourceHelper() { }
  39 
  40     /**
  41      * Returns the <em>package name</em> for a resource or the empty package if
  42      * the resource name does not contain a slash.

  43      */
  44     public static String getPackageName(String name) {
  45         int index = name.lastIndexOf('/');
  46         if (index != -1) {
  47             return name.substring(0, index).replace("/", ".");
  48         } else {













  49             return "";


  50         }
  51     }
  52 
  53     /**
  54      * Returns true if the resource is a <em>simple resource</em>. Simple
  55      * resources can never be encapsulated. Resources ending in "{@code .class}"
  56      * or where the package name is not a legal package name can not be
  57      * encapsulated.
  58      */
  59     public static boolean isSimpleResource(String name) {
  60         int len = name.length();
  61         if (len > 6 && name.endsWith(".class")) {
  62             return true;



  63         }
  64         if (!Checks.isPackageName(getPackageName(name))) {
  65             return true;




















  66         }
  67         return false;
  68     }
  69 
  70     /**
  71      * Converts a resource name to a file path. Returns {@code null} if the
  72      * resource name cannot be converted into a file path. Resource names
  73      * with empty elements, or elements that are "." or ".." are rejected,
  74      * as is a resource name that translates to a file path with a root
  75      * component.

  76      */
  77     public static Path toFilePath(String name) {
  78         // scan the resource name to eagerly reject obviously invalid names
  79         int next;
  80         int off = 0;
  81         while ((next = name.indexOf('/', off)) != -1) {
  82             int len = next - off;
  83             if (!mayTranslate(name, off, len)) {
  84                 return null;
  85             }
  86             off = next + 1;
  87         }
  88         int rem = name.length() - off;
  89         if (!mayTranslate(name, off, rem)) {
  90             return null;
  91         }
  92 
  93         // convert to file path
  94         Path path;
  95         if (File.separatorChar == '/') {
  96             path = Paths.get(name);
  97         } else {
  98             // not allowed to embed file separators




   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 jdk.internal.module;
  26 
  27 import java.io.File;
  28 import java.io.IOException;
  29 import java.nio.file.Files;
  30 import java.nio.file.NoSuchFileException;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.nio.file.attribute.BasicFileAttributes;

  34 
  35 /**
  36  * A helper class to support working with resources in modules. Also provides
  37  * support for translating resource names to file paths.
  38  */
  39 public final class Resources {
  40     private Resources() { }
  41 
  42     /**
  43      * Return true if a resource can be encapsulated. Resource with names
  44      * ending in ".class" or "/" cannot be encapsulated. Resource names
  45      * that map to a legal package name can be encapsulated.
  46      */
  47     public static boolean canEncapsulate(String name) {
  48         int len = name.length();
  49         if (len > 6 && name.endsWith(".class")) {
  50             return false;
  51         } else {
  52             return Checks.isPackageName(toPackageName(name));
  53         }
  54     }
  55 
  56     /**
  57      * Derive a <em>package name</em> for a resource. The package name
  58      * returned by this method may not be a legal package name. This method
  59      * returns null if the the resource name ends with a "/" (a directory)
  60      * or the resource name does not contain a "/".
  61      */
  62     public static String toPackageName(String name) {
  63         int index = name.lastIndexOf('/');
  64         if (index == -1 || index == name.length()-1) {
  65             return "";
  66         } else {
  67             return name.substring(0, index).replace("/", ".");
  68         }
  69     }
  70 
  71     /**
  72      * Returns a resource name corresponding to the relative file path
  73      * between {@code dir} and {@code file}. If the file is a directory
  74      * then the name will end with a  "/", except the top-level directory
  75      * where the empty string is returned.
  76      */
  77     public static String toResourceName(Path dir, Path file) {
  78         String s = dir.relativize(file)
  79                       .toString()
  80                       .replace(File.separatorChar, '/');
  81         if (s.length() > 0 && Files.isDirectory(file))
  82             s += "/";
  83         return s;
  84     }
  85 
  86     /**
  87      * Returns a file path to a resource in a file tree. If the resource
  88      * name has a trailing "/" then the file path will locate a directory.
  89      * Returns {@code null} if the resource does not map to a file in the
  90      * tree file.
  91      */
  92     public static Path toFilePath(Path dir, String name) throws IOException {
  93         boolean expectDirectory = name.endsWith("/");
  94         if (expectDirectory) {
  95             name = name.substring(0, name.length() - 1);  // drop trailing "/"
  96         }
  97         Path path = toSafeFilePath(name);
  98         if (path != null) {
  99             Path file = dir.resolve(path);
 100             try {
 101                 BasicFileAttributes attrs;
 102                 attrs = Files.readAttributes(file, BasicFileAttributes.class);
 103                 if (attrs.isDirectory()
 104                     || (!attrs.isDirectory() && !expectDirectory))
 105                     return file;
 106             } catch (NoSuchFileException ignore) { }
 107         }
 108         return null;
 109     }
 110 
 111     /**
 112      * Map a resource name to a "safe" file path. Returns {@code null} if
 113      * the resource name cannot be converted into a "safe" file path.
 114      *
 115      * Resource names with empty elements, or elements that are "." or ".."
 116      * are rejected, as are resource names that translates to a file path
 117      * with a root component.
 118      */
 119     private static Path toSafeFilePath(String name) {
 120         // scan elements of resource name
 121         int next;
 122         int off = 0;
 123         while ((next = name.indexOf('/', off)) != -1) {
 124             int len = next - off;
 125             if (!mayTranslate(name, off, len)) {
 126                 return null;
 127             }
 128             off = next + 1;
 129         }
 130         int rem = name.length() - off;
 131         if (!mayTranslate(name, off, rem)) {
 132             return null;
 133         }
 134 
 135         // convert to file path
 136         Path path;
 137         if (File.separatorChar == '/') {
 138             path = Paths.get(name);
 139         } else {
 140             // not allowed to embed file separators


< prev index next >