RelativePath.java revision 2674:e284f560acf6
1104834Sobrien/* 2218822Sdim * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved. 3218822Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4104834Sobrien * 5104834Sobrien * This code is free software; you can redistribute it and/or modify it 6104834Sobrien * under the terms of the GNU General Public License version 2 only, as 7104834Sobrien * published by the Free Software Foundation. Oracle designates this 8104834Sobrien * particular file as subject to the "Classpath" exception as provided 9104834Sobrien * by Oracle in the LICENSE file that accompanied this code. 10104834Sobrien * 11104834Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT 12104834Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13104834Sobrien * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14104834Sobrien * version 2 for more details (a copy is included in the LICENSE file that 15104834Sobrien * accompanied this code). 16104834Sobrien * 17104834Sobrien * You should have received a copy of the GNU General Public License version 18104834Sobrien * 2 along with this work; if not, write to the Free Software Foundation, 19218822Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20104834Sobrien * 21104834Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22104834Sobrien * or visit www.oracle.com if you need additional information or have any 23104834Sobrien * questions. 24218822Sdim */ 25104834Sobrien 26104834Sobrienpackage com.sun.tools.javac.file; 27104834Sobrien 28130561Sobrienimport java.nio.file.Path; 29130561Sobrienimport java.nio.file.Paths; 30130561Sobrienimport java.util.zip.ZipEntry; 31130561Sobrienimport java.util.zip.ZipFile; 32130561Sobrien 33130561Sobrienimport javax.tools.JavaFileObject; 34130561Sobrien 35130561Sobrien/** 36104834Sobrien * Used to represent a platform-neutral path within a platform-specific 37104834Sobrien * container, such as a directory or zip file. 38104834Sobrien * Internally, the file separator is always '/'. 39104834Sobrien * 40104834Sobrien * <p><b>This is NOT part of any supported API. 41104834Sobrien * If you write code that depends on this, you do so at your own risk. 42104834Sobrien * This code and its internal interfaces are subject to change or 43104834Sobrien * deletion without notice.</b> 44218822Sdim */ 45104834Sobrienpublic abstract class RelativePath implements Comparable<RelativePath> { 46104834Sobrien /** 47104834Sobrien * @param p must use '/' as an internal separator 48104834Sobrien */ 49104834Sobrien protected RelativePath(String p) { 50104834Sobrien path = p; 51104834Sobrien } 52104834Sobrien 53104834Sobrien public abstract RelativeDirectory dirname(); 54104834Sobrien 55130561Sobrien public abstract String basename(); 56130561Sobrien 57130561Sobrien public Path getFile(Path directory) { 58130561Sobrien if (directory == null) 59104834Sobrien directory = Paths.get(""); 60104834Sobrien String sep = directory.getFileSystem().getSeparator(); 61104834Sobrien return directory.resolve(path.replace("/", sep)); 62104834Sobrien } 63 64 @Override 65 public int compareTo(RelativePath other) { 66 return path.compareTo(other.path); 67 } 68 69 @Override 70 public boolean equals(Object other) { 71 if (!(other instanceof RelativePath)) 72 return false; 73 return path.equals(((RelativePath) other).path); 74 } 75 76 @Override 77 public int hashCode() { 78 return path.hashCode(); 79 } 80 81 @Override 82 public String toString() { 83 return "RelPath[" + path + "]"; 84 } 85 86 public String getPath() { 87 return path; 88 } 89 90 protected final String path; 91 92 /** 93 * Used to represent a platform-neutral subdirectory within a platform-specific 94 * container, such as a directory or zip file. 95 * Internally, the file separator is always '/', and if the path is not empty, 96 * it always ends in a '/' as well. 97 */ 98 public static class RelativeDirectory extends RelativePath { 99 100 static RelativeDirectory forPackage(CharSequence packageName) { 101 return new RelativeDirectory(packageName.toString().replace('.', '/')); 102 } 103 104 /** 105 * @param p must use '/' as an internal separator 106 */ 107 public RelativeDirectory(String p) { 108 super(p.length() == 0 || p.endsWith("/") ? p : p + "/"); 109 } 110 111 /** 112 * @param p must use '/' as an internal separator 113 */ 114 public RelativeDirectory(RelativeDirectory d, String p) { 115 this(d.path + p); 116 } 117 118 @Override 119 public RelativeDirectory dirname() { 120 int l = path.length(); 121 if (l == 0) 122 return this; 123 int sep = path.lastIndexOf('/', l - 2); 124 return new RelativeDirectory(path.substring(0, sep + 1)); 125 } 126 127 @Override 128 public String basename() { 129 int l = path.length(); 130 if (l == 0) 131 return path; 132 int sep = path.lastIndexOf('/', l - 2); 133 return path.substring(sep + 1, l - 1); 134 } 135 136 /** 137 * Return true if this subdirectory "contains" the other path. 138 * A subdirectory path does not contain itself. 139 **/ 140 boolean contains(RelativePath other) { 141 return other.path.length() > path.length() && other.path.startsWith(path); 142 } 143 144 @Override 145 public String toString() { 146 return "RelativeDirectory[" + path + "]"; 147 } 148 } 149 150 /** 151 * Used to represent a platform-neutral file within a platform-specific 152 * container, such as a directory or zip file. 153 * Internally, the file separator is always '/'. It never ends in '/'. 154 */ 155 public static class RelativeFile extends RelativePath { 156 static RelativeFile forClass(CharSequence className, JavaFileObject.Kind kind) { 157 return new RelativeFile(className.toString().replace('.', '/') + kind.extension); 158 } 159 160 public RelativeFile(String p) { 161 super(p); 162 if (p.endsWith("/")) 163 throw new IllegalArgumentException(p); 164 } 165 166 /** 167 * @param p must use '/' as an internal separator 168 */ 169 public RelativeFile(RelativeDirectory d, String p) { 170 this(d.path + p); 171 } 172 173 RelativeFile(RelativeDirectory d, RelativePath p) { 174 this(d, p.path); 175 } 176 177 @Override 178 public RelativeDirectory dirname() { 179 int sep = path.lastIndexOf('/'); 180 return new RelativeDirectory(path.substring(0, sep + 1)); 181 } 182 183 @Override 184 public String basename() { 185 int sep = path.lastIndexOf('/'); 186 return path.substring(sep + 1); 187 } 188 189 ZipEntry getZipEntry(ZipFile zip) { 190 return zip.getEntry(path); 191 } 192 193 @Override 194 public String toString() { 195 return "RelativeFile[" + path + "]"; 196 } 197 198 } 199 200} 201