RelativePath.java revision 3283:01fdf839bbe6
1/* 2 * Copyright (c) 2008, 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 26package com.sun.tools.javac.file; 27 28import java.nio.file.FileSystem; 29import java.nio.file.FileSystems; 30import java.nio.file.InvalidPathException; 31import java.nio.file.Path; 32import java.nio.file.Paths; 33import java.util.zip.ZipEntry; 34import java.util.zip.ZipFile; 35 36import javax.tools.JavaFileObject; 37 38/** 39 * Used to represent a platform-neutral path within a platform-specific 40 * container, such as a directory or zip file. 41 * Internally, the file separator is always '/'. 42 * 43 * <p><b>This is NOT part of any supported API. 44 * If you write code that depends on this, you do so at your own risk. 45 * This code and its internal interfaces are subject to change or 46 * deletion without notice.</b> 47 */ 48public abstract class RelativePath implements Comparable<RelativePath> { 49 /** 50 * @param p must use '/' as an internal separator 51 */ 52 protected RelativePath(String p) { 53 path = p; 54 } 55 56 public abstract RelativeDirectory dirname(); 57 58 public abstract String basename(); 59 60 public Path resolveAgainst(Path directory) throws /*unchecked*/ InvalidPathException { 61 if (directory == null) { 62 String sep = FileSystems.getDefault().getSeparator(); 63 return Paths.get(path.replace("/", sep)); 64 } else { 65 String sep = directory.getFileSystem().getSeparator(); 66 return directory.resolve(path.replace("/", sep)); 67 } 68 } 69 70 public Path resolveAgainst(FileSystem fs) throws /*unchecked*/ InvalidPathException { 71 String sep = fs.getSeparator(); 72 Path root = fs.getRootDirectories().iterator().next(); 73 return root.resolve(path.replace("/", sep)); 74 } 75 76 @Override 77 public int compareTo(RelativePath other) { 78 return path.compareTo(other.path); 79 } 80 81 @Override 82 public boolean equals(Object other) { 83 if (!(other instanceof RelativePath)) 84 return false; 85 return path.equals(((RelativePath) other).path); 86 } 87 88 @Override 89 public int hashCode() { 90 return path.hashCode(); 91 } 92 93 @Override 94 public String toString() { 95 return "RelPath[" + path + "]"; 96 } 97 98 public String getPath() { 99 return path; 100 } 101 102 protected final String path; 103 104 /** 105 * Used to represent a platform-neutral subdirectory within a platform-specific 106 * container, such as a directory or zip file. 107 * Internally, the file separator is always '/', and if the path is not empty, 108 * it always ends in a '/' as well. 109 */ 110 public static class RelativeDirectory extends RelativePath { 111 112 static RelativeDirectory forPackage(CharSequence packageName) { 113 return new RelativeDirectory(packageName.toString().replace('.', '/')); 114 } 115 116 /** 117 * @param p must use '/' as an internal separator 118 */ 119 public RelativeDirectory(String p) { 120 super(p.length() == 0 || p.endsWith("/") ? p : p + "/"); 121 } 122 123 /** 124 * @param p must use '/' as an internal separator 125 */ 126 public RelativeDirectory(RelativeDirectory d, String p) { 127 this(d.path + p); 128 } 129 130 @Override 131 public RelativeDirectory dirname() { 132 int l = path.length(); 133 if (l == 0) 134 return this; 135 int sep = path.lastIndexOf('/', l - 2); 136 return new RelativeDirectory(path.substring(0, sep + 1)); 137 } 138 139 @Override 140 public String basename() { 141 int l = path.length(); 142 if (l == 0) 143 return path; 144 int sep = path.lastIndexOf('/', l - 2); 145 return path.substring(sep + 1, l - 1); 146 } 147 148 /** 149 * Return true if this subdirectory "contains" the other path. 150 * A subdirectory path does not contain itself. 151 **/ 152 boolean contains(RelativePath other) { 153 return other.path.length() > path.length() && other.path.startsWith(path); 154 } 155 156 @Override 157 public String toString() { 158 return "RelativeDirectory[" + path + "]"; 159 } 160 } 161 162 /** 163 * Used to represent a platform-neutral file within a platform-specific 164 * container, such as a directory or zip file. 165 * Internally, the file separator is always '/'. It never ends in '/'. 166 */ 167 public static class RelativeFile extends RelativePath { 168 static RelativeFile forClass(CharSequence className, JavaFileObject.Kind kind) { 169 return new RelativeFile(className.toString().replace('.', '/') + kind.extension); 170 } 171 172 public RelativeFile(String p) { 173 super(p); 174 if (p.endsWith("/")) 175 throw new IllegalArgumentException(p); 176 } 177 178 /** 179 * @param p must use '/' as an internal separator 180 */ 181 public RelativeFile(RelativeDirectory d, String p) { 182 this(d.path + p); 183 } 184 185 RelativeFile(RelativeDirectory d, RelativePath p) { 186 this(d, p.path); 187 } 188 189 @Override 190 public RelativeDirectory dirname() { 191 int sep = path.lastIndexOf('/'); 192 return new RelativeDirectory(path.substring(0, sep + 1)); 193 } 194 195 @Override 196 public String basename() { 197 int sep = path.lastIndexOf('/'); 198 return path.substring(sep + 1); 199 } 200 201 ZipEntry getZipEntry(ZipFile zip) { 202 return zip.getEntry(path); 203 } 204 205 @Override 206 public String toString() { 207 return "RelativeFile[" + path + "]"; 208 } 209 210 } 211 212} 213