/[webpac]/trunk2/out/js/search.js
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk2/out/js/search.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 496 - (show annotations)
Sun Oct 10 06:03:06 2004 UTC (19 years, 6 months ago) by dpavlin
File MIME type: application/javascript
File size: 9616 byte(s)
added support for Opera from jsFind 0.0.5

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
83 var debug_div = null;
84
85 function debug(msg)
86 {
87 // return; // Disable debugging
88
89 if (! debug_div) debug_div = document.getElementById('debug');
90
91 // this will create debug div if it doesn't exist.
92 if (! debug_div) {
93 debug_div = document.createElement('div');
94 document.body.appendChild(debug_div);
95 }
96 if (debug_div) {
97 debug_div.appendChild(document.createTextNode(msg));
98 debug_div.appendChild(document.createElement("br"));
99 }
100 }
101
102 // Convert a number into a base 62 alphanumeric number string
103 function convert(num)
104 {
105 var base = conversion.length;
106 var pow = 1;
107 var pos = 0;
108 var out = "";
109
110 if(num == 0)
111 {
112 return "0";
113 }
114
115 while (num > 0)
116 {
117 pos = num % base;
118 out = conversion.charAt(pos) + out;
119 num = Math.floor(num/base);
120 pow *= base;
121 }
122
123 return out;
124 }
125
126 function watchdog()
127 {
128 debug ("TIMEOUT!");
129 watchdog_callback(new Array());
130 }
131
132 var xmldoc;
133
134 // This function loads the XML document from the specified URL, and when
135 // it is fully loaded, passes that document and the url to the specified
136 // handler function. This function works with any XML document
137 function loadXML(url, handler, data, result_handler)
138 {
139 debug("loadXML("+url+","+data+")");
140
141 // Timeout operation in 10 seconds
142 watchdog_callback = result_handler;
143 watchdog_id=setTimeout("watchdog()", 20000);
144
145 debug("setTimeout = "+watchdog_id);
146
147 try
148 {
149 // Use the standard DOM Level 2 technique, if it is supported
150 if (document.implementation && document.implementation.createDocument)
151 {
152 // Create a new Document object
153 xmldoc = document.implementation.createDocument("", "", null);
154
155 // Specify what should happen when it finishes loading
156 xmldoc.onload = function() { handler(xmldoc, url, data, result_handler); }
157
158 //xmldoc.onerror = docError;
159 //xmldoc.addEventListener("load",docError,false);
160
161 // And tell it what URL to load
162 xmldoc.load(url);
163 return true;
164 }
165 // Otherwise use Microsoft's proprietary API for Internet Explorer
166 // Something about not following standards once again
167 else if (window.ActiveXObject)
168 {
169 xmldoc = new ActiveXObject("Microsoft.XMLDOM"); // Create doc.
170 if (! xmldoc) xmldoc = new ActiveXObject("MSXML2.DOMDocument"); // Create doc.
171 // Specify onload
172 xmldoc.onreadystatechange = function()
173 {
174 if (xmldoc.readyState == 4) handler(xmldoc, url, data, result_handler);
175 }
176 xmldoc.load(url); // Start loading!
177 return true;
178 }
179 // else fallback on usage of iframes to load xml (Opera 7.53 without Java and maybe old Mac browsers)
180 else {
181 debug("using iframe xml loader - experimental and slow");
182 if (! window.xml_iframe) {
183 debug("creating iframe");
184 window.xml_iframe = document.createElement('div');
185 window.xml_iframe.innerHTML = '<iframe src="'+url+'" name="xml_iframe" height="0" width="0" style="display: none;"></iframe>';
186 document.body.appendChild(window.xml_iframe);
187 } else {
188 debug("loading xml in existing iframe");
189 window.frames.xml_iframe.window.document.location.href = url;
190 }
191
192 // set timeout to re-check if iframe is loaded
193 window.iframe_timeout = window.setInterval('iframe_xml_loaded();',100);
194
195 // save some data for iframe_xml_loaded()
196 window.xml_handler = handler;
197 window.xml_url = url;
198 window.xml_data = data;
199 window.xml_result_handler = result_handler;
200 return true;
201 }
202 clearTimeout(watchdog_id);
203 debug("Browser incompatilibity: can't request XML document by one of supported methods");
204 return false;
205 }
206 catch(ex)
207 {
208 clearTimeout(watchdog_id);
209 debug("clearTimeout = "+watchdog_id);
210 debug ("CAUGHT EXCEPTION!");
211 result_handler(new Array());
212 return false;
213 }
214
215 return true;
216 }
217
218 function iframe_xml_loaded() {
219 debug("iframe_xmldoc_loaded");
220 if (! window.frames['xml_iframe']) return;
221 var xml = eval('window.frames.xml_iframe.window.document');
222 if (xml) {
223 clearTimeout(window.iframe_timeout);
224 debug("calling handler with ("+window.xml_url+","+window.xml_data+",...)");
225 window.xml_handler(window.frames.xml_iframe.window.document, window.xml_url, window.xml_data, window.xml_result_handler);
226 } else {
227 debug("can't eval iframe with xml");
228 }
229 }
230
231 function loadData(xmldoc, url, pos, result_handler)
232 {
233 clearTimeout(watchdog_id);
234 debug("clearTimeout = "+watchdog_id);
235
236 debug ("loadData("+url+","+pos+")");
237
238 var data = new Array();
239
240 // Get all entries
241 var entries = xmldoc.getElementsByTagName("e");
242
243 if(entries.length > pos)
244 {
245 // Get the links associated with this query
246 var links = entries[pos].getElementsByTagName("l");
247
248 // Dynamically append results to output
249 for(var i=0; i<links.length; i++)
250 {
251 data.push(new Result(links[i].getAttribute("t"),
252 links[i].firstChild.data,
253 links[i].getAttribute("f")));
254 }
255
256 intersect_results(data);
257
258 if(query_left.length > 0)
259 {
260 doSearch(index_path, query_left, result_handler);
261 }
262 else
263 {
264 results.sort(sortResults);
265 result_handler(results);
266 }
267 }
268 else
269 {
270 debug("INTERNAL ERROR, Inconsistent index");
271 search_err="INTERNAL ERROR, Inconsistent index";
272 }
273 }
274
275 function sortResults(a, b)
276 {
277 return a.frequency - b.frequency;
278 }
279
280 function traverseTree(xmldoc, url, query, result_handler)
281 {
282 clearTimeout(watchdog_id);
283 debug("clearTimeout = "+watchdog_id);
284
285 debug("traverseTree("+xmldoc+","+url+","+query+")");
286
287 var keys = xmldoc.getElementsByTagName("k");
288 var i;
289
290 for(i = 0; i < keys.length; i++)
291 {
292 var key = keys[i].firstChild.data;
293 debug("traverseTree: key="+key+" query="+query);
294 if(key != '' && key != null)
295 {
296 // Case where current key is greater than query, descend
297 if(key > query)
298 {
299 if(key != '' && key != null)
300 {
301 if(!loadXML(url.replace(".xml","/"+convert(i)+".xml"),
302 traverseTree,query,result_handler))
303 {
304 debug("Unable to locate key "+query);
305 result_handler(new Array());
306 }
307 // make sure of garbage collection
308 xmldoc=null;
309 return;
310 }
311 }
312 // Found it!
313 else if(key==query)
314 {
315 if(!loadXML(url.replace(/(\w+\.xml)/, "_$1"),
316 loadData, i, result_handler))
317 {
318 debug("ERROR: Unable to locate data "+query);
319 result_handler(new Array());
320 }
321 // make sure of garbage collection
322 xmldoc=null;
323 return;
324 }
325 }
326 }
327 // Look past the end...
328 if(keys.length == 0 || !loadXML(url.replace(".xml","/"+convert(i)+".xml"),
329 traverseTree,query,result_handler))
330 {
331 debug("Unable to locate key "+query);
332 result_handler(new Array());
333 }
334 // make sure of garbage collection
335 xmldoc=null;
336 return;
337 }
338
339 function doSearch(index_name,query, result_func)
340 {
341 //alert("doSearch("+index_name+","+query+")");
342 var pos=query.search(/[\s\+]/);
343 if (index_name) index_path = index_name+'/';
344
345 if(pos < 0)
346 {
347 query_left = "";
348 }
349 else
350 {
351 query_left = query.slice(pos+1);
352 query = query.slice(0,pos);
353 }
354
355 if(!loadXML(index_path+"0.xml", traverseTree, query.toLowerCase(), result_func))
356 {
357 debug("ERROR: Couldn't find main index 0.xml");
358 search_err = "INTERNAL ERROR: Unable to load main index 0.xml";
359 }
360 }

  ViewVC Help
Powered by ViewVC 1.1.26