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

Thu, 15 Nov 2012 19:54:20 -0800

author
jjg
date
Thu, 15 Nov 2012 19:54:20 -0800
changeset 1412
400a4e8accd3
parent 1381
23fe1a96bc0f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8002079: update DocFile to use a JavaFileManager
Reviewed-by: darcy

jjg@1372 1 /*
jjg@1372 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1372 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1372 4 *
jjg@1372 5 * This code is free software; you can redistribute it and/or modify it
jjg@1372 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1372 7 * published by the Free Software Foundation. Oracle designates this
jjg@1372 8 * particular file as subject to the "Classpath" exception as provided
jjg@1372 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@1372 10 *
jjg@1372 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1372 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1372 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1372 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1372 15 * accompanied this code).
jjg@1372 16 *
jjg@1372 17 * You should have received a copy of the GNU General Public License version
jjg@1372 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1372 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1372 20 *
jjg@1372 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1372 22 * or visit www.oracle.com if you need additional information or have any
jjg@1372 23 * questions.
jjg@1372 24 */
jjg@1372 25
jjg@1372 26 package com.sun.tools.doclets.internal.toolkit.util;
jjg@1372 27
jjg@1372 28 import com.sun.javadoc.ClassDoc;
jjg@1372 29 import com.sun.javadoc.PackageDoc;
jjg@1372 30
jjg@1372 31 /**
jjg@1372 32 * Abstraction for immutable relative paths.
jjg@1372 33 * Paths always use '/' as a separator, and never begin or end with '/'.
jjg@1373 34 *
jjg@1373 35 * <p><b>This is NOT part of any supported API.
jjg@1373 36 * If you write code that depends on this, you do so at your own risk.
jjg@1373 37 * This code and its internal interfaces are subject to change or
jjg@1373 38 * deletion without notice.</b>
jjg@1372 39 */
jjg@1372 40 public class DocPath {
jjg@1372 41 private final String path;
jjg@1372 42
jjg@1372 43 /** The empty path. */
jjg@1372 44 public static final DocPath empty = new DocPath("");
jjg@1372 45
jjg@1372 46 /** The empty path. */
jjg@1372 47 public static final DocPath parent = new DocPath("..");
jjg@1372 48
jjg@1372 49 /**
jjg@1372 50 * Create a path from a string.
jjg@1372 51 */
jjg@1372 52 public static DocPath create(String p) {
jjg@1372 53 return (p == null) || p.isEmpty() ? empty : new DocPath(p);
jjg@1372 54 }
jjg@1372 55
jjg@1372 56 /**
jjg@1372 57 * Return the path for a class.
jjg@1372 58 * For example, if the class is java.lang.Object,
jjg@1372 59 * the path is java/lang/Object.html.
jjg@1372 60 */
jjg@1372 61 public static DocPath forClass(ClassDoc cd) {
jjg@1372 62 return (cd == null) ? empty :
jjg@1372 63 forPackage(cd.containingPackage()).resolve(forName(cd));
jjg@1372 64 }
jjg@1372 65
jjg@1372 66 /**
jjg@1372 67 * Return the path for the simple name of the class.
jjg@1372 68 * For example, if the class is java.lang.Object,
jjg@1372 69 * the path is Object.html.
jjg@1372 70 */
jjg@1372 71 public static DocPath forName(ClassDoc cd) {
jjg@1372 72 return (cd == null) ? empty : new DocPath(cd.name() + ".html");
jjg@1372 73 }
jjg@1372 74
jjg@1372 75 /**
jjg@1372 76 * Return the path for the package of a class.
jjg@1372 77 * For example, if the class is java.lang.Object,
jjg@1372 78 * the path is java/lang.
jjg@1372 79 */
jjg@1372 80 public static DocPath forPackage(ClassDoc cd) {
jjg@1372 81 return (cd == null) ? empty : forPackage(cd.containingPackage());
jjg@1372 82 }
jjg@1372 83
jjg@1372 84 /**
jjg@1372 85 * Return the path for a package.
jjg@1372 86 * For example, if the package is java.lang,
jjg@1372 87 * the path is java/lang.
jjg@1372 88 */
jjg@1372 89 public static DocPath forPackage(PackageDoc pd) {
jjg@1372 90 return (pd == null) ? empty : DocPath.create(pd.name().replace('.', '/'));
jjg@1372 91 }
jjg@1372 92
jjg@1372 93 /**
jjg@1372 94 * Return the inverse path for a package.
jjg@1372 95 * For example, if the package is java.lang,
jjg@1372 96 * the inverse path is ../...
jjg@1372 97 */
jjg@1372 98 public static DocPath forRoot(PackageDoc pd) {
jjg@1372 99 String name = (pd == null) ? "" : pd.name();
jjg@1372 100 if (name.isEmpty())
jjg@1372 101 return empty;
jjg@1372 102 return new DocPath(name.replace('.', '/').replaceAll("[^/]+", ".."));
jjg@1372 103 }
jjg@1372 104
jjg@1372 105 /**
jjg@1372 106 * Return the relative path from one package to another.
jjg@1372 107 */
jjg@1372 108 public static DocPath relativePath(PackageDoc from, PackageDoc to) {
jjg@1372 109 return forRoot(from).resolve(forPackage(to));
jjg@1372 110 }
jjg@1372 111
jjg@1372 112 protected DocPath(String p) {
jjg@1372 113 path = (p.endsWith("/") ? p.substring(0, p.length() - 1) : p);
jjg@1372 114 }
jjg@1372 115
jjg@1381 116 /** {@inheritDoc} */
jjg@1372 117 @Override
jjg@1372 118 public boolean equals(Object other) {
jjg@1372 119 return (other instanceof DocPath) && path.equals(((DocPath)other).path);
jjg@1372 120 }
jjg@1372 121
jjg@1381 122 /** {@inheritDoc} */
jjg@1372 123 @Override
jjg@1372 124 public int hashCode() {
jjg@1372 125 return path.hashCode();
jjg@1372 126 }
jjg@1372 127
jjg@1372 128 public DocPath basename() {
jjg@1372 129 int sep = path.lastIndexOf("/");
jjg@1372 130 return (sep == -1) ? this : new DocPath(path.substring(sep + 1));
jjg@1372 131 }
jjg@1372 132
jjg@1372 133 public DocPath parent() {
jjg@1372 134 int sep = path.lastIndexOf("/");
jjg@1372 135 return (sep == -1) ? empty : new DocPath(path.substring(0, sep));
jjg@1372 136 }
jjg@1372 137
jjg@1372 138 /**
jjg@1372 139 * Return the path formed by appending the specified string to the current path.
jjg@1372 140 */
jjg@1372 141 public DocPath resolve(String p) {
jjg@1372 142 if (p == null || p.isEmpty())
jjg@1372 143 return this;
jjg@1372 144 if (path.isEmpty())
jjg@1372 145 return new DocPath(p);
jjg@1372 146 return new DocPath(path + "/" + p);
jjg@1372 147 }
jjg@1372 148
jjg@1372 149 /**
jjg@1372 150 * Return the path by appending the specified path to the current path.
jjg@1372 151 */
jjg@1372 152 public DocPath resolve(DocPath p) {
jjg@1372 153 if (p == null || p.isEmpty())
jjg@1372 154 return this;
jjg@1372 155 if (path.isEmpty())
jjg@1372 156 return p;
jjg@1372 157 return new DocPath(path + "/" + p.getPath());
jjg@1372 158 }
jjg@1372 159
jjg@1372 160 /**
jjg@1372 161 * Return the inverse path for this path.
jjg@1372 162 * For example, if the path is a/b/c, the inverse path is ../../..
jjg@1372 163 */
jjg@1372 164 public DocPath invert() {
jjg@1372 165 return new DocPath(path.replaceAll("[^/]+", ".."));
jjg@1372 166 }
jjg@1372 167
jjg@1372 168 /**
jjg@1372 169 * Return true if this path is empty.
jjg@1372 170 */
jjg@1372 171 public boolean isEmpty() {
jjg@1372 172 return path.isEmpty();
jjg@1372 173 }
jjg@1372 174
jjg@1373 175 public DocLink fragment(String fragment) {
jjg@1373 176 return new DocLink(path, null, fragment);
jjg@1373 177 }
jjg@1373 178
jjg@1373 179 public DocLink query(String query) {
jjg@1373 180 return new DocLink(path, query, null);
jjg@1373 181 }
jjg@1373 182
jjg@1372 183 /**
jjg@1372 184 * Return this path as a string.
jjg@1372 185 */
jjg@1372 186 // This is provided instead of using toString() to help catch
jjg@1372 187 // unintended use of toString() in string concatenation sequences.
jjg@1372 188 public String getPath() {
jjg@1372 189 return path;
jjg@1372 190 }
jjg@1372 191 }

mercurial