src/share/classes/com/sun/tools/doclets/internal/toolkit/util/DocPath.java

Tue, 23 Oct 2012 13:20:37 -0700

author
jjg
date
Tue, 23 Oct 2012 13:20:37 -0700
changeset 1372
78962d89f283
child 1373
4a1c57a1c410
permissions
-rw-r--r--

8000741: refactor javadoc to use abstraction to handle relative paths
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 1998, 2012, 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  */
    26 package com.sun.tools.doclets.internal.toolkit.util;
    28 import com.sun.javadoc.ClassDoc;
    29 import com.sun.javadoc.PackageDoc;
    30 import java.io.File;
    32 /**
    33  * Abstraction for immutable relative paths.
    34  * Paths always use '/' as a separator, and never begin or end with '/'.
    35  */
    36 public class DocPath {
    37     private final String path;
    39     /** The empty path. */
    40     public static final DocPath empty = new DocPath("");
    42     /** The empty path. */
    43     public static final DocPath parent = new DocPath("..");
    45     /**
    46      * Create a path from a string.
    47      */
    48     public static DocPath create(String p) {
    49         return (p == null) || p.isEmpty() ? empty : new DocPath(p);
    50     }
    52     /**
    53      * Return the path for a class.
    54      * For example, if the class is java.lang.Object,
    55      * the path is java/lang/Object.html.
    56      */
    57     public static DocPath forClass(ClassDoc cd) {
    58         return (cd == null) ? empty :
    59                 forPackage(cd.containingPackage()).resolve(forName(cd));
    60     }
    62     /**
    63      * Return the path for the simple name of the class.
    64      * For example, if the class is java.lang.Object,
    65      * the path is Object.html.
    66      */
    67     public static DocPath forName(ClassDoc cd) {
    68         return (cd == null) ? empty : new DocPath(cd.name() + ".html");
    69     }
    71     /**
    72      * Return the path for the package of a class.
    73      * For example, if the class is java.lang.Object,
    74      * the path is java/lang.
    75      */
    76     public static DocPath forPackage(ClassDoc cd) {
    77         return (cd == null) ? empty : forPackage(cd.containingPackage());
    78     }
    80     /**
    81      * Return the path for a package.
    82      * For example, if the package is java.lang,
    83      * the path is java/lang.
    84      */
    85     public static DocPath forPackage(PackageDoc pd) {
    86         return (pd == null) ? empty : DocPath.create(pd.name().replace('.', '/'));
    87     }
    89     /**
    90      * Return the inverse path for a package.
    91      * For example, if the package is java.lang,
    92      * the inverse path is ../...
    93      */
    94     public static DocPath forRoot(PackageDoc pd) {
    95         String name = (pd == null) ? "" : pd.name();
    96         if (name.isEmpty())
    97             return empty;
    98         return new DocPath(name.replace('.', '/').replaceAll("[^/]+", ".."));
    99     }
   101     /**
   102      * Return the relative path from one package to another.
   103      */
   104     public static DocPath relativePath(PackageDoc from, PackageDoc to) {
   105         return forRoot(from).resolve(forPackage(to));
   106     }
   108     protected DocPath(String p) {
   109         path = (p.endsWith("/") ? p.substring(0, p.length() - 1) : p);
   110     }
   112     /** @inheritDoc */
   113     @Override
   114     public boolean equals(Object other) {
   115         return (other instanceof DocPath) && path.equals(((DocPath)other).path);
   116     }
   118     /** @inheritDoc */
   119     @Override
   120     public int hashCode() {
   121         return path.hashCode();
   122     }
   124     public DocPath basename() {
   125         int sep = path.lastIndexOf("/");
   126         return (sep == -1) ? this : new DocPath(path.substring(sep + 1));
   127     }
   129     public DocPath parent() {
   130         int sep = path.lastIndexOf("/");
   131         return (sep == -1) ? empty : new DocPath(path.substring(0, sep));
   132     }
   134     /**
   135      * Return the path formed by appending the specified string to the current path.
   136      */
   137     public DocPath resolve(String p) {
   138         if (p == null || p.isEmpty())
   139             return this;
   140         if (path.isEmpty())
   141             return new DocPath(p);
   142         return new DocPath(path + "/" + p);
   143     }
   145     /**
   146      * Return the path by appending the specified path to the current path.
   147      */
   148     public DocPath resolve(DocPath p) {
   149         if (p == null || p.isEmpty())
   150             return this;
   151         if (path.isEmpty())
   152             return p;
   153         return new DocPath(path + "/" + p.getPath());
   154     }
   156     /**
   157      * Get the file created by evaluating the path against a specified directory.
   158      */
   159     // Temporary: this signature should not use String for dir.
   160     // Eventually, this should involve javax.tools.Location.
   161     public File resolveAgainst(String dir) {
   162         return dir.isEmpty() ? new File(path) : new File(dir, path);
   163     }
   165     /**
   166      * Return the inverse path for this path.
   167      * For example, if the path is a/b/c, the inverse path is ../../..
   168      */
   169     public DocPath invert() {
   170         return new DocPath(path.replaceAll("[^/]+", ".."));
   171     }
   173     /**
   174      * Return true if this path is empty.
   175      */
   176     public boolean isEmpty() {
   177         return path.isEmpty();
   178     }
   180     /**
   181      * Return this path as a string.
   182      */
   183     // This is provided instead of using toString() to help catch
   184     // unintended use of toString() in string concatenation sequences.
   185     public String getPath() {
   186         return path;
   187     }
   188 }

mercurial