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

Tue, 30 Oct 2012 10:15:19 -0700

author
jjg
date
Tue, 30 Oct 2012 10:15:19 -0700
changeset 1381
23fe1a96bc0f
parent 1373
4a1c57a1c410
child 1412
400a4e8accd3
permissions
-rw-r--r--

8001929: fix doclint errors in langtools doc comments
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 import java.io.File;
jjg@1372 31
jjg@1372 32 /**
jjg@1372 33 * Abstraction for immutable relative paths.
jjg@1372 34 * Paths always use '/' as a separator, and never begin or end with '/'.
jjg@1373 35 *
jjg@1373 36 * <p><b>This is NOT part of any supported API.
jjg@1373 37 * If you write code that depends on this, you do so at your own risk.
jjg@1373 38 * This code and its internal interfaces are subject to change or
jjg@1373 39 * deletion without notice.</b>
jjg@1372 40 */
jjg@1372 41 public class DocPath {
jjg@1372 42 private final String path;
jjg@1372 43
jjg@1372 44 /** The empty path. */
jjg@1372 45 public static final DocPath empty = new DocPath("");
jjg@1372 46
jjg@1372 47 /** The empty path. */
jjg@1372 48 public static final DocPath parent = new DocPath("..");
jjg@1372 49
jjg@1372 50 /**
jjg@1372 51 * Create a path from a string.
jjg@1372 52 */
jjg@1372 53 public static DocPath create(String p) {
jjg@1372 54 return (p == null) || p.isEmpty() ? empty : new DocPath(p);
jjg@1372 55 }
jjg@1372 56
jjg@1372 57 /**
jjg@1372 58 * Return the path for a class.
jjg@1372 59 * For example, if the class is java.lang.Object,
jjg@1372 60 * the path is java/lang/Object.html.
jjg@1372 61 */
jjg@1372 62 public static DocPath forClass(ClassDoc cd) {
jjg@1372 63 return (cd == null) ? empty :
jjg@1372 64 forPackage(cd.containingPackage()).resolve(forName(cd));
jjg@1372 65 }
jjg@1372 66
jjg@1372 67 /**
jjg@1372 68 * Return the path for the simple name of the class.
jjg@1372 69 * For example, if the class is java.lang.Object,
jjg@1372 70 * the path is Object.html.
jjg@1372 71 */
jjg@1372 72 public static DocPath forName(ClassDoc cd) {
jjg@1372 73 return (cd == null) ? empty : new DocPath(cd.name() + ".html");
jjg@1372 74 }
jjg@1372 75
jjg@1372 76 /**
jjg@1372 77 * Return the path for the package of a class.
jjg@1372 78 * For example, if the class is java.lang.Object,
jjg@1372 79 * the path is java/lang.
jjg@1372 80 */
jjg@1372 81 public static DocPath forPackage(ClassDoc cd) {
jjg@1372 82 return (cd == null) ? empty : forPackage(cd.containingPackage());
jjg@1372 83 }
jjg@1372 84
jjg@1372 85 /**
jjg@1372 86 * Return the path for a package.
jjg@1372 87 * For example, if the package is java.lang,
jjg@1372 88 * the path is java/lang.
jjg@1372 89 */
jjg@1372 90 public static DocPath forPackage(PackageDoc pd) {
jjg@1372 91 return (pd == null) ? empty : DocPath.create(pd.name().replace('.', '/'));
jjg@1372 92 }
jjg@1372 93
jjg@1372 94 /**
jjg@1372 95 * Return the inverse path for a package.
jjg@1372 96 * For example, if the package is java.lang,
jjg@1372 97 * the inverse path is ../...
jjg@1372 98 */
jjg@1372 99 public static DocPath forRoot(PackageDoc pd) {
jjg@1372 100 String name = (pd == null) ? "" : pd.name();
jjg@1372 101 if (name.isEmpty())
jjg@1372 102 return empty;
jjg@1372 103 return new DocPath(name.replace('.', '/').replaceAll("[^/]+", ".."));
jjg@1372 104 }
jjg@1372 105
jjg@1372 106 /**
jjg@1372 107 * Return the relative path from one package to another.
jjg@1372 108 */
jjg@1372 109 public static DocPath relativePath(PackageDoc from, PackageDoc to) {
jjg@1372 110 return forRoot(from).resolve(forPackage(to));
jjg@1372 111 }
jjg@1372 112
jjg@1372 113 protected DocPath(String p) {
jjg@1372 114 path = (p.endsWith("/") ? p.substring(0, p.length() - 1) : p);
jjg@1372 115 }
jjg@1372 116
jjg@1381 117 /** {@inheritDoc} */
jjg@1372 118 @Override
jjg@1372 119 public boolean equals(Object other) {
jjg@1372 120 return (other instanceof DocPath) && path.equals(((DocPath)other).path);
jjg@1372 121 }
jjg@1372 122
jjg@1381 123 /** {@inheritDoc} */
jjg@1372 124 @Override
jjg@1372 125 public int hashCode() {
jjg@1372 126 return path.hashCode();
jjg@1372 127 }
jjg@1372 128
jjg@1372 129 public DocPath basename() {
jjg@1372 130 int sep = path.lastIndexOf("/");
jjg@1372 131 return (sep == -1) ? this : new DocPath(path.substring(sep + 1));
jjg@1372 132 }
jjg@1372 133
jjg@1372 134 public DocPath parent() {
jjg@1372 135 int sep = path.lastIndexOf("/");
jjg@1372 136 return (sep == -1) ? empty : new DocPath(path.substring(0, sep));
jjg@1372 137 }
jjg@1372 138
jjg@1372 139 /**
jjg@1372 140 * Return the path formed by appending the specified string to the current path.
jjg@1372 141 */
jjg@1372 142 public DocPath resolve(String p) {
jjg@1372 143 if (p == null || p.isEmpty())
jjg@1372 144 return this;
jjg@1372 145 if (path.isEmpty())
jjg@1372 146 return new DocPath(p);
jjg@1372 147 return new DocPath(path + "/" + p);
jjg@1372 148 }
jjg@1372 149
jjg@1372 150 /**
jjg@1372 151 * Return the path by appending the specified path to the current path.
jjg@1372 152 */
jjg@1372 153 public DocPath resolve(DocPath p) {
jjg@1372 154 if (p == null || p.isEmpty())
jjg@1372 155 return this;
jjg@1372 156 if (path.isEmpty())
jjg@1372 157 return p;
jjg@1372 158 return new DocPath(path + "/" + p.getPath());
jjg@1372 159 }
jjg@1372 160
jjg@1372 161 /**
jjg@1372 162 * Get the file created by evaluating the path against a specified directory.
jjg@1372 163 */
jjg@1372 164 // Temporary: this signature should not use String for dir.
jjg@1372 165 // Eventually, this should involve javax.tools.Location.
jjg@1372 166 public File resolveAgainst(String dir) {
jjg@1372 167 return dir.isEmpty() ? new File(path) : new File(dir, path);
jjg@1372 168 }
jjg@1372 169
jjg@1372 170 /**
jjg@1372 171 * Return the inverse path for this path.
jjg@1372 172 * For example, if the path is a/b/c, the inverse path is ../../..
jjg@1372 173 */
jjg@1372 174 public DocPath invert() {
jjg@1372 175 return new DocPath(path.replaceAll("[^/]+", ".."));
jjg@1372 176 }
jjg@1372 177
jjg@1372 178 /**
jjg@1372 179 * Return true if this path is empty.
jjg@1372 180 */
jjg@1372 181 public boolean isEmpty() {
jjg@1372 182 return path.isEmpty();
jjg@1372 183 }
jjg@1372 184
jjg@1373 185 public DocLink fragment(String fragment) {
jjg@1373 186 return new DocLink(path, null, fragment);
jjg@1373 187 }
jjg@1373 188
jjg@1373 189 public DocLink query(String query) {
jjg@1373 190 return new DocLink(path, query, null);
jjg@1373 191 }
jjg@1373 192
jjg@1372 193 /**
jjg@1372 194 * Return this path as a string.
jjg@1372 195 */
jjg@1372 196 // This is provided instead of using toString() to help catch
jjg@1372 197 // unintended use of toString() in string concatenation sequences.
jjg@1372 198 public String getPath() {
jjg@1372 199 return path;
jjg@1372 200 }
jjg@1372 201 }

mercurial