langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/AbstractIndexWriter.java
changeset 40587 1c355ea550ed
parent 40511 1b3c502e0bdc
child 40606 eb2c81860c86
equal deleted inserted replaced
40519:e17429a7e843 40587:1c355ea550ed
    24  */
    24  */
    25 
    25 
    26 package jdk.javadoc.internal.doclets.formats.html;
    26 package jdk.javadoc.internal.doclets.formats.html;
    27 
    27 
    28 import java.io.*;
    28 import java.io.*;
    29 import java.nio.file.*;
       
    30 import java.util.*;
    29 import java.util.*;
    31 import java.util.zip.*;
    30 import java.util.zip.*;
    32 
    31 
    33 import javax.lang.model.element.Element;
    32 import javax.lang.model.element.Element;
    34 import javax.lang.model.element.ExecutableElement;
    33 import javax.lang.model.element.ExecutableElement;
    44 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
    43 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
    45 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
    44 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
    46 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
    45 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
    47 import jdk.javadoc.internal.doclets.toolkit.Content;
    46 import jdk.javadoc.internal.doclets.toolkit.Content;
    48 import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
    47 import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
       
    48 import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
    49 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
    49 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
    50 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
    50 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
    51 import jdk.javadoc.internal.doclets.toolkit.util.DocletAbortException;
       
    52 import jdk.javadoc.internal.doclets.toolkit.util.IndexBuilder;
    51 import jdk.javadoc.internal.doclets.toolkit.util.IndexBuilder;
    53 
    52 
    54 /**
    53 /**
    55  * Generate Index for all the Member Names with Indexing in
    54  * Generate Index for all the Member Names with Indexing in
    56  * Unicode Order. This class is a base class for {@link SingleIndexWriter} and
    55  * Unicode Order. This class is a base class for {@link SingleIndexWriter} and
    91     /**
    90     /**
    92      * Get the index label for navigation bar.
    91      * Get the index label for navigation bar.
    93      *
    92      *
    94      * @return a content tree for the tree label
    93      * @return a content tree for the tree label
    95      */
    94      */
       
    95     @Override
    96     protected Content getNavLinkIndex() {
    96     protected Content getNavLinkIndex() {
    97         Content li = HtmlTree.LI(HtmlStyle.navBarCell1Rev, contents.indexLabel);
    97         Content li = HtmlTree.LI(HtmlStyle.navBarCell1Rev, contents.indexLabel);
    98         return li;
    98         return li;
    99     }
    99     }
   100 
   100 
   240     /**
   240     /**
   241      * Add one line summary comment for the package.
   241      * Add one line summary comment for the package.
   242      *
   242      *
   243      * @param pkg the package to be documented
   243      * @param pkg the package to be documented
   244      * @param dlTree the content tree to which the description will be added
   244      * @param dlTree the content tree to which the description will be added
       
   245      * @param si the search index item to be updated
   245      */
   246      */
   246     protected void addDescription(PackageElement pkg, Content dlTree, SearchIndexItem si) {
   247     protected void addDescription(PackageElement pkg, Content dlTree, SearchIndexItem si) {
   247         Content link = getPackageLink(pkg, new StringContent(utils.getPackageName(pkg)));
   248         Content link = getPackageLink(pkg, new StringContent(utils.getPackageName(pkg)));
   248         if (configuration.showModules) {
   249         if (configuration.showModules) {
   249             si.setContainingModule(utils.getSimpleName(utils.containingModule(pkg)));
   250             si.setContainingModule(utils.getSimpleName(utils.containingModule(pkg)));
   263     /**
   264     /**
   264      * Add one line summary comment for the class.
   265      * Add one line summary comment for the class.
   265      *
   266      *
   266      * @param typeElement the class being documented
   267      * @param typeElement the class being documented
   267      * @param dlTree the content tree to which the description will be added
   268      * @param dlTree the content tree to which the description will be added
       
   269      * @param si the search index item to be updated
   268      */
   270      */
   269     protected void addDescription(TypeElement typeElement, Content dlTree, SearchIndexItem si) {
   271     protected void addDescription(TypeElement typeElement, Content dlTree, SearchIndexItem si) {
   270         Content link = getLink(new LinkInfoImpl(configuration,
   272         Content link = getLink(new LinkInfoImpl(configuration,
   271                         LinkInfoImpl.Kind.INDEX, typeElement).strong(true));
   273                         LinkInfoImpl.Kind.INDEX, typeElement).strong(true));
   272         si.setContainingPackage(utils.getPackageName(utils.containingPackage(typeElement)));
   274         si.setContainingPackage(utils.getPackageName(utils.containingPackage(typeElement)));
   330         addComment(member, dd);
   332         addComment(member, dd);
   331         dlTree.addContent(dd);
   333         dlTree.addContent(dd);
   332     }
   334     }
   333 
   335 
   334     protected void addDescription(SearchIndexItem sii, Content dlTree) {
   336     protected void addDescription(SearchIndexItem sii, Content dlTree) {
   335         String path = pathToRoot.isEmpty() ? "" : pathToRoot.getPath() + "/";
   337         String siiPath = pathToRoot.isEmpty() ? "" : pathToRoot.getPath() + "/";
   336         path += sii.getUrl();
   338         siiPath += sii.getUrl();
   337         HtmlTree labelLink = HtmlTree.A(path, new StringContent(sii.getLabel()));
   339         HtmlTree labelLink = HtmlTree.A(siiPath, new StringContent(sii.getLabel()));
   338         Content dt = HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.searchTagLink, labelLink));
   340         Content dt = HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.searchTagLink, labelLink));
   339         dt.addContent(" - ");
   341         dt.addContent(" - ");
   340         dt.addContent(contents.getContent("doclet.Search_tag_in", sii.getHolder()));
   342         dt.addContent(contents.getContent("doclet.Search_tag_in", sii.getHolder()));
   341         dlTree.addContent(dt);
   343         dlTree.addContent(dt);
   342         Content dd = new HtmlTree(HtmlTag.DD);
   344         Content dd = new HtmlTree(HtmlTag.DD);
   428      */
   430      */
   429     public String getNameForIndex(String unicode) {
   431     public String getNameForIndex(String unicode) {
   430         return "I:" + getName(unicode);
   432         return "I:" + getName(unicode);
   431     }
   433     }
   432 
   434 
   433     protected void createSearchIndexFiles() {
   435     /**
       
   436      * @throws DocFileIOException if there is a problem creating any of the search index files
       
   437      */
       
   438     protected void createSearchIndexFiles() throws DocFileIOException {
   434         if (configuration.showModules) {
   439         if (configuration.showModules) {
   435             createSearchIndexFile(DocPaths.MODULE_SEARCH_INDEX_JSON, DocPaths.MODULE_SEARCH_INDEX_ZIP,
   440             createSearchIndexFile(DocPaths.MODULE_SEARCH_INDEX_JSON, DocPaths.MODULE_SEARCH_INDEX_ZIP,
   436                     configuration.moduleSearchIndex);
   441                     configuration.moduleSearchIndex);
   437         }
   442         }
   438         createSearchIndexFile(DocPaths.PACKAGE_SEARCH_INDEX_JSON, DocPaths.PACKAGE_SEARCH_INDEX_ZIP,
   443         createSearchIndexFile(DocPaths.PACKAGE_SEARCH_INDEX_JSON, DocPaths.PACKAGE_SEARCH_INDEX_ZIP,
   443                 configuration.memberSearchIndex);
   448                 configuration.memberSearchIndex);
   444         createSearchIndexFile(DocPaths.TAG_SEARCH_INDEX_JSON, DocPaths.TAG_SEARCH_INDEX_ZIP,
   449         createSearchIndexFile(DocPaths.TAG_SEARCH_INDEX_JSON, DocPaths.TAG_SEARCH_INDEX_ZIP,
   445                 configuration.tagSearchIndex);
   450                 configuration.tagSearchIndex);
   446     }
   451     }
   447 
   452 
       
   453     /**
       
   454      * @throws DocFileIOException if there is a problem creating the search index file
       
   455      */
   448     protected void createSearchIndexFile(DocPath searchIndexFile, DocPath searchIndexZip,
   456     protected void createSearchIndexFile(DocPath searchIndexFile, DocPath searchIndexZip,
   449             List<SearchIndexItem> searchIndex) {
   457             List<SearchIndexItem> searchIndex) throws DocFileIOException {
   450         if (!searchIndex.isEmpty()) {
   458         if (!searchIndex.isEmpty()) {
   451             try {
   459             StringBuilder searchVar = new StringBuilder("[");
   452                 StringBuilder searchVar = new StringBuilder("[");
   460             boolean first = true;
   453                 boolean first = true;
   461             for (SearchIndexItem item : searchIndex) {
   454                 DocFile searchFile = DocFile.createFileForOutput(configuration, searchIndexFile);
   462                 if (first) {
   455                 Path p = Paths.get(searchFile.getPath());
   463                     searchVar.append(item.toString());
   456                 for (SearchIndexItem item : searchIndex) {
   464                     first = false;
   457                     if (first) {
   465                 } else {
   458                         searchVar.append(item.toString());
   466                     searchVar.append(",").append(item.toString());
   459                         first = false;
       
   460                     } else {
       
   461                         searchVar.append(",").append(item.toString());
       
   462                     }
       
   463                 }
   467                 }
   464                 searchVar.append("]");
   468             }
   465                 Files.write(p, searchVar.toString().getBytes());
   469             searchVar.append("]");
   466                 DocFile zipFile = DocFile.createFileForOutput(configuration, searchIndexZip);
   470 
   467                 try (FileOutputStream fos = new FileOutputStream(zipFile.getPath());
   471             DocFile zipFile = DocFile.createFileForOutput(configuration, searchIndexZip);
   468                         ZipOutputStream zos = new ZipOutputStream(fos)) {
   472             try (OutputStream fos = zipFile.openOutputStream();
   469                     zipFile(searchFile.getPath(), searchIndexFile, zos);
   473                     ZipOutputStream zos = new ZipOutputStream(fos)) {
       
   474                 try {
       
   475                     ZipEntry ze = new ZipEntry(searchIndexFile.getPath());
       
   476                     zos.putNextEntry(ze);
       
   477                     zos.write(searchVar.toString().getBytes());
       
   478                 } finally {
       
   479                     zos.closeEntry();
   470                 }
   480                 }
   471                 Files.delete(p);
       
   472             } catch (IOException ie) {
   481             } catch (IOException ie) {
   473                 throw new DocletAbortException(ie);
   482                 throw new DocFileIOException(zipFile, DocFileIOException.Mode.WRITE, ie);
   474             }
   483             }
   475         }
       
   476     }
       
   477 
       
   478     protected void zipFile(String inputFile, DocPath file, ZipOutputStream zos) {
       
   479         try {
       
   480             try {
       
   481                 ZipEntry ze = new ZipEntry(file.getPath());
       
   482                 zos.putNextEntry(ze);
       
   483                 try (FileInputStream fis = new FileInputStream(new File(inputFile))) {
       
   484                     byte[] buf = new byte[2048];
       
   485                     int len = fis.read(buf);
       
   486                     while (len > 0) {
       
   487                         zos.write(buf, 0, len);
       
   488                         len = fis.read(buf);
       
   489                     }
       
   490                 }
       
   491             } finally {
       
   492                 zos.closeEntry();
       
   493             }
       
   494         } catch (IOException e) {
       
   495             throw new DocletAbortException(e);
       
   496         }
   484         }
   497     }
   485     }
   498 }
   486 }