Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/nzdl/gsdl/Phind/ResultBox.java


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  
30  This class is used in the Phind java applet (Phind.java).
31  
32  A ResultBox holds the results of a query to phindcgi.  They deal mostly
33  with the information content of the query, and have methods for parsing the
34  input into phrase and document items.  They have little do with display:
35  ResultBoxes are shown to the user through "ResultDisplay" panels, and are
36  drawn using "ResultCanvas" objects.
37  
38  **********************************************************************/
39  
40  package org.nzdl.gsdl.Phind;
41  
42  import java.awt.Panel;
43  import java.awt.BorderLayout;
44  import java.awt.Scrollbar;
45  
46  import java.awt.Label;
47  import java.awt.Event;
48  
49  import java.net.*;
50  import java.applet.*;
51  
52  import java.util.Vector;
53  
54  
55  public 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 = new ResultItemPhrase(idStr, tfStr, dfStr, prefix, body, suffix);
549   
550   if (c.addResultItem(ri)) {
551       expansionsRetrieved++;
552       return true;
553   }
554   
555   return false;
556     }
557 
558 
559     // Add an document tag
560     //
561     // Given a string containing an XML document tag of the form:
562     //   <document num="2" hash="HASH424e64b811fdad933be69c" freq="1" title="CONTENTS"/>
563     //   <document num="3" hash="HASH01f08efd8752ad54ca0a99cf" freq="1" title="Tree mixtures"/>
564     //
565     // Create a new ResultItemDocument for display
566     //
567     // Return true if successful, otherwise false.
568 
569     boolean addDocumentTag( String line ) {
570 
571   // System.out.println( "addDocumentTag: " + line + " (" + documentsRetrieved + ")");
572   
573   String num = "", hash = "", freq = "", title = "";
574   line = line.substring(9);
575   
576   // Break the tag down into its component parts
577   int nextSplit;
578          while (line.length() > 0) {
579       line = line.trim();
580 
581       // Read the expansion number
582       if (line.startsWith("num")) {
583     line = line.substring(5);
584     nextSplit = line.indexOf((int) '"');
585     if (nextSplit >= 1) {
586         num = line.substring(0, nextSplit);
587         line = line.substring(nextSplit + 1);
588     } else {
589         System.err.println("ResultBox addDocumentTag: error parsing: " + line);
590      }
591       } 
592 
593       // Read the document OID hash
594       else if (line.startsWith("hash")) {
595     line = line.substring(6);
596     nextSplit = line.indexOf((int) '"');
597     if (nextSplit >= 1) {
598         hash = line.substring(0, nextSplit);
599         line = line.substring(nextSplit + 1);
600     } else {
601         System.err.println("ResultBox addDocumentTag: error parsing: " + line);
602      }
603       } 
604 
605       // Read the document frequency
606       else if (line.startsWith("freq")) {
607     line = line.substring(6);
608     nextSplit = line.indexOf((int) '"');
609     if (nextSplit >= 1) {
610         freq = line.substring(0, nextSplit);
611         line = line.substring(nextSplit + 1);
612     } else {
613         System.err.println("ResultBox addDocumentTag: error parsing: " + line);
614      }
615       } 
616 
617       // Read the title
618       else if (line.startsWith("title")) {
619     line = line.substring(7);
620     nextSplit = line.indexOf((int) '"');
621     if (nextSplit >= 1) {
622         title = line.substring(0, nextSplit);
623         line = line.substring(nextSplit + 1);
624     } else {
625         System.err.println("ResultBox addDocumentTag: error parsing: " + line);
626      }
627       } 
628 
629       // Read some other tag or close the string off
630       else {
631     nextSplit = line.indexOf((int) ' ');
632     if (nextSplit >= 1) {
633         line = line.substring(nextSplit + 1);
634     } else {
635         line = "";
636     }
637       }
638   }
639   
640   // Create a new ResultItem and add it to the display
641   ResultItemDocument ri = new ResultItemDocument(hash, title, freq);
642   
643   if (c.addResultItem(ri)) {
644       documentsRetrieved++;
645       return true;
646   }
647   
648   return false;
649     }
650 
651 
652     // Add a thesaurus tag
653     //
654     // Given a string containing an XML document tag of the form:
655     //
656     // <thesaurus num="3" id="36506" tf="0" df="0" type="RT" text="ANGLOPHONE AFRICA"/>
657     // <thesaurus num="4" id="28724" tf="0" df="0" type="RT" text="FRANCOPHONE AFRICA"/>
658     //
659     // Create a new ResultItemLink for display
660     //
661     // Return true if successful, otherwise false.
662 
663     boolean addThesaurusTag( String line ) {
664     
665   // System.out.println( "addThesaurusTag: " + line + " (" + thesaurusLinksRetrieved + ")");
666 
667   String num = "", id = "", tf = "", df = "", type = "", text = "";
668   line = line.substring(10);
669 
670   // Break the tag down into its component parts
671   int nextSplit;
672          while (line.length() > 0) {
673       line = line.trim();
674 
675       // Read the expansion number
676       if (line.startsWith("num")) {
677     line = line.substring(5);
678     nextSplit = line.indexOf((int) '"');
679     if (nextSplit >= 1) {
680         num = line.substring(0, nextSplit);
681         line = line.substring(nextSplit + 1);
682     } else {
683         System.err.println("ResultBox addThesaurusTag: error parsing: " + line);
684      }
685       } 
686 
687       // Read the phrase ID
688       else if (line.startsWith("id")) {
689     line = line.substring(4);
690     nextSplit = line.indexOf((int) '"');
691     if (nextSplit >= 1) {
692         id = line.substring(0, nextSplit);
693         line = line.substring(nextSplit + 1);
694     } else {
695         System.err.println("ResultBox addThesaurusTag: error parsing: " + line);
696      }
697       } 
698 
699       // Read the phrase link type
700       else if (line.startsWith("type")) {
701     line = line.substring(6);
702     nextSplit = line.indexOf((int) '"');
703     if (nextSplit >= 1) {
704         type = line.substring(0, nextSplit);
705         line = line.substring(nextSplit + 1);
706     } else {
707         System.err.println("ResultBox addThesaurusTag: error parsing: " + line);
708      }
709       } 
710 
711 
712       // Read the phrase text
713       else if (line.startsWith("text")) {
714     line = line.substring(6);
715     nextSplit = line.indexOf((int) '"');
716     if (nextSplit >= 1) {
717         text = line.substring(0, nextSplit);
718         line = line.substring(nextSplit + 1);
719     } else {
720         System.err.println("ResultBox addThesaurusTag: error parsing: " + line);
721      }
722       } 
723 
724       // Read the frequency
725       else if (line.startsWith("tf")) {
726     line = line.substring(4);
727     nextSplit = line.indexOf((int) '"');
728     if (nextSplit >= 1) {
729         tf = line.substring(0, nextSplit);
730         line = line.substring(nextSplit + 1);
731     } else {
732         System.err.println("ResultBox addExpansionTag: error parsing: " + line);
733      }
734       } 
735 
736       // Read the document frequency
737       else if (line.startsWith("df")) {
738     line = line.substring(4);
739     nextSplit = line.indexOf((int) '"');
740     if (nextSplit >= 1) {
741         df = line.substring(0, nextSplit);
742         line = line.substring(nextSplit + 1);
743     } else {
744         System.err.println("ResultBox addExpansionTag: error parsing: " + line);
745      }
746       } 
747 
748 
749       // Read some other tag or close the string off
750       else {
751     nextSplit = line.indexOf((int) ' ');
752     if (nextSplit >= 1) {
753         line = line.substring(nextSplit + 1);
754     } else {
755         line = "";
756     }
757       }
758   }
759   
760   // Create a new ResultItem and add it to the display
761   ResultItemLink ri = new ResultItemLink(id, text, type, tf, df);
762 
763   if (c.addResultItem(ri)) {
764       thesaurusLinksRetrieved++;
765       return true;
766   }
767   
768   return false;
769     }
770 }
771 
772 
773 
774