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