source: main/trunk/greenstone2/runtime-src/src/java/org/nzdl/gsdl/Phind/ResultBox.java@ 22384

Last change on this file since 22384 was 22384, checked in by kjdon, 14 years ago

hack to make right to left languages look proper. new parameter to phind - textorientation. if set to rtl, set righttoleft to true. If this is true, then when making a new result item phrase, swap suffix and prefix

  • Property svn:keywords set to Author Date Id Revision
File size: 20.6 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
40package org.nzdl.gsdl.Phind;
41
42import java.awt.Panel;
43import java.awt.BorderLayout;
44import java.awt.Scrollbar;
45
46import java.awt.Label;
47import java.awt.Event;
48
49import java.net.*;
50import java.applet.*;
51
52import java.util.Vector;
53
54
55public class ResultBox extends Panel {
56
57 // Objects at a "higher" level than this one
58 Phind phind;
59 ResultDisplay display;
60
61 // Surroundig objects
62 ResultBox prev, next;
63
64 // The components contained by this one.
65 ResultCanvas c;
66 ResultTitle t;
67 Scrollbar s;
68 Panel label;
69
70 // The key identifying the phrase displayed, with its text and the
71 // collection from which it is drawn
72 String searchKey, searchPhrase, searchCollection;
73
74 // The total frequency, expansion frequency, and document frequency
75 // of the phrase
76 int numberOfOccurances;
77 int numberOfExpansions;
78 int numberOfDocuments;
79 int numberOfThesaurusLinks;
80
81 // The number of phrases and documents retrieved, and the number of
82 // times the user has requested more phrases or documents.
83 int expansionsRetrieved;
84 int documentsRetrieved;
85 int thesaurusLinksRetrieved;
86 int nextPhraseBlock;
87 int nextDocumentBlock;
88 int nextThesaurusLinkBlock;
89
90 int mode;
91 final int initMode = 0;
92 public final int emptyMode = 1;
93 final int loadingMode = 2;
94 final int finishedMode = 3;
95
96 String buffer;
97 boolean finished;
98
99
100 // Create a ResultBox
101 // given details of the search that generated it.
102 ResultBox(Phind p, String collect, String key, String phrase, ResultBox rb) {
103
104 super();
105 mode = initMode;
106
107 phind = p;
108 display = null;
109 next = null;
110 prev = rb;
111 if (prev != null) prev.next = this;
112
113 searchKey = key;
114 searchPhrase = phrase;
115 searchCollection = collect;
116
117 numberOfOccurances = -1;
118 numberOfExpansions = -1;
119 numberOfDocuments = -1;
120 numberOfThesaurusLinks = -1;
121
122 expansionsRetrieved = 0;
123 documentsRetrieved = 0;
124 thesaurusLinksRetrieved = 0;
125
126 nextPhraseBlock = 1;
127 nextDocumentBlock = 1;
128 nextThesaurusLinkBlock = 1;
129
130
131 setLayout(new BorderLayout());
132
133 s = new Scrollbar(Scrollbar.VERTICAL);
134 disableScrollbar();
135 add("East", s);
136
137 c = new ResultCanvas(this);
138 add("Center", c);
139
140 t = new ResultTitle(this);
141 add("North", t);
142
143 buffer = "";
144 finished = false;
145 mode = emptyMode;
146 }
147
148 static String describeContents(String phrase, String c) {
149 return( "\"" + phrase + "\" in " + c + ".");
150 }
151
152 String describeContents() {
153 return( describeContents(searchPhrase, searchCollection) );
154 }
155
156
157 // Reset the contents of the box
158 void resetBox( ) {
159 buffer = "";
160 finished = false;
161 c.resetCanvas();
162 disableScrollbar();
163 mode = emptyMode;
164
165 numberOfExpansions = -1;
166 numberOfDocuments = -1;
167 numberOfThesaurusLinks = -1;
168
169 expansionsRetrieved = 0;
170 documentsRetrieved = 0;
171 thesaurusLinksRetrieved = 0;
172 }
173
174 void setStatus( String status ) {
175 phind.setStatus(status);
176 }
177
178 void disableScrollbar() {
179 if (s.isEnabled()) {
180 s.setValues(0, 1, 0, 1);
181 s.setLineIncrement(1);
182 s.setPageIncrement(1);
183 s.disable();
184 }
185 }
186
187 // Are there displays previous to and after this?
188 public boolean prevBoxExists () {
189 return (prev != null);
190 }
191 public boolean nextBoxExists () {
192 return (next != null);
193 }
194
195
196 // Look up a phrase
197 // Phrase lookups are passed on to the Phind applet itself.
198 void lookupPhrase(String key, String phrase, int queryMode) {
199 buffer = "";
200 finished = false;
201 phind.searchForPhrase(this, key, phrase, queryMode);
202 t.repaint();
203 }
204
205 public boolean handleEvent(Event event) {
206 // System.out.println("event: " + event.toString());
207 if ( (event.target == s) &&
208 ( (event.id == Event.SCROLL_ABSOLUTE) ||
209 (event.id == Event.SCROLL_LINE_DOWN) ||
210 (event.id == Event.SCROLL_LINE_UP) ||
211 (event.id == Event.SCROLL_PAGE_DOWN) ||
212 (event.id == Event.SCROLL_PAGE_UP) ) ) {
213 c.repaint();
214 return true;
215 } else {
216 return super.handleEvent(event);
217 }
218 }
219
220
221 // a new section of the byte stream is read
222 void parseBytes(byte[] bytes) {
223 mode = loadingMode;
224
225 // update the buffer that holds the bytes read
226 String input;
227 try {
228 input = new String(bytes, new String("UTF8"));
229 } catch (Exception e) {
230 System.err.println(e);
231 input = "";
232 }
233 buffer = buffer.concat(input);
234
235 // Parse each line until there are no more lines to parse
236 while(parseLine());
237
238 if (finished) {
239 c.updateMarkers();
240 buffer = "";
241 } else {
242 // We haven't finished, give some feedback about progress
243 setStatus("Connected to host, " + c.numberOfItems + " results read");
244 }
245 }
246
247 // Parse a line of XML
248 //
249 // This isn't necessarily real XML. We assume every tag begins and
250 // ends on one line. We read one line out of the buffer at a time,
251 // then "add" them to the box with the addLine method.
252 //
253 // Return true if there is a another line here to parse, otherwise false.
254
255 boolean parseLine() {
256
257 // System.out.print("parseLine " + "(length " + buffer.length() + ")");
258 String item;
259 int eol = buffer.indexOf((int) '\n');
260
261 // If there are no carriage returns, the line is incomplete
262 if (eol == -1) {
263 return false;
264 }
265
266 // The </phinddata> tag concludes the dataset
267 else if (buffer.startsWith("</phinddata>")) {
268 mode = finishedMode;
269 finished = true;
270 return false;
271 }
272
273 // The <expansion> tag
274 else if (buffer.startsWith("<expansion ")) {
275 item = buffer.substring(0, eol);
276 buffer = buffer.substring(eol + 1);
277 addExpansionTag(item);
278 return true;
279 }
280
281 // The <document> tag
282 else if (buffer.startsWith("<document ")) {
283 item = buffer.substring(0, eol);
284 buffer = buffer.substring(eol + 1);
285 addDocumentTag(item);
286 return true;
287 }
288
289 // The <thesaurus> tag
290 else if (buffer.startsWith("<thesaurus ")) {
291 item = buffer.substring(0, eol);
292 buffer = buffer.substring(eol + 1);
293 addThesaurusTag(item);
294 return true;
295 }
296
297 // The <phinddata> tag introduces the dataset
298 else if (buffer.startsWith("<phinddata ")) {
299 item = buffer.substring(0, eol);
300 buffer = buffer.substring(eol + 1);
301 addPhinddataTag(item);
302 return true;
303 }
304
305 // The <phinderror> tag contains an error message
306 else if (buffer.startsWith("<phinderror")) {
307 item = buffer.substring(0, eol);
308 System.out.println("phind reported an error: " + item);
309 buffer = buffer.substring(eol + 1);
310 return true;
311 }
312
313 // Ignore the piece we've read
314 item = buffer.substring(0, eol);
315 // System.out.println("discarding: " + item);
316 buffer = buffer.substring(eol + 1);
317 return true;
318 }
319
320
321
322 // Add a phinddata tag
323 //
324 // Given a string containing an XML phinddata tag of the form:
325 // <phinddata id="35" text="FOREST" df="797" ef="134">
326 // Read the expansion information from the tag
327 //
328 // Return true if successful, otherwise false.
329
330 boolean addPhinddataTag( String line ) {
331
332 // System.out.println( "addExpansionListTag: " + line);
333
334 String idStr = "", text = "", tfStr = "", efStr = "", dfStr = "", lfStr = "";
335 int tf = 0, ef = 0, df = 0, lf = 0;
336
337 // Break the tag down into its component parts
338 int nextSplit;
339 while (line.length() > 0) {
340 line = line.trim();
341
342 // Read the phrase identifier
343 if (line.startsWith("id")) {
344 line = line.substring(4);
345 nextSplit = line.indexOf((int) '"');
346 if (nextSplit >= 1) {
347 idStr = line.substring(0, nextSplit);
348 line = line.substring(nextSplit + 1);
349 } else {
350 System.err.println("addExpansionListTag: error parsing: " + line);
351 }
352 }
353
354 // Read the total frequency
355 else if (line.startsWith("tf")) {
356 line = line.substring(4);
357 nextSplit = line.indexOf((int) '"');
358 if (nextSplit >= 1) {
359 tfStr = line.substring(0, nextSplit);
360 tf = Integer.valueOf(tfStr).intValue();
361 line = line.substring(nextSplit + 1);
362 } else {
363 System.err.println("addExpansionListTag: error parsing: " + line);
364 }
365 }
366
367 // Read the text of the phrase
368 else if (line.startsWith("text")) {
369 line = line.substring(6);
370 nextSplit = line.indexOf((int) '"');
371 if (nextSplit >= 1) {
372 text = line.substring(0, nextSplit);
373 line = line.substring(nextSplit + 1);
374 } else {
375 System.err.println("addExpansionListTag: error parsing: " + line);
376 }
377 }
378
379 // Read the expansion frequency
380 else if (line.startsWith("ef")) {
381 line = line.substring(4);
382 nextSplit = line.indexOf((int) '"');
383 if (nextSplit >= 1) {
384 efStr = line.substring(0, nextSplit);
385 ef = Integer.valueOf(efStr).intValue();
386 line = line.substring(nextSplit + 1);
387 } else {
388 System.err.println("addExpansionListTag: error parsing: " + line);
389 }
390 }
391
392 // Read the document frequency
393 else if (line.startsWith("df")) {
394 line = line.substring(4);
395 nextSplit = line.indexOf((int) '"');
396 if (nextSplit >= 1) {
397 dfStr = line.substring(0, nextSplit);
398 df = Integer.valueOf(dfStr).intValue();
399 line = line.substring(nextSplit + 1);
400 } else {
401 System.err.println("addExpansionListTag: error parsing: " + line);
402 }
403 }
404
405 // Read the thesaurus link frequency
406 else if (line.startsWith("lf")) {
407 line = line.substring(4);
408 nextSplit = line.indexOf((int) '"');
409 if (nextSplit >= 1) {
410 lfStr = line.substring(0, nextSplit);
411 lf = Integer.valueOf(lfStr).intValue();
412 line = line.substring(nextSplit + 1);
413 } else {
414 System.err.println("addExpansionListTag: error parsing: " + line);
415 }
416 }
417
418 // Read and ignore some other tag
419 else {
420 nextSplit = line.indexOf((int) ' ');
421 if (nextSplit >= 1) {
422 line = line.substring(nextSplit + 1);
423 } else {
424 line = "";
425 }
426 }
427 }
428
429 // Add the data to the ResultBox
430 searchKey = idStr;
431 searchPhrase = text;
432 numberOfOccurances = tf;
433 numberOfExpansions = ef;
434 numberOfDocuments = df;
435 numberOfThesaurusLinks = lf;
436 return true;
437 }
438
439
440 // Add an expansion tag
441 //
442 // Given a string containing an XML expansin tag of the form:
443 // <expansion num="3" id="8421" prefix="PEOPLE and" suffix="" tf="3" df="3"/>
444 // <expansion num="4" id="8696" prefix="" suffix="products" tf="3" df="3"/>
445 // Create a new ResultItemPhrase for display
446 //
447 // Return true if successful, otherwise false.
448
449 boolean addExpansionTag( String line ) {
450
451 // System.out.println( "addExpansionTag: " + line + " (" + expansionsRetrieved + ")");
452
453 String numStr = "", idStr = "", tfStr = "", dfStr = "",
454 prefix = "", body = "", suffix = "";
455
456 body = searchPhrase;
457
458 // Break the tag down into its component parts
459 line = line.substring(11);
460 int nextSplit;
461 while (line.length() > 0) {
462 line = line.trim();
463
464 // Read the expansion number
465 if (line.startsWith("num")) {
466 line = line.substring(5);
467 nextSplit = line.indexOf((int) '"');
468 if (nextSplit >= 1) {
469 numStr = line.substring(0, nextSplit);
470 line = line.substring(nextSplit + 1);
471 } else {
472 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
473 }
474 }
475
476 // Read the phrase ID
477 else if (line.startsWith("id")) {
478 line = line.substring(4);
479 nextSplit = line.indexOf((int) '"');
480 if (nextSplit >= 1) {
481 idStr = line.substring(0, nextSplit);
482 line = line.substring(nextSplit + 1);
483 } else {
484 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
485 }
486 }
487
488 // Read the prefix of the phrase
489 else if (line.startsWith("prefix")) {
490 line = line.substring(8);
491 nextSplit = line.indexOf((int) '"');
492 if (nextSplit >= 0) {
493 prefix = line.substring(0, nextSplit);
494 line = line.substring(nextSplit + 1);
495 } else {
496 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
497 }
498 }
499
500 // Read the suffix of the phrase
501 else if (line.startsWith("suffix")) {
502 line = line.substring(8);
503 nextSplit = line.indexOf((int) '"');
504 if (nextSplit >= 0) {
505 suffix = line.substring(0, nextSplit);
506 line = line.substring(nextSplit + 1);
507 } else {
508 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
509 }
510 }
511
512 // Read the frequency
513 else if (line.startsWith("tf")) {
514 line = line.substring(4);
515 nextSplit = line.indexOf((int) '"');
516 if (nextSplit >= 1) {
517 tfStr = line.substring(0, nextSplit);
518 line = line.substring(nextSplit + 1);
519 } else {
520 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
521 }
522 }
523
524 // Read the document frequency
525 else if (line.startsWith("df")) {
526 line = line.substring(4);
527 nextSplit = line.indexOf((int) '"');
528 if (nextSplit >= 1) {
529 dfStr = line.substring(0, nextSplit);
530 line = line.substring(nextSplit + 1);
531 } else {
532 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
533 }
534 }
535
536 // Read some other tag
537 else {
538 nextSplit = line.indexOf((int) ' ');
539 if (nextSplit >= 1) {
540 line = line.substring(nextSplit + 1);
541 } else {
542 line = "";
543 }
544 }
545 }
546
547 // Create a new ResultItem and add it to the display
548 ResultItemPhrase ri;
549 if (phind.righttoleft) {
550 // swap suffix and prefix
551 ri = new ResultItemPhrase(idStr, tfStr, dfStr, suffix, body, prefix);
552 } else {
553 ri = new ResultItemPhrase(idStr, tfStr, dfStr, prefix, body, suffix);
554 }
555 if (c.addResultItem(ri)) {
556 expansionsRetrieved++;
557 return true;
558 }
559
560 return false;
561 }
562
563
564 // Add an document tag
565 //
566 // Given a string containing an XML document tag of the form:
567 // <document num="2" hash="HASH424e64b811fdad933be69c" freq="1" title="CONTENTS"/>
568 // <document num="3" hash="HASH01f08efd8752ad54ca0a99cf" freq="1" title="Tree mixtures"/>
569 //
570 // Create a new ResultItemDocument for display
571 //
572 // Return true if successful, otherwise false.
573
574 boolean addDocumentTag( String line ) {
575
576 // System.out.println( "addDocumentTag: " + line + " (" + documentsRetrieved + ")");
577
578 String num = "", hash = "", freq = "", title = "";
579 line = line.substring(9);
580
581 // Break the tag down into its component parts
582 int nextSplit;
583 while (line.length() > 0) {
584 line = line.trim();
585
586 // Read the expansion number
587 if (line.startsWith("num")) {
588 line = line.substring(5);
589 nextSplit = line.indexOf((int) '"');
590 if (nextSplit >= 1) {
591 num = line.substring(0, nextSplit);
592 line = line.substring(nextSplit + 1);
593 } else {
594 System.err.println("ResultBox addDocumentTag: error parsing: " + line);
595 }
596 }
597
598 // Read the document OID hash
599 else if (line.startsWith("hash")) {
600 line = line.substring(6);
601 nextSplit = line.indexOf((int) '"');
602 if (nextSplit >= 1) {
603 hash = line.substring(0, nextSplit);
604 line = line.substring(nextSplit + 1);
605 } else {
606 System.err.println("ResultBox addDocumentTag: error parsing: " + line);
607 }
608 }
609
610 // Read the document frequency
611 else if (line.startsWith("freq")) {
612 line = line.substring(6);
613 nextSplit = line.indexOf((int) '"');
614 if (nextSplit >= 1) {
615 freq = line.substring(0, nextSplit);
616 line = line.substring(nextSplit + 1);
617 } else {
618 System.err.println("ResultBox addDocumentTag: error parsing: " + line);
619 }
620 }
621
622 // Read the title
623 else if (line.startsWith("title")) {
624 line = line.substring(7);
625 nextSplit = line.indexOf((int) '"');
626 if (nextSplit >= 1) {
627 title = line.substring(0, nextSplit);
628 line = line.substring(nextSplit + 1);
629 } else {
630 System.err.println("ResultBox addDocumentTag: error parsing: " + line);
631 }
632 }
633
634 // Read some other tag or close the string off
635 else {
636 nextSplit = line.indexOf((int) ' ');
637 if (nextSplit >= 1) {
638 line = line.substring(nextSplit + 1);
639 } else {
640 line = "";
641 }
642 }
643 }
644
645 // Create a new ResultItem and add it to the display
646 ResultItemDocument ri = new ResultItemDocument(hash, title, freq);
647
648 if (c.addResultItem(ri)) {
649 documentsRetrieved++;
650 return true;
651 }
652
653 return false;
654 }
655
656
657 // Add a thesaurus tag
658 //
659 // Given a string containing an XML document tag of the form:
660 //
661 // <thesaurus num="3" id="36506" tf="0" df="0" type="RT" text="ANGLOPHONE AFRICA"/>
662 // <thesaurus num="4" id="28724" tf="0" df="0" type="RT" text="FRANCOPHONE AFRICA"/>
663 //
664 // Create a new ResultItemLink for display
665 //
666 // Return true if successful, otherwise false.
667
668 boolean addThesaurusTag( String line ) {
669
670 // System.out.println( "addThesaurusTag: " + line + " (" + thesaurusLinksRetrieved + ")");
671
672 String num = "", id = "", tf = "", df = "", type = "", text = "";
673 line = line.substring(10);
674
675 // Break the tag down into its component parts
676 int nextSplit;
677 while (line.length() > 0) {
678 line = line.trim();
679
680 // Read the expansion number
681 if (line.startsWith("num")) {
682 line = line.substring(5);
683 nextSplit = line.indexOf((int) '"');
684 if (nextSplit >= 1) {
685 num = line.substring(0, nextSplit);
686 line = line.substring(nextSplit + 1);
687 } else {
688 System.err.println("ResultBox addThesaurusTag: error parsing: " + line);
689 }
690 }
691
692 // Read the phrase ID
693 else if (line.startsWith("id")) {
694 line = line.substring(4);
695 nextSplit = line.indexOf((int) '"');
696 if (nextSplit >= 1) {
697 id = line.substring(0, nextSplit);
698 line = line.substring(nextSplit + 1);
699 } else {
700 System.err.println("ResultBox addThesaurusTag: error parsing: " + line);
701 }
702 }
703
704 // Read the phrase link type
705 else if (line.startsWith("type")) {
706 line = line.substring(6);
707 nextSplit = line.indexOf((int) '"');
708 if (nextSplit >= 1) {
709 type = line.substring(0, nextSplit);
710 line = line.substring(nextSplit + 1);
711 } else {
712 System.err.println("ResultBox addThesaurusTag: error parsing: " + line);
713 }
714 }
715
716
717 // Read the phrase text
718 else if (line.startsWith("text")) {
719 line = line.substring(6);
720 nextSplit = line.indexOf((int) '"');
721 if (nextSplit >= 1) {
722 text = line.substring(0, nextSplit);
723 line = line.substring(nextSplit + 1);
724 } else {
725 System.err.println("ResultBox addThesaurusTag: error parsing: " + line);
726 }
727 }
728
729 // Read the frequency
730 else if (line.startsWith("tf")) {
731 line = line.substring(4);
732 nextSplit = line.indexOf((int) '"');
733 if (nextSplit >= 1) {
734 tf = line.substring(0, nextSplit);
735 line = line.substring(nextSplit + 1);
736 } else {
737 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
738 }
739 }
740
741 // Read the document frequency
742 else if (line.startsWith("df")) {
743 line = line.substring(4);
744 nextSplit = line.indexOf((int) '"');
745 if (nextSplit >= 1) {
746 df = line.substring(0, nextSplit);
747 line = line.substring(nextSplit + 1);
748 } else {
749 System.err.println("ResultBox addExpansionTag: error parsing: " + line);
750 }
751 }
752
753
754 // Read some other tag or close the string off
755 else {
756 nextSplit = line.indexOf((int) ' ');
757 if (nextSplit >= 1) {
758 line = line.substring(nextSplit + 1);
759 } else {
760 line = "";
761 }
762 }
763 }
764
765 // Create a new ResultItem and add it to the display
766 ResultItemLink ri = new ResultItemLink(id, text, type, tf, df);
767
768 if (c.addResultItem(ri)) {
769 thesaurusLinksRetrieved++;
770 return true;
771 }
772
773 return false;
774 }
775}
776
777
778
779
Note: See TracBrowser for help on using the repository browser.