1 |
/** |
2 |
search.js searchs an XML index to HTML files. |
3 |
|
4 |
A part of the jsfind project (http://projects.elucidsoft.net/jsfind) |
5 |
Copyright (C) 2003 Shawn Garbett |
6 |
|
7 |
This program is free software; you can redistribute it and/or |
8 |
modify it under the terms of the GNU General Public License |
9 |
as published by the Free Software Foundation; either version 2 |
10 |
of the License, or (at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with this program; if not, write to the Free Software |
19 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
20 |
|
21 |
Contact Info: |
22 |
Shawn Garbett <Shawn@eLucidSoft.net> |
23 |
http://www.elucidsoft.net |
24 |
4037 General Bate Drive |
25 |
Nashville, TN 37204 |
26 |
*/ |
27 |
|
28 |
// Constants |
29 |
var conversion = new String |
30 |
("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY"); |
31 |
|
32 |
// State variables |
33 |
var query_left = ""; |
34 |
var search_err = ""; |
35 |
var results = null; |
36 |
var index_path = ""; |
37 |
|
38 |
var watchdog_id = 0; |
39 |
var watchdog_callback = null; |
40 |
|
41 |
// Object to hold search results |
42 |
function Result(title, link, freq) |
43 |
{ |
44 |
this.title=title; |
45 |
this.link=link; |
46 |
this.frequency=Number(freq); |
47 |
} |
48 |
|
49 |
// Function to merge (intersect) two result sets |
50 |
function intersect_results(data) |
51 |
{ |
52 |
// If there are no stored results, then these are the results |
53 |
if(!results) |
54 |
{ |
55 |
results = data; |
56 |
return; |
57 |
} |
58 |
|
59 |
var output=new Array(); |
60 |
|
61 |
// There are existing results, to do an intersect... |
62 |
for(var i=0; i<results.length; i++) |
63 |
{ |
64 |
for(var j=0; j<data.length; j++) |
65 |
{ |
66 |
if(data[j].title == results[i].title) |
67 |
{ |
68 |
results[i].frequency += data[j].frequency; |
69 |
output.push(results[i]); |
70 |
break; |
71 |
} |
72 |
} |
73 |
} |
74 |
|
75 |
results = output; |
76 |
} |
77 |
|
78 |
/* |
79 |
From David Flanagan's, _Javascript:_The Definitive_Guide_, pg. 294-5, |
80 |
published by O'Reilly, 4th edition, 2002 |
81 |
*/ |
82 |
function debug(msg) |
83 |
{ |
84 |
// return; // Disable debugging |
85 |
|
86 |
var debug_div = element_id('debug'); |
87 |
|
88 |
if (debug_div) debug_div.innerHTML += msg+"<br/>\n"; |
89 |
} |
90 |
|
91 |
// |
92 |
|
93 |
// Convert a number into a base 62 alphanumeric number string |
94 |
function convert(num) |
95 |
{ |
96 |
var base = conversion.length; |
97 |
var pow = 1; |
98 |
var pos = 0; |
99 |
var out = ""; |
100 |
|
101 |
if(num == 0) |
102 |
{ |
103 |
return "0"; |
104 |
} |
105 |
|
106 |
while (num > 0) |
107 |
{ |
108 |
pos = num % base; |
109 |
out = conversion.charAt(pos) + out; |
110 |
num = Math.floor(num/base); |
111 |
pow *= base; |
112 |
} |
113 |
|
114 |
return out; |
115 |
} |
116 |
|
117 |
function watchdog() |
118 |
{ |
119 |
debug ("TIMEOUT!"); |
120 |
watchdog_callback(new Array()); |
121 |
} |
122 |
|
123 |
// This function loads the XML document from the specified URL, and when |
124 |
// it is fully loaded, passes that document and the url to the specified |
125 |
// handler function. This function works with any XML document |
126 |
function loadXML(url, handler, data, result_handler) |
127 |
{ |
128 |
debug("loadXML("+url+","+data+")"); |
129 |
|
130 |
// Timeout operation in 10 seconds |
131 |
watchdog_callback = result_handler; |
132 |
watchdog_id=setTimeout("watchdog()", 20000); |
133 |
|
134 |
debug("setTimeout = "+watchdog_id); |
135 |
|
136 |
try |
137 |
{ |
138 |
var xmldoc; |
139 |
// Use the standard DOM Level 2 technique, if it is supported |
140 |
if (document.implementation && document.implementation.createDocument) |
141 |
{ |
142 |
// Create a new Document object |
143 |
xmldoc = document.implementation.createDocument("", "", null); |
144 |
|
145 |
// Specify what should happen when it finishes loading |
146 |
xmldoc.onload = function() { handler(xmldoc, url, data, result_handler); } |
147 |
|
148 |
//xmldoc.onerror = docError; |
149 |
//xmldoc.addEventListener("load",docError,false); |
150 |
|
151 |
// And tell it what URL to load |
152 |
xmldoc.load(url); |
153 |
} |
154 |
// Otherwise use Microsoft's proprietary API for Internet Explorer |
155 |
// Something about not following standards once again |
156 |
else if (window.ActiveXObject) |
157 |
{ |
158 |
xmldoc = new ActiveXObject("Microsoft.XMLDOM"); // Create doc. |
159 |
if (! xmldoc) xmldoc = new ActiveXObject("MSXML2.DOMDocument"); // Create doc. |
160 |
// Specify onload |
161 |
xmldoc.onreadystatechange = function() |
162 |
{ |
163 |
if (xmldoc.readyState == 4) handler(xmldoc, url, data, result_handler); |
164 |
} |
165 |
xmldoc.load(url); // Start loading! |
166 |
} |
167 |
} |
168 |
catch(ex) |
169 |
{ |
170 |
clearTimeout(watchdog_id); |
171 |
debug("clearTimeout = "+watchdog_id); |
172 |
debug ("CAUGHT EXCEPTION!"); |
173 |
result_handler(new Array()); |
174 |
return false; |
175 |
} |
176 |
|
177 |
return true; |
178 |
} |
179 |
|
180 |
function loadData(xmldoc, url, pos, result_handler) |
181 |
{ |
182 |
clearTimeout(watchdog_id); |
183 |
debug("clearTimeout = "+watchdog_id); |
184 |
|
185 |
debug ("loadData("+url+","+pos+")"); |
186 |
|
187 |
var data = new Array(); |
188 |
|
189 |
// Get all entries |
190 |
var entries = xmldoc.getElementsByTagName("e"); |
191 |
|
192 |
if(entries.length > pos) |
193 |
{ |
194 |
// Get the links associated with this query |
195 |
var links = entries[pos].getElementsByTagName("l"); |
196 |
|
197 |
// Dynamically append results to output |
198 |
for(var i=0; i<links.length; i++) |
199 |
{ |
200 |
data.push(new Result(links[i].getAttribute("t"), |
201 |
links[i].firstChild.data, |
202 |
links[i].getAttribute("f"))); |
203 |
} |
204 |
|
205 |
intersect_results(data); |
206 |
|
207 |
if(query_left.length > 0) |
208 |
{ |
209 |
doSearch(index_path, query_left, result_handler); |
210 |
} |
211 |
else |
212 |
{ |
213 |
results.sort(sortResults); |
214 |
result_handler(results); |
215 |
} |
216 |
} |
217 |
else |
218 |
{ |
219 |
debug("INTERNAL ERROR, Inconsistent index"); |
220 |
search_err="INTERNAL ERROR, Inconsistent index"; |
221 |
} |
222 |
} |
223 |
|
224 |
function sortResults(a, b) |
225 |
{ |
226 |
return a.frequency - b.frequency; |
227 |
} |
228 |
|
229 |
function traverseTree(xmldoc, url, query, result_handler) |
230 |
{ |
231 |
clearTimeout(watchdog_id); |
232 |
debug("clearTimeout = "+watchdog_id); |
233 |
|
234 |
debug("traverseTree("+xmldoc+","+url+","+query+")"); |
235 |
|
236 |
var keys = xmldoc.getElementsByTagName("k"); |
237 |
var i; |
238 |
|
239 |
for(i = 0; i < keys.length; i++) |
240 |
{ |
241 |
var key = keys[i].firstChild.data; |
242 |
debug("traverseTree: key="+key+" query="+query); |
243 |
if(key != '' && key != null) |
244 |
{ |
245 |
// Case where current key is greater than query, descend |
246 |
if(key > query) |
247 |
{ |
248 |
if(key != '' && key != null) |
249 |
{ |
250 |
if(!loadXML(url.replace(".xml","/"+convert(i)+".xml"), |
251 |
traverseTree,query,result_handler)) |
252 |
{ |
253 |
debug("Unable to locate key "+query); |
254 |
result_handler(new Array()); |
255 |
} |
256 |
// make sure of garbage collection |
257 |
xmldoc=null; |
258 |
return; |
259 |
} |
260 |
} |
261 |
// Found it! |
262 |
else if(key==query) |
263 |
{ |
264 |
if(!loadXML(url.replace(/(\w+\.xml)/, "_$1"), |
265 |
loadData, i, result_handler)) |
266 |
{ |
267 |
debug("ERROR: Unable to locate data "+query); |
268 |
result_handler(new Array()); |
269 |
} |
270 |
// make sure of garbage collection |
271 |
xmldoc=null; |
272 |
return; |
273 |
} |
274 |
} |
275 |
} |
276 |
// Look past the end... |
277 |
if(keys.length == 0 || !loadXML(url.replace(".xml","/"+convert(i)+".xml"), |
278 |
traverseTree,query,result_handler)) |
279 |
{ |
280 |
debug("Unable to locate key "+query); |
281 |
result_handler(new Array()); |
282 |
} |
283 |
// make sure of garbage collection |
284 |
xmldoc=null; |
285 |
return; |
286 |
} |
287 |
|
288 |
function doSearch(index_name,query, result_func) |
289 |
{ |
290 |
//alert("doSearch("+index_name+","+query+")"); |
291 |
var pos=query.search(/[\s\+]/); |
292 |
if (index_name) index_path = index_name+'/'; |
293 |
|
294 |
if(pos < 0) |
295 |
{ |
296 |
query_left = ""; |
297 |
} |
298 |
else |
299 |
{ |
300 |
query_left = query.slice(pos+1); |
301 |
query = query.slice(0,pos); |
302 |
} |
303 |
|
304 |
if(!loadXML(index_path+"0.xml", traverseTree, query.toLowerCase(), result_func)) |
305 |
{ |
306 |
debug("ERROR: Couldn't find main index 0.xml"); |
307 |
search_err = "INTERNAL ERROR: Unable to load main index 0.xml"; |
308 |
} |
309 |
} |