1/*
2 * Copyright (c) 1998, 2017, 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
26package jdk.javadoc.internal.doclets.formats.html;
27
28import java.util.*;
29
30import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
31import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
32import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
33import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
34import jdk.javadoc.internal.doclets.toolkit.Content;
35import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
36import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
37import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
38import jdk.javadoc.internal.doclets.toolkit.util.IndexBuilder;
39
40
41/**
42 * Generate only one index file for all the Member Names with Indexing in
43 * Unicode Order. The name of the generated file is "index-all.html" and it is
44 * generated in current or the destination directory.
45 *
46 *  <p><b>This is NOT part of any supported API.
47 *  If you write code that depends on this, you do so at your own risk.
48 *  This code and its internal interfaces are subject to change or
49 *  deletion without notice.</b>
50 *
51 * @see java.lang.Character
52 * @author Atul M Dambalkar
53 * @author Bhavesh Patel (Modified)
54 */
55public class SingleIndexWriter extends AbstractIndexWriter {
56
57    private Set<Character> elements;
58
59    /**
60     * Construct the SingleIndexWriter with filename "index-all.html" and the
61     * {@link IndexBuilder}
62     *
63     * @param configuration the configuration for this doclet
64     * @param filename     Name of the index file to be generated.
65     * @param indexbuilder Unicode based Index from {@link IndexBuilder}
66     */
67    public SingleIndexWriter(HtmlConfiguration configuration,
68                             DocPath filename,
69                             IndexBuilder indexbuilder) {
70        super(configuration, filename, indexbuilder);
71    }
72
73    /**
74     * Generate single index file, for all Unicode characters.
75     *
76     * @param configuration the configuration for this doclet
77     * @param indexbuilder IndexBuilder built by {@link IndexBuilder}
78     * @throws DocFileIOException if there is a problem generating the index
79     */
80    public static void generate(HtmlConfiguration configuration,
81                                IndexBuilder indexbuilder) throws DocFileIOException {
82        DocPath filename = DocPaths.INDEX_ALL;
83        SingleIndexWriter indexgen = new SingleIndexWriter(configuration,
84                                         filename, indexbuilder);
85        indexgen.generateIndexFile();
86    }
87
88    /**
89     * Generate the contents of each index file, with Header, Footer,
90     * Member Field, Method and Constructor Description.
91     * @throws DocFileIOException if there is a problem generating the index
92     */
93    protected void generateIndexFile() throws DocFileIOException {
94        String title = configuration.getText("doclet.Window_Single_Index");
95        HtmlTree body = getBody(true, getWindowTitle(title));
96        HtmlTree htmlTree = (configuration.allowTag(HtmlTag.HEADER))
97                ? HtmlTree.HEADER()
98                : body;
99        addTop(htmlTree);
100        addNavLinks(true, htmlTree);
101        if (configuration.allowTag(HtmlTag.HEADER)) {
102            body.addContent(htmlTree);
103        }
104        HtmlTree divTree = new HtmlTree(HtmlTag.DIV);
105        divTree.addStyle(HtmlStyle.contentContainer);
106        elements = new TreeSet<>(indexbuilder.getIndexMap().keySet());
107        elements.addAll(configuration.tagSearchIndexKeys);
108        addLinksForIndexes(divTree);
109        for (Character unicode : elements) {
110            if (configuration.tagSearchIndexMap.get(unicode) == null) {
111                addContents(unicode, indexbuilder.getMemberList(unicode), divTree);
112            } else if (indexbuilder.getMemberList(unicode) == null) {
113                addSearchContents(unicode, configuration.tagSearchIndexMap.get(unicode), divTree);
114            } else {
115                addContents(unicode, indexbuilder.getMemberList(unicode),
116                        configuration.tagSearchIndexMap.get(unicode), divTree);
117            }
118        }
119        addLinksForIndexes(divTree);
120        body.addContent((configuration.allowTag(HtmlTag.MAIN))
121                ? HtmlTree.MAIN(divTree)
122                : divTree);
123        if (configuration.allowTag(HtmlTag.FOOTER)) {
124            htmlTree = HtmlTree.FOOTER();
125        }
126        addNavLinks(false, htmlTree);
127        addBottom(htmlTree);
128        if (configuration.allowTag(HtmlTag.FOOTER)) {
129            body.addContent(htmlTree);
130        }
131        createSearchIndexFiles();
132        printHtmlDocument(null, true, body);
133    }
134
135    /**
136     * Add links for all the Index Files per unicode character.
137     *
138     * @param contentTree the content tree to which the links for indexes will be added
139     */
140    protected void addLinksForIndexes(Content contentTree) {
141        for (Object ch : elements) {
142            String unicode = ch.toString();
143            contentTree.addContent(
144                    getHyperLink(getNameForIndex(unicode),
145                            new StringContent(unicode)));
146            contentTree.addContent(Contents.SPACE);
147        }
148    }
149}
150