KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.io.OutputStream JavaDoc;
15 import java.io.OutputStreamWriter JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.util.Locale JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.http.HttpServlet JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.eclipse.help.ITopic;
26 import org.eclipse.help.UAContentFilter;
27 import org.eclipse.help.internal.HelpPlugin;
28 import org.eclipse.help.internal.base.HelpEvaluationContext;
29 import org.eclipse.help.internal.webapp.WebappResources;
30 import org.eclipse.help.internal.webapp.data.UrlUtil;
31
32 /*
33  * Generates navigation pages where topics are not present in the table
34  * of contents. Displays links to the direct child topics.
35  */

36 public class NavServlet extends HttpServlet JavaDoc {
37     
38     private static final long serialVersionUID = 1L;
39
40     private static final String JavaDoc XHTML_1 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<link rel=\"stylesheet\" type=\"text/css\" HREF=\"../content/PLUGINS_ROOT/PRODUCT_PLUGIN/book.css\"/>\n<title>"; //$NON-NLS-1$
41
private static final String JavaDoc XHTML_2 = "</title>\n</head>\n<body>\n"; //$NON-NLS-1$
42
private static final String JavaDoc XHTML_3 = "</body>\n</html>"; //$NON-NLS-1$
43

44     private static final IFilter filters[] = new IFilter[]{
45         new FramesetFilter(), new InjectionFilter(), new BreadcrumbsFilter() };
46
47     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc {
48         Locale JavaDoc locale = getLocale(req, resp);
49
50         req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
51
resp.setContentType("text/html; charset=UTF-8"); //$NON-NLS-1$
52

53         String JavaDoc path = req.getPathInfo().substring(1);
54         ITopic topic = getTopic(path, locale);
55
56         OutputStream JavaDoc out = resp.getOutputStream();
57         for (int i = 0; i < filters.length; i++) {
58             out = filters[i].filter(req, out);
59         }
60
61         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(out, "UTF-8")); //$NON-NLS-1$
62
writeContent(topic, path, locale, writer);
63         writer.close();
64     }
65     
66     private Locale JavaDoc getLocale (HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) {
67         Locale JavaDoc locale;
68         String JavaDoc nl = UrlUtil.getLocale(req, resp);
69         // break the string into tokens to get the Locale object
70
StringTokenizer JavaDoc locales = new StringTokenizer JavaDoc(nl, "_"); //$NON-NLS-1$
71
if (locales.countTokens() == 1)
72             locale = new Locale JavaDoc(locales.nextToken(), ""); //$NON-NLS-1$
73
else if (locales.countTokens() == 2)
74             locale = new Locale JavaDoc(locales.nextToken(), locales.nextToken());
75         else if (locales.countTokens() == 3)
76             locale = new Locale JavaDoc(locales.nextToken(), locales.nextToken(), locales
77                     .nextToken());
78         else
79             locale = Locale.getDefault();
80         return locale;
81     }
82     
83     private ITopic getTopic(String JavaDoc topicPath, Locale JavaDoc locale) {
84         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(topicPath, "_"); //$NON-NLS-1$
85
int index = Integer.parseInt(tok.nextToken());
86         ITopic topic = HelpPlugin.getTocManager().getTocs(locale.toString())[index].getTopic(null);
87         while (tok.hasMoreTokens()) {
88             index = Integer.parseInt(tok.nextToken());
89             topic = topic.getSubtopics()[index];
90         }
91         return topic;
92     }
93     
94     private void writeContent(ITopic topic, String JavaDoc path, Locale JavaDoc locale, PrintWriter JavaDoc writer) {
95         writer.write(XHTML_1);
96         writer.write(topic.getLabel());
97         writer.write(XHTML_2);
98         writer.write("<h1 class=\"NavTitle\">" + topic.getLabel() + "</h1>\n"); //$NON-NLS-1$ //$NON-NLS-2$
99
writer.write("<h3 class=\"NavListTitle\">" + WebappResources.getString("TocHeading", locale) + "</h3>\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
100
writer.write("<ul class=\"NavList\">\n"); //$NON-NLS-1$
101
ITopic[] subtopics = topic.getSubtopics();
102         for (int i=0;i<subtopics.length;++i) {
103             if (!UAContentFilter.isFiltered(subtopics[i], HelpEvaluationContext.getContext())) {
104                 writer.write("<li><a HREF=\""); //$NON-NLS-1$
105
String JavaDoc href = subtopics[i].getHref();
106                 if (href == null) {
107                     href = path + '_' + i;
108                 }
109                 else {
110                     href = XMLGenerator.xmlEscape(UrlUtil.getHelpURL(href));
111                 }
112                 writer.write(href);
113                 writer.write("\">" + subtopics[i].getLabel() + "</a></li>\n"); //$NON-NLS-1$//$NON-NLS-2$
114
}
115         }
116         writer.write("</ul>\n"); //$NON-NLS-1$
117
writer.write(XHTML_3);
118     }
119 }
120
Popular Tags