001    /*
002     * Copyright 1998-2005 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.doclets.formats.html;
027    
028    import com.sun.tools.doclets.internal.toolkit.util.*;
029    
030    import java.io.*;
031    
032    /**
033     * Generate Separate Index Files for all the member names with Indexing in
034     * Unicode Order. This will create "index-files" directory in the current or
035     * destination directory and will generate separate file for each unicode index.
036     *
037     * @see java.lang.Character
038     * @author Atul M Dambalkar
039     */
040    public class SplitIndexWriter extends AbstractIndexWriter {
041    
042        /**
043         * Previous unicode character index in the built index.
044         */
045        protected int prev;
046    
047        /**
048         * Next unicode character in the built index.
049         */
050        protected int next;
051    
052        /**
053         * Construct the SplitIndexWriter. Uses path to this file and relative path
054         * from this file.
055         *
056         * @param path       Path to the file which is getting generated.
057         * @param filename   Name of the file which is getting genrated.
058         * @param relpath    Relative path from this file to the current directory.
059         * @param indexbuilder Unicode based Index from {@link IndexBuilder}
060         */
061        public SplitIndexWriter(ConfigurationImpl configuration,
062                                String path, String filename,
063                                String relpath, IndexBuilder indexbuilder,
064                                int prev, int next) throws IOException {
065            super(configuration, path, filename, relpath, indexbuilder);
066            this.prev = prev;
067            this.next = next;
068        }
069    
070        /**
071         * Generate separate index files, for each Unicode character, listing all
072         * the members starting with the particular unicode character.
073         *
074         * @param indexbuilder IndexBuilder built by {@link IndexBuilder}
075         * @throws DocletAbortException
076         */
077        public static void generate(ConfigurationImpl configuration,
078                                    IndexBuilder indexbuilder) {
079            SplitIndexWriter indexgen;
080            String filename = "";
081            String path = DirectoryManager.getPath("index-files");
082            String relpath = DirectoryManager.getRelativePath("index-files");
083            try {
084                for (int i = 0; i < indexbuilder.elements().length; i++) {
085                    int j = i + 1;
086                    int prev = (j == 1)? -1: i;
087                    int next = (j == indexbuilder.elements().length)? -1: j + 1;
088                    filename = "index-" + j +".html";
089                    indexgen = new SplitIndexWriter(configuration,
090                                                    path, filename, relpath,
091                                                    indexbuilder, prev, next);
092                    indexgen.generateIndexFile((Character)indexbuilder.
093                                                                     elements()[i]);
094                    indexgen.close();
095                }
096            } catch (IOException exc) {
097                configuration.standardmessage.error(
098                            "doclet.exception_encountered",
099                            exc.toString(), filename);
100                throw new DocletAbortException();
101            }
102        }
103    
104        /**
105         * Generate the contents of each index file, with Header, Footer,
106         * Member Field, Method and Constructor Description.
107         *
108         * @param unicode Unicode character referring to the character for the
109         * index.
110         */
111        protected void generateIndexFile(Character unicode) throws IOException {
112            printHtmlHeader(configuration.getText("doclet.Window_Split_Index",
113                unicode.toString()), null, true);
114            printTop();
115            navLinks(true);
116            printLinksForIndexes();
117    
118            hr();
119    
120            generateContents(unicode, indexbuilder.getMemberList(unicode));
121    
122            navLinks(false);
123            printLinksForIndexes();
124    
125            printBottom();
126            printBodyHtmlEnd();
127        }
128    
129        /**
130         * Print Links for all the Index Files per unicode character.
131         */
132        protected void printLinksForIndexes() {
133            for (int i = 0; i < indexbuilder.elements().length; i++) {
134                int j = i + 1;
135                printHyperLink("index-" + j + ".html",
136                               indexbuilder.elements()[i].toString());
137                print(' ');
138            }
139        }
140    
141        /**
142         * Print the previous unicode character index link.
143         */
144        protected void navLinkPrevious() {
145            if (prev == -1) {
146                printText("doclet.Prev_Letter");
147            } else {
148                printHyperLink("index-" + prev + ".html", "",
149                    configuration.getText("doclet.Prev_Letter"), true);
150            }
151        }
152    
153        /**
154         * Print the next unicode character index link.
155         */
156        protected void navLinkNext() {
157            if (next == -1) {
158                printText("doclet.Next_Letter");
159            } else {
160                printHyperLink("index-" + next + ".html","",
161                    configuration.getText("doclet.Next_Letter"), true);
162            }
163        }
164    }