001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki;
021:
022: import java.io.IOException;
023: import java.io.BufferedReader;
024: import java.io.StringReader;
025:
026: /**
027: * SearchMatcher performs the task of matching a search query to a page's
028: * contents. This utility class is isolated to simplify WikiPageProvider
029: * implementations and to offer an easy target for upgrades. The upcoming(?)
030: * TranslatorReader rewrite will presumably invalidate this, among other things.
031: *
032: * @since 2.1.5
033: * @author ebu at ecyrd dot com
034: */
035: // FIXME: Move to the "search" -package in 3.0
036: public class SearchMatcher {
037: private QueryItem[] m_queries;
038: private WikiEngine m_engine;
039:
040: /**
041: * Creates a new SearchMatcher.
042: *
043: * @param engine The WikiEngine
044: * @param queries A list of queries
045: */
046: public SearchMatcher(WikiEngine engine, QueryItem[] queries) {
047: m_engine = engine;
048: m_queries = queries;
049: }
050:
051: /**
052: * Compares the page content, available through the given stream, to the
053: * query items of this matcher. Returns a search result object describing
054: * the quality of the match.
055: *
056: * <p>This method would benefit of regexps (1.4) and streaming. FIXME!
057: *
058: * @param wikiname The name of the page
059: * @param pageText The content of the page
060: * @return A SearchResult item, or null, there are no queries
061: * @throws IOException If reading page content fails
062: */
063: public SearchResult matchPageContent(String wikiname,
064: String pageText) throws IOException {
065: if (m_queries == null) {
066: return null;
067: }
068:
069: int[] scores = new int[m_queries.length];
070: BufferedReader in = new BufferedReader(new StringReader(
071: pageText));
072: String line = null;
073:
074: while ((line = in.readLine()) != null) {
075: line = line.toLowerCase();
076:
077: for (int j = 0; j < m_queries.length; j++) {
078: int index = -1;
079:
080: while ((index = line.indexOf(m_queries[j].word,
081: index + 1)) != -1) {
082: if (m_queries[j].type != QueryItem.FORBIDDEN) {
083: scores[j]++; // Mark, found this word n times
084: } else {
085: // Found something that was forbidden.
086: return null;
087: }
088: }
089: }
090: }
091:
092: //
093: // Check that we have all required words.
094: //
095:
096: int totalscore = 0;
097:
098: for (int j = 0; j < scores.length; j++) {
099: // Give five points for each occurrence
100: // of the word in the wiki name.
101:
102: if (wikiname.toLowerCase().indexOf(m_queries[j].word) != -1
103: && m_queries[j].type != QueryItem.FORBIDDEN)
104: scores[j] += 5;
105:
106: // Filter out pages if the search word is marked 'required'
107: // but they have no score.
108:
109: if (m_queries[j].type == QueryItem.REQUIRED
110: && scores[j] == 0) {
111: return null;
112: }
113:
114: //
115: // Count the total score for this page.
116: //
117: totalscore += scores[j];
118: }
119:
120: if (totalscore > 0) {
121: return new SearchResultImpl(wikiname, totalscore);
122: }
123:
124: return null;
125: }
126:
127: /**
128: * A local search result.
129: *
130: */
131: public class SearchResultImpl implements SearchResult {
132: int m_score;
133: WikiPage m_page;
134:
135: /**
136: * Create a new SearchResult with a given name and a score.
137: *
138: * @param name Page Name
139: * @param score A score from 0+
140: */
141: public SearchResultImpl(String name, int score) {
142: m_page = new WikiPage(m_engine, name);
143: m_score = score;
144: }
145:
146: /**
147: * Returns Wikipage for this result.
148: * @return WikiPage
149: */
150: public WikiPage getPage() {
151: return m_page;
152: }
153:
154: /**
155: * Returns a score for this match.
156: *
157: * @return Score from 0+
158: */
159: public int getScore() {
160: return m_score;
161: }
162:
163: /**
164: * Returns an empty array, since BasicSearchProvider does not support
165: * context matching.
166: *
167: * @return an empty array
168: */
169: public String[] getContexts() {
170: // Unimplemented
171: return new String[0];
172: }
173: }
174:
175: }
|