KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
15 import java.util.*;
16
17 import javax.servlet.http.*;
18
19 import org.eclipse.help.internal.base.HelpBasePlugin;
20 import org.eclipse.help.internal.webapp.data.*;
21
22 public class HighlightFilter implements IFilter {
23     private static final String JavaDoc HIGHLIGHT_ON = "highlight-on"; //$NON-NLS-1$
24

25     private static final String JavaDoc scriptPart1 = "\n<script language=\"JavaScript\">\n<!--\nvar keywords = new Array ("; //$NON-NLS-1$
26
private static final String JavaDoc scriptPart2 = ");\nvar pluginDefault = "; //$NON-NLS-1$
27
private static final String JavaDoc scriptPart3 = ";\n-->\n</script>\n<script language=\"JavaScript\" SRC=\""; //$NON-NLS-1$
28
private static final String JavaDoc scriptPart5 = "advanced/highlight.js\"></script>\n"; //$NON-NLS-1$
29

30     private static final String JavaDoc sheetRefPart1 = "<link id=\"highlightStyle\" rel=\"STYLESHEET\" HREF=\""; //$NON-NLS-1$
31
private static final String JavaDoc sheetRefPart3 = "advanced/highlight.css\" charset=\"ISO-8859-1\" type=\"text/css\"></link>\n"; //$NON-NLS-1$
32

33     private static final String JavaDoc noHighlightScript1 = "<script language=\"JavaScript\">\n<!--\nif (parent.ContentToolbarFrame) parent.ContentToolbarFrame.setButtonState(\"toggle_highlight\",\"hidden\");\n-->\n</script>\n"; //$NON-NLS-1$
34
/*
35      * @see IFilter#filter(HttpServletRequest, OutputStream)
36      */

37     public OutputStream filter(HttpServletRequest req, OutputStream out) {
38         String JavaDoc uri = req.getRequestURI();
39         if (uri == null) {
40             return out;
41         }
42         if (!(UrlUtil.isIE(req) || UrlUtil.isMozilla(req))) {
43             return out;
44         }
45
46         Collection keywords = getWords(req);
47         if (keywords.size() == 0) {
48             try {
49                 return new FilterHTMLHeadOutputStream(out, noHighlightScript1.getBytes("ASCII")); //$NON-NLS-1$
50
} catch (UnsupportedEncodingException uee) {
51                 return out;
52             }
53         }
54         keywords = removeWildCards(keywords);
55         keywords = encodeKeyWords(keywords);
56         byte[] script = createJScript(req, keywords);
57         if (script == null) {
58             return out;
59         }
60
61         return new FilterHTMLHeadOutputStream(out, script);
62     }
63
64     /**
65      * Creates Java Script that does highlighting
66      *
67      * @param keywords
68      * @return byte[]
69      */

70     private byte[] createJScript(HttpServletRequest req, Collection keywords) {
71         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(scriptPart1);
72         StringBuffer JavaDoc buf2 = new StringBuffer JavaDoc(sheetRefPart1);
73         // append comma separated list of keywords
74
Iterator it = keywords.iterator();
75         if (!it.hasNext())
76             return null;
77         String JavaDoc keyword = (String JavaDoc) it.next();
78         buf.append("\"").append(keyword).append("\""); //$NON-NLS-1$ //$NON-NLS-2$
79
while (it.hasNext()) {
80             keyword = (String JavaDoc) it.next();
81             buf.append(", \"").append(keyword).append("\""); //$NON-NLS-1$ //$NON-NLS-2$
82
}
83         buf.append(scriptPart2);
84         buf.append(HelpBasePlugin.getDefault().getPluginPreferences().getBoolean(HIGHLIGHT_ON));
85         buf.append(scriptPart3);
86         // append "../" to get to the webapp
87
String JavaDoc path = getPathLength(req);
88         buf.append(path);
89         buf2.append(path);
90         
91         buf.append(scriptPart5);
92         buf.append(buf2.toString());
93         buf.append(sheetRefPart3);
94         try {
95             return buf.toString().getBytes("ASCII"); //$NON-NLS-1$
96
} catch (UnsupportedEncodingException uee) {
97             return new byte[0];
98         }
99     }
100     
101     private String JavaDoc getPathLength(HttpServletRequest req) {
102         // append "../" to get to the webapp
103
StringBuffer JavaDoc result = new StringBuffer JavaDoc(""); //$NON-NLS-1$
104
String JavaDoc path = req.getPathInfo();
105         if (path != null) {
106             for (int i; 0 <= (i = path.indexOf('/')); path = path
107                     .substring(i + 1)) {
108                 result.append("../"); //$NON-NLS-1$
109
}
110         }
111         return result.toString();
112     }
113     /**
114      * Extracts keywords from query that contains keywords dobule quoted and
115      * separated by space
116      *
117      * @return Collection of String
118      */

119     private Collection getWords(HttpServletRequest req) {
120         // Collect words to hash set to eliminate duplcates
121
Collection tokens = new ArrayList();
122
123         String JavaDoc searchWord = req.getParameter("resultof"); //$NON-NLS-1$
124
if (searchWord == null) {
125             return tokens;
126         }
127         //Divide along quotation marks
128
StringTokenizer qTokenizer = new StringTokenizer(searchWord.trim(),
129                 "\"", true); //$NON-NLS-1$
130
boolean withinQuotation = false;
131         String JavaDoc quotedString = ""; //$NON-NLS-1$
132
while (qTokenizer.hasMoreTokens()) {
133             String JavaDoc curToken = qTokenizer.nextToken();
134             if (curToken.equals("\"")) { //$NON-NLS-1$
135
if (!withinQuotation) {
136                     //beginning of quoted string
137
quotedString = ""; //$NON-NLS-1$
138
} else {
139                     //end of quoted string
140
tokens.add(quotedString);
141                 }
142                 withinQuotation = !withinQuotation;
143                 continue;
144             }
145             if (withinQuotation) {
146                 tokens.add(curToken);
147             }
148         }
149
150         return tokens;
151
152     }
153     /**
154      * Encodes strings inside collection for embedding in HTML source
155      *
156      * @return Collection of String
157      */

158     private Collection encodeKeyWords(Collection col) {
159         if (col == null)
160             return col;
161         Collection result = new ArrayList();
162         for (Iterator it = col.iterator(); it.hasNext();) {
163             String JavaDoc word = (String JavaDoc) it.next();
164             int l = word.length();
165             if (l < 1)
166                 continue;
167             result.add(UrlUtil.JavaScriptEncode(word));
168         }
169         return result;
170     }
171
172     /**
173      * Removes wildcard characters from words, by splitting words around wild
174      * cards
175      *
176      * @return Collection of String
177      */

178     private Collection removeWildCards(Collection col) {
179         if (col == null)
180             return col;
181
182         // Split words into parts: before "*" and after "*"
183
Collection resultPass1 = new ArrayList();
184         for (Iterator it = col.iterator(); it.hasNext();) {
185             String JavaDoc word = (String JavaDoc) it.next();
186             int index;
187             while ((index = word.indexOf("*")) >= 0) { //$NON-NLS-1$
188
if (index > 0)
189                     resultPass1.add(word.substring(0, index));
190                 if (word.length() > index)
191                     word = word.substring(index + 1);
192             }
193             if (word.length() > 0)
194                 resultPass1.add(word);
195         }
196
197         // Split words into parts: before "?" and after "?"
198
Collection resultPass2 = new ArrayList();
199         for (Iterator it = resultPass1.iterator(); it.hasNext();) {
200             String JavaDoc word = (String JavaDoc) it.next();
201             int index;
202             while ((index = word.indexOf("?")) >= 0) { //$NON-NLS-1$
203
if (index > 0)
204                     resultPass2.add(word.substring(0, index));
205                 if (word.length() > index)
206                     word = word.substring(index + 1);
207             }
208             if (word.length() > 0)
209                 resultPass2.add(word);
210         }
211
212         return resultPass2;
213     }
214 }
215
Popular Tags