1 11 package org.eclipse.help.internal.webapp.servlet; 12 13 import java.io.IOException ; 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Collections ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import javax.servlet.ServletException ; 21 import javax.servlet.http.HttpServlet ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.eclipse.core.runtime.NullProgressMonitor; 26 import org.eclipse.help.internal.base.BaseHelpSystem; 27 import org.eclipse.help.internal.search.ISearchHitCollector; 28 import org.eclipse.help.internal.search.ISearchQuery; 29 import org.eclipse.help.internal.search.SearchHit; 30 import org.eclipse.help.internal.search.SearchQuery; 31 import org.eclipse.help.internal.util.URLCoder; 32 import org.eclipse.help.internal.webapp.data.UrlUtil; 33 34 40 public class SearchServlet extends HttpServlet { 41 42 private static final long serialVersionUID = 1L; 43 private static final String PARAMETER_PHRASE = "phrase"; private Collection results = new ArrayList (); 45 private ISearchHitCollector collector = new ISearchHitCollector() { 46 public void addHits(List hits, String wordsSearched) { 47 if (results != null) { 48 results.addAll(hits); 49 } 50 } 51 }; 52 53 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 54 throws ServletException , IOException { 55 String locale = UrlUtil.getLocale(req, resp); 56 req.setCharacterEncoding("UTF-8"); resp.setContentType("application/xml; charset=UTF-8"); String phrase = URLCoder.decode(req.getParameter(PARAMETER_PHRASE)); 59 if (phrase != null) { 60 ISearchQuery query = new SearchQuery(phrase, false, Collections.EMPTY_LIST, locale); 61 results.clear(); 62 BaseHelpSystem.getSearchManager().search(query, collector, new NullProgressMonitor()); 63 String response = serialize(results); 64 resp.getWriter().write(response); 65 } 66 else { 67 resp.sendError(400); } 69 } 70 71 public static String serialize(Collection results) { 72 StringBuffer buf = new StringBuffer (); 73 buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); buf.append("<searchHits>\n"); Iterator iter = results.iterator(); 76 while (iter.hasNext()) { 77 SearchHit hit = (SearchHit)iter.next(); 78 serialize(hit, buf, " "); } 80 buf.append("</searchHits>\n"); return buf.toString(); 82 } 83 84 private static void serialize(SearchHit hit, StringBuffer buf, String indent) { 85 buf.append(indent + "<hit"); if (hit.getHref() != null) { 87 buf.append('\n' + indent + " HREF=\"" + XMLGenerator.xmlEscape(hit.getHref()) + '"'); } 89 if (hit.getLabel() != null) { 90 buf.append('\n' + indent + " label=\"" + XMLGenerator.xmlEscape(hit.getLabel()) + '"'); } 92 if (hit.isPotentialHit()) { 93 buf.append('\n' + indent + " isPotentialHit=\"true\""); } 95 buf.append('\n' + indent + " score=\"" + hit.getScore() + '"'); buf.append(">\n"); 98 String summary = hit.getSummary(); 99 if (summary != null) { 100 serialize(summary, buf, indent + " "); } 102 buf.append(indent + "</hit>\n"); } 104 105 private static void serialize(String summary, StringBuffer buf, String indent) { 106 buf.append(indent + "<summary>"); buf.append(XMLGenerator.xmlEscape(summary)); 108 buf.append("</summary>\n"); } 110 } 111 | Popular Tags |