/[jsFind]/trunk/html/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 /trunk/html/js/search.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (show annotations)
Thu Oct 7 22:41:16 2004 UTC (19 years, 8 months ago) by dpavlin
File MIME type: application/javascript
File size: 7805 byte(s)
create debug with appendChild, great speedup for test

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

  ViewVC Help
Powered by ViewVC 1.1.26