source: trunk/gsdl/src/phind/client/ResultBox.java@ 1636

Last change on this file since 1636 was 1635, checked in by paynter, 24 years ago

Read phrases in prefix/body/suffix form for formatted display.

  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 KB
Line 
1/**********************************************************************
2 *
3 * ResultBox.java -- a list of phrases in the Phind java interface
4 *
5 * Copyright 1997-2000 Gordon W. Paynter
6 * Copyright 2000 The New Zealand Digital Library Project
7 *
8 * A component of the Greenstone digital library software
9 * from the New Zealand Digital Library Project at the
10 * University of Waikato, New Zealand.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *********************************************************************/
27
28/*********************************************************************
29
30This class is used in the Phind java applet (Phind.java).
31
32A ResultBox holds the results of a query to phindcgi. They deal mostly
33with the information content of the query, and have methods for parsing the
34input into phrase and document items. They have little do with display:
35ResultBoxes are shown to the user through "ResultDisplay" panels, and are
36drawn using "ResultCanvas" objects.
37
38**********************************************************************/
39
40
41import java.awt.Panel;
42import java.awt.BorderLayout;
43import java.awt.Scrollbar;
44
45import java.awt.Label;
46import java.awt.Event;
47
48import java.net.*;
49import java.applet.*;
50
51import java.util.Vector;
52
53
54public class ResultBox extends Panel {
55
56 // Objects at a "higher" level than this one
57 Phind phind;
58 ResultDisplay display;
59
60 // Surroundig objects
61 ResultBox prev, next;
62
63 // The components contained by this one.
64 ResultCanvas c;
65 ResultTitle t;
66 Scrollbar s;
67 Panel label;
68
69 // The key identifying the phrase displayed, with its text and the
70 // collection from which it is drawn
71 String searchKey, searchPhrase, searchCollection;
72
73 // The total frequency, expansion frequency, and document frequency
74 // of the phrase
75 int numberOfOccurances;
76 int numberOfExpansions;
77 int numberOfDocuments;
78
79 // The number of phrases and documents retrieved, and the number of
80 // times the user has requested more phrases or documents.
81 int documentsRetrieved;
82 int expansionsRetrieved;
83 int nextPhraseBlock;
84 int nextDocumentBlock;
85
86 // just why did i add these variables?
87 int mode;
88 final int initMode = 0;
89 public final int emptyMode = 1;
90 final int loadingMode = 2;
91 final int finishedMode = 3;
92
93 String buffer;
94 boolean finished;
95
96
97 // Create a ResultBox
98 // given details of the search that generated it.
99 ResultBox(Phind p, String collect, String key, String phrase, ResultBox rb) {
100
101 super();
102 mode = initMode;
103
104 phind = p;
105 display = null;
106 next = null;
107 prev = rb;
108 if (prev != null) prev.next = this;
109
110 searchKey = key;
111 searchPhrase = phrase;
112 searchCollection = collect;
113
114 numberOfOccurances = -1;
115 numberOfExpansions = -1;
116 numberOfDocuments = -1;
117
118 expansionsRetrieved = 0;
119 documentsRetrieved = 0;
120 nextPhraseBlock = 1;
121 nextDocumentBlock = 1;
122
123
124 setLayout(new BorderLayout());
125
126 s = new Scrollbar(Scrollbar.VERTICAL);
127 disableScrollbar();
128 add("East", s);
129
130 c = new ResultCanvas(this);
131 add("Center", c);
132
133 t = new ResultTitle(this);
134 add("North", t);
135
136 buffer = "";
137 finished = false;
138 mode = emptyMode;
139 }
140
141 static String describeContents(String phrase, String c) {
142 return( "\"" +phrase + "\" in " + c + ".");
143 }
144
145 String describeContents() {
146 return( describeContents(searchPhrase, searchCollection) );
147 }
148
149
150 // Reset the contents of the box
151 void resetBox( ) {
152 buffer = "";
153 finished = false;
154 c.resetCanvas();
155 disableScrollbar();
156 mode = emptyMode;
157
158 numberOfExpansions = -1;
159 expansionsRetrieved = 0;
160 numberOfDocuments = -1;
161 documentsRetrieved = 0;
162 }
163
164 void setStatus( String status ) {
165 phind.setStatus(status);
166 }
167
168 void disableScrollbar() {
169 if (s.isEnabled()) {
170 s.setValues(0, 1, 0, 1);
171 s.setLineIncrement(1);
172 s.setPageIncrement(1);
173 s.disable();
174 }
175 }
176
177 // Are there displays previous to and after this?
178 public boolean prevBoxExists () {
179 return (prev != null);
180 }
181 public boolean nextBoxExists () {
182 return (next != null);
183 }
184
185
186 // Look up a phrase
187 // Phrase lookups are passed on to the Phind applet itself.
188 void lookupPhrase(String key, String phrase, int queryMode) {
189 buffer = "";
190 finished = false;
191 phind.searchForPhrase(this, key, phrase, queryMode);
192 t.repaint();
193 }
194
195 public boolean handleEvent(Event event) {
196 // System.out.println("event: " + event.toString());
197 if ( (event.target == s) &&
198 ( (event.id == Event.SCROLL_ABSOLUTE) ||
199 (event.id == Event.SCROLL_LINE_DOWN) ||
200 (event.id == Event.SCROLL_LINE_UP) ||
201 (event.id == Event.SCROLL_PAGE_DOWN) ||
202 (event.id == Event.SCROLL_PAGE_UP) ) ) {
203 c.repaint();
204 return true;
205 } else {
206 return super.handleEvent(event);
207 }
208 }
209
210
211 // a new section of the byte stream is read
212 void parseBytes(byte[] bytes) {
213 mode = loadingMode;
214
215 // update the buffer that holds the bytes read
216 String input;
217 try {
218 input = new String(bytes, new String("Latin1"));
219 } catch (Exception e) {
220 System.err.println(e);
221 input = "";
222 }
223 buffer = buffer.concat(input);
224
225 // if buffer ends with "</phinddata>" then we are finished reading
226 finished = buffer.endsWith("</phinddata>\n");
227
228 // Parse each line until there are no more lines to parse
229 while(parseLine());
230
231 if (finished) {
232 c.updateMorePhrasesMarker();
233 c.updateMoreDocumentsMarker();
234 } else {
235 // We haven't finished, give some feedback about progress
236 setStatus("Connected to host, " + c.numberOfPhrases + " results read");
237 }
238 }
239
240 // Parse a line of XML
241 //
242 // This isn't necessarily real XML. We assume every tag begins and
243 // ends on one line. We read one line out of the buffer at a time,
244 // then "add" them to the box with the addLine method.
245 //
246 // Return true if there is a another line here to parse, otherwise false.
247
248 boolean parseLine() {
249
250 // System.out.println("parseLine " + finished + "(" + expansionsRetrieved + ")");
251 String item;
252 int eol = buffer.indexOf((int) '\n');
253
254 // If there are no carriage returns, the line is incomplete
255 if (eol == -1) {
256 return false;
257 }
258
259 // The <expansion> tag
260 else if (buffer.startsWith("<expansion ")) {
261 item = buffer.substring(0, eol);
262 buffer = buffer.substring(eol + 1);
263 addExpansionTag(item);
264 return true;
265 }
266
267 // The <document> tag
268 else if (buffer.startsWith("<document ")) {
269 item = buffer.substring(0, eol);
270 buffer = buffer.substring(eol + 1);
271 addDocumentTag(item);
272 return true;
273 }
274
275 // The <phinddata> tag introduces the dataset
276 else if (buffer.startsWith("<phinddata ")) {
277 item = buffer.substring(0, eol);
278 buffer = buffer.substring(eol + 1);
279 addPhinddataTag(item);
280 return true;
281 }
282
283 // The </phinddata> tag concludes the dataset
284 else if (buffer.startsWith("</phinddata>")) {
285 mode = finishedMode;
286 return false;
287 }
288
289 // The <expansionlist> tag
290 else if (buffer.startsWith("<expansionlist ")) {
291
292 }
293
294 // The <documentlist> tag
295 else if (buffer.startsWith("<documentlist ")) {
296
297 }
298
299 // Ignore the piece we've read
300 item = buffer.substring(0, eol);
301 buffer = buffer.substring(eol + 1);
302 return true;
303 }
304
305
306
307 // Add a phinddata tag
308 //
309 // Given a string containing an XML phinddata tag of the form:
310 // <phinddata id="35" text="FOREST" df="797" ef="134">
311 // Read the expansion information from the tag
312 //
313 // Return true if successful, otherwise false.
314
315 boolean addPhinddataTag( String line ) {
316
317 // System.out.println( "addExpansionListTag: " + line);
318
319 String idStr = "", text = "", tfStr = "", efStr = "", dfStr = "";
320 int tf = 0, ef = 0, df = 0;
321
322 // Break the tag down into its component parts
323 int nextSplit;
324 while (line.length() > 0) {
325 line = line.trim();
326
327 // Read the phrase identifier
328 if (line.startsWith("id")) {
329 line = line.substring(4);
330 nextSplit = line.indexOf((int) '"');
331 if (nextSplit >= 1) {
332 idStr = line.substring(0, nextSplit);
333 line = line.substring(nextSplit + 1);
334 } else {
335 System.err.println("addExpansionListTag: error parsing: " + line);
336 }
337 }
338
339 // Read the total frequency
340 else if (line.startsWith("tf")) {
341 line = line.substring(4);
342 nextSplit = line.indexOf((int) '"');
343 if (nextSplit >= 1) {
344 tfStr = line.substring(0, nextSplit);
345 tf = Integer.valueOf(tfStr).intValue();
346 line = line.substring(nextSplit + 1);
347 } else {
348 System.err.println("addExpansionListTag: error parsing: " + line);
349 }
350 }
351
352 // Read the text of the phrase
353 else if (line.startsWith("text")) {
354 line = line.substring(6);
355 nextSplit = line.indexOf((int) '"');
356 if (nextSplit >= 1) {
357 text = line.substring(0, nextSplit);
358 line = line.substring(nextSplit + 1);
359 } else {
360 System.err.println("addExpansionListTag: error parsing: " + line);
361 }
362 }
363
364 // Read the expansion frequency
365 else if (line.startsWith("ef")) {
366 line = line.substring(4);
367 nextSplit = line.indexOf((int) '"');
368 if (nextSplit >= 1) {
369 efStr = line.substring(0, nextSplit);
370 ef = Integer.valueOf(efStr).intValue();
371 line = line.substring(nextSplit + 1);
372 } else {
373 System.err.println("addExpansionListTag: error parsing: " + line);
374 }
375 }
376
377 // Read the document frequency
378 else if (line.startsWith("df")) {
379 line = line.substring(4);
380 nextSplit = line.indexOf((int) '"');
381 if (nextSplit >= 1) {
382 dfStr = line.substring(0, nextSplit);
383 df = Integer.valueOf(dfStr).intValue();
384 line = line.substring(nextSplit + 1);
385 } else {
386 System.err.println("addExpansionListTag: error parsing: " + line);
387 }
388 }
389
390 // Read and ignore some other tag
391 else {
392 nextSplit = line.indexOf((int) ' ');
393 if (nextSplit >= 1) {
394 line = line.substring(nextSplit + 1);
395 } else {
396 line = "";
397 }
398 }
399 }
400
401 // Add the data to the ResultBox
402 searchKey = idStr;
403 searchPhrase = text;
404 numberOfOccurances = tf;
405 numberOfExpansions = ef;
406 numberOfDocuments = df;
407 return true;
408 }
409
410
411 // Add an expansion tag
412 //
413 // Given a string containing an XML expansin tag of the form:
414 // <expansion num="3" id="8421" text="PEOPLE and TREES" tf="3" df="3"/>
415 // <expansion num="4" id="8696" text="FOREST TREES" tf="3" df="3"/>
416 // Create a new ResultItem for display
417 //
418 // Return true if successful, otherwise false.
419
420 boolean addExpansionTag( String line ) {
421
422 // System.out.println( "addExpansionTag: " + line + " (" + expansionsRetrieved + ")");
423
424 String numStr = "", idStr = "", tfStr = "", dfStr = "",
425 prefix = "", body = "", suffix = "";
426
427 body = searchPhrase;
428
429 // Break the tag down into its component parts
430 line = line.substring(11);
431 int nextSplit;
432 while (line.length() > 0) {
433 line = line.trim();
434
435 // Read the expansion number
436 if (line.startsWith("num")) {
437 line = line.substring(5);
438 nextSplit = line.indexOf((int) '"');
439 if (nextSplit >= 1) {
440 numStr = line.substring(0, nextSplit);
441 line = line.substring(nextSplit + 1);
442 } else {
443 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
444 }
445 }
446
447 // Read the phrase ID
448 else if (line.startsWith("id")) {
449 line = line.substring(4);
450 nextSplit = line.indexOf((int) '"');
451 if (nextSplit >= 1) {
452 idStr = line.substring(0, nextSplit);
453 line = line.substring(nextSplit + 1);
454 } else {
455 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
456 }
457 }
458
459 // Read the prefix of the phrase
460 else if (line.startsWith("prefix")) {
461 line = line.substring(8);
462 nextSplit = line.indexOf((int) '"');
463 if (nextSplit >= 1) {
464 prefix = line.substring(0, nextSplit);
465 line = line.substring(nextSplit + 1);
466 } else {
467 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
468 }
469 }
470
471 // Read the suffix of the phrase
472 else if (line.startsWith("suffix")) {
473 line = line.substring(8);
474 nextSplit = line.indexOf((int) '"');
475 if (nextSplit >= 1) {
476 suffix = line.substring(0, nextSplit);
477 line = line.substring(nextSplit + 1);
478 } else {
479 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
480 }
481 }
482
483 // Read the text of the phrase
484 // else if (line.startsWith("text")) {
485 //line = line.substring(6);
486 //nextSplit = line.indexOf((int) '"');
487 //if (nextSplit >= 1) {
488 // body = line.substring(0, nextSplit);
489 // line = line.substring(nextSplit + 1);
490 //} else {
491 // System.err.println("ResultBox addExpansionTag: error parsing: " + line);
492 //}
493 //}
494
495 // Read the frequency
496 else if (line.startsWith("tf")) {
497 line = line.substring(4);
498 nextSplit = line.indexOf((int) '"');
499 if (nextSplit >= 1) {
500 tfStr = line.substring(0, nextSplit);
501 line = line.substring(nextSplit + 1);
502 } else {
503 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
504 }
505 }
506
507 // Read the document frequency
508 else if (line.startsWith("df")) {
509 line = line.substring(4);
510 nextSplit = line.indexOf((int) '"');
511 if (nextSplit >= 1) {
512 dfStr = line.substring(0, nextSplit);
513 line = line.substring(nextSplit + 1);
514 } else {
515 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
516 }
517 }
518
519 // Read some other tag
520 else {
521 nextSplit = line.indexOf((int) ' ');
522 if (nextSplit >= 1) {
523 line = line.substring(nextSplit + 1);
524 } else {
525 line = "";
526 }
527 }
528 }
529
530 // Create a new ResultItem and add it to the display
531 ResultItem ri = new ResultItem(idStr, tfStr, dfStr, prefix, body, suffix);
532
533 if (c.addResultItem(ri)) {
534 expansionsRetrieved++;
535 return true;
536 }
537
538 return false;
539 }
540
541
542 // Add an document tag
543 //
544 // Given a string containing an XML document tag of the form:
545 // <document num="2" hash="HASH424e64b811fdad933be69c" freq="1" title="CONTENTS"/>
546 // <document num="3" hash="HASH01f08efd8752ad54ca0a99cf" freq="1" title="Tree mixtures"/>
547 //
548 // Create a new ResultItem for display
549 //
550 // Return true if successful, otherwise false.
551
552 boolean addDocumentTag( String line ) {
553
554 // System.out.println( "addDocumentTag: " + line + " (" + documentsRetrieved + ")");
555
556 String num = "", hash = "", freq = "", title = "";
557 line = line.substring(9);
558
559 // Break the tag down into its component parts
560 int nextSplit;
561 while (line.length() > 0) {
562 line = line.trim();
563
564 // Read the expansion number
565 if (line.startsWith("num")) {
566 line = line.substring(5);
567 nextSplit = line.indexOf((int) '"');
568 if (nextSplit >= 1) {
569 num = line.substring(0, nextSplit);
570 line = line.substring(nextSplit + 1);
571 } else {
572 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
573 }
574 }
575
576 // Read the document OID hash
577 else if (line.startsWith("hash")) {
578 line = line.substring(6);
579 nextSplit = line.indexOf((int) '"');
580 if (nextSplit >= 1) {
581 hash = line.substring(0, nextSplit);
582 line = line.substring(nextSplit + 1);
583 } else {
584 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
585 }
586 }
587
588 // Read the document frequency
589 else if (line.startsWith("freq")) {
590 line = line.substring(6);
591 nextSplit = line.indexOf((int) '"');
592 if (nextSplit >= 1) {
593 freq = line.substring(0, nextSplit);
594 line = line.substring(nextSplit + 1);
595 } else {
596 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
597 }
598 }
599
600 // Read the title
601 else if (line.startsWith("title")) {
602 line = line.substring(7);
603 nextSplit = line.indexOf((int) '"');
604 if (nextSplit >= 1) {
605 title = line.substring(0, nextSplit);
606 line = line.substring(nextSplit + 1);
607 } else {
608 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
609 }
610 }
611
612 // Read some other tag or close the string off
613 else {
614 nextSplit = line.indexOf((int) ' ');
615 if (nextSplit >= 1) {
616 line = line.substring(nextSplit + 1);
617 } else {
618 line = "";
619 }
620 }
621 }
622
623 // Create a new ResultItem and add it to the display
624 ResultItem ri = new ResultItem(hash, title, freq);
625
626 if (c.addResultItem(ri)) {
627 documentsRetrieved++;
628 return true;
629 }
630
631 return false;
632 }
633}
634
635
636
637
Note: See TracBrowser for help on using the repository browser.