aoqi@0: /* aoqi@0: * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.doclets.internal.toolkit.util; aoqi@0: aoqi@0: import com.sun.javadoc.ClassDoc; aoqi@0: import com.sun.javadoc.PackageDoc; aoqi@0: aoqi@0: /** aoqi@0: * Abstraction for immutable relative paths. aoqi@0: * Paths always use '/' as a separator, and never begin or end with '/'. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: */ aoqi@0: public class DocPath { aoqi@0: private final String path; aoqi@0: aoqi@0: /** The empty path. */ aoqi@0: public static final DocPath empty = new DocPath(""); aoqi@0: aoqi@0: /** The empty path. */ aoqi@0: public static final DocPath parent = new DocPath(".."); aoqi@0: aoqi@0: /** aoqi@0: * Create a path from a string. aoqi@0: */ aoqi@0: public static DocPath create(String p) { aoqi@0: return (p == null) || p.isEmpty() ? empty : new DocPath(p); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the path for a class. aoqi@0: * For example, if the class is java.lang.Object, aoqi@0: * the path is java/lang/Object.html. aoqi@0: */ aoqi@0: public static DocPath forClass(ClassDoc cd) { aoqi@0: return (cd == null) ? empty : aoqi@0: forPackage(cd.containingPackage()).resolve(forName(cd)); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the path for the simple name of the class. aoqi@0: * For example, if the class is java.lang.Object, aoqi@0: * the path is Object.html. aoqi@0: */ aoqi@0: public static DocPath forName(ClassDoc cd) { aoqi@0: return (cd == null) ? empty : new DocPath(cd.name() + ".html"); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the path for the package of a class. aoqi@0: * For example, if the class is java.lang.Object, aoqi@0: * the path is java/lang. aoqi@0: */ aoqi@0: public static DocPath forPackage(ClassDoc cd) { aoqi@0: return (cd == null) ? empty : forPackage(cd.containingPackage()); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the path for a package. aoqi@0: * For example, if the package is java.lang, aoqi@0: * the path is java/lang. aoqi@0: */ aoqi@0: public static DocPath forPackage(PackageDoc pd) { aoqi@0: return (pd == null) ? empty : DocPath.create(pd.name().replace('.', '/')); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the inverse path for a package. aoqi@0: * For example, if the package is java.lang, aoqi@0: * the inverse path is ../... aoqi@0: */ aoqi@0: public static DocPath forRoot(PackageDoc pd) { aoqi@0: String name = (pd == null) ? "" : pd.name(); aoqi@0: if (name.isEmpty()) aoqi@0: return empty; aoqi@0: return new DocPath(name.replace('.', '/').replaceAll("[^/]+", "..")); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the relative path from one package to another. aoqi@0: */ aoqi@0: public static DocPath relativePath(PackageDoc from, PackageDoc to) { aoqi@0: return forRoot(from).resolve(forPackage(to)); aoqi@0: } aoqi@0: aoqi@0: protected DocPath(String p) { aoqi@0: path = (p.endsWith("/") ? p.substring(0, p.length() - 1) : p); aoqi@0: } aoqi@0: aoqi@0: /** {@inheritDoc} */ aoqi@0: @Override aoqi@0: public boolean equals(Object other) { aoqi@0: return (other instanceof DocPath) && path.equals(((DocPath)other).path); aoqi@0: } aoqi@0: aoqi@0: /** {@inheritDoc} */ aoqi@0: @Override aoqi@0: public int hashCode() { aoqi@0: return path.hashCode(); aoqi@0: } aoqi@0: aoqi@0: public DocPath basename() { aoqi@0: int sep = path.lastIndexOf("/"); aoqi@0: return (sep == -1) ? this : new DocPath(path.substring(sep + 1)); aoqi@0: } aoqi@0: aoqi@0: public DocPath parent() { aoqi@0: int sep = path.lastIndexOf("/"); aoqi@0: return (sep == -1) ? empty : new DocPath(path.substring(0, sep)); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the path formed by appending the specified string to the current path. aoqi@0: */ aoqi@0: public DocPath resolve(String p) { aoqi@0: if (p == null || p.isEmpty()) aoqi@0: return this; aoqi@0: if (path.isEmpty()) aoqi@0: return new DocPath(p); aoqi@0: return new DocPath(path + "/" + p); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the path by appending the specified path to the current path. aoqi@0: */ aoqi@0: public DocPath resolve(DocPath p) { aoqi@0: if (p == null || p.isEmpty()) aoqi@0: return this; aoqi@0: if (path.isEmpty()) aoqi@0: return p; aoqi@0: return new DocPath(path + "/" + p.getPath()); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the inverse path for this path. aoqi@0: * For example, if the path is a/b/c, the inverse path is ../../.. aoqi@0: */ aoqi@0: public DocPath invert() { aoqi@0: return new DocPath(path.replaceAll("[^/]+", "..")); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return true if this path is empty. aoqi@0: */ aoqi@0: public boolean isEmpty() { aoqi@0: return path.isEmpty(); aoqi@0: } aoqi@0: aoqi@0: public DocLink fragment(String fragment) { aoqi@0: return new DocLink(path, null, fragment); aoqi@0: } aoqi@0: aoqi@0: public DocLink query(String query) { aoqi@0: return new DocLink(path, query, null); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return this path as a string. aoqi@0: */ aoqi@0: // This is provided instead of using toString() to help catch aoqi@0: // unintended use of toString() in string concatenation sequences. aoqi@0: public String getPath() { aoqi@0: return path; aoqi@0: } aoqi@0: }