src/share/classes/com/sun/tools/javac/file/RelativePath.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 581
f2fdd52e4e87
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial