35 |
var results = null; |
var results = null; |
36 |
var index_path = ""; |
var index_path = ""; |
37 |
|
|
38 |
|
// wildcard data about found keys URLs and position |
39 |
|
var wildcard_url_pos = new Array(); |
40 |
|
|
41 |
var watchdog_id = 0; |
var watchdog_id = 0; |
42 |
var watchdog_callback = null; |
var watchdog_callback = null; |
43 |
|
|
72 |
results = output; |
results = output; |
73 |
} |
} |
74 |
|
|
|
/* |
|
|
From David Flanagan's, _Javascript:_The Definitive_Guide_, pg. 294-5, |
|
|
published by O'Reilly, 4th edition, 2002 |
|
|
*/ |
|
|
|
|
75 |
var debug_div = null; |
var debug_div = null; |
76 |
|
|
77 |
function debug(msg) { |
function debug(msg) { |
129 |
watchdog_callback = result_handler; |
watchdog_callback = result_handler; |
130 |
watchdog_id=setTimeout("watchdog()", 20000); |
watchdog_id=setTimeout("watchdog()", 20000); |
131 |
|
|
132 |
debug("setTimeout = "+watchdog_id); |
//debug("setTimeout = "+watchdog_id); |
133 |
|
|
134 |
try { |
try { |
135 |
// Use the standard DOM Level 2 technique, if it is supported |
// Use the standard DOM Level 2 technique, if it is supported |
190 |
|
|
191 |
catch(ex) { |
catch(ex) { |
192 |
clearTimeout(watchdog_id); |
clearTimeout(watchdog_id); |
193 |
debug("clearTimeout = "+watchdog_id); |
//debug("clearTimeout = "+watchdog_id); |
194 |
debug ("CAUGHT EXCEPTION!"); |
debug ("CAUGHT EXCEPTION!"); |
195 |
result_handler(new Array()); |
result_handler(new Array()); |
196 |
return false; |
return false; |
216 |
|
|
217 |
function loadData_intersect(xmldoc, url, pos, result_handler) { |
function loadData_intersect(xmldoc, url, pos, result_handler) { |
218 |
data = new Array(); |
data = new Array(); |
219 |
if (loadData(xmldoc, url, pos, result_handler)) { |
if (loadData(xmldoc, url, pos)) { |
220 |
intersect_results(data); |
intersect_results(data); |
221 |
search_query_left(result_handler); |
search_query_left(result_handler); |
222 |
} else { |
} else { |
225 |
} |
} |
226 |
} |
} |
227 |
|
|
228 |
function loadData(xmldoc, url, pos, result_handler) { |
function loadData(xmldoc, url, pos) { |
229 |
|
|
230 |
clearTimeout(watchdog_id); |
clearTimeout(watchdog_id); |
231 |
debug("clearTimeout = "+watchdog_id); |
//debug("clearTimeout = "+watchdog_id); |
232 |
|
|
233 |
debug ("loadData("+url+","+pos+")"); |
debug ("loadData("+url+","+pos+")"); |
234 |
|
|
272 |
return a.frequency - b.frequency; |
return a.frequency - b.frequency; |
273 |
} |
} |
274 |
|
|
275 |
|
function end_traverseTree(wildcard,query,result_handler) { |
276 |
|
if (! wildcard) { |
277 |
|
debug("Unable to locate key "+query); |
278 |
|
result_handler(new Array()); |
279 |
|
} else { |
280 |
|
debug("wildcard "+query+" produced "+(wildcard_url_pos.length / 2)+" results: "+wildcard_url_pos.join(" ")); |
281 |
|
} |
282 |
|
|
283 |
|
} |
284 |
|
|
285 |
function traverseTree(xmldoc, url, query, result_handler) { |
function traverseTree(xmldoc, url, query, result_handler) { |
286 |
clearTimeout(watchdog_id); |
clearTimeout(watchdog_id); |
287 |
debug("clearTimeout = "+watchdog_id); |
//debug("clearTimeout = "+watchdog_id); |
288 |
|
|
289 |
debug("traverseTree("+xmldoc+","+url+","+query+")"); |
debug("traverseTree("+xmldoc+","+url+","+query+")"); |
290 |
|
|
304 |
for(i = 0; i < keys.length; i++) { |
for(i = 0; i < keys.length; i++) { |
305 |
var key = keys[i].firstChild.data; |
var key = keys[i].firstChild.data; |
306 |
|
|
307 |
if (wildcard) { |
if (wildcard) key = key.substr(0,qlen); |
|
key = key.substr(0,qlen); |
|
|
debug("wildcard key "+key+"*"); |
|
|
} |
|
308 |
|
|
309 |
debug("traverseTree: "+key+"=="+query); |
debug("? "+key+" -- "+query); |
310 |
|
|
311 |
if (key != '' && key != null) { |
if (key != '' && key != null) { |
312 |
// Case where current key is greater than query, descend |
// Case where current key is greater than query, descend |
313 |
if (key > query) { |
if (key > query) { |
314 |
if (key != '' && key != null) { |
if (! loadXML(url.replace(".xml","/"+convert(i)+".xml"), traverseTree, query_full, result_handler)) { |
315 |
if (! loadXML(url.replace(".xml","/"+convert(i)+".xml"), traverseTree,query_full,result_handler)) { |
end_traverseTree(wildcard, query_full, result_handler); |
|
debug("Unable to locate key "+query); |
|
|
result_handler(new Array()); |
|
|
} |
|
|
// make sure of garbage collection |
|
|
xmldoc=null; |
|
|
return; |
|
316 |
} |
} |
317 |
|
// make sure of garbage collection |
318 |
|
xmldoc=null; |
319 |
|
return; |
320 |
} |
} |
321 |
// Found it! |
// Found it! |
322 |
else if (key==query) { |
else if (key==query) { |
323 |
if (wildcard) { |
if (wildcard) { |
324 |
data = new Array(); |
wildcard_url_pos.push(url.replace(/(\w+\.xml)/, "_$1")); |
325 |
while (xmldoc && keys[i].firstChild.data.substr(0,qlen) == query) { |
wildcard_url_pos.push(i); |
326 |
var url2 = url; |
debug("+"+i+": "+keys[i].firstChild.data); |
|
if (loadXML(url2.replace(/(\w+\.xml)/, "_$1"), loadData, i, result_handler)) { |
|
|
// add loaded data to results |
|
|
if (! results) { |
|
|
results = data; |
|
|
debug("wildcard loop: "+i+" created "+data.length+" results"); |
|
|
} else { |
|
|
for (var j=0; j<data.length; j++) { |
|
|
results.push(data[j]); |
|
|
} |
|
|
debug("wildcard loop: "+i+" added "+j+" results"); |
|
|
} |
|
|
|
|
|
} else { |
|
|
// this will break out of loop |
|
|
xmldoc = null; |
|
|
debug("wildcard loop stop at "+i); |
|
|
} |
|
|
i++; |
|
|
} |
|
|
debug("wildcard results: "+results.length); |
|
|
search_query_left(result_handler); |
|
|
return; |
|
327 |
} else { |
} else { |
328 |
// exact match |
// exact match |
329 |
if (! loadXML(url.replace(/(\w+\.xml)/, "_$1"), loadData_intersect, i, result_handler)) { |
if (! loadXML(url.replace(/(\w+\.xml)/, "_$1"), loadData_intersect, i, result_handler)) { |
330 |
debug("ERROR: Unable to locate data "+query); |
debug("ERROR: Unable to locate data "+query_full); |
331 |
result_handler(new Array()); |
result_handler(new Array()); |
332 |
} |
} |
333 |
// make sure of garbage collection |
// make sure of garbage collection |
339 |
} // for |
} // for |
340 |
|
|
341 |
// Look past the end... |
// Look past the end... |
342 |
if (wildcard) query += '*'; |
if (keys.length == 0 || !loadXML(url.replace(".xml","/"+convert(i)+".xml"), traverseTree, query_full, result_handler)) { |
343 |
|
end_traverseTree(wildcard, query_full, result_handler); |
|
if (keys.length == 0 || !loadXML(url.replace(".xml","/"+convert(i)+".xml"), traverseTree,query,result_handler)) { |
|
|
debug("Unable to locate key "+query); |
|
|
result_handler(new Array()); |
|
344 |
} |
} |
345 |
|
|
346 |
// make sure of garbage collection |
// make sure of garbage collection |