Skip to main content
summaryrefslogtreecommitdiffstats
blob: 250cf804ca99e9321dabdaf4efed58aa91ce21d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package org.eclipse.help.internal.util;

/*
 * Licensed Materials - Property of IBM,
 * WebSphere Studio Workbench
 * (c) Copyright IBM Corp 2000
 */

import java.io.*;
import java.util.*;
import org.eclipse.help.internal.HelpSystem;
import org.eclipse.help.internal.contributions.*;

/**
 * This class is resposible for generating a Table of Contents for
 * a given Topic hierarchy.
 */
public class TableOfContentsGenerator {

	// flag needed when doing recursion in findTopic()
	private static boolean foundTopic = false;
	public TableOfContentsGenerator() {
		super();
	}
	/**
	 * Recursively finds of a Topic in a list of Topics with the given topicId
	 */
	private Topic findTopic(List children, String topicId) {
		Topic topic = null;
		for (int i = 0; i < children.size() && !foundTopic; i++) {
			topic = ((Topic) children.get(i));
			if (topic.getID().equals(topicId)) {
				foundTopic = true;
				break;
			}
		}

		if (foundTopic)
			return topic;
		else {
			for (int i = 0; i < children.size() && !foundTopic; i++) {
				Topic child = topic = ((Topic) children.get(i));
				topic = findTopic(child.getChildrenList(), topicId);
				if ((topic != null) && (foundTopic))
					return topic;
			}
			return null;
		}

	}
	private void generateHeader(StringBuffer buffer) {
		buffer.append("<HEAD>");
		// officially, a HEAD element needs a TITLE. fake it.
		buffer.append("<TITLE/>");

		// make sure that we have everything in UTF-8 because this is
		// what this string buffer will converted to.
		buffer.append("<META http-equiv=\"Content-Type\" ");
		buffer.append("content=\"text/html; charset=utf-8\">");

		// set Expires to any old date to avoid caching by IE.
		// HTTP servers sometimes return this info as part of the
		// respone.  
		buffer.append("<META HTTP-EQUIV=\"Expires\" ");
		buffer.append("CONTENT=\"Mon, 04 Dec 2000 11:11:11 GMT\"> ");
		buffer.append("</HEAD>");

	}
	/** 
	 * generates a Table Of Contents as an InputStream
	 */
	private InputStream generateTableOfContents(Topic[] topicList) {

		StringBuffer tableOfContents = new StringBuffer();
		tableOfContents.append("<html>");
		generateHeader(tableOfContents);

		tableOfContents.append("<body>");
		tableOfContents.append("<h1 ALIGN=CENTER>");
		tableOfContents.append(Resources.getString("Table_Of_Contents"));
		tableOfContents.append("</h1>");
		tableOfContents.append("<h3>");

		try {
			tableOfContents.append("<ol>");
			for (int i = 0; i < topicList.length; i++) {
				tableOfContents.append("<li>");
				tableOfContents.append(topicList[i].getLabel());
				tableOfContents.append("</li>");
			}
			tableOfContents.append("<ol>");
			tableOfContents.append("</h3>");

			tableOfContents.append("</body></html>");
			byte[] bytes = tableOfContents.toString().getBytes("UTF-8");

			ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
			return inputStream;
		} catch (Exception e) {
			// null handled by calling class
			return null;
		}

	}
	/** 
	 * generates a Table Of Contents as an InputStream
	 */
	public InputStream generateTableOfContents(
		String infosetId,
		String viewId,
		String topicId) {

		try {
			// get to the topic in the navigation model. Find the infoset, and then
			// the view.
			InfoSet infoset =
				HelpSystem.getInstance().getNavigationManager().getInfoSet(infosetId);
			InfoView view = infoset.getView(viewId);
			List children = view.getChildrenList();

			// now find the topic in the view.
			Topic topic = findTopic(children, topicId);

			// cleanup for next rev.
			foundTopic = false;

			// create the list of children topics.
			Topic[] topicList = getTopicList(topic);

			return generateTableOfContents(topicList);

		} catch (Exception e) {
			// return null to signal problem.
			return null;
		}

	}
	/**
	 * Returns an Topic array off all topics to be printed.
	 * returns null if array could not be created.
	 */
	public static Topic[] getTopicList(Topic rootTopic) {
		try {
			Vector objectVector = new Vector();
			populateTopicVector(rootTopic, objectVector);

			Topic[] topicList = new Topic[objectVector.size()];
			objectVector.toArray(topicList);

			return topicList;
		} catch (Exception e) {
			// signal problem
			return null;
		}
	}
	/** 
	 * recursive method to generate the left to right, top to bottom 
	 * list of topic URLs.
	 */
	private static void populateTopicVector(Object selection, Vector topicVector) {

		if (selection instanceof Topic) {
			Topic topicElement = (Topic) selection;

			if (topicElement.getHref() != null && topicElement.getHref() != "")
				topicVector.add(topicElement);
			java.util.List topicList = topicElement.getChildrenList();
			for (int i = 0; i < topicList.size(); i++) {
				populateTopicVector(topicList.get(i), topicVector);
			}
		}
	}
}

Back to the top