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