KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > webapp > servlet > TocFragmentServlet


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.servlet;
12
13 import java.io.IOException JavaDoc;
14 import java.util.Locale JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.WeakHashMap JavaDoc;
17
18 import javax.servlet.ServletException JavaDoc;
19 import javax.servlet.http.HttpServlet JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23 import org.eclipse.help.IToc;
24 import org.eclipse.help.ITopic;
25 import org.eclipse.help.internal.webapp.WebappResources;
26 import org.eclipse.help.internal.webapp.data.EnabledTopicUtils;
27 import org.eclipse.help.internal.webapp.data.TocData;
28 import org.eclipse.help.internal.webapp.data.UrlUtil;
29
30 /*
31  * Creates xml representing selected parts of one or more TOCs depending on the parameters
32  * With no parameters the head of each toc is included
33  * With parameter "href" the node and all its ancestors and siblings is included, corresponds to show in toc
34  * With parameter "toc" and optionally "path" the node, its ancestors and children are included
35  */

36 public class TocFragmentServlet extends HttpServlet JavaDoc {
37     
38     private static final long serialVersionUID = 1L;
39     private static Map JavaDoc locale2Response = new WeakHashMap JavaDoc();
40
41     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
42             throws ServletException JavaDoc, IOException JavaDoc {
43         String JavaDoc locale = UrlUtil.getLocale(req, resp);
44         req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
45
resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
46
resp.setHeader("Cache-Control","no-cache"); //$NON-NLS-1$//$NON-NLS-2$
47
resp.setHeader("Pragma","no-cache"); //$NON-NLS-1$ //$NON-NLS-2$
48
resp.setDateHeader ("Expires", 0); //$NON-NLS-1$
49
TocData data = new TocData(this.getServletContext(), req, resp);
50         Serializer serializer = new Serializer(data, req.getLocale());
51         String JavaDoc response = serializer.generateTreeXml();
52         locale2Response.put(locale, response);
53         resp.getWriter().write(response);
54     }
55     
56     /*
57      * Class which creates the xml file based upon the request parameters
58      */

59     private class Serializer {
60         
61         private TocData tocData;
62         private StringBuffer JavaDoc buf;
63         private int requestKind;
64         private Locale JavaDoc locale;
65         private static final int REQUEST_SHOW_IN_TOC = 1; // Show an element based on its href
66
private static final int REQUEST_SHOW_TOCS = 2; // Show all the tocs but not their children
67
private static final int REQUEST_SHOW_CHILDREN = 3; // Show the children of a node
68

69         public Serializer(TocData data, Locale JavaDoc locale) {
70             tocData = data;
71             buf = new StringBuffer JavaDoc();
72             this.locale = locale;
73             if (tocData.getTopicHref() != null) {
74                 requestKind = REQUEST_SHOW_IN_TOC;
75             } else if (tocData.getSelectedToc() == -1) {
76                 requestKind = REQUEST_SHOW_TOCS;
77             } else {
78                 requestKind = REQUEST_SHOW_CHILDREN;
79             }
80         }
81             
82         public String JavaDoc generateTreeXml() {
83             buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
84
buf.append("<tree_data>\n"); //$NON-NLS-1$
85

86
87             if (tocData.isRemoteHelpError()) {
88                 addError(WebappResources.getString("remoteHelpErrorMessage", locale)); //$NON-NLS-1$
89
}
90                     
91             // Return an error for show in toc if topic was not found in toc
92
if (requestKind == REQUEST_SHOW_IN_TOC && tocData.getTopicPath() == null) {
93                 addError(WebappResources.getString("CannotSync", locale)); //$NON-NLS-1$
94
} else {
95                 serializeTocs();
96             }
97             buf.append("</tree_data>\n"); //$NON-NLS-1$
98
return buf.toString();
99         }
100
101         private void addError(String JavaDoc message) {
102             buf.append("<error>"); //$NON-NLS-1$
103
buf.append(XMLGenerator.xmlEscape(message));
104             buf.append("</error>"); //$NON-NLS-1$
105
}
106
107         private void serializeTocs() {
108             ITopic[] topicPath = tocData.getTopicPath();
109     
110             int selectedToc = tocData.getSelectedToc();
111             // Iterate over all tocs - if there is a selected toc only generate that
112
// toc, otherwise generate the root of every toc.
113
for (int toc=0; toc< tocData.getTocCount(); toc++) {
114                 boolean shouldLoad = requestKind == REQUEST_SHOW_TOCS || toc == selectedToc;
115                 if(!tocData.isEnabled(toc)){
116                     shouldLoad = false;
117                 }
118                 if (shouldLoad) {
119                     boolean isSelected = false; // Should this node be selected in the tree
120
if (requestKind == REQUEST_SHOW_TOCS) {
121                         isSelected = toc == 0;
122                     } else if (requestKind == REQUEST_SHOW_CHILDREN) {
123                         isSelected = tocData.getRootPath() == null;
124                     }
125                     serializeToc(tocData.getTocs()[toc], toc, topicPath, isSelected);
126                 }
127             }
128         }
129     
130         private void serializeToc(IToc toc, int tocIndex, ITopic[] topicPath, boolean isSelected) {
131             ITopic[] topics = EnabledTopicUtils.getEnabled(toc.getTopics());
132             if (topics.length <= 0) {
133                 // do not generate toc when there are no leaf topics
134
return;
135             }
136             
137             if (requestKind == REQUEST_SHOW_CHILDREN) {
138                 topicPath = getTopicPathFromRootPath(toc);
139             }
140             
141             buf.append("<node"); //$NON-NLS-1$
142
if (toc.getLabel() != null) {
143                 buf.append('\n' + " title=\"" + XMLGenerator.xmlEscape(toc.getLabel()) + '"'); //$NON-NLS-1$
144
}
145             buf.append('\n' + " id=\"" + XMLGenerator.xmlEscape(toc.getHref()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
146

147             String JavaDoc href = toc.getTopic(null).getHref();
148             if (href == null) {
149                 href = "/../nav/" + tocIndex; //$NON-NLS-1$
150
}
151             buf.append('\n' + " HREF=\"" + XMLGenerator.xmlEscape(UrlUtil.getHelpURL(href)) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
152

153             buf.append('\n' + " image=\"toc_closed\""); //$NON-NLS-1$
154

155             boolean serializeChildren = true;
156             if (requestKind == REQUEST_SHOW_TOCS) {
157                 serializeChildren = false;
158             }
159             if (requestKind == REQUEST_SHOW_IN_TOC && topicPath.length == 0) {
160                 serializeChildren = false;
161                 buf.append('\n' + " is_selected=\"true\"" ); //$NON-NLS-1$
162
buf.append('\n' + " is_highlighted=\"true\"" ); //$NON-NLS-1$
163
}
164             buf.append(">\n"); //$NON-NLS-1$
165
if (serializeChildren) {
166                 serializeChildTopics(topics, topicPath, "", isSelected); //$NON-NLS-1$
167
}
168             buf.append("</node>\n"); //$NON-NLS-1$
169

170         }
171         
172         private ITopic[] getTopicPathFromRootPath(IToc toc) {
173             ITopic[] topicPath;
174             // Determine the topicPath from the path passed in as a parameter
175
int[] rootPath = tocData.getRootPath();
176             if (rootPath == null) {
177                 return null;
178             }
179             int pathLength = rootPath.length;
180             topicPath = new ITopic[pathLength];
181             ITopic[] children = EnabledTopicUtils.getEnabled(toc.getTopics());
182             for (int i = 0; i < pathLength; i++) {
183                 int index = rootPath[i];
184                 if (index < children.length) {
185                     topicPath[i] = children[index];
186                     children = EnabledTopicUtils.getEnabled(topicPath[i].getSubtopics());
187                 } else {
188                     return null; // Mismatch between expected and actual children
189
}
190             }
191             return topicPath;
192         }
193     
194         private void serializeTopic(ITopic topic, ITopic[] topicPath, boolean isSelected, String JavaDoc parentPath) {
195             ITopic[] subtopics = EnabledTopicUtils.getEnabled(topic.getSubtopics());
196             buf.append("<node"); //$NON-NLS-1$
197
if (topic.getLabel() != null) {
198                 buf.append('\n' + " title=\"" + XMLGenerator.xmlEscape(topic.getLabel()) + '"'); //$NON-NLS-1$
199
}
200     
201             buf.append('\n' + " id=\"" + parentPath + "\""); //$NON-NLS-1$ //$NON-NLS-2$
202

203             String JavaDoc href = topic.getHref();
204             if (href == null) {
205                 href = "/../nav/" + tocData.getSelectedToc() + '_' + parentPath; //$NON-NLS-1$
206
}
207             buf.append('\n' + " HREF=\"" + XMLGenerator.xmlEscape( //$NON-NLS-1$
208
UrlUtil.getHelpURL(href)) + '"');
209             if (subtopics.length == 0 ) {
210                 buf.append('\n' + " is_leaf=\"true\"" ); //$NON-NLS-1$
211
}
212             if (isSelected && requestKind == REQUEST_SHOW_IN_TOC) {
213                 buf.append('\n' + " is_selected=\"true\"" ); //$NON-NLS-1$
214
buf.append('\n' + " is_highlighted=\"true\"" ); //$NON-NLS-1$
215
}
216             String JavaDoc icon;
217             if (subtopics.length == 0) {
218                 icon = "topic"; //$NON-NLS-1$
219
} else if (topic.getHref() == null) {
220                 icon = "container_obj"; //$NON-NLS-1$
221
} else {
222                 icon = "container_topic"; //$NON-NLS-1$
223
}
224             buf.append('\n' + " image=\"" + icon + "\""); //$NON-NLS-1$ //$NON-NLS-2$
225

226             buf.append(">\n"); //$NON-NLS-1$
227
serializeChildTopics(subtopics, topicPath, parentPath, isSelected);
228             buf.append("</node>\n"); //$NON-NLS-1$
229
}
230     
231         private void serializeChildTopics(ITopic[] childTopics, ITopic[] topicPath, String JavaDoc parentPath, boolean parentIsSelected) {
232             if (parentIsSelected && requestKind == REQUEST_SHOW_CHILDREN) {
233                 // Show the children of this node
234
for (int subtopic = 0; subtopic < childTopics.length; subtopic++) {
235                     serializeTopic(childTopics[subtopic], null, false, addSuffix(parentPath, subtopic));
236                 }
237             } else if (topicPath != null) {
238                 for (int subtopic = 0; subtopic < childTopics.length; subtopic++) {
239                     if (topicPath[0].getLabel().equals(childTopics[subtopic].getLabel())) {
240                         ITopic[] newPath = null;
241                         if (topicPath.length > 1) {
242                             newPath = new ITopic[topicPath.length - 1];
243                             System.arraycopy(topicPath, 1, newPath, 0, topicPath.length - 1);
244                         }
245                         serializeTopic(childTopics[subtopic], newPath, topicPath.length == 1, addSuffix(parentPath, subtopic));
246                     } else {
247                         serializeTopic(childTopics[subtopic], null, false, addSuffix(parentPath, subtopic));
248                     }
249                 }
250             }
251         }
252
253         private String JavaDoc addSuffix(String JavaDoc parentPath, int subtopic) {
254             if (parentPath.length() == 0) {
255                 return parentPath + subtopic;
256             }
257             return parentPath + '_' + subtopic;
258         }
259     }
260
261 }
262
Popular Tags