KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > index > IndexBuilder


1 /*******************************************************************************
2  * Copyright (c) 2005 Intel Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Intel Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.index;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.Stack JavaDoc;
17
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.help.IIndex;
20 import org.eclipse.help.ITopic;
21 import org.eclipse.help.internal.HelpPlugin;
22 import org.eclipse.help.internal.model.ITocElement;
23
24
25 /**
26  * @author sturmash
27  *
28  * To change the template for this generated type comment go to
29  * Window - Preferences - Java - Code Generation - Code and Comments
30  */

31 public class IndexBuilder {
32     
33     private Collection JavaDoc contributedIndexFiles;
34     private Collection JavaDoc unprocessedIndexFiles;
35     private Index index;
36     private IndexEntry current;
37     private Stack JavaDoc entries;
38     private ITocElement[] tocs;
39     
40     /**
41      * Constructs the index builder
42      */

43     public IndexBuilder(Comparator JavaDoc comparator) {
44         unprocessedIndexFiles = new ArrayList JavaDoc();
45         index = new Index(comparator);
46         entries = new Stack JavaDoc();
47         tocs = HelpPlugin.getTocManager().getTocs(Platform.getNL());
48     }
49     
50     public void build(Collection JavaDoc contributedIndexFiles) {
51         this.contributedIndexFiles = contributedIndexFiles;
52         unprocessedIndexFiles.addAll(this.contributedIndexFiles);
53         while (!unprocessedIndexFiles.isEmpty()) {
54             IndexFile indexFile = (IndexFile) unprocessedIndexFiles.iterator().next();
55             indexFile.build(this);
56         }
57     }
58
59     /**
60      * @param file
61      */

62     public void buildIndexFile(IndexFile file) {
63         unprocessedIndexFiles.remove(file);
64         IndexFileParser parser = new IndexFileParser(this);
65         parser.parse(file);
66     }
67     /**
68      * Adds a new entry to the index
69      * @param keyword
70      * @param hrefs
71      */

72     protected void addIndexEntry(String JavaDoc keyword) {
73         Index currIndex = current == null ? index : current;
74         IndexEntry newEntry = currIndex.addEntry(keyword);
75         if(current != null) entries.push(current);
76         current = newEntry;
77     }
78
79     protected void exitIndexEntry() {
80         if(entries.empty())
81             current = null;
82         else
83             current = (IndexEntry)entries.pop();
84     }
85
86     protected void addTopic(String JavaDoc label, String JavaDoc href, String JavaDoc location) {
87         boolean emptyLabel = label == null || label.length() == 0;
88         boolean emptyLocation = location == null || location.length() == 0;
89         
90         if ( emptyLabel || emptyLocation ) {
91             for (int i = 0; i < tocs.length; i++) {
92                 ITopic topic = tocs[i].getTopic(href);
93                 if (topic != null) {
94                     if(emptyLabel) {
95                         label = topic.getLabel();
96                         emptyLabel = false;
97                     }
98                     if(emptyLocation) {
99                         location = tocs[i].getLabel();
100                         emptyLocation = false;
101                     }
102                 }
103             }
104         }
105
106         if(emptyLocation) location = ""; //$NON-NLS-1$
107
if(emptyLabel) label = ""; //$NON-NLS-1$
108
if (current != null) {
109             current.addTopic(label,href,location);
110         }
111     }
112
113 // /**
114
// * @param string
115
// * @return
116
// */
117
// private ITopic findTopicByHref(String href) {
118
// for (int i = 0; i < tocs.length; i++) {
119
// ITopic topic = tocs[i].getTopic(href);
120
// if ((topic != null) && (topic.getHref().equals(href)))
121
// return topic;
122
// else
123
// continue;
124
// }
125
// return null;
126
// }
127

128     /**
129      * @return
130      */

131     protected IIndex getBuiltIndex() {
132         return index;
133     }
134
135 }
136
Popular Tags