jjg@103: /* ohair@554: * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. jjg@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@103: * jjg@103: * This code is free software; you can redistribute it and/or modify it jjg@103: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this jjg@103: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. jjg@103: * jjg@103: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@103: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@103: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@103: * version 2 for more details (a copy is included in the LICENSE file that jjg@103: * accompanied this code). jjg@103: * jjg@103: * You should have received a copy of the GNU General Public License version jjg@103: * 2 along with this work; if not, write to the Free Software Foundation, jjg@103: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@103: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@103: */ jjg@103: jjg@103: package com.sun.tools.javac.file; jjg@103: jjg@103: import java.io.File; jjg@103: import java.util.zip.ZipEntry; jjg@103: import java.util.zip.ZipFile; jjg@103: import javax.tools.JavaFileObject; jjg@103: jjg@103: /** jjg@103: * Used to represent a platform-neutral path within a platform-specific jjg@103: * container, such as a directory or zip file. jjg@103: * Internally, the file separator is always '/'. jjg@333: * jjg@581: *

This is NOT part of any supported API. jjg@333: * If you write code that depends on this, you do so at your own risk. jjg@333: * This code and its internal interfaces are subject to change or jjg@333: * deletion without notice. jjg@103: */ jjg@103: public abstract class RelativePath implements Comparable { jjg@103: /** jjg@103: * @param p must use '/' as an internal separator jjg@103: */ jjg@103: protected RelativePath(String p) { jjg@103: path = p; jjg@103: } jjg@103: jjg@103: public abstract RelativeDirectory dirname(); jjg@103: jjg@103: public abstract String basename(); jjg@103: jjg@103: public File getFile(File directory) { jjg@103: if (path.length() == 0) jjg@103: return directory; jjg@103: return new File(directory, path.replace('/', File.separatorChar)); jjg@103: } jjg@103: jjg@103: public int compareTo(RelativePath other) { jjg@103: return path.compareTo(other.path); jjg@103: } jjg@103: jjg@103: @Override jjg@103: public boolean equals(Object other) { jjg@103: if (!(other instanceof RelativePath)) jjg@103: return false; jjg@103: return path.equals(((RelativePath) other).path); jjg@103: } jjg@103: jjg@103: @Override jjg@103: public int hashCode() { jjg@103: return path.hashCode(); jjg@103: } jjg@103: jjg@103: @Override jjg@103: public String toString() { jjg@103: return "RelPath[" + path + "]"; jjg@103: } jjg@103: jjg@103: public String getPath() { jjg@103: return path; jjg@103: } jjg@103: jjg@103: protected final String path; jjg@103: jjg@103: /** jjg@103: * Used to represent a platform-neutral subdirectory within a platform-specific jjg@103: * container, such as a directory or zip file. jjg@103: * Internally, the file separator is always '/', and if the path is not empty, jjg@103: * it always ends in a '/' as well. jjg@103: */ jjg@103: public static class RelativeDirectory extends RelativePath { jjg@103: jjg@103: static RelativeDirectory forPackage(CharSequence packageName) { jjg@103: return new RelativeDirectory(packageName.toString().replace('.', '/')); jjg@103: } jjg@103: jjg@103: /** jjg@103: * @param p must use '/' as an internal separator jjg@103: */ jjg@103: public RelativeDirectory(String p) { jjg@103: super(p.length() == 0 || p.endsWith("/") ? p : p + "/"); jjg@103: } jjg@103: jjg@103: /** jjg@103: * @param p must use '/' as an internal separator jjg@103: */ jjg@103: public RelativeDirectory(RelativeDirectory d, String p) { jjg@103: this(d.path + p); jjg@103: } jjg@103: jjg@103: @Override jjg@103: public RelativeDirectory dirname() { jjg@103: int l = path.length(); jjg@103: if (l == 0) jjg@103: return this; jjg@103: int sep = path.lastIndexOf('/', l - 2); jjg@103: return new RelativeDirectory(path.substring(0, sep + 1)); jjg@103: } jjg@103: jjg@103: @Override jjg@103: public String basename() { jjg@103: int l = path.length(); jjg@103: if (l == 0) jjg@103: return path; jjg@103: int sep = path.lastIndexOf('/', l - 2); jjg@103: return path.substring(sep + 1, l - 1); jjg@103: } jjg@103: jjg@103: /** jjg@103: * Return true if this subdirectory "contains" the other path. jjg@103: * A subdirectory path does not contain itself. jjg@103: **/ jjg@103: boolean contains(RelativePath other) { jjg@103: return other.path.length() > path.length() && other.path.startsWith(path); jjg@103: } jjg@103: jjg@103: @Override jjg@103: public String toString() { jjg@103: return "RelativeDirectory[" + path + "]"; jjg@103: } jjg@103: } jjg@103: jjg@103: /** jjg@103: * Used to represent a platform-neutral file within a platform-specific jjg@103: * container, such as a directory or zip file. jjg@103: * Internally, the file separator is always '/'. It never ends in '/'. jjg@103: */ jjg@103: public static class RelativeFile extends RelativePath { jjg@103: static RelativeFile forClass(CharSequence className, JavaFileObject.Kind kind) { jjg@103: return new RelativeFile(className.toString().replace('.', '/') + kind.extension); jjg@103: } jjg@103: jjg@103: public RelativeFile(String p) { jjg@103: super(p); jjg@103: if (p.endsWith("/")) jjg@103: throw new IllegalArgumentException(p); jjg@103: } jjg@103: jjg@103: /** jjg@103: * @param p must use '/' as an internal separator jjg@103: */ jjg@103: public RelativeFile(RelativeDirectory d, String p) { jjg@103: this(d.path + p); jjg@103: } jjg@103: jjg@103: RelativeFile(RelativeDirectory d, RelativePath p) { jjg@103: this(d, p.path); jjg@103: } jjg@103: jjg@103: @Override jjg@103: public RelativeDirectory dirname() { jjg@103: int sep = path.lastIndexOf('/'); jjg@103: return new RelativeDirectory(path.substring(0, sep + 1)); jjg@103: } jjg@103: jjg@103: @Override jjg@103: public String basename() { jjg@103: int sep = path.lastIndexOf('/'); jjg@103: return path.substring(sep + 1); jjg@103: } jjg@103: jjg@103: ZipEntry getZipEntry(ZipFile zip) { jjg@103: return zip.getEntry(path); jjg@103: } jjg@103: jjg@103: @Override jjg@103: public String toString() { jjg@103: return "RelativeFile[" + path + "]"; jjg@103: } jjg@103: jjg@103: } jjg@103: jjg@103: }