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 |
if(!debug.box) |
87 |
{ |
88 |
debug.box = document.createElement("div"); |
89 |
debug.box.setAttribute("style", |
90 |
"background-color:white" + |
91 |
"font-family: monospace; " + |
92 |
"border: solid black 3px; "+ |
93 |
"padding: 10px;"); |
94 |
|
95 |
document.body.appendChild(debug.box); |
96 |
debug.box.innerHTML = "<h1 style='test-align:cent'>Debugging Output</h1>"; |
97 |
} |
98 |
|
99 |
var p = document.createElement("p"); |
100 |
p.appendChild(document.createTextNode(msg)); |
101 |
debug.box.appendChild(p); |
102 |
} |
103 |
|
104 |
// |
105 |
|
106 |
// Convert a number into a base 62 alphanumeric number string |
107 |
function convert(num) |
108 |
{ |
109 |
var base = conversion.length; |
110 |
var pow = 1; |
111 |
var pos = 0; |
112 |
var out = ""; |
113 |
|
114 |
if(num == 0) |
115 |
{ |
116 |
return "0"; |
117 |
} |
118 |
|
119 |
while (num > 0) |
120 |
{ |
121 |
pos = num % base; |
122 |
out = conversion.charAt(pos) + out; |
123 |
num = Math.floor(num/base); |
124 |
pow *= base; |
125 |
} |
126 |
|
127 |
return out; |
128 |
} |
129 |
|
130 |
function watchdog() |
131 |
{ |
132 |
debug ("TIMEOUT!"); |
133 |
watchdog_callback(new Array()); |
134 |
} |
135 |
|
136 |
// This function loads the XML document from the specified URL, and when |
137 |
// it is fully loaded, passes that document and the url to the specified |
138 |
// handler function. This function works with any XML document |
139 |
function loadXML(url, handler, data, result_handler) |
140 |
{ |
141 |
debug("loadXML("+url+","+data+")"); |
142 |
|
143 |
// Timeout operation in 10 seconds |
144 |
watchdog_callback = result_handler; |
145 |
watchdog_id=setTimeout("watchdog()", 20000); |
146 |
|
147 |
debug("setTimeout = "+watchdog_id); |
148 |
|
149 |
try |
150 |
{ |
151 |
// Use the standard DOM Level 2 technique, if it is supported |
152 |
if (document.implementation && document.implementation.createDocument) |
153 |
{ |
154 |
// Create a new Document object |
155 |
var xmldoc = document.implementation.createDocument("", "", null); |
156 |
|
157 |
// Specify what should happen when it finishes loading |
158 |
xmldoc.onload = function() { handler(xmldoc, url, data, result_handler); } |
159 |
|
160 |
//xmldoc.onerror = docError; |
161 |
//xmldoc.addEventListener("load",docError,false); |
162 |
|
163 |
// And tell it what URL to load |
164 |
xmldoc.load(url); |
165 |
} |
166 |
// Otherwise use Microsoft's proprietary API for Internet Explorer |
167 |
// Something about not following standards once again |
168 |
else if (window.ActiveXObject) |
169 |
{ |
170 |
//var xmldoc = new ActiveXObject("MSXML2.DOMDocument"); // Create doc. |
171 |
var xmldoc = new ActiveXObject("Microsoft.XMLDOM"); // Create doc. |
172 |
// Specify onload |
173 |
xmldoc.onreadystatechange = function() |
174 |
{ |
175 |
if (xmldoc.readyState == 4) handler(xmldoc, url, data, result_handler); |
176 |
} |
177 |
xmldoc.load(url); // Start loading! |
178 |
} |
179 |
} |
180 |
catch(ex) |
181 |
{ |
182 |
clearTimeout(watchdog_id); |
183 |
debug("clearTimeout = "+watchdog_id); |
184 |
debug ("CAUGHT EXCEPTION!"); |
185 |
result_handler(new Array()); |
186 |
return false; |
187 |
} |
188 |
|
189 |
return true; |
190 |
} |
191 |
|
192 |
function loadData(xmldoc, url, pos, result_handler) |
193 |
{ |
194 |
clearTimeout(watchdog_id); |
195 |
debug("clearTimeout = "+watchdog_id); |
196 |
|
197 |
debug ("loadData("+url+","+pos+")"); |
198 |
|
199 |
var data = new Array(); |
200 |
|
201 |
// Get all entries |
202 |
var entries = xmldoc.getElementsByTagName("e"); |
203 |
|
204 |
if(entries.length > pos) |
205 |
{ |
206 |
// Get the links associated with this query |
207 |
var links = entries[pos].getElementsByTagName("l"); |
208 |
|
209 |
// Dynamically append results to output |
210 |
for(var i=0; i<links.length; i++) |
211 |
{ |
212 |
data.push(new Result(links[i].getAttribute("t"), |
213 |
links[i].firstChild.data, |
214 |
links[i].getAttribute("f"))); |
215 |
} |
216 |
|
217 |
intersect_results(data); |
218 |
|
219 |
if(query_left.length > 0) |
220 |
{ |
221 |
doSearch(index_path, query_left, result_handler); |
222 |
} |
223 |
else |
224 |
{ |
225 |
results.sort(sortResults); |
226 |
result_handler(results); |
227 |
} |
228 |
} |
229 |
else |
230 |
{ |
231 |
debug("INTERNAL ERROR, Inconsistent index"); |
232 |
search_err="INTERNAL ERROR, Inconsistent index"; |
233 |
} |
234 |
} |
235 |
|
236 |
function sortResults(a, b) |
237 |
{ |
238 |
return a.frequency - b.frequency; |
239 |
} |
240 |
|
241 |
function traverseTree(xmldoc, url, query, result_handler) |
242 |
{ |
243 |
clearTimeout(watchdog_id); |
244 |
debug("clearTimeout = "+watchdog_id); |
245 |
|
246 |
debug("traverseTree("+xmldoc+","+url+","+query+")"); |
247 |
|
248 |
var keys = xmldoc.getElementsByTagName("k"); |
249 |
var i; |
250 |
|
251 |
for(i = 0; i < keys.length; i++) |
252 |
{ |
253 |
var key = keys[i].firstChild.data; |
254 |
debug("traverseTree: key="+key+" query="+query); |
255 |
if(key != '' && key != null) |
256 |
{ |
257 |
// Case where current key is greater than query, descend |
258 |
if(key > query) |
259 |
{ |
260 |
if(key != '' && key != null) |
261 |
{ |
262 |
if(!loadXML(url.replace(".xml","/"+convert(i)+".xml"), |
263 |
traverseTree,query,result_handler)) |
264 |
{ |
265 |
debug("Unable to locate key "+query); |
266 |
result_handler(new Array()); |
267 |
} |
268 |
// make sure of garbage collection |
269 |
xmldoc=null; |
270 |
return; |
271 |
} |
272 |
} |
273 |
// Found it! |
274 |
else if(key==query) |
275 |
{ |
276 |
if(!loadXML(url.replace(/(\w+\.xml)/, "_$1"), |
277 |
loadData, i, result_handler)) |
278 |
{ |
279 |
debug("ERROR: Unable to locate data "+query); |
280 |
result_handler(new Array()); |
281 |
} |
282 |
// make sure of garbage collection |
283 |
xmldoc=null; |
284 |
return; |
285 |
} |
286 |
} |
287 |
} |
288 |
// Look past the end... |
289 |
if(keys.length == 0 || !loadXML(url.replace(".xml","/"+convert(i)+".xml"), |
290 |
traverseTree,query,result_handler)) |
291 |
{ |
292 |
debug("Unable to locate key "+query); |
293 |
result_handler(new Array()); |
294 |
} |
295 |
// make sure of garbage collection |
296 |
xmldoc=null; |
297 |
return; |
298 |
} |
299 |
|
300 |
function doSearch(index_name,query, result_func) |
301 |
{ |
302 |
//alert("doSearch("+index_name+","+query+")"); |
303 |
var pos=query.search(/[\s\+]/); |
304 |
if (index_name) index_path = index_name+'/'; |
305 |
|
306 |
if(pos < 0) |
307 |
{ |
308 |
query_left = ""; |
309 |
} |
310 |
else |
311 |
{ |
312 |
query_left = query.slice(pos+1); |
313 |
query = query.slice(0,pos); |
314 |
} |
315 |
|
316 |
if(!loadXML(index_path+"0.xml", traverseTree, query.toLowerCase(), result_func)) |
317 |
{ |
318 |
debug("ERROR: Couldn't find main index 0.xml"); |
319 |
search_err = "INTERNAL ERROR: Unable to load main index 0.xml"; |
320 |
} |
321 |
} |