Tue, 26 Aug 2008 14:52:59 -0700
6508981: cleanup file separator handling in JavacFileManager
Reviewed-by: mcimadamore
jjg@103 | 1 | /* |
jjg@103 | 2 | * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. |
jjg@103 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@103 | 4 | * |
jjg@103 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@103 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@103 | 7 | * published by the Free Software Foundation. Sun designates this |
jjg@103 | 8 | * particular file as subject to the "Classpath" exception as provided |
jjg@103 | 9 | * by Sun in the LICENSE file that accompanied this code. |
jjg@103 | 10 | * |
jjg@103 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@103 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@103 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@103 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@103 | 15 | * accompanied this code). |
jjg@103 | 16 | * |
jjg@103 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@103 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@103 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@103 | 20 | * |
jjg@103 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@103 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@103 | 23 | * have any questions. |
jjg@103 | 24 | */ |
jjg@103 | 25 | |
jjg@103 | 26 | package com.sun.tools.javac.file; |
jjg@103 | 27 | |
jjg@103 | 28 | import java.io.File; |
jjg@103 | 29 | import java.util.zip.ZipEntry; |
jjg@103 | 30 | import java.util.zip.ZipFile; |
jjg@103 | 31 | import javax.tools.JavaFileObject; |
jjg@103 | 32 | |
jjg@103 | 33 | /** |
jjg@103 | 34 | * Used to represent a platform-neutral path within a platform-specific |
jjg@103 | 35 | * container, such as a directory or zip file. |
jjg@103 | 36 | * Internally, the file separator is always '/'. |
jjg@103 | 37 | */ |
jjg@103 | 38 | public abstract class RelativePath implements Comparable<RelativePath> { |
jjg@103 | 39 | /** |
jjg@103 | 40 | * @param p must use '/' as an internal separator |
jjg@103 | 41 | */ |
jjg@103 | 42 | protected RelativePath(String p) { |
jjg@103 | 43 | path = p; |
jjg@103 | 44 | } |
jjg@103 | 45 | |
jjg@103 | 46 | public abstract RelativeDirectory dirname(); |
jjg@103 | 47 | |
jjg@103 | 48 | public abstract String basename(); |
jjg@103 | 49 | |
jjg@103 | 50 | public File getFile(File directory) { |
jjg@103 | 51 | if (path.length() == 0) |
jjg@103 | 52 | return directory; |
jjg@103 | 53 | return new File(directory, path.replace('/', File.separatorChar)); |
jjg@103 | 54 | } |
jjg@103 | 55 | |
jjg@103 | 56 | public int compareTo(RelativePath other) { |
jjg@103 | 57 | return path.compareTo(other.path); |
jjg@103 | 58 | } |
jjg@103 | 59 | |
jjg@103 | 60 | @Override |
jjg@103 | 61 | public boolean equals(Object other) { |
jjg@103 | 62 | if (!(other instanceof RelativePath)) |
jjg@103 | 63 | return false; |
jjg@103 | 64 | return path.equals(((RelativePath) other).path); |
jjg@103 | 65 | } |
jjg@103 | 66 | |
jjg@103 | 67 | @Override |
jjg@103 | 68 | public int hashCode() { |
jjg@103 | 69 | return path.hashCode(); |
jjg@103 | 70 | } |
jjg@103 | 71 | |
jjg@103 | 72 | @Override |
jjg@103 | 73 | public String toString() { |
jjg@103 | 74 | return "RelPath[" + path + "]"; |
jjg@103 | 75 | } |
jjg@103 | 76 | |
jjg@103 | 77 | public String getPath() { |
jjg@103 | 78 | return path; |
jjg@103 | 79 | } |
jjg@103 | 80 | |
jjg@103 | 81 | protected final String path; |
jjg@103 | 82 | |
jjg@103 | 83 | /** |
jjg@103 | 84 | * Used to represent a platform-neutral subdirectory within a platform-specific |
jjg@103 | 85 | * container, such as a directory or zip file. |
jjg@103 | 86 | * Internally, the file separator is always '/', and if the path is not empty, |
jjg@103 | 87 | * it always ends in a '/' as well. |
jjg@103 | 88 | */ |
jjg@103 | 89 | public static class RelativeDirectory extends RelativePath { |
jjg@103 | 90 | |
jjg@103 | 91 | static RelativeDirectory forPackage(CharSequence packageName) { |
jjg@103 | 92 | return new RelativeDirectory(packageName.toString().replace('.', '/')); |
jjg@103 | 93 | } |
jjg@103 | 94 | |
jjg@103 | 95 | /** |
jjg@103 | 96 | * @param p must use '/' as an internal separator |
jjg@103 | 97 | */ |
jjg@103 | 98 | public RelativeDirectory(String p) { |
jjg@103 | 99 | super(p.length() == 0 || p.endsWith("/") ? p : p + "/"); |
jjg@103 | 100 | } |
jjg@103 | 101 | |
jjg@103 | 102 | /** |
jjg@103 | 103 | * @param p must use '/' as an internal separator |
jjg@103 | 104 | */ |
jjg@103 | 105 | public RelativeDirectory(RelativeDirectory d, String p) { |
jjg@103 | 106 | this(d.path + p); |
jjg@103 | 107 | } |
jjg@103 | 108 | |
jjg@103 | 109 | @Override |
jjg@103 | 110 | public RelativeDirectory dirname() { |
jjg@103 | 111 | int l = path.length(); |
jjg@103 | 112 | if (l == 0) |
jjg@103 | 113 | return this; |
jjg@103 | 114 | int sep = path.lastIndexOf('/', l - 2); |
jjg@103 | 115 | return new RelativeDirectory(path.substring(0, sep + 1)); |
jjg@103 | 116 | } |
jjg@103 | 117 | |
jjg@103 | 118 | @Override |
jjg@103 | 119 | public String basename() { |
jjg@103 | 120 | int l = path.length(); |
jjg@103 | 121 | if (l == 0) |
jjg@103 | 122 | return path; |
jjg@103 | 123 | int sep = path.lastIndexOf('/', l - 2); |
jjg@103 | 124 | return path.substring(sep + 1, l - 1); |
jjg@103 | 125 | } |
jjg@103 | 126 | |
jjg@103 | 127 | /** |
jjg@103 | 128 | * Return true if this subdirectory "contains" the other path. |
jjg@103 | 129 | * A subdirectory path does not contain itself. |
jjg@103 | 130 | **/ |
jjg@103 | 131 | boolean contains(RelativePath other) { |
jjg@103 | 132 | return other.path.length() > path.length() && other.path.startsWith(path); |
jjg@103 | 133 | } |
jjg@103 | 134 | |
jjg@103 | 135 | @Override |
jjg@103 | 136 | public String toString() { |
jjg@103 | 137 | return "RelativeDirectory[" + path + "]"; |
jjg@103 | 138 | } |
jjg@103 | 139 | } |
jjg@103 | 140 | |
jjg@103 | 141 | /** |
jjg@103 | 142 | * Used to represent a platform-neutral file within a platform-specific |
jjg@103 | 143 | * container, such as a directory or zip file. |
jjg@103 | 144 | * Internally, the file separator is always '/'. It never ends in '/'. |
jjg@103 | 145 | */ |
jjg@103 | 146 | public static class RelativeFile extends RelativePath { |
jjg@103 | 147 | static RelativeFile forClass(CharSequence className, JavaFileObject.Kind kind) { |
jjg@103 | 148 | return new RelativeFile(className.toString().replace('.', '/') + kind.extension); |
jjg@103 | 149 | } |
jjg@103 | 150 | |
jjg@103 | 151 | public RelativeFile(String p) { |
jjg@103 | 152 | super(p); |
jjg@103 | 153 | if (p.endsWith("/")) |
jjg@103 | 154 | throw new IllegalArgumentException(p); |
jjg@103 | 155 | } |
jjg@103 | 156 | |
jjg@103 | 157 | /** |
jjg@103 | 158 | * @param p must use '/' as an internal separator |
jjg@103 | 159 | */ |
jjg@103 | 160 | public RelativeFile(RelativeDirectory d, String p) { |
jjg@103 | 161 | this(d.path + p); |
jjg@103 | 162 | } |
jjg@103 | 163 | |
jjg@103 | 164 | RelativeFile(RelativeDirectory d, RelativePath p) { |
jjg@103 | 165 | this(d, p.path); |
jjg@103 | 166 | } |
jjg@103 | 167 | |
jjg@103 | 168 | @Override |
jjg@103 | 169 | public RelativeDirectory dirname() { |
jjg@103 | 170 | int sep = path.lastIndexOf('/'); |
jjg@103 | 171 | return new RelativeDirectory(path.substring(0, sep + 1)); |
jjg@103 | 172 | } |
jjg@103 | 173 | |
jjg@103 | 174 | @Override |
jjg@103 | 175 | public String basename() { |
jjg@103 | 176 | int sep = path.lastIndexOf('/'); |
jjg@103 | 177 | return path.substring(sep + 1); |
jjg@103 | 178 | } |
jjg@103 | 179 | |
jjg@103 | 180 | ZipEntry getZipEntry(ZipFile zip) { |
jjg@103 | 181 | return zip.getEntry(path); |
jjg@103 | 182 | } |
jjg@103 | 183 | |
jjg@103 | 184 | @Override |
jjg@103 | 185 | public String toString() { |
jjg@103 | 186 | return "RelativeFile[" + path + "]"; |
jjg@103 | 187 | } |
jjg@103 | 188 | |
jjg@103 | 189 | } |
jjg@103 | 190 | |
jjg@103 | 191 | } |