KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > toc > TocBuilder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.toc;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.help.internal.HelpPlugin;
19
20 public class TocBuilder {
21
22     // list of all toc files
23
protected Collection JavaDoc contributedTocFiles;
24
25     // list of unprocessed toc files
26
protected Collection JavaDoc unprocessedTocFiles;
27
28     // list of unprocessed toc (the target of attach_to was not available at the
29
// time)
30
protected List JavaDoc unprocessedTocs;
31
32     /**
33      * Constructor.
34      */

35     public TocBuilder() {
36         unprocessedTocFiles = new ArrayList JavaDoc();
37         unprocessedTocs = new ArrayList JavaDoc();
38     }
39
40     public List JavaDoc getBuiltTocs() {
41         // returns the list of root Toc trees
42
List JavaDoc tocCol = new ArrayList JavaDoc(contributedTocFiles.size());
43         for (Iterator JavaDoc it = contributedTocFiles.iterator(); it.hasNext();) {
44             TocFile tocFile = (TocFile) it.next();
45             Toc toc = tocFile.getToc();
46             if (toc != null && toc.getTocFile().isPrimary() && !isIntegrated(toc))
47                 tocCol.add((toc));
48         }
49         return tocCol;
50     }
51
52     /**
53      */

54     public void build(Collection JavaDoc contributedTocFiles) {
55         this.contributedTocFiles = contributedTocFiles;
56         unprocessedTocFiles.addAll(contributedTocFiles);
57         // process all the toc files.
58
// A side-effect is that linked files are also processed
59
while (!unprocessedTocFiles.isEmpty()) {
60             TocFile tocFile = (TocFile) unprocessedTocFiles.iterator().next();
61             tocFile.build(this);
62         }
63         // At the end, unprocessedTocs may contain TOCs that need be build.
64
// All these toc could not be attached because the
65
// target node was not parsed at that time
66

67         // try processing as many toc (link_to) as possible now
68
int remaining = unprocessedTocs.size();
69         for (int i = 0; i < remaining; i++) {
70             Toc toc = (Toc) unprocessedTocs.get(i);
71             buildToc(toc);
72         }
73     }
74
75     public void buildTocFile(TocFile tocFile) {
76         try {
77             unprocessedTocFiles.remove(tocFile);
78             TocFileParser parser = new TocFileParser(this);
79             parser.parse(tocFile);
80         } catch (Exception JavaDoc e) {
81             String JavaDoc msg = "Error occurred processing file " + tocFile.getHref() + "."; //$NON-NLS-1$ //$NON-NLS-2$
82
HelpPlugin.logError(msg, e);
83         }
84     }
85
86     public void buildAnchor(Anchor anchor) {
87         // cache the anchor in the toc file
88
anchor.getTocFile().addAnchor(anchor);
89     }
90
91     public void buildLink(Link link) {
92         // parse the linked file
93
String JavaDoc linkedToc = link.getToc();
94         TocFile includedTocFile = getTocFile(linkedToc);
95         if (includedTocFile == null)
96             return;
97         Toc toc = includedTocFile.getToc();
98         if (toc == null)
99             return;
100         // link the two Toc objects
101
link.addChild(toc);
102     }
103
104     public void buildTopic(Topic topic) {
105         // nothing to do
106
}
107
108     public void buildToc(Toc toc) {
109         // link toc if so specified
110
String JavaDoc href = toc.getLink_to();
111         if (href == null || href.equals("")) //$NON-NLS-1$
112
return;
113         TocFile targetTocFile = getTocFile(href);
114         if (targetTocFile == null)
115             return;
116         Anchor anchor = targetTocFile.getAnchor(href);
117         if (anchor == null) {
118             unprocessedTocs.add(toc);
119             return;
120         }
121         // link the two toc objects
122
anchor.addChild(toc);
123     }
124
125     private TocFile getTocFile(String JavaDoc href) {
126         String JavaDoc plugin = HrefUtil.getPluginIDFromHref(href);
127         if (plugin == null)
128             return null;
129         String JavaDoc path = HrefUtil.getResourcePathFromHref(href);
130         if (path == null)
131             return null;
132         TocFile tocFile = null;
133         for (Iterator JavaDoc it = contributedTocFiles.iterator(); it.hasNext();) {
134             tocFile = (TocFile) it.next();
135             if (tocFile.getPluginID().equals(plugin) && tocFile.getHref().equals(path))
136                 break;
137             tocFile = null;
138         }
139         if (tocFile == null)
140             return null;
141         if (unprocessedTocFiles.contains(tocFile))
142             buildTocFile(tocFile);
143         return tocFile;
144     }
145
146     /**
147      * Checks if navigation element has been integrated into another TOC.
148      */

149     private boolean isIntegrated(TocNode element) {
150         // check if there if there is TOC in ancestor hierarchy (depth first)
151
for (Iterator JavaDoc it = element.getParents().iterator(); it.hasNext();) {
152             TocNode parent = (TocNode) it.next();
153             if (parent instanceof Toc && ((Toc) parent).getTocFile().isPrimary()) {
154                 return true;
155             } else if (isIntegrated(parent)) {
156                 return true;
157             }
158         }
159         // no ancestor is a TOC
160
return false;
161     }
162 }
163
Popular Tags