KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > webapp > data > TocData


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.webapp.data;
12
13 import java.io.IOException JavaDoc;
14 import java.io.Writer JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.eclipse.help.IToc;
27 import org.eclipse.help.ITopic;
28 import org.eclipse.help.UAContentFilter;
29 import org.eclipse.help.internal.HelpPlugin;
30 import org.eclipse.help.internal.base.HelpBasePlugin;
31 import org.eclipse.help.internal.base.HelpEvaluationContext;
32 import org.eclipse.help.internal.base.remote.RemoteHelp;
33
34 /**
35  * Helper class for tocView.jsp initialization
36  */

37 public class TocData extends ActivitiesData {
38     // maximum number of topics in a book for generating all topics at once
39
private static int loadBookAtOnceLimit;
40     // suggested number of topic levels for large books
41
private static int dynamicLoadDepths;
42     // maximum number of topics generated when loading levels dynamically
43
// above which dynamicLoadDepths is ignored, the rest of branches will be 1
44
// deep
45
private static int honorLevelsLimit;
46
47     // Request parameters
48
private String JavaDoc tocParameter;
49     private String JavaDoc topicHref;
50
51     // help form of selected topic href
52
private String JavaDoc topicHelpHref;
53     // Selected TOC
54
private int selectedToc;
55     // path from TOC to the root topic of the TOC fragment
56
private int[] rootPath = null;
57     // path from TOC to the selected topic, excluding TOC;
58
private ITopic[] topicPath = null;
59     // Number of topics generated so far
60
private int topicsGenerated = 0;
61
62     // List of TOC's, unfiltered
63
private IToc[] tocs;
64     // List of TOC's, filtered by roles
65
//private IToc[] filteredTocs;
66

67     // images directory
68
private String JavaDoc imagesDirectory;
69
70     /**
71      * Constructs the xml data for the contents page.
72      *
73      * @param context
74      * @param request
75      */

76     public TocData(ServletContext JavaDoc context, HttpServletRequest JavaDoc request,
77             HttpServletResponse JavaDoc response) {
78         super(context, request, response);
79         if (dynamicLoadDepths < 1) {
80             WebappPreferences pref = new WebappPreferences();
81             loadBookAtOnceLimit = pref.getBookAtOnceLimit();
82             dynamicLoadDepths = pref.getLoadDepth();
83             honorLevelsLimit = loadBookAtOnceLimit / 4;
84         }
85
86         this.tocParameter = request.getParameter("toc"); //$NON-NLS-1$
87
this.topicHref = request.getParameter("topic"); //$NON-NLS-1$
88
if (tocParameter != null && tocParameter.length() == 0)
89             tocParameter = null;
90         if (topicHref != null && topicHref.length() == 0)
91             topicHref = null;
92         
93         String JavaDoc anchor = request.getParameter("anchor"); //$NON-NLS-1$
94
if (topicHref != null && anchor != null) {
95             topicHref = topicHref + '#' + anchor;
96         }
97         // initialize rootPath
98
String JavaDoc pathStr = request.getParameter("path"); //$NON-NLS-1$
99
if (pathStr != null && pathStr.length() > 0) {
100             String JavaDoc[] paths = pathStr.split("_", -1); //$NON-NLS-1$
101
int[] indexes = new int[paths.length];
102             boolean indexesOK = true;
103             for (int i = 0; i < paths.length; i++) {
104                 try {
105                     indexes[i] = Integer.parseInt(paths[i]);
106                 } catch (NumberFormatException JavaDoc nfe) {
107                     indexesOK = false;
108                     break;
109                 }
110                 if (indexesOK) {
111                     rootPath = indexes;
112                 }
113             }
114         }
115
116         imagesDirectory = preferences.getImagesDirectory();
117
118         loadTocs();
119     }
120
121     /*
122      * Counts and returns the number of topics inside the given TOC (all
123      * descendants, not just subtopics), including the overall book's topic.
124      */

125     private static int countTopics(IToc toc) {
126         return countTopics(toc.getTopics()) + 1;
127     }
128     
129     /*
130      * Counts and returns the number of topics in the given array and all
131      * subtopics.
132      */

133     private static int countTopics(ITopic[] topics) {
134         int count = topics.length;
135         for (int i=0;i<topics.length;++i) {
136             ITopic[] subtopics = topics[i].getSubtopics();
137             if (subtopics != null && subtopics.length > 0) {
138                 count += countTopics(subtopics);
139             }
140         }
141         return count;
142     }
143     
144     // Accessor methods to avoid exposing help classes directly to JSP.
145
// Note: this seems ok for now, but maybe we need to reconsider this
146
// and allow help classes in JSP's.
147

148     public boolean isRemoteHelpError() {
149         boolean isError = (RemoteHelp.getError() != null);
150         if (isError) {
151             RemoteHelp.clearError();
152         }
153         return isError;
154     }
155     
156     public int getTocCount() {
157         return tocs.length;
158     }
159
160     public String JavaDoc getTocLabel(int i) {
161         return tocs[i].getLabel();
162     }
163
164     public String JavaDoc getTocHref(int i) {
165         return tocs[i].getHref();
166     }
167
168     public String JavaDoc getTocDescriptionTopic(int i) {
169         return UrlUtil.getHelpURL(tocs[i].getTopic(null).getHref());
170     }
171
172     /*
173      * Finds a path of ITopics in the given IToc to the given topic. If the
174      * toc doesn't contain the topic, returns null.
175      */

176     private static ITopic[] getTopicPathInToc(ITopic topicToFind, IToc toc) {
177         if (topicToFind.getLabel().equals(toc.getLabel())) {
178             return new ITopic[0];
179         }
180         ITopic topics[] = toc.getTopics();
181         if (topics != null) {
182             for (int i=0;i<topics.length;++i) {
183                 // returns path in reverse order
184
List JavaDoc reversePath = getTopicPathInToc(topicToFind, topics[i]);
185                 if (reversePath != null) {
186                     // reverse and return
187
ITopic[] path = new ITopic[reversePath.size()];
188                     for (int j=0;j<path.length;++j) {
189                         path[j] = (ITopic)reversePath.get((path.length - 1) - j);
190                     }
191                     return path;
192                 }
193             }
194         }
195         return null;
196     }
197     
198     /*
199      * Finds the topic in the given topic sub-tree. Returns a path of ITopics
200      * to that topic in reverse order (from the topic up).
201      */

202     private static List JavaDoc getTopicPathInToc(ITopic topicToFind, ITopic topic) {
203         if (topic.getLabel().equals(topicToFind.getLabel())) {
204             // found it. start the list to be created recursively
205
List JavaDoc path = new ArrayList JavaDoc();
206             path.add(topic);
207             return path;
208         }
209         else {
210             ITopic[] subtopics = topic.getSubtopics();
211             for (int i=0;i<subtopics.length;++i) {
212                 List JavaDoc path = getTopicPathInToc(topicToFind, subtopics[i]);
213                 if (path != null) {
214                     // it was in a subtopic.. add to the path and return
215
path.add(topic);
216                     return path;
217                 }
218             }
219         }
220         return null;
221     }
222
223     /**
224      * Returns the selected TOC
225      *
226      * @return int
227      */

228     public int getSelectedToc() {
229         return selectedToc;
230     }
231
232     /**
233      * Returns the topic to display. If there is a TOC, return its topic
234      * description. Return null if no topic is specified and there is no toc
235      * description.
236      *
237      * @return String
238      */

239     public String JavaDoc getSelectedTopic() {
240         if (topicHref != null && topicHref.length() > 0)
241             return UrlUtil.getHelpURL(topicHref);
242         else
243         if (selectedToc == -1)
244             return null;
245         IToc toc = tocs[selectedToc];
246         ITopic tocDescription = toc.getTopic(null);
247         if (tocDescription != null)
248             return UrlUtil.getHelpURL(tocDescription.getHref());
249         return UrlUtil.getHelpURL(null);
250     }
251
252     /**
253      * Returns a list of all the TOC's as xml elements. Individual TOC's are not
254      * loaded yet.
255      *
256      * @return IToc[]
257      */

258     public IToc[] getTocs() {
259         return tocs;
260     }
261
262     /**
263      * Check if given TOC is visible
264      *
265      * @param toc
266      * @return true if TOC should be visible
267      */

268     public boolean isEnabled(int toc) {
269         if (!isEnabled(tocs[toc])) {
270             return false;
271         }
272         // do not generate toc when there are no leaf topics
273
return (getEnabledSubtopicList(tocs[toc]).size() > 0);
274     }
275     /**
276      * Check if given TOC is visible
277      *
278      * @param toc
279      * @return true if TOC should be visible
280      */

281     private boolean isEnabled(IToc toc) {
282         if(!isAdvancedUI()){
283             // activities never filtered for basic browsers
284
return true;
285         }
286         return HelpBasePlugin.getActivitySupport().isEnabled(toc.getHref()) &&
287             !UAContentFilter.isFiltered(toc, HelpEvaluationContext.getContext());
288     }
289
290     private void loadTocs() {
291         tocs = HelpPlugin.getTocManager().getTocs(getLocale());
292         // Find the requested TOC
293
selectedToc = -1;
294         if (tocParameter != null && tocParameter.length() > 0) {
295             tocs = getTocs();
296             for (int i = 0; selectedToc == -1 && i < tocs.length; i++) {
297                 if (tocParameter.equals(tocs[i].getHref())) {
298                     selectedToc = i;
299                 }
300             }
301         } else {
302             // try obtaining the TOC from the topic
303

304             int index = -1;
305             do {
306                 selectedToc = findTocContainingTopic(topicHref);
307                 
308                 ITopic topic = findTopic();
309                 if (topic != null && selectedToc >= 0) {
310                     topicPath = getTopicPathInToc(topic, tocs[selectedToc]);
311                 }
312                 // if no match has been found, check if there is an anchor
313
if (topicPath == null && topicHref != null) {
314                     index = topicHref.indexOf('#');
315                     if (index != -1)
316                         topicHref = topicHref.substring(0, index);
317                 }
318                 // if there was an anchor, search again without it
319
} while (topicPath == null && index != -1);
320         }
321     }
322
323     /**
324      * Finds a TOC that contains specified topic
325      *
326      * @param topic
327      * the topic href
328      */

329     private int findTocContainingTopic(String JavaDoc topic) {
330         if (topic == null || topic.equals("")) //$NON-NLS-1$
331
return -1;
332
333         int index = topic.indexOf("/topic/"); //$NON-NLS-1$
334
if (index != -1) {
335             topic = topic.substring(index + 6);
336         }
337         else {
338             // auto-generated nav urls, e.g. "/help/nav/0_1_5"
339
index = topic.indexOf("/nav/"); //$NON-NLS-1$
340
if (index != -1) {
341                 // first number is toc index
342
String JavaDoc nav = topic.substring(index + 5);
343                 String JavaDoc book;
344                 index = nav.indexOf('_');
345                 if (index == -1) {
346                     book = nav;
347                 } else {
348                     book = nav.substring(0, index);
349                 }
350                 
351                 try {
352                     return Integer.parseInt(book);
353                 }
354                 catch (Exception JavaDoc e) {
355                     // shouldn't happen
356
}
357             }
358         }
359         index = topic.indexOf('?');
360         if (index != -1)
361             topic = topic.substring(0, index);
362
363         if (topic == null || topic.equals("")) //$NON-NLS-1$
364
return -1;
365
366         tocs = getTocs();
367         // try to find in enabled tocs first
368
for (int i = 0; i < tocs.length; i++)
369             if (isEnabled(i))
370                 if (tocs[i].getTopic(topic) != null)
371                     return i;
372         // try disabled tocs second
373
for (int i = 0; i < tocs.length; i++)
374             if (!isEnabled(i))
375                 if (tocs[i].getTopic(topic) != null)
376                     return i;
377         // nothing found
378
return -1;
379     }
380     /**
381      * Finds topic in a TOC
382      *
383      * @return ITopic or null
384      */

385     private ITopic findTopic() {
386         String JavaDoc topic = getSelectedTopic();
387         if (topic == null || topic.equals("")) //$NON-NLS-1$
388
return null;
389
390         int index = topic.indexOf("/topic/"); //$NON-NLS-1$
391
if (index != -1) {
392             topic = topic.substring(index + 6);
393         }
394         else {
395             // auto-generated nav urls, e.g. "/help/nav/0_1_5"
396
index = topic.indexOf("/nav/"); //$NON-NLS-1$
397
if (index != -1) {
398                 String JavaDoc nav = topic.substring(index + 5);
399                 StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(nav, "_"); //$NON-NLS-1$
400
try {
401                     // first number is toc index
402
index = Integer.parseInt(tok.nextToken());
403                     ITopic current = getTocs()[index].getTopic(null);
404                     while (tok.hasMoreTokens()) {
405                         index = Integer.parseInt(tok.nextToken());
406                         current = current.getSubtopics()[index];
407                     }
408                     return current;
409                 }
410                 catch (Exception JavaDoc e) {
411                     // shouldn't happen
412
}
413             }
414         }
415         index = topic.indexOf('?');
416         if (index != -1)
417             topic = topic.substring(0, index);
418
419         if (topic == null || topic.equals("")) //$NON-NLS-1$
420
return null;
421
422         if (getSelectedToc() < 0)
423             return null;
424         IToc selectedToc = getTocs()[getSelectedToc()];
425         if (selectedToc == null)
426             return null;
427         return selectedToc.getTopic(topic);
428     }
429
430     /**
431      * Generates the HTML code (a tree) for a TOC.
432      *
433      * @param toc
434      * @param out
435      * @throws IOException
436      */

437     public void generateToc(int toc, Writer JavaDoc out) throws IOException JavaDoc {
438         ITopic[] topics = getEnabledSubtopics(tocs[toc]);
439         if (topics.length <= 0) {
440             // do not generate toc when there are no leaf topics
441
return;
442         }
443
444         int maxLevels = dynamicLoadDepths;
445         if (countTopics(tocs[toc]) <= loadBookAtOnceLimit) {
446             maxLevels = -1;
447         }
448         // Construct ID of subtree root
449
StringBuffer JavaDoc id = new StringBuffer JavaDoc();
450         if (rootPath != null) {
451             // navigate to root topic, skipping parents
452
for (int p = 0; p < rootPath.length; p++) {
453                 if (id.length() > 0) {
454                     id.append('_');
455                 }
456                 topics = getEnabledSubtopics(topics[rootPath[p]]);
457                 id.append(rootPath[p]);
458             }
459             out.write("<ul class='expanded' id=\"" + id.toString() + "\">\n"); //$NON-NLS-1$ //$NON-NLS-2$
460
}
461
462         for (int i = 0; i < topics.length; i++) {
463             String JavaDoc idPrefix = id.toString();
464             if (idPrefix.length() > 0) {
465                 idPrefix = idPrefix + "_" + Integer.toString(i); //$NON-NLS-1$
466
} else {
467                 idPrefix = Integer.toString(i);
468             }
469
470             generateTopic(topics[i], out, idPrefix, maxLevels, rootPath == null
471                     ? 0
472                     : rootPath.length);
473         }
474
475         if (rootPath != null) {
476             out.write("</ul>\n"); //$NON-NLS-1$
477
}
478
479     }
480
481     /**
482      * @param topic
483      * @param out
484      * @param maxLevels
485      * relative number of topic levels to generate (pass <0 for
486      * inifinite), 1 generates this topic as last level topic
487      * @param currentLevel
488      * current level of topic, 0 is first Level under TOC
489      * @throws IOException
490      */

491     private void generateTopic(ITopic topic, Writer JavaDoc out, String JavaDoc id,
492             int maxLevels, int currentLevel) throws IOException JavaDoc {
493         if (maxLevels == 0) {
494             return;
495         }
496
497         topicsGenerated++;
498         if (maxLevels > 1 && topicsGenerated > honorLevelsLimit) {
499             maxLevels = 1;
500         }
501
502         ITopic[] topics = getEnabledSubtopics(topic);
503         boolean hasNodes = topics.length > 0;
504
505         if (hasNodes) {
506             out.write("<li>"); //$NON-NLS-1$
507
out.write("<img SRC='"); //$NON-NLS-1$
508
out.write(imagesDirectory);
509             out
510                     .write("/plus.gif' class='collapsed' alt=\"" + ServletResources.getString("topicClosed", request) + "\" title=\"" + ServletResources.getString("topicClosed", request) + "\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
511
out.write("<a HREF=" +"\""+ UrlUtil.getHelpURL(topic.getHref()) + "\""+">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
512
out.write("<img SRC='"); //$NON-NLS-1$
513
out.write(imagesDirectory);
514             out.write("/container_obj.gif' alt=\"\">"); //$NON-NLS-1$
515
out.write(UrlUtil.htmlEncode(topic.getLabel()));
516             out.write("</a>"); //$NON-NLS-1$
517

518             // is it ancestor of topic to reveal
519
boolean isAncestor = topicPath != null
520                     && topicPath.length > currentLevel + 1
521                     && topicPath[currentLevel] == topic;
522
523             if (maxLevels != 1 || isAncestor) {
524                 out.write("<ul class='collapsed'>\n"); //$NON-NLS-1$
525
} else {
526                 // children will not be generated
527
out.write("<ul class='collapsed' id=\"" + id + "\">\n"); //$NON-NLS-1$ //$NON-NLS-2$
528
}
529
530             if (1 <= maxLevels && maxLevels <= dynamicLoadDepths && isAncestor) {
531                 // ignore max levels, show children
532
for (int i = 0; i < topics.length; i++) {
533                     generateTopic(topics[i], out, id + "_" + i, //$NON-NLS-1$
534
dynamicLoadDepths, currentLevel + 1);
535                 }
536             } else {
537                 for (int i = 0; i < topics.length; i++) {
538                     generateTopic(topics[i], out, id + "_" + i, //$NON-NLS-1$
539
maxLevels - 1, currentLevel + 1);
540                 }
541             }
542
543             out.write("</ul>\n"); //$NON-NLS-1$
544
} else {
545             out.write("<li>"); //$NON-NLS-1$
546
out.write("<img SRC='"); //$NON-NLS-1$
547
out.write(imagesDirectory);
548             out.write("/plus.gif' class='h' alt=\"\">"); //$NON-NLS-1$
549
out.write("<a HREF=" +"\""+ UrlUtil.getHelpURL(topic.getHref()) + "\""+">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
550
out.write("<img SRC='"); //$NON-NLS-1$
551
out.write(imagesDirectory);
552             out.write("/topic.gif' alt=\"\">"); //$NON-NLS-1$
553
out.write(UrlUtil.htmlEncode(topic.getLabel()));
554             out.write("</a>"); //$NON-NLS-1$
555
}
556
557         out.write("</li>\n"); //$NON-NLS-1$
558
}
559
560     /**
561      * Generates the HTML code (a tree) for a TOC.
562      *
563      * @param toc
564      * @param out
565      * @throws IOException
566      */

567     public void generateBasicToc(int toc, Writer JavaDoc out) throws IOException JavaDoc {
568         ITopic[] topics = getEnabledSubtopics(tocs[toc]);
569         for (int i = 0; i < topics.length; i++) {
570             generateBasicTopic(topics[i], out);
571         }
572
573     }
574
575     private void generateBasicTopic(ITopic topic, Writer JavaDoc out)
576             throws IOException JavaDoc {
577
578         out.write("<li>"); //$NON-NLS-1$
579
ITopic[] topics = getEnabledSubtopics(topic);
580         boolean hasNodes = topics.length > 0;
581         if (hasNodes) {
582             out.write("<nobr>"); //$NON-NLS-1$
583
out.write("<a "); //$NON-NLS-1$
584
if (getSelectedTopicHelpHref().equals(topic.getHref())) {
585                 out.write("name=\"selectedItem\" "); //$NON-NLS-1$
586
}
587             out.write("href="+"\"" + UrlUtil.getHelpURL(topic.getHref())+"\"" + ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
588
out.write("<img SRC='"); //$NON-NLS-1$
589
out.write(imagesDirectory);
590             out.write("/container_obj.gif' alt=\"\" border=0>&nbsp;"); //$NON-NLS-1$
591
out.write(UrlUtil.htmlEncode(topic.getLabel()));
592             out.write("</a>"); //$NON-NLS-1$
593
out.write("</nobr>"); //$NON-NLS-1$
594

595             out.write("<ul>\n"); //$NON-NLS-1$
596

597             for (int i = 0; i < topics.length; i++) {
598                 generateBasicTopic(topics[i], out);
599             }
600
601             out.write("</ul>\n"); //$NON-NLS-1$
602
} else {
603             out.write("<nobr>"); //$NON-NLS-1$
604
out.write("<a "); //$NON-NLS-1$
605
if (getSelectedTopicHelpHref().equals(topic.getHref())) {
606                 out.write("name=\"selectedItem\" "); //$NON-NLS-1$
607
}
608             out.write("href="+"\"" + UrlUtil.getHelpURL(topic.getHref()) +"\""+ ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
609
out.write("<img SRC='"); //$NON-NLS-1$
610
out.write(imagesDirectory);
611             out.write("/topic.gif' alt=\"\" border=0>&nbsp;"); //$NON-NLS-1$
612
out.write(UrlUtil.htmlEncode(topic.getLabel()));
613             out.write("</a>"); //$NON-NLS-1$
614
out.write("</nobr>"); //$NON-NLS-1$
615
}
616
617         out.write("</li>\n"); //$NON-NLS-1$
618
}
619     /**
620      * @return String - help form of selected topic URL, or ""
621      */

622     private String JavaDoc getSelectedTopicHelpHref() {
623         if (topicHelpHref == null) {
624             String JavaDoc topic = getSelectedTopic();
625             if (topic == null || topic.length() == 0) {
626                 topicHelpHref = ""; //$NON-NLS-1$
627
return topicHelpHref;
628             }
629             int index = topic.indexOf("/topic/"); //$NON-NLS-1$
630
if (index != -1)
631                 topic = topic.substring(index + 6);
632             index = topic.indexOf('?');
633             if (index != -1)
634                 topic = topic.substring(0, index);
635             topicHelpHref = topic;
636             if (topic == null) {
637                 topicHelpHref = ""; //$NON-NLS-1$
638
}
639         }
640         return topicHelpHref;
641     }
642     /**
643      * Obtains children topics for a given navigation element. Topics from TOCs
644      * not matching enabled activities are filtered out.
645      *
646      * @param element ITopic or IToc
647      * @return ITopic[]
648      */

649     public ITopic[] getEnabledSubtopics(Object JavaDoc element) {
650         List JavaDoc topics = getEnabledSubtopicList(element);
651         return (ITopic[])topics.toArray(new ITopic[topics.size()]);
652     }
653     /**
654      * Obtains children topics for a given navigation element. Topics from TOCs
655      * not matching enabled activities are filtered out.
656      *
657      * @param navigationElement
658      * @return List of ITopic
659      */

660     private List JavaDoc getEnabledSubtopicList(Object JavaDoc element) {
661         if (element instanceof IToc && !isEnabled((IToc) element))
662             return Collections.EMPTY_LIST;
663         List JavaDoc children;
664         if (element instanceof IToc) {
665             children = Arrays.asList(((IToc)element).getTopics());
666         }
667         else if (element instanceof ITopic) {
668             children = Arrays.asList(((ITopic)element).getSubtopics());
669         }
670         else {
671             // unknown element type
672
return Collections.EMPTY_LIST;
673         }
674         List JavaDoc childTopics = new ArrayList JavaDoc(children.size());
675         for (Iterator JavaDoc childrenIt = children.iterator(); childrenIt.hasNext();) {
676             Object JavaDoc c = childrenIt.next();
677             if ((c instanceof ITopic)) {
678                 // add topic only if it will not end up being an empty
679
// container
680
if (((((ITopic) c).getHref() != null && ((ITopic) c)
681                         .getHref().length() > 0) || getEnabledSubtopicList(c).size() > 0) &&
682                         !UAContentFilter.isFiltered(c, HelpEvaluationContext.getContext())) {
683                     childTopics.add(c);
684                 }
685             } else {
686                 // it is a Toc, Anchor or Link,
687
// which may have children attached to it.
688
childTopics.addAll(getEnabledSubtopicList(c));
689             }
690         }
691         return childTopics;
692     }
693     private void generateTopicLinks(ITopic topic, Writer JavaDoc w, int indent) {
694         String JavaDoc topicHref = topic.getHref();
695         try {
696             if (indent == 0)
697                 w.write("<b>"); //$NON-NLS-1$
698
for (int tab = 0; tab < indent; tab++) {
699                 w.write("&nbsp;&nbsp;"); //$NON-NLS-1$
700
}
701             if (topicHref != null && topicHref.length() > 0) {
702                 w.write("<a HREF=\""); //$NON-NLS-1$
703
if ('/' == topicHref.charAt(0)) {
704                     w.write("topic"); //$NON-NLS-1$
705
}
706                 w.write(topicHref);
707                 w.write("\">"); //$NON-NLS-1$
708
w.write(UrlUtil.htmlEncode(topic.getLabel()));
709                 w.write("</a>"); //$NON-NLS-1$
710
} else {
711                 w.write(UrlUtil.htmlEncode(topic.getLabel()));
712             }
713             w.write("<br>\n"); //$NON-NLS-1$
714
if (indent == 0)
715                 w.write("</b>"); //$NON-NLS-1$
716
} catch (IOException JavaDoc ioe) {
717         }
718         ITopic[] topics = topic.getSubtopics();
719         for (int i = 0; i < topics.length; i++) {
720             generateTopicLinks(topics[i], w, indent + 1);
721         }
722     }
723
724     public void generateLinks(Writer JavaDoc out) {
725         for (int i = 0; i < tocs.length; i++) {
726             IToc toc = tocs[i];
727             ITopic tocTopic = toc.getTopic(null);
728             generateTopicLinks(tocTopic, out, 0);
729             ITopic[] topics = toc.getTopics();
730             for (int t = 0; t < topics.length; t++) {
731                 generateTopicLinks(topics[t], out, 1);
732             }
733         }
734
735     }
736     
737     public ITopic[] getTopicPath() {
738         return topicPath;
739     }
740
741     public int[] getRootPath() {
742         return rootPath;
743     }
744     
745     public String JavaDoc getTopicHref() {
746         return topicHref;
747     }
748 }
749
Popular Tags