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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     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	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,196 @@
     1.4 +/*
     1.5 + * Copyright (c) 2008, Oracle and/or its affiliates. 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.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * 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 + * <p><b>This is NOT part of any supported API.
    1.42 + * If you write code that depends on this, you do so at your own risk.
    1.43 + * This code and its internal interfaces are subject to change or
    1.44 + * deletion without notice.</b>
    1.45 + */
    1.46 +public abstract class RelativePath implements Comparable<RelativePath> {
    1.47 +    /**
    1.48 +     * @param p must use '/' as an internal separator
    1.49 +     */
    1.50 +    protected RelativePath(String p) {
    1.51 +        path = p;
    1.52 +    }
    1.53 +
    1.54 +    public abstract RelativeDirectory dirname();
    1.55 +
    1.56 +    public abstract String basename();
    1.57 +
    1.58 +    public File getFile(File directory) {
    1.59 +        if (path.length() == 0)
    1.60 +            return directory;
    1.61 +        return new File(directory, path.replace('/', File.separatorChar));
    1.62 +    }
    1.63 +
    1.64 +    public int compareTo(RelativePath other) {
    1.65 +        return path.compareTo(other.path);
    1.66 +    }
    1.67 +
    1.68 +    @Override
    1.69 +    public boolean equals(Object other) {
    1.70 +        if (!(other instanceof RelativePath))
    1.71 +            return false;
    1.72 +         return path.equals(((RelativePath) other).path);
    1.73 +    }
    1.74 +
    1.75 +    @Override
    1.76 +    public int hashCode() {
    1.77 +        return path.hashCode();
    1.78 +    }
    1.79 +
    1.80 +    @Override
    1.81 +    public String toString() {
    1.82 +        return "RelPath[" + path + "]";
    1.83 +    }
    1.84 +
    1.85 +    public String getPath() {
    1.86 +        return path;
    1.87 +    }
    1.88 +
    1.89 +    protected final String path;
    1.90 +
    1.91 +    /**
    1.92 +     * Used to represent a platform-neutral subdirectory within a platform-specific
    1.93 +     * container, such as a directory or zip file.
    1.94 +     * Internally, the file separator is always '/', and if the path is not empty,
    1.95 +     * it always ends in a '/' as well.
    1.96 +     */
    1.97 +    public static class RelativeDirectory extends RelativePath {
    1.98 +
    1.99 +        static RelativeDirectory forPackage(CharSequence packageName) {
   1.100 +            return new RelativeDirectory(packageName.toString().replace('.', '/'));
   1.101 +        }
   1.102 +
   1.103 +        /**
   1.104 +         * @param p must use '/' as an internal separator
   1.105 +         */
   1.106 +        public RelativeDirectory(String p) {
   1.107 +            super(p.length() == 0 || p.endsWith("/") ? p : p + "/");
   1.108 +        }
   1.109 +
   1.110 +        /**
   1.111 +         * @param p must use '/' as an internal separator
   1.112 +         */
   1.113 +        public RelativeDirectory(RelativeDirectory d, String p) {
   1.114 +            this(d.path + p);
   1.115 +        }
   1.116 +
   1.117 +        @Override
   1.118 +        public RelativeDirectory dirname() {
   1.119 +            int l = path.length();
   1.120 +            if (l == 0)
   1.121 +                return this;
   1.122 +            int sep = path.lastIndexOf('/', l - 2);
   1.123 +            return new RelativeDirectory(path.substring(0, sep + 1));
   1.124 +        }
   1.125 +
   1.126 +        @Override
   1.127 +        public String basename() {
   1.128 +            int l = path.length();
   1.129 +            if (l == 0)
   1.130 +                return path;
   1.131 +            int sep = path.lastIndexOf('/', l - 2);
   1.132 +            return path.substring(sep + 1, l - 1);
   1.133 +        }
   1.134 +
   1.135 +        /**
   1.136 +         * Return true if this subdirectory "contains" the other path.
   1.137 +         * A subdirectory path does not contain itself.
   1.138 +         **/
   1.139 +        boolean contains(RelativePath other) {
   1.140 +            return other.path.length() > path.length() && other.path.startsWith(path);
   1.141 +        }
   1.142 +
   1.143 +        @Override
   1.144 +        public String toString() {
   1.145 +            return "RelativeDirectory[" + path + "]";
   1.146 +        }
   1.147 +    }
   1.148 +
   1.149 +    /**
   1.150 +     * Used to represent a platform-neutral file within a platform-specific
   1.151 +     * container, such as a directory or zip file.
   1.152 +     * Internally, the file separator is always '/'. It never ends in '/'.
   1.153 +     */
   1.154 +    public static class RelativeFile extends RelativePath {
   1.155 +        static RelativeFile forClass(CharSequence className, JavaFileObject.Kind kind) {
   1.156 +            return new RelativeFile(className.toString().replace('.', '/') + kind.extension);
   1.157 +        }
   1.158 +
   1.159 +        public RelativeFile(String p) {
   1.160 +            super(p);
   1.161 +            if (p.endsWith("/"))
   1.162 +                throw new IllegalArgumentException(p);
   1.163 +        }
   1.164 +
   1.165 +        /**
   1.166 +         * @param p must use '/' as an internal separator
   1.167 +         */
   1.168 +        public RelativeFile(RelativeDirectory d, String p) {
   1.169 +            this(d.path + p);
   1.170 +        }
   1.171 +
   1.172 +        RelativeFile(RelativeDirectory d, RelativePath p) {
   1.173 +            this(d, p.path);
   1.174 +        }
   1.175 +
   1.176 +        @Override
   1.177 +        public RelativeDirectory dirname() {
   1.178 +            int sep = path.lastIndexOf('/');
   1.179 +            return new RelativeDirectory(path.substring(0, sep + 1));
   1.180 +        }
   1.181 +
   1.182 +        @Override
   1.183 +        public String basename() {
   1.184 +            int sep = path.lastIndexOf('/');
   1.185 +            return path.substring(sep + 1);
   1.186 +        }
   1.187 +
   1.188 +        ZipEntry getZipEntry(ZipFile zip) {
   1.189 +            return zip.getEntry(path);
   1.190 +        }
   1.191 +
   1.192 +        @Override
   1.193 +        public String toString() {
   1.194 +            return "RelativeFile[" + path + "]";
   1.195 +        }
   1.196 +
   1.197 +    }
   1.198 +
   1.199 +}

mercurial