Thu, 15 Nov 2012 19:54:20 -0800
8002079: update DocFile to use a JavaFileManager
Reviewed-by: darcy
duke@1 | 1 | /* |
jjg@1357 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | package com.sun.tools.doclets.formats.html; |
duke@1 | 26 | |
jjg@1357 | 27 | import java.io.*; |
jjg@1357 | 28 | import java.util.*; |
jjg@1357 | 29 | |
jjg@1357 | 30 | import com.sun.javadoc.*; |
duke@1 | 31 | import com.sun.tools.doclets.internal.toolkit.*; |
duke@1 | 32 | import com.sun.tools.doclets.internal.toolkit.builders.*; |
duke@1 | 33 | import com.sun.tools.doclets.internal.toolkit.util.*; |
duke@1 | 34 | |
duke@1 | 35 | /** |
duke@1 | 36 | * The class with "start" method, calls individual Writers. |
duke@1 | 37 | * |
jjg@1359 | 38 | * <p><b>This is NOT part of any supported API. |
jjg@1359 | 39 | * If you write code that depends on this, you do so at your own risk. |
jjg@1359 | 40 | * This code and its internal interfaces are subject to change or |
jjg@1359 | 41 | * deletion without notice.</b> |
jjg@1359 | 42 | * |
duke@1 | 43 | * @author Atul M Dambalkar |
duke@1 | 44 | * @author Robert Field |
duke@1 | 45 | * @author Jamie Ho |
duke@1 | 46 | * |
duke@1 | 47 | */ |
duke@1 | 48 | public class HtmlDoclet extends AbstractDoclet { |
jjg@1410 | 49 | // An instance will be created by validOptions, and used by start. |
jjg@1410 | 50 | private static HtmlDoclet docletToStart = null; |
jjg@1410 | 51 | |
jjg@140 | 52 | public HtmlDoclet() { |
jjg@1410 | 53 | configuration = new ConfigurationImpl(); |
jjg@140 | 54 | } |
duke@1 | 55 | |
duke@1 | 56 | /** |
duke@1 | 57 | * The global configuration information for this run. |
duke@1 | 58 | */ |
jjg@1410 | 59 | public final ConfigurationImpl configuration; |
duke@1 | 60 | |
duke@1 | 61 | /** |
duke@1 | 62 | * The "start" method as required by Javadoc. |
duke@1 | 63 | * |
duke@1 | 64 | * @param root the root of the documentation tree. |
duke@1 | 65 | * @see com.sun.javadoc.RootDoc |
duke@1 | 66 | * @return true if the doclet ran without encountering any errors. |
duke@1 | 67 | */ |
duke@1 | 68 | public static boolean start(RootDoc root) { |
jjg@1410 | 69 | // In typical use, options will have been set up by calling validOptions, |
jjg@1410 | 70 | // which will create an HtmlDoclet for use here. |
jjg@1410 | 71 | HtmlDoclet doclet; |
jjg@1410 | 72 | if (docletToStart != null) { |
jjg@1410 | 73 | doclet = docletToStart; |
jjg@1410 | 74 | docletToStart = null; |
jjg@1410 | 75 | } else { |
jjg@1410 | 76 | doclet = new HtmlDoclet(); |
jjg@140 | 77 | } |
jjg@1410 | 78 | return doclet.start(doclet, root); |
duke@1 | 79 | } |
duke@1 | 80 | |
duke@1 | 81 | /** |
duke@1 | 82 | * Create the configuration instance. |
duke@1 | 83 | * Override this method to use a different |
duke@1 | 84 | * configuration. |
duke@1 | 85 | */ |
duke@1 | 86 | public Configuration configuration() { |
jjg@1410 | 87 | return configuration; |
duke@1 | 88 | } |
duke@1 | 89 | |
duke@1 | 90 | /** |
duke@1 | 91 | * Start the generation of files. Call generate methods in the individual |
duke@1 | 92 | * writers, which will in turn genrate the documentation files. Call the |
duke@1 | 93 | * TreeWriter generation first to ensure the Class Hierarchy is built |
duke@1 | 94 | * first and then can be used in the later generation. |
duke@1 | 95 | * |
duke@1 | 96 | * For new format. |
duke@1 | 97 | * |
duke@1 | 98 | * @see com.sun.javadoc.RootDoc |
duke@1 | 99 | */ |
duke@1 | 100 | protected void generateOtherFiles(RootDoc root, ClassTree classtree) |
duke@1 | 101 | throws Exception { |
duke@1 | 102 | super.generateOtherFiles(root, classtree); |
duke@1 | 103 | if (configuration.linksource) { |
jjg@1372 | 104 | SourceToHTMLConverter.convertRoot(configuration, |
jjg@1372 | 105 | root, DocPaths.SOURCE_OUTPUT); |
duke@1 | 106 | } |
duke@1 | 107 | |
jjg@1372 | 108 | if (configuration.topFile.isEmpty()) { |
duke@1 | 109 | configuration.standardmessage. |
duke@1 | 110 | error("doclet.No_Non_Deprecated_Classes_To_Document"); |
duke@1 | 111 | return; |
duke@1 | 112 | } |
duke@1 | 113 | boolean nodeprecated = configuration.nodeprecated; |
jjg@1383 | 114 | performCopy(configuration.helpfile); |
jjg@1383 | 115 | performCopy(configuration.stylesheetfile); |
jjg@1383 | 116 | copyResourceFile("background.gif"); |
jjg@1383 | 117 | copyResourceFile("tab.gif"); |
jjg@1383 | 118 | copyResourceFile("titlebar.gif"); |
jjg@1383 | 119 | copyResourceFile("titlebar_end.gif"); |
duke@1 | 120 | // do early to reduce memory footprint |
duke@1 | 121 | if (configuration.classuse) { |
duke@1 | 122 | ClassUseWriter.generate(configuration, classtree); |
duke@1 | 123 | } |
duke@1 | 124 | IndexBuilder indexbuilder = new IndexBuilder(configuration, nodeprecated); |
duke@1 | 125 | |
duke@1 | 126 | if (configuration.createtree) { |
duke@1 | 127 | TreeWriter.generate(configuration, classtree); |
duke@1 | 128 | } |
duke@1 | 129 | if (configuration.createindex) { |
duke@1 | 130 | if (configuration.splitindex) { |
duke@1 | 131 | SplitIndexWriter.generate(configuration, indexbuilder); |
duke@1 | 132 | } else { |
duke@1 | 133 | SingleIndexWriter.generate(configuration, indexbuilder); |
duke@1 | 134 | } |
duke@1 | 135 | } |
duke@1 | 136 | |
duke@1 | 137 | if (!(configuration.nodeprecatedlist || nodeprecated)) { |
duke@1 | 138 | DeprecatedListWriter.generate(configuration); |
duke@1 | 139 | } |
duke@1 | 140 | |
duke@1 | 141 | AllClassesFrameWriter.generate(configuration, |
duke@1 | 142 | new IndexBuilder(configuration, nodeprecated, true)); |
duke@1 | 143 | |
duke@1 | 144 | FrameOutputWriter.generate(configuration); |
duke@1 | 145 | |
duke@1 | 146 | if (configuration.createoverview) { |
duke@1 | 147 | PackageIndexWriter.generate(configuration); |
duke@1 | 148 | } |
duke@1 | 149 | if (configuration.helpfile.length() == 0 && |
duke@1 | 150 | !configuration.nohelp) { |
duke@1 | 151 | HelpWriter.generate(configuration); |
duke@1 | 152 | } |
bpatel@793 | 153 | // If a stylesheet file is not specified, copy the default stylesheet |
bpatel@793 | 154 | // and replace newline with platform-specific newline. |
duke@1 | 155 | if (configuration.stylesheetfile.length() == 0) { |
jjg@1383 | 156 | DocFile f = DocFile.createFileForOutput(configuration, DocPaths.STYLESHEET); |
jjg@1383 | 157 | f.copyResource(DocPaths.RESOURCES.resolve(DocPaths.STYLESHEET), false, true); |
duke@1 | 158 | } |
duke@1 | 159 | } |
duke@1 | 160 | |
duke@1 | 161 | /** |
duke@1 | 162 | * {@inheritDoc} |
duke@1 | 163 | */ |
duke@1 | 164 | protected void generateClassFiles(ClassDoc[] arr, ClassTree classtree) { |
duke@1 | 165 | Arrays.sort(arr); |
duke@1 | 166 | for(int i = 0; i < arr.length; i++) { |
duke@1 | 167 | if (!(configuration.isGeneratedDoc(arr[i]) && arr[i].isIncluded())) { |
duke@1 | 168 | continue; |
duke@1 | 169 | } |
duke@1 | 170 | ClassDoc prev = (i == 0)? |
duke@1 | 171 | null: |
duke@1 | 172 | arr[i-1]; |
duke@1 | 173 | ClassDoc curr = arr[i]; |
duke@1 | 174 | ClassDoc next = (i+1 == arr.length)? |
duke@1 | 175 | null: |
duke@1 | 176 | arr[i+1]; |
duke@1 | 177 | try { |
duke@1 | 178 | if (curr.isAnnotationType()) { |
duke@1 | 179 | AbstractBuilder annotationTypeBuilder = |
duke@1 | 180 | configuration.getBuilderFactory() |
duke@1 | 181 | .getAnnotationTypeBuilder((AnnotationTypeDoc) curr, |
duke@1 | 182 | prev, next); |
duke@1 | 183 | annotationTypeBuilder.build(); |
duke@1 | 184 | } else { |
duke@1 | 185 | AbstractBuilder classBuilder = |
duke@1 | 186 | configuration.getBuilderFactory() |
duke@1 | 187 | .getClassBuilder(curr, prev, next, classtree); |
duke@1 | 188 | classBuilder.build(); |
duke@1 | 189 | } |
duke@1 | 190 | } catch (Exception e) { |
duke@1 | 191 | e.printStackTrace(); |
duke@1 | 192 | throw new DocletAbortException(); |
duke@1 | 193 | } |
duke@1 | 194 | } |
duke@1 | 195 | } |
duke@1 | 196 | |
duke@1 | 197 | /** |
duke@1 | 198 | * {@inheritDoc} |
duke@1 | 199 | */ |
duke@1 | 200 | protected void generatePackageFiles(ClassTree classtree) throws Exception { |
duke@1 | 201 | PackageDoc[] packages = configuration.packages; |
duke@1 | 202 | if (packages.length > 1) { |
duke@1 | 203 | PackageIndexFrameWriter.generate(configuration); |
duke@1 | 204 | } |
duke@1 | 205 | PackageDoc prev = null, next; |
bpatel@995 | 206 | for (int i = 0; i < packages.length; i++) { |
bpatel@995 | 207 | // if -nodeprecated option is set and the package is marked as |
bpatel@995 | 208 | // deprecated, do not generate the package-summary.html, package-frame.html |
bpatel@995 | 209 | // and package-tree.html pages for that package. |
bpatel@995 | 210 | if (!(configuration.nodeprecated && Util.isDeprecated(packages[i]))) { |
bpatel@995 | 211 | PackageFrameWriter.generate(configuration, packages[i]); |
bpatel@995 | 212 | next = (i + 1 < packages.length && |
bpatel@995 | 213 | packages[i + 1].name().length() > 0) ? packages[i + 1] : null; |
bpatel@995 | 214 | //If the next package is unnamed package, skip 2 ahead if possible |
bpatel@995 | 215 | next = (i + 2 < packages.length && next == null) ? packages[i + 2] : next; |
bpatel@995 | 216 | AbstractBuilder packageSummaryBuilder = |
bpatel@995 | 217 | configuration.getBuilderFactory().getPackageSummaryBuilder( |
bpatel@995 | 218 | packages[i], prev, next); |
bpatel@995 | 219 | packageSummaryBuilder.build(); |
bpatel@995 | 220 | if (configuration.createtree) { |
bpatel@995 | 221 | PackageTreeWriter.generate(configuration, |
bpatel@995 | 222 | packages[i], prev, next, |
bpatel@995 | 223 | configuration.nodeprecated); |
bpatel@995 | 224 | } |
bpatel@995 | 225 | prev = packages[i]; |
duke@1 | 226 | } |
duke@1 | 227 | } |
duke@1 | 228 | } |
duke@1 | 229 | |
jjg@1410 | 230 | public static final ConfigurationImpl sharedInstanceForOptions = |
jjg@1410 | 231 | new ConfigurationImpl(); |
jjg@1410 | 232 | |
duke@1 | 233 | /** |
duke@1 | 234 | * Check for doclet added options here. |
duke@1 | 235 | * |
duke@1 | 236 | * @return number of arguments to option. Zero return means |
duke@1 | 237 | * option not known. Negative value means error occurred. |
duke@1 | 238 | */ |
duke@1 | 239 | public static int optionLength(String option) { |
duke@1 | 240 | // Construct temporary configuration for check |
jjg@1410 | 241 | return sharedInstanceForOptions.optionLength(option); |
duke@1 | 242 | } |
duke@1 | 243 | |
duke@1 | 244 | /** |
duke@1 | 245 | * Check that options have the correct arguments here. |
duke@1 | 246 | * <P> |
duke@1 | 247 | * This method is not required and will default gracefully |
duke@1 | 248 | * (to true) if absent. |
duke@1 | 249 | * <P> |
duke@1 | 250 | * Printing option related error messages (using the provided |
duke@1 | 251 | * DocErrorReporter) is the responsibility of this method. |
duke@1 | 252 | * |
duke@1 | 253 | * @return true if the options are valid. |
duke@1 | 254 | */ |
duke@1 | 255 | public static boolean validOptions(String options[][], |
duke@1 | 256 | DocErrorReporter reporter) { |
jjg@1410 | 257 | docletToStart = new HtmlDoclet(); |
jjg@1410 | 258 | return docletToStart.configuration.validOptions(options, reporter); |
duke@1 | 259 | } |
duke@1 | 260 | |
jjg@1383 | 261 | /** |
jjg@1383 | 262 | * Copy a file in the resources directory to the destination directory. |
jjg@1383 | 263 | * @param resource The name of the resource file to copy |
jjg@1383 | 264 | */ |
jjg@1383 | 265 | private void copyResourceFile(String resource) { |
jjg@1383 | 266 | DocPath p = DocPaths.RESOURCES.resolve(resource); |
jjg@1383 | 267 | DocFile f = DocFile.createFileForOutput(configuration, p); |
jjg@1383 | 268 | f.copyResource(p, false, false); |
jjg@1383 | 269 | } |
jjg@1383 | 270 | |
jjg@1383 | 271 | private void performCopy(String filename) { |
jjg@1383 | 272 | if (filename.isEmpty()) |
jjg@1383 | 273 | return; |
jjg@1383 | 274 | |
duke@1 | 275 | try { |
jjg@1383 | 276 | DocFile fromfile = DocFile.createFileForInput(configuration, filename); |
jjg@1383 | 277 | DocPath path = DocPath.create(fromfile.getName()); |
jjg@1383 | 278 | DocFile toFile = DocFile.createFileForOutput(configuration, path); |
jjg@1383 | 279 | if (toFile.isSameFile(fromfile)) |
jjg@1383 | 280 | return; |
jjg@1383 | 281 | |
jjg@1383 | 282 | configuration.message.notice((SourcePosition) null, |
jjg@1383 | 283 | "doclet.Copying_File_0_To_File_1", |
jjg@1383 | 284 | fromfile.toString(), path.getPath()); |
jjg@1383 | 285 | toFile.copyFile(fromfile); |
duke@1 | 286 | } catch (IOException exc) { |
jjg@1383 | 287 | configuration.message.error((SourcePosition) null, |
duke@1 | 288 | "doclet.perform_copy_exception_encountered", |
duke@1 | 289 | exc.toString()); |
duke@1 | 290 | throw new DocletAbortException(); |
duke@1 | 291 | } |
duke@1 | 292 | } |
duke@1 | 293 | } |