1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/file/RelativePath.java Tue Aug 26 14:52:59 2008 -0700 1.3 @@ -0,0 +1,191 @@ 1.4 +/* 1.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.file; 1.30 + 1.31 +import java.io.File; 1.32 +import java.util.zip.ZipEntry; 1.33 +import java.util.zip.ZipFile; 1.34 +import javax.tools.JavaFileObject; 1.35 + 1.36 +/** 1.37 + * Used to represent a platform-neutral path within a platform-specific 1.38 + * container, such as a directory or zip file. 1.39 + * Internally, the file separator is always '/'. 1.40 + */ 1.41 +public abstract class RelativePath implements Comparable<RelativePath> { 1.42 + /** 1.43 + * @param p must use '/' as an internal separator 1.44 + */ 1.45 + protected RelativePath(String p) { 1.46 + path = p; 1.47 + } 1.48 + 1.49 + public abstract RelativeDirectory dirname(); 1.50 + 1.51 + public abstract String basename(); 1.52 + 1.53 + public File getFile(File directory) { 1.54 + if (path.length() == 0) 1.55 + return directory; 1.56 + return new File(directory, path.replace('/', File.separatorChar)); 1.57 + } 1.58 + 1.59 + public int compareTo(RelativePath other) { 1.60 + return path.compareTo(other.path); 1.61 + } 1.62 + 1.63 + @Override 1.64 + public boolean equals(Object other) { 1.65 + if (!(other instanceof RelativePath)) 1.66 + return false; 1.67 + return path.equals(((RelativePath) other).path); 1.68 + } 1.69 + 1.70 + @Override 1.71 + public int hashCode() { 1.72 + return path.hashCode(); 1.73 + } 1.74 + 1.75 + @Override 1.76 + public String toString() { 1.77 + return "RelPath[" + path + "]"; 1.78 + } 1.79 + 1.80 + public String getPath() { 1.81 + return path; 1.82 + } 1.83 + 1.84 + protected final String path; 1.85 + 1.86 + /** 1.87 + * Used to represent a platform-neutral subdirectory within a platform-specific 1.88 + * container, such as a directory or zip file. 1.89 + * Internally, the file separator is always '/', and if the path is not empty, 1.90 + * it always ends in a '/' as well. 1.91 + */ 1.92 + public static class RelativeDirectory extends RelativePath { 1.93 + 1.94 + static RelativeDirectory forPackage(CharSequence packageName) { 1.95 + return new RelativeDirectory(packageName.toString().replace('.', '/')); 1.96 + } 1.97 + 1.98 + /** 1.99 + * @param p must use '/' as an internal separator 1.100 + */ 1.101 + public RelativeDirectory(String p) { 1.102 + super(p.length() == 0 || p.endsWith("/") ? p : p + "/"); 1.103 + } 1.104 + 1.105 + /** 1.106 + * @param p must use '/' as an internal separator 1.107 + */ 1.108 + public RelativeDirectory(RelativeDirectory d, String p) { 1.109 + this(d.path + p); 1.110 + } 1.111 + 1.112 + @Override 1.113 + public RelativeDirectory dirname() { 1.114 + int l = path.length(); 1.115 + if (l == 0) 1.116 + return this; 1.117 + int sep = path.lastIndexOf('/', l - 2); 1.118 + return new RelativeDirectory(path.substring(0, sep + 1)); 1.119 + } 1.120 + 1.121 + @Override 1.122 + public String basename() { 1.123 + int l = path.length(); 1.124 + if (l == 0) 1.125 + return path; 1.126 + int sep = path.lastIndexOf('/', l - 2); 1.127 + return path.substring(sep + 1, l - 1); 1.128 + } 1.129 + 1.130 + /** 1.131 + * Return true if this subdirectory "contains" the other path. 1.132 + * A subdirectory path does not contain itself. 1.133 + **/ 1.134 + boolean contains(RelativePath other) { 1.135 + return other.path.length() > path.length() && other.path.startsWith(path); 1.136 + } 1.137 + 1.138 + @Override 1.139 + public String toString() { 1.140 + return "RelativeDirectory[" + path + "]"; 1.141 + } 1.142 + } 1.143 + 1.144 + /** 1.145 + * Used to represent a platform-neutral file within a platform-specific 1.146 + * container, such as a directory or zip file. 1.147 + * Internally, the file separator is always '/'. It never ends in '/'. 1.148 + */ 1.149 + public static class RelativeFile extends RelativePath { 1.150 + static RelativeFile forClass(CharSequence className, JavaFileObject.Kind kind) { 1.151 + return new RelativeFile(className.toString().replace('.', '/') + kind.extension); 1.152 + } 1.153 + 1.154 + public RelativeFile(String p) { 1.155 + super(p); 1.156 + if (p.endsWith("/")) 1.157 + throw new IllegalArgumentException(p); 1.158 + } 1.159 + 1.160 + /** 1.161 + * @param p must use '/' as an internal separator 1.162 + */ 1.163 + public RelativeFile(RelativeDirectory d, String p) { 1.164 + this(d.path + p); 1.165 + } 1.166 + 1.167 + RelativeFile(RelativeDirectory d, RelativePath p) { 1.168 + this(d, p.path); 1.169 + } 1.170 + 1.171 + @Override 1.172 + public RelativeDirectory dirname() { 1.173 + int sep = path.lastIndexOf('/'); 1.174 + return new RelativeDirectory(path.substring(0, sep + 1)); 1.175 + } 1.176 + 1.177 + @Override 1.178 + public String basename() { 1.179 + int sep = path.lastIndexOf('/'); 1.180 + return path.substring(sep + 1); 1.181 + } 1.182 + 1.183 + ZipEntry getZipEntry(ZipFile zip) { 1.184 + return zip.getEntry(path); 1.185 + } 1.186 + 1.187 + @Override 1.188 + public String toString() { 1.189 + return "RelativeFile[" + path + "]"; 1.190 + } 1.191 + 1.192 + } 1.193 + 1.194 +}