langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/SourceToHTMLConverter.java
changeset 40587 1c355ea550ed
parent 40508 74ef30d16fb9
child 42277 2668b0bc7ad7
equal deleted inserted replaced
40519:e17429a7e843 40587:1c355ea550ed
    40 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
    40 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
    41 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
    41 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
    42 import jdk.javadoc.internal.doclets.toolkit.Content;
    42 import jdk.javadoc.internal.doclets.toolkit.Content;
    43 import jdk.javadoc.internal.doclets.toolkit.Messages;
    43 import jdk.javadoc.internal.doclets.toolkit.Messages;
    44 import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
    44 import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
       
    45 import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
    45 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
    46 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
    46 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
    47 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
    47 import jdk.javadoc.internal.doclets.toolkit.util.DocletAbortException;
       
    48 import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants;
    48 import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants;
       
    49 import jdk.javadoc.internal.doclets.toolkit.util.SimpleDocletException;
    49 import jdk.javadoc.internal.doclets.toolkit.util.Utils;
    50 import jdk.javadoc.internal.doclets.toolkit.util.Utils;
    50 
    51 
    51 /**
    52 /**
    52  * Converts Java Source Code to HTML.
    53  * Converts Java Source Code to HTML.
    53  *
    54  *
    77     private final Messages messages;
    78     private final Messages messages;
    78     private final Utils utils;
    79     private final Utils utils;
    79 
    80 
    80     private final DocletEnvironment docEnv;
    81     private final DocletEnvironment docEnv;
    81 
    82 
    82     private DocPath outputdir;
    83     private final DocPath outputdir;
    83 
    84 
    84     /**
    85     /**
    85      * Relative path from the documentation root to the file that is being
    86      * Relative path from the documentation root to the file that is being
    86      * generated.
    87      * generated.
    87      */
    88      */
   100      * Translate the TypeElements in the given DocletEnvironment to HTML representation.
   101      * Translate the TypeElements in the given DocletEnvironment to HTML representation.
   101      *
   102      *
   102      * @param configuration the configuration.
   103      * @param configuration the configuration.
   103      * @param docEnv the DocletEnvironment to convert.
   104      * @param docEnv the DocletEnvironment to convert.
   104      * @param outputdir the name of the directory to output to.
   105      * @param outputdir the name of the directory to output to.
       
   106      * @throws DocFileIOException if there is a problem generating an output file
       
   107      * @throws SimpleDocletException if there is a problem reading a source file
   105      */
   108      */
   106     public static void convertRoot(ConfigurationImpl configuration, DocletEnvironment docEnv,
   109     public static void convertRoot(ConfigurationImpl configuration, DocletEnvironment docEnv,
   107             DocPath outputdir) {
   110             DocPath outputdir) throws DocFileIOException, SimpleDocletException {
   108         new SourceToHTMLConverter(configuration, docEnv, outputdir).generate();
   111         new SourceToHTMLConverter(configuration, docEnv, outputdir).generate();
   109     }
   112     }
   110 
   113 
   111     void generate() {
   114     void generate() throws DocFileIOException, SimpleDocletException {
   112         if (docEnv == null || outputdir == null) {
   115         if (docEnv == null || outputdir == null) {
   113             return;
   116             return;
   114         }
   117         }
   115         for (PackageElement pkg : configuration.getSpecifiedPackages()) {
   118         for (PackageElement pkg : configuration.getSpecifiedPackages()) {
   116             // If -nodeprecated option is set and the package is marked as deprecated,
   119             // If -nodeprecated option is set and the package is marked as deprecated,
   127                 convertClass(te, outputdir);
   130                 convertClass(te, outputdir);
   128         }
   131         }
   129     }
   132     }
   130 
   133 
   131     /**
   134     /**
   132      * Convert the Classes in the given Package to an HTML.
   135      * Convert the Classes in the given Package to an HTML file.
   133      *
   136      *
   134      * @param pkg the Package to convert.
   137      * @param pkg the Package to convert.
   135      * @param outputdir the name of the directory to output to.
   138      * @param outputdir the name of the directory to output to.
   136      */
   139      * @throws DocFileIOException if there is a problem generating an output file
   137     public void convertPackage(PackageElement pkg, DocPath outputdir) {
   140      * @throws SimpleDocletException if there is a problem reading a source file
       
   141      */
       
   142     public void convertPackage(PackageElement pkg, DocPath outputdir)
       
   143             throws DocFileIOException, SimpleDocletException {
   138         if (pkg == null) {
   144         if (pkg == null) {
   139             return;
   145             return;
   140         }
   146         }
   141         for (Element te : utils.getAllClasses(pkg)) {
   147         for (Element te : utils.getAllClasses(pkg)) {
   142             // If -nodeprecated option is set and the class is marked as deprecated,
   148             // If -nodeprecated option is set and the class is marked as deprecated,
   150 
   156 
   151     /**
   157     /**
   152      * Convert the given Class to an HTML.
   158      * Convert the given Class to an HTML.
   153      *
   159      *
   154      * @param te the class to convert.
   160      * @param te the class to convert.
   155      * @param outputdir the name of the directory to output to.
   161      * @param outputdir the name of the directory to output to
   156      */
   162      * @throws DocFileIOException if there is a problem generating the output file
   157     public void convertClass(TypeElement te, DocPath outputdir) {
   163      * @throws SimpleDocletException if there is a problem reading the source file
       
   164      */
       
   165     public void convertClass(TypeElement te, DocPath outputdir)
       
   166             throws DocFileIOException, SimpleDocletException {
   158         if (te == null) {
   167         if (te == null) {
   159             return;
   168             return;
   160         }
   169         }
       
   170         FileObject fo = utils.getFileObject(te);
       
   171         if (fo == null)
       
   172             return;
       
   173 
   161         try {
   174         try {
   162             FileObject fo = utils.getFileObject(te);
       
   163             if (fo == null)
       
   164                 return;
       
   165             Reader r = fo.openReader(true);
   175             Reader r = fo.openReader(true);
   166             int lineno = 1;
   176             int lineno = 1;
   167             String line;
   177             String line;
   168             relativePath = DocPaths.SOURCE_OUTPUT
   178             relativePath = DocPaths.SOURCE_OUTPUT
   169                     .resolve(DocPath.forPackage(utils, te))
   179                     .resolve(DocPath.forPackage(utils, te))
   180             addBlankLines(pre);
   190             addBlankLines(pre);
   181             Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre);
   191             Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre);
   182             body.addContent((configuration.allowTag(HtmlTag.MAIN)) ? HtmlTree.MAIN(div) : div);
   192             body.addContent((configuration.allowTag(HtmlTag.MAIN)) ? HtmlTree.MAIN(div) : div);
   183             writeToFile(body, outputdir.resolve(DocPath.forClass(utils, te)));
   193             writeToFile(body, outputdir.resolve(DocPath.forClass(utils, te)));
   184         } catch (IOException e) {
   194         } catch (IOException e) {
   185             throw new DocletAbortException(e);
   195             String message = configuration.resources.getText("doclet.exception.read.file", fo.getName());
       
   196             throw new SimpleDocletException(message, e);
   186         }
   197         }
   187     }
   198     }
   188 
   199 
   189     /**
   200     /**
   190      * Write the output to the file.
   201      * Write the output to the file.
   191      *
   202      *
   192      * @param body the documentation content to be written to the file.
   203      * @param body the documentation content to be written to the file.
   193      * @param path the path for the file.
   204      * @param path the path for the file.
   194      */
   205      */
   195     private void writeToFile(Content body, DocPath path) throws IOException {
   206     private void writeToFile(Content body, DocPath path) throws DocFileIOException {
   196         Content htmlDocType = configuration.isOutputHtml5()
   207         Content htmlDocType = configuration.isOutputHtml5()
   197                 ? DocType.HTML5
   208                 ? DocType.HTML5
   198                 : DocType.TRANSITIONAL;
   209                 : DocType.TRANSITIONAL;
   199         Content head = new HtmlTree(HtmlTag.HEAD);
   210         Content head = new HtmlTree(HtmlTag.HEAD);
   200         head.addContent(HtmlTree.TITLE(new StringContent(
   211         head.addContent(HtmlTree.TITLE(new StringContent(
   205         Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree);
   216         Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree);
   206         messages.notice("doclet.Generating_0", path.getPath());
   217         messages.notice("doclet.Generating_0", path.getPath());
   207         DocFile df = DocFile.createFileForOutput(configuration, path);
   218         DocFile df = DocFile.createFileForOutput(configuration, path);
   208         try (Writer w = df.openWriter()) {
   219         try (Writer w = df.openWriter()) {
   209             htmlDocument.write(w, true);
   220             htmlDocument.write(w, true);
       
   221         } catch (IOException e) {
       
   222             throw new DocFileIOException(df, DocFileIOException.Mode.WRITE, e);
   210         }
   223         }
   211 
   224 
   212     }
   225     }
   213 
   226 
   214     /**
   227     /**
   285             pre.addContent(NEW_LINE);
   298             pre.addContent(NEW_LINE);
   286         }
   299         }
   287     }
   300     }
   288 
   301 
   289     /**
   302     /**
   290      * Given a <code>Doc</code>, return an anchor name for it.
   303      * Given an element, return an anchor name for it.
   291      *
   304      *
   292      * @param d the <code>Doc</code> to check.
   305      * @param utils the utility class, used to get the line number of the element
       
   306      * @param e the element to check.
   293      * @return the name of the anchor.
   307      * @return the name of the anchor.
   294      */
   308      */
   295     public static String getAnchorName(Utils utils, Element e) {
   309     public static String getAnchorName(Utils utils, Element e) {
   296         return "line." + utils.getLineNumber(e);
   310         return "line." + utils.getLineNumber(e);
   297     }
   311     }