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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1412
400a4e8accd3
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial