View Javadoc
1   /*
2    * Copyright (c) 2008, 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  
26  package com.sun.tools.javac.file;
27  
28  import java.io.File;
29  import java.util.zip.ZipEntry;
30  import java.util.zip.ZipFile;
31  import javax.tools.JavaFileObject;
32  
33  /**
34   * Used to represent a platform-neutral path within a platform-specific
35   * container, such as a directory or zip file.
36   * Internally, the file separator is always '/'.
37   *
38   * <p><b>This is NOT part of any supported API.
39   * If you write code that depends on this, you do so at your own risk.
40   * This code and its internal interfaces are subject to change or
41   * deletion without notice.</b>
42   */
43  public abstract class RelativePath implements Comparable<RelativePath> {
44      /**
45       * @param p must use '/' as an internal separator
46       */
47      protected RelativePath(String p) {
48          path = p;
49      }
50  
51      public abstract RelativeDirectory dirname();
52  
53      public abstract String basename();
54  
55      public File getFile(File directory) {
56          if (path.length() == 0)
57              return directory;
58          return new File(directory, path.replace('/', File.separatorChar));
59      }
60  
61      public int compareTo(RelativePath other) {
62          return path.compareTo(other.path);
63      }
64  
65      @Override
66      public boolean equals(Object other) {
67          if (!(other instanceof RelativePath))
68              return false;
69           return path.equals(((RelativePath) other).path);
70      }
71  
72      @Override
73      public int hashCode() {
74          return path.hashCode();
75      }
76  
77      @Override
78      public String toString() {
79          return "RelPath[" + path + "]";
80      }
81  
82      public String getPath() {
83          return path;
84      }
85  
86      protected final String path;
87  
88      /**
89       * Used to represent a platform-neutral subdirectory within a platform-specific
90       * container, such as a directory or zip file.
91       * Internally, the file separator is always '/', and if the path is not empty,
92       * it always ends in a '/' as well.
93       */
94      public static class RelativeDirectory extends RelativePath {
95  
96          static RelativeDirectory forPackage(CharSequence packageName) {
97              return new RelativeDirectory(packageName.toString().replace('.', '/'));
98          }
99  
100         /**
101          * @param p must use '/' as an internal separator
102          */
103         public RelativeDirectory(String p) {
104             super(p.length() == 0 || p.endsWith("/") ? p : p + "/");
105         }
106 
107         /**
108          * @param p must use '/' as an internal separator
109          */
110         public RelativeDirectory(RelativeDirectory d, String p) {
111             this(d.path + p);
112         }
113 
114         @Override
115         public RelativeDirectory dirname() {
116             int l = path.length();
117             if (l == 0)
118                 return this;
119             int sep = path.lastIndexOf('/', l - 2);
120             return new RelativeDirectory(path.substring(0, sep + 1));
121         }
122 
123         @Override
124         public String basename() {
125             int l = path.length();
126             if (l == 0)
127                 return path;
128             int sep = path.lastIndexOf('/', l - 2);
129             return path.substring(sep + 1, l - 1);
130         }
131 
132         /**
133          * Return true if this subdirectory "contains" the other path.
134          * A subdirectory path does not contain itself.
135          **/
136         boolean contains(RelativePath other) {
137             return other.path.length() > path.length() && other.path.startsWith(path);
138         }
139 
140         @Override
141         public String toString() {
142             return "RelativeDirectory[" + path + "]";
143         }
144     }
145 
146     /**
147      * Used to represent a platform-neutral file within a platform-specific
148      * container, such as a directory or zip file.
149      * Internally, the file separator is always '/'. It never ends in '/'.
150      */
151     public static class RelativeFile extends RelativePath {
152         static RelativeFile forClass(CharSequence className, JavaFileObject.Kind kind) {
153             return new RelativeFile(className.toString().replace('.', '/') + kind.extension);
154         }
155 
156         public RelativeFile(String p) {
157             super(p);
158             if (p.endsWith("/"))
159                 throw new IllegalArgumentException(p);
160         }
161 
162         /**
163          * @param p must use '/' as an internal separator
164          */
165         public RelativeFile(RelativeDirectory d, String p) {
166             this(d.path + p);
167         }
168 
169         RelativeFile(RelativeDirectory d, RelativePath p) {
170             this(d, p.path);
171         }
172 
173         @Override
174         public RelativeDirectory dirname() {
175             int sep = path.lastIndexOf('/');
176             return new RelativeDirectory(path.substring(0, sep + 1));
177         }
178 
179         @Override
180         public String basename() {
181             int sep = path.lastIndexOf('/');
182             return path.substring(sep + 1);
183         }
184 
185         ZipEntry getZipEntry(ZipFile zip) {
186             return zip.getEntry(path);
187         }
188 
189         @Override
190         public String toString() {
191             return "RelativeFile[" + path + "]";
192         }
193 
194     }
195 
196 }