Path: blob/master/src/java.base/share/classes/jdk/internal/module/Resources.java
41159 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package jdk.internal.module;2526import java.io.File;27import java.io.IOException;28import java.nio.file.FileSystem;29import java.nio.file.Files;30import java.nio.file.NoSuchFileException;31import java.nio.file.Path;32import java.nio.file.attribute.BasicFileAttributes;3334/**35* A helper class to support working with resources in modules. Also provides36* support for translating resource names to file paths.37*/38public final class Resources {39private Resources() { }4041/**42* Return true if a resource can be encapsulated. Resource with names43* ending in ".class" or "/" cannot be encapsulated. Resource names44* that map to a legal package name can be encapsulated.45*/46public static boolean canEncapsulate(String name) {47int len = name.length();48if (len > 6 && name.endsWith(".class")) {49return false;50} else {51return Checks.isPackageName(toPackageName(name));52}53}5455/**56* Derive a <em>package name</em> for a resource. The package name57* returned by this method may not be a legal package name. This method58* returns null if the resource name ends with a "/" (a directory)59* or the resource name does not contain a "/".60*/61public static String toPackageName(String name) {62int index = name.lastIndexOf('/');63if (index == -1 || index == name.length()-1) {64return "";65} else {66return name.substring(0, index).replace('/', '.');67}68}6970/**71* Returns a resource name corresponding to the relative file path72* between {@code dir} and {@code file}. If the file is a directory73* then the name will end with a "/", except the top-level directory74* where the empty string is returned.75*/76public static String toResourceName(Path dir, Path file) {77String s = dir.relativize(file)78.toString()79.replace(File.separatorChar, '/');80if (!s.isEmpty() && Files.isDirectory(file))81s += "/";82return s;83}8485/**86* Returns a file path to a resource in a file tree. If the resource87* name has a trailing "/" then the file path will locate a directory.88* Returns {@code null} if the resource does not map to a file in the89* tree file.90*/91public static Path toFilePath(Path dir, String name) throws IOException {92boolean expectDirectory = name.endsWith("/");93if (expectDirectory) {94name = name.substring(0, name.length() - 1); // drop trailing "/"95}96Path path = toSafeFilePath(dir.getFileSystem(), name);97if (path != null) {98Path file = dir.resolve(path);99try {100BasicFileAttributes attrs;101attrs = Files.readAttributes(file, BasicFileAttributes.class);102if (attrs.isDirectory()103|| (!attrs.isDirectory() && !expectDirectory))104return file;105} catch (NoSuchFileException ignore) { }106}107return null;108}109110/**111* Map a resource name to a "safe" file path. Returns {@code null} if112* the resource name cannot be converted into a "safe" file path.113*114* Resource names with empty elements, or elements that are "." or ".."115* are rejected, as are resource names that translates to a file path116* with a root component.117*/118private static Path toSafeFilePath(FileSystem fs, String name) {119// scan elements of resource name120int next;121int off = 0;122while ((next = name.indexOf('/', off)) != -1) {123int len = next - off;124if (!mayTranslate(name, off, len)) {125return null;126}127off = next + 1;128}129int rem = name.length() - off;130if (!mayTranslate(name, off, rem)) {131return null;132}133134// convert to file path135Path path;136if (File.separatorChar == '/') {137path = fs.getPath(name);138} else {139// not allowed to embed file separators140if (name.contains(File.separator))141return null;142path = fs.getPath(name.replace('/', File.separatorChar));143}144145// file path not allowed to have root component146return (path.getRoot() == null) ? path : null;147}148149/**150* Returns {@code true} if the element in a resource name is a candidate151* to translate to the element of a file path.152*/153private static boolean mayTranslate(String name, int off, int len) {154if (len <= 2) {155if (len == 0)156return false;157boolean starsWithDot = (name.charAt(off) == '.');158if (len == 1 && starsWithDot)159return false;160if (len == 2 && starsWithDot && (name.charAt(off+1) == '.'))161return false;162}163return true;164}165166}167168169