src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/SourceToHTMLConverter.java

Print this page


   1 /*
   2  * Copyright (c) 2001, 2015, 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.formats.html;
  27 
  28 import java.io.*;
  29 



  30 import javax.tools.FileObject;
  31 
  32 import com.sun.javadoc.*;
  33 import com.sun.tools.doclets.formats.html.markup.*;
  34 import com.sun.tools.doclets.internal.toolkit.*;
  35 import com.sun.tools.doclets.internal.toolkit.util.*;










  36 
  37 /**
  38  * Converts Java Source Code to HTML.
  39  *
  40  *  <p><b>This is NOT part of any supported API.
  41  *  If you write code that depends on this, you do so at your own risk.
  42  *  This code and its internal interfaces are subject to change or
  43  *  deletion without notice.</b>
  44  *
  45  * @author Jamie Ho
  46  * @author Bhavesh Patel (Modified)
  47  * @since 1.4
  48  */
  49 public class SourceToHTMLConverter {
  50 
  51     /**
  52      * The number of trailing blank lines at the end of the page.
  53      * This is inserted so that anchors at the bottom of small pages
  54      * can be reached.
  55      */
  56     private static final int NUM_BLANK_LINES = 60;
  57 
  58     /**
  59      * New line to be added to the documentation.
  60      */
  61     private static final String NEW_LINE = DocletConstants.NL;
  62 
  63     private final ConfigurationImpl configuration;
  64     private final Utils utils;
  65 
  66     private final RootDoc rootDoc;
  67 
  68     private DocPath outputdir;
  69 
  70     /**
  71      * Relative path from the documentation root to the file that is being
  72      * generated.
  73      */
  74     private DocPath relativePath = DocPath.empty;
  75 
  76     private SourceToHTMLConverter(ConfigurationImpl configuration, RootDoc rd,
  77             DocPath outputdir) {
  78         this.configuration  = configuration;
  79         this.utils = configuration.utils;
  80         this.rootDoc = rd;
  81         this.outputdir = outputdir;
  82     }
  83 
  84     /**
  85      * Convert the Classes in the given RootDoc to an HTML.
  86      *
  87      * @param configuration the configuration.
  88      * @param rd the RootDoc to convert.
  89      * @param outputdir the name of the directory to output to.
  90      */
  91     public static void convertRoot(ConfigurationImpl configuration, RootDoc rd,
  92             DocPath outputdir) {
  93         new SourceToHTMLConverter(configuration, rd, outputdir).generate();
  94     }
  95 
  96     void generate() {
  97         if (rootDoc == null || outputdir == null) {
  98             return;
  99         }
 100         for (PackageDoc pd : rootDoc.specifiedPackages()) {
 101             // If -nodeprecated option is set and the package is marked as deprecated,
 102             // do not convert the package files to HTML.
 103             if (!(configuration.nodeprecated && utils.isDeprecated(pd)))
 104                 convertPackage(pd, outputdir);
 105         }
 106         for (ClassDoc cd : rootDoc.specifiedClasses()) {
 107             // If -nodeprecated option is set and the class is marked as deprecated
 108             // or the containing package is deprecated, do not convert the
 109             // package files to HTML.
 110             if (!(configuration.nodeprecated &&
 111                   (utils.isDeprecated(cd) || utils.isDeprecated(cd.containingPackage()))))
 112                 convertClass(cd, outputdir);
 113         }
 114     }
 115 
 116     /**
 117      * Convert the Classes in the given Package to an HTML.
 118      *
 119      * @param pd the Package to convert.
 120      * @param outputdir the name of the directory to output to.
 121      */
 122     public void convertPackage(PackageDoc pd, DocPath outputdir) {
 123         if (pd == null) {
 124             return;
 125         }
 126         for (ClassDoc cd : pd.allClasses()) {
 127             // If -nodeprecated option is set and the class is marked as deprecated,
 128             // do not convert the package files to HTML. We do not check for
 129             // containing package deprecation since it is already check in
 130             // the calling method above.
 131             if (!(configuration.nodeprecated && utils.isDeprecated(cd)))
 132                 convertClass(cd, outputdir);
 133         }
 134     }
 135 
 136     /**
 137      * Convert the given Class to an HTML.
 138      *
 139      * @param cd the class to convert.
 140      * @param outputdir the name of the directory to output to.
 141      */
 142     public void convertClass(ClassDoc cd, DocPath outputdir) {
 143         if (cd == null) {
 144             return;
 145         }
 146         try {
 147             SourcePosition sp = cd.position();
 148             if (sp == null)
 149                 return;
 150             Reader r;
 151             // temp hack until we can update SourcePosition API.
 152             if (sp instanceof com.sun.tools.javadoc.SourcePositionImpl) {
 153                 FileObject fo = ((com.sun.tools.javadoc.SourcePositionImpl) sp).fileObject();
 154                 if (fo == null)
 155                     return;
 156                 r = fo.openReader(true);
 157             } else {
 158                 File file = sp.file();
 159                 if (file == null)
 160                     return;
 161                 r = new FileReader(file);
 162             }
 163             int lineno = 1;
 164             String line;
 165             relativePath = DocPaths.SOURCE_OUTPUT
 166                     .resolve(DocPath.forPackage(cd))
 167                     .invert();
 168             Content body = getHeader();
 169             Content pre = new HtmlTree(HtmlTag.PRE);
 170             try (LineNumberReader reader = new LineNumberReader(r)) {
 171                 while ((line = reader.readLine()) != null) {
 172                     addLineNo(pre, lineno);
 173                     addLine(pre, line, lineno);
 174                     lineno++;
 175                 }
 176             }
 177             addBlankLines(pre);
 178             Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre);
 179             body.addContent((configuration.allowTag(HtmlTag.MAIN)) ? HtmlTree.MAIN(div) : div);
 180             writeToFile(body, outputdir.resolve(DocPath.forClass(cd)));
 181         } catch (IOException e) {
 182             e.printStackTrace();
 183         }
 184     }
 185 
 186     /**
 187      * Write the output to the file.
 188      *
 189      * @param body the documentation content to be written to the file.
 190      * @param path the path for the file.
 191      */
 192     private void writeToFile(Content body, DocPath path) throws IOException {
 193         Content htmlDocType = configuration.isOutputHtml5()
 194                 ? DocType.HTML5
 195                 : DocType.TRANSITIONAL;
 196         Content head = new HtmlTree(HtmlTag.HEAD);
 197         head.addContent(HtmlTree.TITLE(new StringContent(
 198                 configuration.getText("doclet.Window_Source_title"))));
 199         head.addContent(getStyleSheetProperties());
 200         Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
 201                 head, body);
 202         Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree);


 249             span.addContent("00" + Integer.toString(lineno));
 250         } else if (lineno < 100) {
 251             span.addContent("0" + Integer.toString(lineno));
 252         } else {
 253             span.addContent(Integer.toString(lineno));
 254         }
 255         pre.addContent(span);
 256     }
 257 
 258     /**
 259      * Add a line from source to the HTML file that is generated.
 260      *
 261      * @param pre the content tree to which the line will be added.
 262      * @param line the string to format.
 263      * @param currentLineNo the current number.
 264      */
 265     private void addLine(Content pre, String line, int currentLineNo) {
 266         if (line != null) {
 267             Content anchor = HtmlTree.A(configuration.htmlVersion,
 268                     "line." + Integer.toString(currentLineNo),
 269                     new StringContent(utils.replaceTabs(configuration, line)));
 270             pre.addContent(anchor);
 271             pre.addContent(NEW_LINE);
 272         }
 273     }
 274 
 275     /**
 276      * Add trailing blank lines at the end of the page.
 277      *
 278      * @param pre the content tree to which the blank lines will be added.
 279      */
 280     private static void addBlankLines(Content pre) {
 281         for (int i = 0; i < NUM_BLANK_LINES; i++) {
 282             pre.addContent(NEW_LINE);
 283         }
 284     }
 285 
 286     /**
 287      * Given a <code>Doc</code>, return an anchor name for it.
 288      *
 289      * @param d the <code>Doc</code> to check.
 290      * @return the name of the anchor.
 291      */
 292     public static String getAnchorName(Doc d) {
 293         return "line." + d.position().line();
 294     }
 295 }
   1 /*
   2  * Copyright (c) 2001, 2016, 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.formats.html;
  27 
  28 import java.io.*;
  29 
  30 import javax.lang.model.element.Element;
  31 import javax.lang.model.element.PackageElement;
  32 import javax.lang.model.element.TypeElement;
  33 import javax.tools.FileObject;
  34 
  35 import jdk.javadoc.doclet.DocletEnvironment;
  36 import jdk.javadoc.internal.doclets.formats.html.markup.DocType;
  37 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlDocument;
  38 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  39 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
  40 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
  41 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
  42 import jdk.javadoc.internal.doclets.toolkit.Content;
  43 import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
  44 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
  45 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
  46 import jdk.javadoc.internal.doclets.toolkit.util.DocletAbortException;
  47 import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants;
  48 import jdk.javadoc.internal.doclets.toolkit.util.Utils;
  49 
  50 /**
  51  * Converts Java Source Code to HTML.
  52  *
  53  *  <p><b>This is NOT part of any supported API.
  54  *  If you write code that depends on this, you do so at your own risk.
  55  *  This code and its internal interfaces are subject to change or
  56  *  deletion without notice.</b>
  57  *
  58  * @author Jamie Ho
  59  * @author Bhavesh Patel (Modified)
  60  * @since 1.4
  61  */
  62 public class SourceToHTMLConverter {
  63 
  64     /**
  65      * The number of trailing blank lines at the end of the page.
  66      * This is inserted so that anchors at the bottom of small pages
  67      * can be reached.
  68      */
  69     private static final int NUM_BLANK_LINES = 60;
  70 
  71     /**
  72      * New line to be added to the documentation.
  73      */
  74     private static final String NEW_LINE = DocletConstants.NL;
  75 
  76     private final ConfigurationImpl configuration;
  77     private final Utils utils;
  78 
  79     private final DocletEnvironment rootDoc;
  80 
  81     private DocPath outputdir;
  82 
  83     /**
  84      * Relative path from the documentation root to the file that is being
  85      * generated.
  86      */
  87     private DocPath relativePath = DocPath.empty;
  88 
  89     private SourceToHTMLConverter(ConfigurationImpl configuration, DocletEnvironment rd,
  90             DocPath outputdir) {
  91         this.configuration  = configuration;
  92         this.utils = configuration.utils;
  93         this.rootDoc = rd;
  94         this.outputdir = outputdir;
  95     }
  96 
  97     /**
  98      * Translate the TypeElements in the given DocletEnvironment to HTML representation.
  99      *
 100      * @param configuration the configuration.
 101      * @param root the DocletEnvironment to convert.
 102      * @param outputdir the name of the directory to output to.
 103      */
 104     public static void convertRoot(ConfigurationImpl configuration, DocletEnvironment root,
 105             DocPath outputdir) {
 106         new SourceToHTMLConverter(configuration, root, outputdir).generate();
 107     }
 108 
 109     void generate() {
 110         if (rootDoc == null || outputdir == null) {
 111             return;
 112         }
 113         for (PackageElement pkg : utils.getSpecifiedPackages()) {
 114             // If -nodeprecated option is set and the package is marked as deprecated,
 115             // do not convert the package files to HTML.
 116             if (!(configuration.nodeprecated && utils.isDeprecated(pkg)))
 117                 convertPackage(pkg, outputdir);
 118         }
 119         for (TypeElement te : utils.getSpecifiedClasses()) {
 120             // If -nodeprecated option is set and the class is marked as deprecated
 121             // or the containing package is deprecated, do not convert the
 122             // package files to HTML.
 123             if (!(configuration.nodeprecated &&
 124                   (utils.isDeprecated(te) || utils.isDeprecated(utils.containingPackage(te)))))
 125                 convertClass(te, outputdir);
 126         }
 127     }
 128 
 129     /**
 130      * Convert the Classes in the given Package to an HTML.
 131      *
 132      * @param pkg the Package to convert.
 133      * @param outputdir the name of the directory to output to.
 134      */
 135     public void convertPackage(PackageElement pkg, DocPath outputdir) {
 136         if (pkg == null) {
 137             return;
 138         }
 139         for (Element te : utils.getAllClasses(pkg)) {
 140             // If -nodeprecated option is set and the class is marked as deprecated,
 141             // do not convert the package files to HTML. We do not check for
 142             // containing package deprecation since it is already check in
 143             // the calling method above.
 144             if (!(configuration.nodeprecated && utils.isDeprecated(te)))
 145                 convertClass((TypeElement)te, outputdir);
 146         }
 147     }
 148 
 149     /**
 150      * Convert the given Class to an HTML.
 151      *
 152      * @param te the class to convert.
 153      * @param outputdir the name of the directory to output to.
 154      */
 155     public void convertClass(TypeElement te, DocPath outputdir) {
 156         if (te == null) {
 157             return;
 158         }
 159         try {
 160             FileObject fo = utils.getFileObject(te);






 161             if (fo == null)
 162                 return;
 163             Reader r = fo.openReader(true);






 164             int lineno = 1;
 165             String line;
 166             relativePath = DocPaths.SOURCE_OUTPUT
 167                     .resolve(DocPath.forPackage(utils, te))
 168                     .invert();
 169             Content body = getHeader();
 170             Content pre = new HtmlTree(HtmlTag.PRE);
 171             try (LineNumberReader reader = new LineNumberReader(r)) {
 172                 while ((line = reader.readLine()) != null) {
 173                     addLineNo(pre, lineno);
 174                     addLine(pre, line, lineno);
 175                     lineno++;
 176                 }
 177             }
 178             addBlankLines(pre);
 179             Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre);
 180             body.addContent((configuration.allowTag(HtmlTag.MAIN)) ? HtmlTree.MAIN(div) : div);
 181             writeToFile(body, outputdir.resolve(DocPath.forClass(utils, te)));
 182         } catch (IOException e) {
 183             throw new DocletAbortException(e);
 184         }
 185     }
 186 
 187     /**
 188      * Write the output to the file.
 189      *
 190      * @param body the documentation content to be written to the file.
 191      * @param path the path for the file.
 192      */
 193     private void writeToFile(Content body, DocPath path) throws IOException {
 194         Content htmlDocType = configuration.isOutputHtml5()
 195                 ? DocType.HTML5
 196                 : DocType.TRANSITIONAL;
 197         Content head = new HtmlTree(HtmlTag.HEAD);
 198         head.addContent(HtmlTree.TITLE(new StringContent(
 199                 configuration.getText("doclet.Window_Source_title"))));
 200         head.addContent(getStyleSheetProperties());
 201         Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
 202                 head, body);
 203         Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree);


 250             span.addContent("00" + Integer.toString(lineno));
 251         } else if (lineno < 100) {
 252             span.addContent("0" + Integer.toString(lineno));
 253         } else {
 254             span.addContent(Integer.toString(lineno));
 255         }
 256         pre.addContent(span);
 257     }
 258 
 259     /**
 260      * Add a line from source to the HTML file that is generated.
 261      *
 262      * @param pre the content tree to which the line will be added.
 263      * @param line the string to format.
 264      * @param currentLineNo the current number.
 265      */
 266     private void addLine(Content pre, String line, int currentLineNo) {
 267         if (line != null) {
 268             Content anchor = HtmlTree.A(configuration.htmlVersion,
 269                     "line." + Integer.toString(currentLineNo),
 270                     new StringContent(utils.replaceTabs(line)));
 271             pre.addContent(anchor);
 272             pre.addContent(NEW_LINE);
 273         }
 274     }
 275 
 276     /**
 277      * Add trailing blank lines at the end of the page.
 278      *
 279      * @param pre the content tree to which the blank lines will be added.
 280      */
 281     private static void addBlankLines(Content pre) {
 282         for (int i = 0; i < NUM_BLANK_LINES; i++) {
 283             pre.addContent(NEW_LINE);
 284         }
 285     }
 286 
 287     /**
 288      * Given a <code>Doc</code>, return an anchor name for it.
 289      *
 290      * @param d the <code>Doc</code> to check.
 291      * @return the name of the anchor.
 292      */
 293     public static String getAnchorName(Utils utils, Element e) {
 294         return "line." + utils.getLineNumber(e);
 295     }
 296 }