langtools/src/share/classes/com/sun/tools/javac/file/RelativePath.java
changeset 1205 b316e32eb90c
child 3380 a6c2bcab0fec
equal deleted inserted replaced
1109:853d8c191eac 1205:b316e32eb90c
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any 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 public abstract class RelativePath implements Comparable<RelativePath> {
       
    39     /**
       
    40      * @param p must use '/' as an internal separator
       
    41      */
       
    42     protected RelativePath(String p) {
       
    43         path = p;
       
    44     }
       
    45 
       
    46     public abstract RelativeDirectory dirname();
       
    47 
       
    48     public abstract String basename();
       
    49 
       
    50     public File getFile(File directory) {
       
    51         if (path.length() == 0)
       
    52             return directory;
       
    53         return new File(directory, path.replace('/', File.separatorChar));
       
    54     }
       
    55 
       
    56     public int compareTo(RelativePath other) {
       
    57         return path.compareTo(other.path);
       
    58     }
       
    59 
       
    60     @Override
       
    61     public boolean equals(Object other) {
       
    62         if (!(other instanceof RelativePath))
       
    63             return false;
       
    64          return path.equals(((RelativePath) other).path);
       
    65     }
       
    66 
       
    67     @Override
       
    68     public int hashCode() {
       
    69         return path.hashCode();
       
    70     }
       
    71 
       
    72     @Override
       
    73     public String toString() {
       
    74         return "RelPath[" + path + "]";
       
    75     }
       
    76 
       
    77     public String getPath() {
       
    78         return path;
       
    79     }
       
    80 
       
    81     protected final String path;
       
    82 
       
    83     /**
       
    84      * Used to represent a platform-neutral subdirectory within a platform-specific
       
    85      * container, such as a directory or zip file.
       
    86      * Internally, the file separator is always '/', and if the path is not empty,
       
    87      * it always ends in a '/' as well.
       
    88      */
       
    89     public static class RelativeDirectory extends RelativePath {
       
    90 
       
    91         static RelativeDirectory forPackage(CharSequence packageName) {
       
    92             return new RelativeDirectory(packageName.toString().replace('.', '/'));
       
    93         }
       
    94 
       
    95         /**
       
    96          * @param p must use '/' as an internal separator
       
    97          */
       
    98         public RelativeDirectory(String p) {
       
    99             super(p.length() == 0 || p.endsWith("/") ? p : p + "/");
       
   100         }
       
   101 
       
   102         /**
       
   103          * @param p must use '/' as an internal separator
       
   104          */
       
   105         public RelativeDirectory(RelativeDirectory d, String p) {
       
   106             this(d.path + p);
       
   107         }
       
   108 
       
   109         @Override
       
   110         public RelativeDirectory dirname() {
       
   111             int l = path.length();
       
   112             if (l == 0)
       
   113                 return this;
       
   114             int sep = path.lastIndexOf('/', l - 2);
       
   115             return new RelativeDirectory(path.substring(0, sep + 1));
       
   116         }
       
   117 
       
   118         @Override
       
   119         public String basename() {
       
   120             int l = path.length();
       
   121             if (l == 0)
       
   122                 return path;
       
   123             int sep = path.lastIndexOf('/', l - 2);
       
   124             return path.substring(sep + 1, l - 1);
       
   125         }
       
   126 
       
   127         /**
       
   128          * Return true if this subdirectory "contains" the other path.
       
   129          * A subdirectory path does not contain itself.
       
   130          **/
       
   131         boolean contains(RelativePath other) {
       
   132             return other.path.length() > path.length() && other.path.startsWith(path);
       
   133         }
       
   134 
       
   135         @Override
       
   136         public String toString() {
       
   137             return "RelativeDirectory[" + path + "]";
       
   138         }
       
   139     }
       
   140 
       
   141     /**
       
   142      * Used to represent a platform-neutral file within a platform-specific
       
   143      * container, such as a directory or zip file.
       
   144      * Internally, the file separator is always '/'. It never ends in '/'.
       
   145      */
       
   146     public static class RelativeFile extends RelativePath {
       
   147         static RelativeFile forClass(CharSequence className, JavaFileObject.Kind kind) {
       
   148             return new RelativeFile(className.toString().replace('.', '/') + kind.extension);
       
   149         }
       
   150 
       
   151         public RelativeFile(String p) {
       
   152             super(p);
       
   153             if (p.endsWith("/"))
       
   154                 throw new IllegalArgumentException(p);
       
   155         }
       
   156 
       
   157         /**
       
   158          * @param p must use '/' as an internal separator
       
   159          */
       
   160         public RelativeFile(RelativeDirectory d, String p) {
       
   161             this(d.path + p);
       
   162         }
       
   163 
       
   164         RelativeFile(RelativeDirectory d, RelativePath p) {
       
   165             this(d, p.path);
       
   166         }
       
   167 
       
   168         @Override
       
   169         public RelativeDirectory dirname() {
       
   170             int sep = path.lastIndexOf('/');
       
   171             return new RelativeDirectory(path.substring(0, sep + 1));
       
   172         }
       
   173 
       
   174         @Override
       
   175         public String basename() {
       
   176             int sep = path.lastIndexOf('/');
       
   177             return path.substring(sep + 1);
       
   178         }
       
   179 
       
   180         ZipEntry getZipEntry(ZipFile zip) {
       
   181             return zip.getEntry(path);
       
   182         }
       
   183 
       
   184         @Override
       
   185         public String toString() {
       
   186             return "RelativeFile[" + path + "]";
       
   187         }
       
   188 
       
   189     }
       
   190 
       
   191 }