001: /*
002: * Copyright 2002 Gareth Cronin
003: * This software is subject to the GNU Lesser General Public Licence (LGPL)
004: */
005: package vqwiki.servlets;
006:
007: import java.io.IOException;
008: import java.text.MessageFormat;
009: import java.util.Collection;
010: import java.util.Iterator;
011: import java.util.ResourceBundle;
012:
013: import javax.servlet.ServletException;
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletResponse;
016:
017: import org.apache.log4j.Logger;
018:
019: import vqwiki.SearchEngine;
020: import vqwiki.SearchResultEntry;
021: import vqwiki.WikiBase;
022: import vqwiki.utils.JSPUtils;
023:
024: public class SearchServlet extends VQWikiServlet {
025:
026: private static final Logger logger = Logger
027: .getLogger(SearchServlet.class);
028:
029: /**
030: *
031: */
032: protected void doPost(HttpServletRequest request,
033: HttpServletResponse response) throws ServletException,
034: IOException {
035: ResourceBundle messages = ResourceBundle.getBundle(
036: "ApplicationResources", request.getLocale());
037: MessageFormat formatter = new MessageFormat("");
038: formatter.setLocale(request.getLocale());
039: try {
040: String searchField = request.getParameter("text");
041: formatter.applyPattern(messages
042: .getString("searchresult.title"));
043: request.setAttribute("title", formatter
044: .format(new Object[] { searchField }));
045: // It's best to get the vwiki from the request, if it's there.
046: String virtualWiki = (String) request
047: .getAttribute("virtualWiki");
048: // forward back to the search page if the request is blank or null
049: if (searchField == null || "".equals(searchField)) {
050: redirect("Wiki?WikiSearch", response);
051: return;
052: }
053: // grab search engine instance and find
054: boolean fuzzy = false;
055: if (request.getParameter("fuzzy") != null)
056: fuzzy = true;
057: SearchEngine sedb = WikiBase.getInstance()
058: .getSearchEngineInstance();
059: Collection results = sedb.findMultiple(virtualWiki,
060: searchField, fuzzy);
061: StringBuffer contents = new StringBuffer();
062: if (results != null && results.size() > 0) {
063: Iterator it = results.iterator();
064: while (it.hasNext()) {
065: SearchResultEntry result = (SearchResultEntry) it
066: .next();
067: contents.append("<p>");
068: contents.append("<div class=\"searchresult\">");
069: contents.append("<a href=\"");
070: contents.append(JSPUtils.createLocalRootPath(
071: request, virtualWiki));
072: contents.append("Wiki?");
073: contents.append(result.getTopic());
074: if (result.getFoundWord().length() > 0) {
075: contents.append("&highlight=");
076: contents.append(JSPUtils.encodeURL(result
077: .getFoundWord()));
078: }
079: contents.append("\">" + result.getTopic() + "</a>");
080: contents.append("</div>");
081: if (result.getTextBefore().length() > 0
082: || result.getTextAfter().length() > 0
083: || result.getFoundWord().length() > 0) {
084: contents.append("<br>");
085: contents.append(result.getTextBefore());
086: contents
087: .append("<a style=\"background:yellow\" href=\"");
088: contents.append(JSPUtils.createLocalRootPath(
089: request, virtualWiki));
090: contents.append("Wiki?");
091: contents.append(result.getTopic());
092: contents.append("&highlight=");
093: contents.append(JSPUtils.encodeURL(result
094: .getFoundWord()));
095: contents.append("\">");
096: contents.append(result.getFoundWord());
097: contents.append("</a> ");
098: contents.append(result.getTextAfter());
099: }
100: contents.append("</p>");
101: }
102: } else {
103: contents.append("<p>");
104: formatter = new MessageFormat("");
105: formatter.setLocale(request.getLocale());
106: formatter.applyPattern(messages
107: .getString("searchresult.notfound"));
108: contents.append(formatter
109: .format(new Object[] { searchField }));
110: contents.append("</p>");
111: }
112: request.setAttribute("results", contents.toString());
113: request.setAttribute("titlelink", "Wiki?WikiSearch");
114: dispatch("/jsp/searchResults.jsp", request, response);
115: } catch (Exception err) {
116: logger.error(err);
117: err.printStackTrace();
118: throw new WikiServletException(err.toString());
119: }
120: }
121:
122: /**
123: *
124: */
125: protected void doGet(HttpServletRequest request,
126: HttpServletResponse response) throws ServletException,
127: IOException {
128: doPost(request, response);
129: }
130: }
|