1 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 HIGHLIGHT_ON = "highlight-on"; 25 private static final String scriptPart1 = "\n<script language=\"JavaScript\">\n<!--\nvar keywords = new Array ("; private static final String scriptPart2 = ");\nvar pluginDefault = "; private static final String scriptPart3 = ";\n-->\n</script>\n<script language=\"JavaScript\" SRC=\""; private static final String scriptPart5 = "advanced/highlight.js\"></script>\n"; 30 private static final String sheetRefPart1 = "<link id=\"highlightStyle\" rel=\"STYLESHEET\" HREF=\""; private static final String sheetRefPart3 = "advanced/highlight.css\" charset=\"ISO-8859-1\" type=\"text/css\"></link>\n"; 33 private static final String noHighlightScript1 = "<script language=\"JavaScript\">\n<!--\nif (parent.ContentToolbarFrame) parent.ContentToolbarFrame.setButtonState(\"toggle_highlight\",\"hidden\");\n-->\n</script>\n"; 37 public OutputStream filter(HttpServletRequest req, OutputStream out) { 38 String 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")); } 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 70 private byte[] createJScript(HttpServletRequest req, Collection keywords) { 71 StringBuffer buf = new StringBuffer (scriptPart1); 72 StringBuffer buf2 = new StringBuffer (sheetRefPart1); 73 Iterator it = keywords.iterator(); 75 if (!it.hasNext()) 76 return null; 77 String keyword = (String ) it.next(); 78 buf.append("\"").append(keyword).append("\""); while (it.hasNext()) { 80 keyword = (String ) it.next(); 81 buf.append(", \"").append(keyword).append("\""); } 83 buf.append(scriptPart2); 84 buf.append(HelpBasePlugin.getDefault().getPluginPreferences().getBoolean(HIGHLIGHT_ON)); 85 buf.append(scriptPart3); 86 String 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"); } catch (UnsupportedEncodingException uee) { 97 return new byte[0]; 98 } 99 } 100 101 private String getPathLength(HttpServletRequest req) { 102 StringBuffer result = new StringBuffer (""); String path = req.getPathInfo(); 105 if (path != null) { 106 for (int i; 0 <= (i = path.indexOf('/')); path = path 107 .substring(i + 1)) { 108 result.append("../"); } 110 } 111 return result.toString(); 112 } 113 119 private Collection getWords(HttpServletRequest req) { 120 Collection tokens = new ArrayList(); 122 123 String searchWord = req.getParameter("resultof"); if (searchWord == null) { 125 return tokens; 126 } 127 StringTokenizer qTokenizer = new StringTokenizer(searchWord.trim(), 129 "\"", true); boolean withinQuotation = false; 131 String quotedString = ""; while (qTokenizer.hasMoreTokens()) { 133 String curToken = qTokenizer.nextToken(); 134 if (curToken.equals("\"")) { if (!withinQuotation) { 136 quotedString = ""; } else { 139 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 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 word = (String ) 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 178 private Collection removeWildCards(Collection col) { 179 if (col == null) 180 return col; 181 182 Collection resultPass1 = new ArrayList(); 184 for (Iterator it = col.iterator(); it.hasNext();) { 185 String word = (String ) it.next(); 186 int index; 187 while ((index = word.indexOf("*")) >= 0) { 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 Collection resultPass2 = new ArrayList(); 199 for (Iterator it = resultPass1.iterator(); it.hasNext();) { 200 String word = (String ) it.next(); 201 int index; 202 while ((index = word.indexOf("?")) >= 0) { 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 |