01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.components;
04:
05: import fitnesse.wiki.WikiPage;
06:
07: public class Searcher implements FitNesseTraversalListener {
08: WikiPage root;
09:
10: SearchObserver observer;
11:
12: boolean isTitleSearch = false;
13:
14: private String searchString;
15:
16: public Searcher(String exp, WikiPage root) throws Exception {
17: searchString = exp.toLowerCase();
18: this .root = root;
19: }
20:
21: public void searchContent(SearchObserver observer) throws Exception {
22: search(observer);
23: }
24:
25: private void search(SearchObserver observer) throws Exception {
26: this .observer = observer;
27: processPage(root);
28: root.getPageCrawler().traverse(root, this );
29: }
30:
31: public void processPage(WikiPage currentPage) throws Exception {
32: if (isHit(currentPage))
33: observer.hit(currentPage);
34: }
35:
36: private boolean isHit(WikiPage page) throws Exception {
37: String content = page.getName().toLowerCase();
38: if (!isTitleSearch)
39: content = page.getData().getContent().toLowerCase();
40:
41: boolean matches = content.indexOf(searchString) != -1;
42: return matches;
43: }
44:
45: public String getSearchPattern() throws Exception {
46: return searchString;
47: }
48:
49: public void searchTitles(SearchObserver observer) throws Exception {
50: isTitleSearch = true;
51: search(observer);
52: }
53: }
|