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