KickJava   Java API By Example, From Geeks To Geeks.

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


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.OutputStream JavaDoc;
14 import java.io.UnsupportedEncodingException JavaDoc;
15
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.help.ITopic;
20 import org.eclipse.help.internal.HelpPlugin;
21 import org.eclipse.help.internal.webapp.HelpWebappPlugin;
22 import org.eclipse.help.internal.webapp.data.UrlUtil;
23
24 /**
25  * Injects breadcrumbs at the top of help documents, e.g.
26  * "Workbench User Guide > Concepts > Workbench".
27  */

28 public class BreadcrumbsFilter implements IFilter {
29
30     private static final String JavaDoc HEAD_CONTENT =
31             "\n<link rel=\"stylesheet\" HREF=\"../content/PLUGINS_ROOT/" + HelpWebappPlugin.PLUGIN_ID + "/advanced/breadcrumbs.css\" charset=\"ISO-8859-1\" type=\"text/css\"></link>" //$NON-NLS-1$//$NON-NLS-2$
32
+ "\n<script language=\"JavaScript\" SRC=\"../content/PLUGINS_ROOT/" + HelpPlugin.PLUGIN_ID + "/livehelp.js\"> </script>"; //$NON-NLS-1$ //$NON-NLS-2$
33

34     /* (non-Javadoc)
35      * @see org.eclipse.help.internal.webapp.servlet.IFilter#filter(javax.servlet.http.HttpServletRequest, java.io.OutputStream)
36      */

37     public OutputStream JavaDoc filter(HttpServletRequest JavaDoc req, OutputStream JavaDoc out) {
38         String JavaDoc uri = req.getRequestURI();
39         if (uri == null || !uri.endsWith("html") && !uri.endsWith("htm") && !uri.startsWith("/help/nav/")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
40
return out;
41         }
42         if ("/rtopic".equals(req.getServletPath())) { //$NON-NLS-1$
43
return out;
44         }
45         if (UrlUtil.isBot(req)) {
46             return out;
47         }
48         String JavaDoc pathInfo = req.getPathInfo();
49         if (pathInfo == null) {
50             return out;
51         }
52         int[] path = UrlUtil.getTopicPath(uri);
53         if (path != null && path.length > 1) {
54             boolean isNarrow = "/ntopic".equals(req.getServletPath()); //$NON-NLS-1$
55
String JavaDoc locale = UrlUtil.getLocale(req, null);
56             String JavaDoc bodyContent = getBodyContent(path, getBackpath(uri), isNarrow, locale);
57             try {
58                 return new FilterHTMLHeadAndBodyOutputStream(out, HEAD_CONTENT.getBytes("ASCII"), bodyContent); //$NON-NLS-1$
59
}
60             catch (UnsupportedEncodingException JavaDoc e) {
61             }
62         }
63         return out;
64     }
65     
66     private String JavaDoc getBackpath(String JavaDoc path) {
67         int num = new Path(path).segmentCount() - 2;
68         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
69         for (int i=0;i<num;++i) {
70             if (i > 0) {
71                 buf.append('/');
72             }
73             buf.append(".."); //$NON-NLS-1$
74
}
75         return buf.toString();
76     }
77     
78     private String JavaDoc getBodyContent(int[] path, String JavaDoc backPath, boolean isNarrow, String JavaDoc locale) {
79         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
80         StringBuffer JavaDoc pathBuf = new StringBuffer JavaDoc();
81         ITopic topic = HelpPlugin.getTocManager().getTocs(locale)[path[0]].getTopic(null);
82         pathBuf.append(path[0]);
83         buf.append("<div class=\"help_breadcrumbs\">"); //$NON-NLS-1$
84

85         for (int i=0;i<path.length-1;++i) {
86             
87             // add the link
88
buf.append("<a HREF=\""); //$NON-NLS-1$
89
String JavaDoc href = topic.getHref();
90             if (href != null) {
91                 href = backPath + (isNarrow ? "/ntopic" : "/topic") + href; //$NON-NLS-1$ //$NON-NLS-2$
92
}
93             else {
94                 if (isNarrow) {
95                     href = "javascript:liveAction('org.eclipse.help.ui', 'org.eclipse.help.ui.internal.ShowInTocAction', '" + pathBuf.toString() + "')"; //$NON-NLS-1$ //$NON-NLS-2$
96
}
97                 else {
98                     href = backPath + "/nav/" + pathBuf.toString(); //$NON-NLS-1$
99
}
100             }
101             buf.append(href);
102             buf.append("\">"); //$NON-NLS-1$
103
buf.append(topic.getLabel());
104             buf.append("</a>"); //$NON-NLS-1$
105

106             // add separator
107
if (i < path.length - 2 || path.length == 2) {
108                 // always add if there's only one link
109
buf.append(" > "); //$NON-NLS-1$
110
}
111             
112             // move to the next topic in the path
113
topic = topic.getSubtopics()[path[i + 1]];
114             pathBuf.append('_');
115             pathBuf.append(path[i + 1]);
116         }
117         buf.append("</div>"); //$NON-NLS-1$
118
return buf.toString();
119     }
120 }
121
Popular Tags