SplitIndexWriter.java revision 3233:b5d08bc0d224
1/*
2 * Copyright (c) 1998, 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
26package jdk.javadoc.internal.doclets.formats.html;
27
28import java.io.*;
29import java.util.ArrayList;
30import java.util.Collection;
31import java.util.List;
32import java.util.ListIterator;
33import java.util.Set;
34import java.util.TreeSet;
35import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
36import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
37import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
38import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
39import jdk.javadoc.internal.doclets.toolkit.Content;
40import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
41import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
42import jdk.javadoc.internal.doclets.toolkit.util.DocletAbortException;
43import jdk.javadoc.internal.doclets.toolkit.util.IndexBuilder;
44
45
46/**
47 * Generate Separate Index Files for all the member names with Indexing in
48 * Unicode Order. This will create "index-files" directory in the current or
49 * destination directory and will generate separate file for each unicode index.
50 *
51 *  <p><b>This is NOT part of any supported API.
52 *  If you write code that depends on this, you do so at your own risk.
53 *  This code and its internal interfaces are subject to change or
54 *  deletion without notice.</b>
55 *
56 * @see java.lang.Character
57 * @author Atul M Dambalkar
58 * @author Bhavesh Patel (Modified)
59 */
60public class SplitIndexWriter extends AbstractIndexWriter {
61
62    /**
63     * Previous unicode character index in the built index.
64     */
65    protected int prev;
66
67    /**
68     * Next unicode character in the built index.
69     */
70    protected int next;
71
72    private List<Character> indexElements;
73
74    /**
75     * Construct the SplitIndexWriter. Uses path to this file and relative path
76     * from this file.
77     *
78     * @param path       Path to the file which is getting generated.
79     * @param indexbuilder Unicode based Index from {@link IndexBuilder}
80     */
81    public SplitIndexWriter(ConfigurationImpl configuration,
82                            DocPath path,
83                            IndexBuilder indexbuilder,
84                            Collection<Character> elements,
85                            int prev, int next) throws IOException {
86        super(configuration, path, indexbuilder);
87        this.indexElements = new ArrayList<>(elements);
88        this.prev = prev;
89        this.next = next;
90    }
91
92    /**
93     * Generate separate index files, for each Unicode character, listing all
94     * the members starting with the particular unicode character.
95     *
96     * @param indexbuilder IndexBuilder built by {@link IndexBuilder}
97     * @throws DocletAbortException
98     */
99    public static void generate(ConfigurationImpl configuration,
100                                IndexBuilder indexbuilder) {
101        SplitIndexWriter indexgen;
102        DocPath filename = DocPath.empty;
103        DocPath path = DocPaths.INDEX_FILES;
104        try {
105            Set<Character> keys = new TreeSet<>(indexbuilder.getIndexMap().keySet());
106            keys.addAll(configuration.tagSearchIndexKeys);
107            ListIterator<Character> li = new ArrayList<>(keys).listIterator();
108            while (li.hasNext()) {
109                Object ch = li.next();
110                filename = DocPaths.indexN(li.nextIndex());
111                indexgen = new SplitIndexWriter(configuration,
112                        path.resolve(filename),
113                        indexbuilder, keys, li.previousIndex(), li.nextIndex());
114                indexgen.generateIndexFile((Character) ch);
115                if (!li.hasNext()) {
116                    indexgen.createSearchIndexFiles();
117                }
118                indexgen.close();
119            }
120        } catch (IOException exc) {
121            configuration.standardmessage.error(
122                        "doclet.exception_encountered",
123                        exc.toString(), filename.getPath());
124            throw new DocletAbortException(exc);
125        }
126    }
127
128    /**
129     * Generate the contents of each index file, with Header, Footer,
130     * Member Field, Method and Constructor Description.
131     *
132     * @param unicode Unicode character referring to the character for the
133     * index.
134     */
135    protected void generateIndexFile(Character unicode) throws IOException {
136        String title = configuration.getText("doclet.Window_Split_Index",
137                unicode.toString());
138        HtmlTree body = getBody(true, getWindowTitle(title));
139        HtmlTree htmlTree = (configuration.allowTag(HtmlTag.HEADER))
140                ? HtmlTree.HEADER()
141                : body;
142        addTop(htmlTree);
143        addNavLinks(true, htmlTree);
144        if (configuration.allowTag(HtmlTag.HEADER)) {
145            body.addContent(htmlTree);
146        }
147        HtmlTree divTree = new HtmlTree(HtmlTag.DIV);
148        divTree.addStyle(HtmlStyle.contentContainer);
149        addLinksForIndexes(divTree);
150        if (configuration.tagSearchIndexMap.get(unicode) == null) {
151            addContents(unicode, indexbuilder.getMemberList(unicode), divTree);
152        } else if (indexbuilder.getMemberList(unicode) == null) {
153            addSearchContents(unicode, configuration.tagSearchIndexMap.get(unicode), divTree);
154        } else {
155            addContents(unicode, indexbuilder.getMemberList(unicode),
156                    configuration.tagSearchIndexMap.get(unicode), divTree);
157        }
158        addLinksForIndexes(divTree);
159        body.addContent((configuration.allowTag(HtmlTag.MAIN)) ? HtmlTree.MAIN(divTree) : divTree);
160        if (configuration.allowTag(HtmlTag.FOOTER)) {
161            htmlTree = HtmlTree.FOOTER();
162        }
163        addNavLinks(false, htmlTree);
164        addBottom(htmlTree);
165        if (configuration.allowTag(HtmlTag.FOOTER)) {
166            body.addContent(htmlTree);
167        }
168        printHtmlDocument(null, true, body);
169    }
170
171    /**
172     * Add links for all the Index Files per unicode character.
173     *
174     * @param contentTree the content tree to which the links for indexes will be added
175     */
176    protected void addLinksForIndexes(Content contentTree) {
177        for (int i = 0; i < indexElements.size(); i++) {
178            int j = i + 1;
179            contentTree.addContent(getHyperLink(DocPaths.indexN(j),
180                    new StringContent(indexElements.get(i).toString())));
181            contentTree.addContent(getSpace());
182        }
183    }
184
185    /**
186     * Get link to the previous unicode character.
187     *
188     * @return a content tree for the link
189     */
190    public Content getNavLinkPrevious() {
191        Content prevletterLabel = getResource("doclet.Prev_Letter");
192        if (prev == -1) {
193            return HtmlTree.LI(prevletterLabel);
194        }
195        else {
196            Content prevLink = getHyperLink(DocPaths.indexN(prev),
197                    prevletterLabel);
198            return HtmlTree.LI(prevLink);
199        }
200    }
201
202    /**
203     * Get link to the next unicode character.
204     *
205     * @return a content tree for the link
206     */
207    public Content getNavLinkNext() {
208        Content nextletterLabel = getResource("doclet.Next_Letter");
209        if (next == -1) {
210            return HtmlTree.LI(nextletterLabel);
211        }
212        else {
213            Content nextLink = getHyperLink(DocPaths.indexN(next),
214                    nextletterLabel);
215            return HtmlTree.LI(nextLink);
216        }
217    }
218}
219