RelativePath.java revision 3155:30e288cb2d22
18871Srgrimes/*
24Srgrimes * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
34Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44Srgrimes *
58871Srgrimes * This code is free software; you can redistribute it and/or modify it
64Srgrimes * under the terms of the GNU General Public License version 2 only, as
74Srgrimes * published by the Free Software Foundation.  Oracle designates this
84Srgrimes * particular file as subject to the "Classpath" exception as provided
94Srgrimes * by Oracle in the LICENSE file that accompanied this code.
104Srgrimes *
118871Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
124Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
134Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
144Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
158871Srgrimes * accompanied this code).
164Srgrimes *
178871Srgrimes * You should have received a copy of the GNU General Public License version
184Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
194Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
204Srgrimes *
214Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
228871Srgrimes * or visit www.oracle.com if you need additional information or have any
234Srgrimes * questions.
244Srgrimes */
254Srgrimes
264Srgrimespackage com.sun.tools.javac.file;
2737415Scharnier
2837415Scharnierimport java.io.File;
2940946Sjkoshyimport java.nio.file.FileSystem;
3037415Scharnierimport java.nio.file.FileSystems;
3137415Scharnierimport java.nio.file.InvalidPathException;
324Srgrimesimport java.nio.file.Path;
3337415Scharnierimport java.nio.file.Paths;
3437415Scharnierimport java.util.zip.ZipEntry;
3537415Scharnierimport java.util.zip.ZipFile;
3637415Scharnier
3737415Scharnierimport javax.tools.JavaFileObject;
384Srgrimes
3937415Scharnier/**
4016561Salex * Used to represent a platform-neutral path within a platform-specific
4116561Salex * container, such as a directory or zip file.
424Srgrimes * Internally, the file separator is always '/'.
434Srgrimes *
444Srgrimes * <p><b>This is NOT part of any supported API.
454Srgrimes * If you write code that depends on this, you do so at your own risk.
464Srgrimes * This code and its internal interfaces are subject to change or
474Srgrimes * deletion without notice.</b>
484Srgrimes */
494Srgrimespublic abstract class RelativePath implements Comparable<RelativePath> {
504Srgrimes    /**
514Srgrimes     * @param p must use '/' as an internal separator
524Srgrimes     */
534Srgrimes    protected RelativePath(String p) {
544Srgrimes        path = p;
554Srgrimes    }
564Srgrimes
574Srgrimes    public abstract RelativeDirectory dirname();
584Srgrimes
594Srgrimes    public abstract String basename();
604Srgrimes
614Srgrimes    public Path resolveAgainst(Path directory) throws /*unchecked*/ InvalidPathException {
624Srgrimes        if (directory == null) {
6320061Ssos            String sep = FileSystems.getDefault().getSeparator();
6420061Ssos            return Paths.get(path.replace("/", sep));
6520061Ssos        } else {
664Srgrimes            String sep = directory.getFileSystem().getSeparator();
6710514Sjoerg            return directory.resolve(path.replace("/", sep));
6810514Sjoerg        }
6910514Sjoerg    }
7010514Sjoerg
7110514Sjoerg    public Path resolveAgainst(FileSystem fs) throws /*unchecked*/ InvalidPathException {
7210514Sjoerg        String sep = fs.getSeparator();
734Srgrimes        Path root = fs.getRootDirectories().iterator().next();
744Srgrimes        return root.resolve(path.replace("/", sep));
754Srgrimes    }
764Srgrimes
774Srgrimes    @Override
784Srgrimes    public int compareTo(RelativePath other) {
7937415Scharnier        return path.compareTo(other.path);
804Srgrimes    }
814Srgrimes
824Srgrimes    @Override
8320061Ssos    public boolean equals(Object other) {
8420061Ssos        if (!(other instanceof RelativePath))
854Srgrimes            return false;
864Srgrimes         return path.equals(((RelativePath) other).path);
874Srgrimes    }
884Srgrimes
894Srgrimes    @Override
904Srgrimes    public int hashCode() {
914Srgrimes        return path.hashCode();
924Srgrimes    }
934Srgrimes
944Srgrimes    @Override
954Srgrimes    public String toString() {
964Srgrimes        return "RelPath[" + path + "]";
974Srgrimes    }
984Srgrimes
994Srgrimes    public String getPath() {
1004Srgrimes        return path;
10119459Sjkh    }
10219459Sjkh
10319459Sjkh    protected final String path;
10419459Sjkh
10519459Sjkh    /**
10619459Sjkh     * Used to represent a platform-neutral subdirectory within a platform-specific
10719459Sjkh     * container, such as a directory or zip file.
10819459Sjkh     * Internally, the file separator is always '/', and if the path is not empty,
10919459Sjkh     * it always ends in a '/' as well.
11019459Sjkh     */
11119459Sjkh    public static class RelativeDirectory extends RelativePath {
11219459Sjkh
11319459Sjkh        static RelativeDirectory forPackage(CharSequence packageName) {
11419459Sjkh            return new RelativeDirectory(packageName.toString().replace('.', '/'));
11519459Sjkh        }
11619459Sjkh
11719459Sjkh        /**
11819459Sjkh         * @param p must use '/' as an internal separator
11919459Sjkh         */
1204Srgrimes        public RelativeDirectory(String p) {
1214Srgrimes            super(p.length() == 0 || p.endsWith("/") ? p : p + "/");
1224Srgrimes        }
12319459Sjkh
12419459Sjkh        /**
12519459Sjkh         * @param p must use '/' as an internal separator
1264Srgrimes         */
1274Srgrimes        public RelativeDirectory(RelativeDirectory d, String p) {
1288871Srgrimes            this(d.path + p);
1298871Srgrimes        }
1308871Srgrimes
1318871Srgrimes        @Override
1328871Srgrimes        public RelativeDirectory dirname() {
1338871Srgrimes            int l = path.length();
1348871Srgrimes            if (l == 0)
1358871Srgrimes                return this;
1364Srgrimes            int sep = path.lastIndexOf('/', l - 2);
1374Srgrimes            return new RelativeDirectory(path.substring(0, sep + 1));
1384Srgrimes        }
1398871Srgrimes
1404Srgrimes        @Override
1414Srgrimes        public String basename() {
1424Srgrimes            int l = path.length();
1434Srgrimes            if (l == 0)
1448871Srgrimes                return path;
1454Srgrimes            int sep = path.lastIndexOf('/', l - 2);
1468871Srgrimes            return path.substring(sep + 1, l - 1);
1478871Srgrimes        }
1488871Srgrimes
1498871Srgrimes        /**
1508871Srgrimes         * Return true if this subdirectory "contains" the other path.
1518871Srgrimes         * A subdirectory path does not contain itself.
1528871Srgrimes         **/
1538871Srgrimes        boolean contains(RelativePath other) {
1548871Srgrimes            return other.path.length() > path.length() && other.path.startsWith(path);
1558871Srgrimes        }
1568871Srgrimes
1578871Srgrimes        @Override
1588871Srgrimes        public String toString() {
1594Srgrimes            return "RelativeDirectory[" + path + "]";
1604Srgrimes        }
1614Srgrimes    }
1624Srgrimes
1634Srgrimes    /**
1644Srgrimes     * Used to represent a platform-neutral file within a platform-specific
1654Srgrimes     * container, such as a directory or zip file.
1664Srgrimes     * Internally, the file separator is always '/'. It never ends in '/'.
1678871Srgrimes     */
1688871Srgrimes    public static class RelativeFile extends RelativePath {
1698871Srgrimes        static RelativeFile forClass(CharSequence className, JavaFileObject.Kind kind) {
1708871Srgrimes            return new RelativeFile(className.toString().replace('.', '/') + kind.extension);
17134582Sache        }
1728871Srgrimes
1738871Srgrimes        public RelativeFile(String p) {
17434952Sobrien            super(p);
1758871Srgrimes            if (p.endsWith("/"))
1768871Srgrimes                throw new IllegalArgumentException(p);
1778871Srgrimes        }
17834582Sache
17934582Sache        /**
18034582Sache         * @param p must use '/' as an internal separator
18134582Sache         */
1828871Srgrimes        public RelativeFile(RelativeDirectory d, String p) {
1838871Srgrimes            this(d.path + p);
1848871Srgrimes        }
1858871Srgrimes
1868871Srgrimes        RelativeFile(RelativeDirectory d, RelativePath p) {
1878871Srgrimes            this(d, p.path);
1888871Srgrimes        }
1898871Srgrimes
1908871Srgrimes        @Override
1918871Srgrimes        public RelativeDirectory dirname() {
1928871Srgrimes            int sep = path.lastIndexOf('/');
1938871Srgrimes            return new RelativeDirectory(path.substring(0, sep + 1));
1948871Srgrimes        }
19539377Sobrien
1968871Srgrimes        @Override
1978871Srgrimes        public String basename() {
1988871Srgrimes            int sep = path.lastIndexOf('/');
19940946Sjkoshy            return path.substring(sep + 1);
2008871Srgrimes        }
20125378Simp
2027902Sgpalmer        ZipEntry getZipEntry(ZipFile zip) {
2038871Srgrimes            return zip.getEntry(path);
2048871Srgrimes        }
2058871Srgrimes
2068871Srgrimes        @Override
2078871Srgrimes        public String toString() {
2088871Srgrimes            return "RelativeFile[" + path + "]";
2098871Srgrimes        }
2108871Srgrimes
2118871Srgrimes    }
2128871Srgrimes
2134Srgrimes}
2144Srgrimes