KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.help.internal.webapp.servlet;
13
14 import java.io.OutputStream JavaDoc;
15 import java.io.UnsupportedEncodingException JavaDoc;
16
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.help.internal.base.BaseHelpSystem;
23 import org.eclipse.help.internal.base.HelpBasePlugin;
24 import org.eclipse.help.internal.webapp.data.UrlUtil;
25
26 /**
27  * This class inserts a CSSs for narrow and disabled CSSs when called from the
28  * dynamic help view.
29  */

30 public class InjectionFilter implements IFilter {
31     private static final String JavaDoc narrowBook1 = "\n<link rel=\"stylesheet\" HREF=\""; //$NON-NLS-1$
32

33     private static final String JavaDoc narrowBook2 = "narrow_book.css\" charset=\"ISO-8859-1\" type=\"text/css\">"; //$NON-NLS-1$
34

35     private static final String JavaDoc osNarrowBook2 = "_narrow_book.css\" charset=\"ISO-8859-1\" type=\"text/css\">"; //$NON-NLS-1$
36
private static final String JavaDoc disabledBook1 = "\n<link rel=\"stylesheet\" HREF=\""; //$NON-NLS-1$
37

38     private static final String JavaDoc disabledBook2 = "disabled_book.css\" charset=\"ISO-8859-1\" type=\"text/css\">"; //$NON-NLS-1$
39

40     private static final String JavaDoc disabledBook3 = "\n<script language=\"JavaScript\" SRC=\""; //$NON-NLS-1$
41

42     private static final String JavaDoc disabledBook4 = "livehelp.js\"> </script>"; //$NON-NLS-1$
43

44     /*
45      * @see IFilter#filter(HttpServletRequest, OutputStream)
46      */

47     public OutputStream JavaDoc filter(HttpServletRequest JavaDoc req, OutputStream JavaDoc out) {
48         // This filter only works inside the workbench
49
if (BaseHelpSystem.getMode() != BaseHelpSystem.MODE_WORKBENCH)
50             return out;
51
52         boolean addNarrow = false;
53         boolean addDisabled = false;
54         boolean needsLiveHelp = false;
55
56         String JavaDoc uri = req.getRequestURI();
57         if (uri == null || !uri.endsWith("html") && !uri.endsWith("htm")) { //$NON-NLS-1$ //$NON-NLS-2$
58
return out;
59         }
60         if (UrlUtil.isBot(req)) {
61             return out;
62         }
63         String JavaDoc pathInfo = req.getPathInfo();
64         if (pathInfo == null) {
65             return out;
66         }
67         boolean enabled = HelpBasePlugin.getActivitySupport().isRoleEnabled(
68                 pathInfo);
69         if ("/ntopic".equals(req.getServletPath())) //$NON-NLS-1$
70
addNarrow = true;
71         if (!enabled) {
72             addDisabled = true;
73         }
74         if (!addNarrow && !addDisabled)
75             return out;
76         
77         needsLiveHelp = HelpBasePlugin.getActivitySupport().getDocumentMessageUsesLiveHelp(addNarrow);
78
79         IPath path = new Path(pathInfo);
80         int upLevels = path.segmentCount() - 1;
81         StringBuffer JavaDoc script = new StringBuffer JavaDoc();
82         StringBuffer JavaDoc disabledContent = new StringBuffer JavaDoc();
83         //StringBuffer feedbackContent = new StringBuffer();
84
if (addNarrow) {
85             script.append(narrowBook1);
86             appendRelativePath(script, upLevels);
87             script.append(narrowBook2);
88             script.append(narrowBook1);
89             appendRelativePath(script, upLevels);
90             script.append(Platform.getOS());
91             script.append(osNarrowBook2);
92         }
93         if (addDisabled) {
94             script.append(disabledBook1);
95             appendRelativePath(script, upLevels);
96             script.append(disabledBook2);
97             if (needsLiveHelp) {
98                 script.append(disabledBook3);
99                 appendRelativePath(script, upLevels, "org.eclipse.help"); //$NON-NLS-1$
100
script.append(disabledBook4);
101             }
102             appendDisabled(disabledContent, upLevels, addNarrow);
103         }
104         try {
105             return new FilterHTMLHeadAndBodyOutputStream(
106                     out,
107                     script.toString().getBytes("ASCII"), addDisabled ? disabledContent.toString() : null); //$NON-NLS-1$
108
} catch (UnsupportedEncodingException JavaDoc uee) {
109             return out;
110         }
111     }
112
113     private void appendRelativePath(StringBuffer JavaDoc buff, int nsteps,
114             String JavaDoc pluginId) {
115         for (int i = 0; i < nsteps; i++) {
116             buff.append("../"); //$NON-NLS-1$
117
}
118         buff.append(pluginId + "/"); //$NON-NLS-1$
119
}
120
121     private void appendRelativePath(StringBuffer JavaDoc buff, int nsteps) {
122         appendRelativePath(buff, nsteps, "PRODUCT_PLUGIN"); //$NON-NLS-1$
123
}
124
125     private void appendDisabled(StringBuffer JavaDoc buff, int nsteps, boolean narrow) {
126         String JavaDoc message = HelpBasePlugin.getActivitySupport().getDocumentMessage(narrow);
127         if (message==null)
128             return;
129         buff.append("<div id=\"help-disabledTopic\">"); //$NON-NLS-1$
130
buff.append("<img SRC=\""); //$NON-NLS-1$
131
appendRelativePath(buff, nsteps, "org.eclipse.help.webapp"); //$NON-NLS-1$
132
buff.append("advanced/images/e_show_all.gif\" border=\"0\" align=\"bottom\">&nbsp;"); //$NON-NLS-1$
133
buff.append(message);
134         buff.append("<br><hr></div>"); //$NON-NLS-1$
135
}
136 }
137
Popular Tags