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 | package org.greenstone.applet.phind;
|
---|
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.AWTEvent;
|
---|
48 | import java.awt.event.AdjustmentListener;
|
---|
49 | import java.awt.event.AdjustmentEvent;
|
---|
50 | import java.net.*;
|
---|
51 | import java.applet.*;
|
---|
52 |
|
---|
53 | import java.util.Vector;
|
---|
54 |
|
---|
55 | import org.w3c.dom.Element;
|
---|
56 | import org.w3c.dom.Node;
|
---|
57 | import org.w3c.dom.NodeList;
|
---|
58 | import org.greenstone.gsdl3.util.*;
|
---|
59 |
|
---|
60 | public class ResultBox extends Panel
|
---|
61 | implements AdjustmentListener {
|
---|
62 |
|
---|
63 | // Objects at a "higher" level than this one
|
---|
64 | Phind phind;
|
---|
65 | ResultDisplay display;
|
---|
66 |
|
---|
67 | // Surroundig objects
|
---|
68 | ResultBox prev, next;
|
---|
69 |
|
---|
70 | // The components contained by this one.
|
---|
71 | ResultCanvas c;
|
---|
72 | ResultTitle t;
|
---|
73 | Scrollbar s;
|
---|
74 | Panel label;
|
---|
75 |
|
---|
76 | // The key identifying the phrase displayed, with its text and the
|
---|
77 | // collection from which it is drawn
|
---|
78 | String searchKey, searchPhrase, searchCollection;
|
---|
79 |
|
---|
80 | // The total frequency, expansion frequency, and document frequency
|
---|
81 | // of the phrase
|
---|
82 | int numberOfOccurances;
|
---|
83 | int numberOfExpansions;
|
---|
84 | int numberOfDocuments;
|
---|
85 | int numberOfThesaurusLinks;
|
---|
86 |
|
---|
87 | // The number of phrases and documents retrieved, and the number of
|
---|
88 | // times the user has requested more phrases or documents.
|
---|
89 | int expansionsRetrieved;
|
---|
90 | int documentsRetrieved;
|
---|
91 | int thesaurusLinksRetrieved;
|
---|
92 | int nextPhraseBlock;
|
---|
93 | int nextDocumentBlock;
|
---|
94 | int nextThesaurusLinkBlock;
|
---|
95 |
|
---|
96 | int mode;
|
---|
97 | final int initMode = 0;
|
---|
98 | public final int emptyMode = 1;
|
---|
99 | final int loadingMode = 2;
|
---|
100 | final int finishedMode = 3;
|
---|
101 |
|
---|
102 | String buffer;
|
---|
103 | boolean finished;
|
---|
104 |
|
---|
105 |
|
---|
106 | // Create a ResultBox
|
---|
107 | // given details of the search that generated it.
|
---|
108 | ResultBox(Phind p, String collect, String key, String phrase, ResultBox rb) {
|
---|
109 |
|
---|
110 | super();
|
---|
111 | mode = initMode;
|
---|
112 |
|
---|
113 | phind = p;
|
---|
114 | display = null;
|
---|
115 | next = null;
|
---|
116 | prev = rb;
|
---|
117 | if (prev != null) prev.next = this;
|
---|
118 |
|
---|
119 | searchKey = key;
|
---|
120 | searchPhrase = phrase;
|
---|
121 | searchCollection = collect;
|
---|
122 |
|
---|
123 | numberOfOccurances = -1;
|
---|
124 | numberOfExpansions = -1;
|
---|
125 | numberOfDocuments = -1;
|
---|
126 | numberOfThesaurusLinks = -1;
|
---|
127 |
|
---|
128 | expansionsRetrieved = 0;
|
---|
129 | documentsRetrieved = 0;
|
---|
130 | thesaurusLinksRetrieved = 0;
|
---|
131 |
|
---|
132 | nextPhraseBlock = 1;
|
---|
133 | nextDocumentBlock = 1;
|
---|
134 | nextThesaurusLinkBlock = 1;
|
---|
135 |
|
---|
136 |
|
---|
137 | setLayout(new BorderLayout());
|
---|
138 |
|
---|
139 | s = new Scrollbar(Scrollbar.VERTICAL);
|
---|
140 | disableScrollbar();
|
---|
141 | s.addAdjustmentListener(this);
|
---|
142 | add("East", s);
|
---|
143 |
|
---|
144 | c = new ResultCanvas(this);
|
---|
145 | add("Center", c);
|
---|
146 |
|
---|
147 | t = new ResultTitle(this);
|
---|
148 | add("North", t);
|
---|
149 |
|
---|
150 | buffer = "";
|
---|
151 | finished = false;
|
---|
152 | mode = emptyMode;
|
---|
153 | }
|
---|
154 |
|
---|
155 | static String describeContents(String phrase, String c) {
|
---|
156 | return( "\"" + phrase + "\" in " + c + ".");
|
---|
157 | }
|
---|
158 |
|
---|
159 | String describeContents() {
|
---|
160 | return( describeContents(searchPhrase, searchCollection) );
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | // Reset the contents of the box
|
---|
165 | void resetBox( ) {
|
---|
166 | buffer = "";
|
---|
167 | finished = false;
|
---|
168 | c.resetCanvas();
|
---|
169 | disableScrollbar();
|
---|
170 | mode = emptyMode;
|
---|
171 |
|
---|
172 | numberOfExpansions = -1;
|
---|
173 | numberOfDocuments = -1;
|
---|
174 | numberOfThesaurusLinks = -1;
|
---|
175 |
|
---|
176 | expansionsRetrieved = 0;
|
---|
177 | documentsRetrieved = 0;
|
---|
178 | thesaurusLinksRetrieved = 0;
|
---|
179 | }
|
---|
180 |
|
---|
181 | void setStatus( String status ) {
|
---|
182 | phind.setStatus(status);
|
---|
183 | }
|
---|
184 |
|
---|
185 | void disableScrollbar() {
|
---|
186 | if (s.isEnabled()) {
|
---|
187 | s.setValues(0, 1, 0, 1);
|
---|
188 | s.setUnitIncrement(1);
|
---|
189 | s.setBlockIncrement(1);
|
---|
190 | s.setEnabled(false);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | // Are there displays previous to and after this?
|
---|
195 | public boolean prevBoxExists () {
|
---|
196 | return (prev != null);
|
---|
197 | }
|
---|
198 | public boolean nextBoxExists () {
|
---|
199 | return (next != null);
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | // Look up a phrase
|
---|
204 | // Phrase lookups are passed on to the Phind applet itself.
|
---|
205 | void lookupPhrase(String key, String phrase, int queryMode) {
|
---|
206 | buffer = "";
|
---|
207 | finished = false;
|
---|
208 | phind.searchForPhrase(this, key, phrase, queryMode);
|
---|
209 | t.repaint();
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* public void processEvent(AWTEvent event) {
|
---|
213 | // System.out.println("event: " + event.toString());
|
---|
214 | if ( (event.target == s) &&
|
---|
215 | ( (event.id == Event.SCROLL_ABSOLUTE) ||
|
---|
216 | (event.id == Event.SCROLL_LINE_DOWN) ||
|
---|
217 | (event.id == Event.SCROLL_LINE_UP) ||
|
---|
218 | (event.id == Event.SCROLL_PAGE_DOWN) ||
|
---|
219 | (event.id == Event.SCROLL_PAGE_UP) ) ) {
|
---|
220 | c.repaint();
|
---|
221 |
|
---|
222 | } else {
|
---|
223 | super.processEvent(event);
|
---|
224 | }
|
---|
225 | } */
|
---|
226 |
|
---|
227 | public void adjustmentValueChanged(AdjustmentEvent evt) {
|
---|
228 | c.repaint();
|
---|
229 | }
|
---|
230 |
|
---|
231 | void parseXML(Element data) {
|
---|
232 |
|
---|
233 | //System.out.println("phinddata:"+data.toString());
|
---|
234 |
|
---|
235 | //String id="", phrase="";
|
---|
236 | //int df=0, ef=0, lf=0, tf=0;
|
---|
237 |
|
---|
238 | searchKey = data.getAttribute("id");
|
---|
239 |
|
---|
240 | numberOfDocuments = Integer.valueOf(data.getAttribute("df")).intValue();
|
---|
241 | numberOfExpansions = Integer.valueOf(data.getAttribute("ef")).intValue();
|
---|
242 | numberOfThesaurusLinks = Integer.valueOf(data.getAttribute("lf")).intValue();
|
---|
243 | numberOfOccurances = Integer.valueOf(data.getAttribute("tf")).intValue();
|
---|
244 |
|
---|
245 | //searchPhrase = "cat"; // for now
|
---|
246 |
|
---|
247 | // go through children
|
---|
248 | Element e = (Element)data.getFirstChild();
|
---|
249 | while (e!=null) {
|
---|
250 | String node_name = e.getNodeName();
|
---|
251 | if (node_name.equals("phrase")) {
|
---|
252 | // the value of the phrase
|
---|
253 | searchPhrase = getNodeText(e);
|
---|
254 | } else if (node_name.equals("expansionList")) {
|
---|
255 |
|
---|
256 | NodeList expansions = e.getElementsByTagName("expansion");
|
---|
257 | for (int i=0; i<expansions.getLength(); i++) {
|
---|
258 | processExpansionElement((Element)expansions.item(i));
|
---|
259 | }
|
---|
260 | } else if (node_name.equals("documentList")) {
|
---|
261 |
|
---|
262 | NodeList documents = e.getElementsByTagName("document");
|
---|
263 | for (int i=0; i<documents.getLength(); i++) {
|
---|
264 | processDocumentElement((Element)documents.item(i));
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | } else if (node_name.equals("thesaurusList")) {
|
---|
269 |
|
---|
270 | NodeList thesaurai = e.getElementsByTagName("thesaurus");
|
---|
271 | for (int i=0; i<thesaurai.getLength(); i++) {
|
---|
272 | processThesaurusElement((Element)thesaurai.item(i));
|
---|
273 | }
|
---|
274 |
|
---|
275 | } else {
|
---|
276 | System.out.println("error in phinddata format, hav unwanted node, "+node_name);
|
---|
277 | }
|
---|
278 | e = (Element)e.getNextSibling();
|
---|
279 | }
|
---|
280 | // finished parsing, update the getMoreDocs markers
|
---|
281 | c.updateMarkers();
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | /** Add an expansion tag
|
---|
286 | *
|
---|
287 | * Given a string containing an XML expansion element of the form:
|
---|
288 | * <expansion num="3" id="8421" prefix="PEOPLE and" suffix="" tf="3" df="3"><suffix></suffix><prefix>PEOPLE</prefix></expansion>
|
---|
289 | *
|
---|
290 | * Create a new ResultItemPhrase for display
|
---|
291 | *
|
---|
292 | * Return true if successful, otherwise false. */
|
---|
293 | boolean processExpansionElement(Element e) {
|
---|
294 |
|
---|
295 | String num = "", id = "", tf = "", df = "",
|
---|
296 | prefix = "", body = "", suffix = "";
|
---|
297 |
|
---|
298 | body = searchPhrase;
|
---|
299 |
|
---|
300 | id = e.getAttribute("id");
|
---|
301 | tf = e.getAttribute("tf");
|
---|
302 | df = e.getAttribute("df");
|
---|
303 | num = e.getAttribute("num");
|
---|
304 |
|
---|
305 | // get prefix child
|
---|
306 | Node prefix_node = getChildByTagName(e, "prefix");
|
---|
307 | if (prefix_node!=null) {
|
---|
308 | prefix = getNodeText(prefix_node);
|
---|
309 | }
|
---|
310 | Node suffix_node = getChildByTagName(e, "suffix");
|
---|
311 | if (suffix_node !=null) {
|
---|
312 | suffix = getNodeText(suffix_node);
|
---|
313 | }
|
---|
314 |
|
---|
315 | ResultItemPhrase ri = new ResultItemPhrase(id, tf, df, prefix, body, suffix);
|
---|
316 |
|
---|
317 | if (c.addResultItem(ri)) {
|
---|
318 | expansionsRetrieved++;
|
---|
319 | return true;
|
---|
320 | }
|
---|
321 |
|
---|
322 | return false;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /** Add a document tag
|
---|
326 | *
|
---|
327 | * Given an XML Element of the form:
|
---|
328 | * <document num="2" hash="HASH424e64b811fdad933be69c" freq="1"><title>CONTENTS</title></document>
|
---|
329 | *
|
---|
330 | * Create a new ResultItemDocument for display
|
---|
331 | *
|
---|
332 | * Return true if successful, otherwise false. */
|
---|
333 |
|
---|
334 | boolean processDocumentElement(Element d ) {
|
---|
335 | // why do we have num??
|
---|
336 | String num = "", hash = "", freq = "", title = "";
|
---|
337 |
|
---|
338 | num = d.getAttribute("num");
|
---|
339 | freq = d.getAttribute("freq");
|
---|
340 | hash = d.getAttribute("hash");
|
---|
341 |
|
---|
342 | Node title_node = getChildByTagName(d, "title");
|
---|
343 | if (title_node != null) {
|
---|
344 | title = getNodeText(title_node);
|
---|
345 | }
|
---|
346 | // Create a new ResultItem and add it to the display
|
---|
347 | ResultItemDocument ri = new ResultItemDocument(hash, title, freq);
|
---|
348 |
|
---|
349 | if (c.addResultItem(ri)) {
|
---|
350 | documentsRetrieved++;
|
---|
351 | return true;
|
---|
352 | }
|
---|
353 |
|
---|
354 | return false;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /** Add a thesaurus tag
|
---|
358 | *
|
---|
359 | * Given an XML element of the form:
|
---|
360 | *
|
---|
361 | * <thesaurus num="3" id="36506" tf="0" df="0" type="RT"><phrase>ANGLOPHONE AFRICA</phrase></thesaurus>
|
---|
362 | *
|
---|
363 | * Create a new ResultItemLink for display
|
---|
364 | *
|
---|
365 | * Return true if successful, otherwise false. */
|
---|
366 |
|
---|
367 | boolean processThesaurusElement(Element t ) {
|
---|
368 |
|
---|
369 | // why do we have num??? - not used anywhere
|
---|
370 | String num = "", id = "", tf = "", df = "", type = "", phrase = "";
|
---|
371 |
|
---|
372 | id = t.getAttribute("id");
|
---|
373 | tf = t.getAttribute("tf");
|
---|
374 | df = t.getAttribute("df");
|
---|
375 | type = t.getAttribute("type");
|
---|
376 | num = t.getAttribute("num");
|
---|
377 |
|
---|
378 | Node phrase_node = getChildByTagName(t, "phrase");
|
---|
379 | if (phrase_node !=null) {
|
---|
380 | phrase = getNodeText(phrase_node);
|
---|
381 | }
|
---|
382 | // Create a new ResultItem and add it to the display
|
---|
383 | ResultItemLink ri = new ResultItemLink(id, phrase, type, tf, df);
|
---|
384 |
|
---|
385 | if (c.addResultItem(ri)) {
|
---|
386 | thesaurusLinksRetrieved++;
|
---|
387 | return true;
|
---|
388 | }
|
---|
389 |
|
---|
390 | return false;
|
---|
391 | }
|
---|
392 |
|
---|
393 | /** extracts the text out of a node */
|
---|
394 | protected String getNodeText(Node elem) {
|
---|
395 | elem.normalize();
|
---|
396 | Node n = elem.getFirstChild();
|
---|
397 | while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
|
---|
398 | n=n.getNextSibling();
|
---|
399 | }
|
---|
400 | if (n==null) { // no text node
|
---|
401 | return "";
|
---|
402 | }
|
---|
403 | return n.getNodeValue();
|
---|
404 | }
|
---|
405 |
|
---|
406 | /** returns the (first) child element with the given name */
|
---|
407 | protected Node getChildByTagName(Node n, String name) {
|
---|
408 |
|
---|
409 | Node child = n.getFirstChild();
|
---|
410 | while (child!=null) {
|
---|
411 | if (child.getNodeName().equals(name)) {
|
---|
412 | return child;
|
---|
413 | }
|
---|
414 | child = child.getNextSibling();
|
---|
415 | }
|
---|
416 | return null; //not found
|
---|
417 | }
|
---|
418 |
|
---|
419 |
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 |
|
---|
424 |
|
---|