1 /*
2  * Copyright (c) 1998, 2018, 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 jdk.javadoc.internal.doclets.toolkit.util;
27 
28 import javax.lang.model.element.ModuleElement;
29 import javax.lang.model.element.PackageElement;
30 import javax.lang.model.element.TypeElement;
31 
32 /**
33  * Standard DocPath objects.
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  */
41 public class DocPaths {
42     private final boolean useModuleDirectories;
43     private final String moduleSeparator;
44     private final Utils utils;
45 
DocPaths(Utils utils, boolean useModuleDirectories)46     public DocPaths(Utils utils, boolean useModuleDirectories) {
47         this.utils = utils;
48         this.useModuleDirectories = useModuleDirectories;
49         moduleSeparator = useModuleDirectories ? "/module-" : "-";
50     }
51 
52     public static final DocPath DOT_DOT = DocPath.create("..");
53 
54     /** The name of the file for all classes, without using frames, when --no-frames is specified. */
55     public static final DocPath ALLCLASSES = DocPath.create("allclasses.html");
56 
57     /** The name of the file for all classes, using frames. */
58     public static final DocPath ALLCLASSES_FRAME = DocPath.create("allclasses-frame.html");
59 
60     /** The name of the file for all classes index. */
61     public static final DocPath ALLCLASSES_INDEX = DocPath.create("allclasses-index.html");
62 
63     /** The name of the file for all packages index. */
64     public static final DocPath ALLPACKAGES_INDEX = DocPath.create("allpackages-index.html");
65 
66     /** The name of the file for all classes, without using frames. */
67     public static final DocPath ALLCLASSES_NOFRAME = DocPath.create("allclasses-noframe.html");
68 
AllClasses(boolean frames)69     public static DocPath AllClasses(boolean frames) {
70         return frames ? ALLCLASSES_NOFRAME : ALLCLASSES;
71     }
72 
73     /** The name of the sub-directory for storing class usage info. */
74     public static final DocPath CLASS_USE = DocPath.create("class-use");
75 
76     /** The name of the file for constant values. */
77     public static final DocPath CONSTANT_VALUES = DocPath.create("constant-values.html");
78 
79     /** The name of the fie for deprecated elements. */
80     public static final DocPath DEPRECATED_LIST = DocPath.create("deprecated-list.html");
81 
82     /** The name of the subdirectory for user-provided additional documentation files. */
83     public static final DocPath DOC_FILES = DocPath.create("doc-files");
84 
85     /** The name of the file for the element list. */
86     public static final DocPath ELEMENT_LIST = DocPath.create("element-list");
87 
88     /** The name of the image file showing a magnifying glass on the search box. */
89     public static final DocPath GLASS_IMG = DocPath.create("glass.png");
90 
91     /** The name of the file for help info. */
92     public static final DocPath HELP_DOC = DocPath.create("help-doc.html");
93 
94     /** The name of the main index file. */
95     public static final DocPath INDEX = DocPath.create("index.html");
96 
97     /** The name of the single index file for all classes. */
98     public static final DocPath INDEX_ALL = DocPath.create("index-all.html");
99 
100     /** The name of the directory for the split index files. */
101     public static final DocPath INDEX_FILES = DocPath.create("index-files");
102 
103     /**
104      * Generate the name of one of the files in the split index.
105      * @param n the position in the index
106      * @return the path
107      */
indexN(int n)108     public static DocPath indexN(int n) {
109         return DocPath.create("index-" + n + ".html");
110     }
111 
112     /** The name of the default javascript file. */
113     public static final DocPath JAVASCRIPT = DocPath.create("script.js");
114 
115     /** The name of the directory for the jQuery. */
116     public static final DocPath JQUERY_FILES = DocPath.create("jquery");
117 
118     /** The name of the default jQuery stylesheet file. */
119     public static final DocPath JQUERY_STYLESHEET_FILE = DocPath.create("jquery-ui.css");
120 
121     /** The name of the default jQuery javascript file. */
122     public static final DocPath JQUERY_JS_3_3 = DocPath.create("jquery-3.3.1.js");
123 
124     /** The name of jquery-migrate javascript file. */
125     public static final DocPath JQUERY_MIGRATE = DocPath.create("jquery-migrate-3.0.1.js");
126 
127     /** The name of the default jQuery javascript file. */
128     public static final DocPath JQUERY_JS = DocPath.create("jquery-ui.js");
129 
130     /** The name of the default jszip javascript file. */
131     public static final DocPath JSZIP = DocPath.create("jszip/dist/jszip.js");
132 
133     /** The name of the default jszip javascript file. */
134     public static final DocPath JSZIP_MIN = DocPath.create("jszip/dist/jszip.min.js");
135 
136     /** The name of the default jszip-utils javascript file. */
137     public static final DocPath JSZIPUTILS = DocPath.create("jszip-utils/dist/jszip-utils.js");
138 
139     /** The name of the default jszip-utils javascript file. */
140     public static final DocPath JSZIPUTILS_MIN = DocPath.create("jszip-utils/dist/jszip-utils.min.js");
141 
142     /** The name of the default jszip-utils javascript file. */
143     public static final DocPath JSZIPUTILS_IE = DocPath.create("jszip-utils/dist/jszip-utils-ie.js");
144 
145     /** The name of the default jszip-utils javascript file. */
146     public static final DocPath JSZIPUTILS_IE_MIN = DocPath.create("jszip-utils/dist/jszip-utils-ie.min.js");
147 
148     /** The name of the member search index file. */
149     public static final DocPath MEMBER_SEARCH_INDEX_JSON = DocPath.create("member-search-index.json");
150 
151     /** The name of the member search index zip file. */
152     public static final DocPath MEMBER_SEARCH_INDEX_ZIP = DocPath.create("member-search-index.zip");
153 
154     /** The name of the member search index js file. */
155     public static final DocPath MEMBER_SEARCH_INDEX_JS = DocPath.create("member-search-index.js");
156 
157     /** The name of the module search index file. */
158     public static final DocPath MODULE_SEARCH_INDEX_JSON = DocPath.create("module-search-index.json");
159 
160     /** The name of the module search index zip file. */
161     public static final DocPath MODULE_SEARCH_INDEX_ZIP = DocPath.create("module-search-index.zip");
162 
163     /** The name of the module search index js file. */
164     public static final DocPath MODULE_SEARCH_INDEX_JS = DocPath.create("module-search-index.js");
165 
166     /** The name of the file for the overview frame. */
167     public static final DocPath OVERVIEW_FRAME = DocPath.create("overview-frame.html");
168 
169     /** The name of the file for the overview summary. */
170     public static final DocPath OVERVIEW_SUMMARY = DocPath.create("overview-summary.html");
171 
overviewSummary(boolean frames)172     public static DocPath overviewSummary(boolean frames) {
173         return frames ? OVERVIEW_SUMMARY : INDEX;
174     }
175 
176     /** The name of the file for the overview tree. */
177     public static final DocPath OVERVIEW_TREE = DocPath.create("overview-tree.html");
178 
179     /** The name of the file for the package frame. */
180     public static final DocPath PACKAGE_FRAME = DocPath.create("package-frame.html");
181 
182     /** The name of the file for the package list. This is to support the legacy mode. */
183     public static final DocPath PACKAGE_LIST = DocPath.create("package-list");
184 
185     /** The name of the package search index file. */
186     public static final DocPath PACKAGE_SEARCH_INDEX_JSON = DocPath.create("package-search-index.json");
187 
188     /** The name of the package search index zip file. */
189     public static final DocPath PACKAGE_SEARCH_INDEX_ZIP = DocPath.create("package-search-index.zip");
190 
191     /** The name of the package search index js file. */
192     public static final DocPath PACKAGE_SEARCH_INDEX_JS = DocPath.create("package-search-index.js");
193 
194     /** The name of the file for the package summary. */
195     public static final DocPath PACKAGE_SUMMARY = DocPath.create("package-summary.html");
196 
197     /** The name of the file for the package tree. */
198     public static final DocPath PACKAGE_TREE = DocPath.create("package-tree.html");
199 
200     /** The name of the file for the package usage info. */
201     public static final DocPath PACKAGE_USE = DocPath.create("package-use.html");
202 
203     /**
204      * Returns the path for a type element.
205      * For example, if the type element is {@code java.lang.Object},
206      * the path is {@code java/lang/Object.html}.
207      *
208      * @param typeElement the type element
209      * @return the path
210      */
forClass(TypeElement typeElement)211     public DocPath forClass(TypeElement typeElement) {
212         return (typeElement == null)
213                 ? DocPath.empty
214                 : forPackage(utils.containingPackage(typeElement)).resolve(forName(typeElement));
215     }
216 
217     /**
218      * Returns the path for the simple name of a type element.
219      * For example, if the type element is {@code java.lang.Object},
220      * the path is {@code Object.html}.
221      *
222      * @param typeElement the type element
223      * @return the path
224      */
forName(TypeElement typeElement)225     public DocPath forName(TypeElement typeElement) {
226         return (typeElement == null)
227                 ? DocPath.empty
228                 : new DocPath(utils.getSimpleName(typeElement) + ".html");
229     }
230 
forModule(ModuleElement mdle)231     public static DocPath forModule(ModuleElement mdle) {
232         return mdle == null || mdle.isUnnamed()
233                 ? DocPath.empty
234                 : DocPath.create(mdle.getQualifiedName().toString());
235     }
236 
237     /**
238      * Returns the path for the package of a type element.
239      * For example, if the type element is {@code java.lang.Object},
240      * the path is {@code java/lang}.
241      *
242      * @param typeElement the type element
243      * @return the path
244      */
forPackage(TypeElement typeElement)245     public DocPath forPackage(TypeElement typeElement) {
246         return (typeElement == null)
247                 ? DocPath.empty
248                 : forPackage(utils.containingPackage(typeElement));
249     }
250 
251     /**
252      * Returns the path for a package.
253      * For example, if the package is {@code java.lang},
254      * the path is {@code java/lang}.
255      *
256      * @param pkgElement the package element
257      * @return the path
258      */
forPackage(PackageElement pkgElement)259     public DocPath forPackage(PackageElement pkgElement) {
260         if (pkgElement == null || pkgElement.isUnnamed()) {
261             return DocPath.empty;
262         }
263 
264         DocPath pkgPath = DocPath.create(pkgElement.getQualifiedName().toString().replace('.', '/'));
265         if (useModuleDirectories) {
266             ModuleElement mdle = (ModuleElement) pkgElement.getEnclosingElement();
267             return forModule(mdle).resolve(pkgPath);
268         } else {
269             return pkgPath;
270         }
271     }
272 
273     /**
274      * Returns the inverse path for a package.
275      * For example, if the package is {@code java.lang},
276      * the inverse path is {@code ../..}.
277      *
278      * @param pkgElement the package element
279      * @return the path
280      */
forRoot(PackageElement pkgElement)281     public static DocPath forRoot(PackageElement pkgElement) {
282         String name = (pkgElement == null || pkgElement.isUnnamed())
283                 ? ""
284                 : pkgElement.getQualifiedName().toString();
285         return new DocPath(name.replace('.', '/').replaceAll("[^/]+", ".."));
286     }
287 
288     /**
289      * Returns a relative path from one package to another.
290      *
291      * @param from the origin of the relative path
292      * @param to the destination of the relative path
293      * @return the path
294      */
relativePath(PackageElement from, PackageElement to)295     public DocPath relativePath(PackageElement from, PackageElement to) {
296         return forRoot(from).resolve(forPackage(to));
297     }
298 
299     /**
300      * The path for the output directory for module documentation files.
301      * @param mdle the module
302      * @return the path
303      */
moduleDocFiles(ModuleElement mdle)304     public DocPath moduleDocFiles(ModuleElement mdle) {
305         return createModulePath(mdle, "doc-files");
306     }
307 
308     /**
309      * The path for the file for a module's frame page.
310      * @param mdle the module
311      * @return the path
312      */
moduleFrame(ModuleElement mdle)313     public DocPath moduleFrame(ModuleElement mdle) {
314         return createModulePath(mdle, "frame.html");
315     }
316 
317     /**
318      * The path for the file for a module's summary page.
319      * @param mdle the module
320      * @return the path
321      */
moduleSummary(ModuleElement mdle)322     public DocPath moduleSummary(ModuleElement mdle) {
323         return createModulePath(mdle, "summary.html");
324     }
325 
326     /**
327      * The path for the file for a module's summary page.
328      * @param mdleName the module
329      * @return the path
330      */
moduleSummary(String mdleName)331     public DocPath moduleSummary(String mdleName) {
332         return createModulePath(mdleName, "summary.html");
333     }
334 
335     /**
336      * The path for the file for a module's type frame page.
337      * @param mdle the module
338      * @return the path
339      */
moduleTypeFrame(ModuleElement mdle)340     public DocPath moduleTypeFrame(ModuleElement mdle) {
341         return createModulePath(mdle, "type-frame.html");
342     }
343 
createModulePath(ModuleElement mdle, String path)344     private DocPath createModulePath(ModuleElement mdle, String path) {
345         return DocPath.create(mdle.getQualifiedName() + moduleSeparator + path);
346     }
347 
createModulePath(String moduleName, String path)348     private DocPath createModulePath(String moduleName, String path) {
349         return DocPath.create(moduleName + moduleSeparator + path);
350     }
351 
352     /** The name of the file for the module overview frame. */
353     public static final DocPath MODULE_OVERVIEW_FRAME = DocPath.create("module-overview-frame.html");
354 
355     /** The name of the sub-package from which resources are read. */
356     public static final DocPath RESOURCES = DocPath.create("resources");
357 
358     /** The name of the search javascript file. */
359     public static final DocPath SEARCH_JS = DocPath.create("search.js");
360 
361     /** The name of the file for the serialized form info. */
362     public static final DocPath SERIALIZED_FORM = DocPath.create("serialized-form.html");
363 
364     /** The name of the directory in which HTML versions of the source code
365      *  are generated.
366      */
367     public static final DocPath SOURCE_OUTPUT = DocPath.create("src-html");
368 
369     /** The name of the default stylesheet. */
370     public static final DocPath STYLESHEET = DocPath.create("stylesheet.css");
371 
372     /** The name of the tag search index file. */
373     public static final DocPath TAG_SEARCH_INDEX_JSON = DocPath.create("tag-search-index.json");
374 
375     /** The name of the tag search index zip file. */
376     public static final DocPath TAG_SEARCH_INDEX_ZIP = DocPath.create("tag-search-index.zip");
377 
378     /** The name of the tag search index js file. */
379     public static final DocPath TAG_SEARCH_INDEX_JS = DocPath.create("tag-search-index.js");
380 
381     /** The name of the type search index file. */
382     public static final DocPath TYPE_SEARCH_INDEX_JSON = DocPath.create("type-search-index.json");
383 
384     /** The name of the type search index zip file. */
385     public static final DocPath TYPE_SEARCH_INDEX_ZIP = DocPath.create("type-search-index.zip");
386 
387     /** The name of the type search index js file. */
388     public static final DocPath TYPE_SEARCH_INDEX_JS = DocPath.create("type-search-index.js");
389 
390     /** The name of the image file for undo button on the search box. */
391     public static final DocPath X_IMG = DocPath.create("x.png");
392 
393 }
394