Source code: com/eireneh/bible/control/search/Engine.java
1
2 package com.eireneh.bible.control.search;
3
4 import java.util.*;
5
6 import com.eireneh.util.*;
7 import com.eireneh.bible.passage.*;
8 import com.eireneh.bible.book.*;
9 import com.eireneh.bible.control.dictionary.*;
10 import com.eireneh.bible.control.search.words.*;
11
12 /**
13 * The central interface to all searching.
14 * Functionality the I invisage includes:<ul>
15 * <li>A simple search syntax that goes something like this.<ul>
16 * <li>aaron, moses (verses containing aaron and moses. Can also use & or +)
17 * <li>aaron/moses (verses containing aaron or moses. Can also use |)
18 * <li>aaron - moses (verses containing aaron but not moses)
19 * <li>aaron ~5 , moses (verses with aaron within 5 verses of moses)
20 * <li>soundslike aaron (verses with words that sound like aaron. Can also use sl ...)
21 * <li>thesaurus happy (verses with words that mean happy. Can also use th ...)
22 * <li>grammar have (words like has have had and so on. Can also use gr ...)</ul>
23 * <li>The ability to add soundslike type extensions.</ul>
24 *
25 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
26 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
27 * Distribution Licence:<br />
28 * Project B is free software; you can redistribute it
29 * and/or modify it under the terms of the GNU General Public License,
30 * version 2 as published by the Free Software Foundation.<br />
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 * General Public License for more details.<br />
35 * The License is available on the internet
36 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
37 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
38 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
39 * The copyright to this program is held by it's authors.
40 * </font></td></tr></table>
41 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
42 * @see docs.Licence
43 * @author Joe Walker
44 */
45 public class Engine
46 {
47 /**
48 * Create a new search engine. Bible should probably be Book however
49 * Book does not yet have a well defined interface.
50 * @param bible The book to search
51 */
52 public Engine(Bible bible)
53 {
54 this.bible = bible;
55 this.commands = SearchDefault.getHashtable();
56 }
57
58 /**
59 * Create a new search engine. Bible should probably be Book however
60 * Book does not yet have a well defined interface.
61 * @param bible The book to search
62 * @param commands The commands to make available to the search engine
63 */
64 public Engine(Bible bible, Hashtable commands)
65 {
66 this.bible = bible;
67 this.commands = commands;
68 }
69
70 /**
71 * Take a search string and decipher it into a Passage.
72 * @param ref The Passage to alter
73 * @param sought The string to be searched for
74 * @return The matching verses
75 */
76 public Passage search(Passage ref, String sought) throws SearchException
77 {
78 output = CustomTokenizer.tokenize(sought, commands);
79 return search(ref, output);
80 }
81
82 /**
83 * Take a search string and decipher it into a Passage.
84 * @param sought The string to be searched for
85 * @return The matching verses
86 */
87 public Passage search(String sought) throws SearchException
88 {
89 output = CustomTokenizer.tokenize(sought, commands);
90 return search(output);
91 }
92
93 /**
94 * Take a search string and decipher it into a Passage.
95 * @param output The string to be searched for as a Vector of SearchWords
96 * @return The matching verses
97 */
98 public Passage search(Vector output) throws SearchException
99 {
100 Passage ref = PassageFactory.createPassage();
101
102 return search(ref, output);
103 }
104
105 /**
106 * Take a search string and decipher it into a Passage.
107 * @param ref The Passage to alter
108 * @param output The string to be searched for as a Vector of SearchWords
109 * @return The Passage passed in
110 */
111 public Passage search(Passage ref, Vector output) throws SearchException
112 {
113 // Check that there is a CommandWord first
114 if (!(output.elementAt(0) instanceof CommandWord))
115 {
116 // Add a default AddCommandWord if not
117 output.insertElementAt(new AddCommandWord(), 0);
118 }
119
120 en = output.elements();
121 while (en.hasMoreElements())
122 {
123 Object temp = en.nextElement();
124 try
125 {
126 CommandWord command = (CommandWord) temp;
127 command.updatePassage(this, ref);
128 }
129 catch (ClassCastException ex)
130 {
131 throw new SearchException("search_engine_syntax", new Object[] { temp });
132 }
133 }
134
135 // Set these to null so that people can't play around
136 // with them once they're done with, and to save memory.
137 output = null;
138 en = null;
139
140 return ref;
141 }
142
143 /**
144 * Accessor for the Bible to search.
145 * @return The current Bible
146 */
147 public Bible getBible()
148 {
149 return bible;
150 }
151
152 /**
153 * Accessor for the available SearchWords. This is probably
154 * the same as from Options.getSearchHashtable() but just in
155 * case anyone has been playing around with it...
156 * @return The SearchWord Hashtable
157 */
158 public Hashtable getSearchHashtable()
159 {
160 return commands;
161 }
162
163 /**
164 * Accessor for the whole array of SearchWords. I'm not sure
165 * why anyone would want this, but here it is anyway.
166 * @return The SearchWord Vector
167 */
168 public Vector getSearchWords()
169 {
170 return output;
171 }
172
173 /**
174 * Most SearchWords (Almost all except the DefaultParamWord) need
175 * to access parameters, this method allows them access to the
176 * Engine's own Enumerator. Use with care, and only if you are a
177 * SearchWord taking part in the current search.
178 * @return The current Enumerator
179 */
180 public Enumeration elements()
181 {
182 return en;
183 }
184
185 /** The book to search */
186 private Bible bible;
187
188 /** The parsed version of the current string */
189 private Vector output = null;
190
191 /** The commands that we know about */
192 private Hashtable commands = null;
193
194 /** While the answer is being worked out ... */
195 private Enumeration en = null;
196 }