001: /*
002: * Copyright (c) 2000 Sun Microsystems. All Rights Reserved.
003: */
004:
005: package demo;
006:
007: import soif.*;
008:
009: import java.applet.Applet;
010: import java.awt.Button;
011: import java.awt.FlowLayout;
012: import java.awt.Frame;
013: import java.awt.Panel;
014: import java.awt.TextField;
015: import java.awt.Event;
016: import java.awt.AWTEvent;
017:
018: import java.io.*;
019:
020: /**
021: * Performs a simple search and displays its results.
022: */
023: class SimpleSearch {
024: String RDMServer;
025: CSID csid;
026: String SOIFOutputFile;
027:
028: /**
029: * SimpleSearch constructor
030: * @param rdm - url of rdm server, eg, http://compass_server:port
031: * @param csid - the collection to search on this rdm server
032: * if null, server will search its default csid
033: */
034: public SimpleSearch(String rdm, String csidstr) {
035: System.out.println("Compass Java Search Demo");
036:
037: RDMServer = rdm;
038:
039: if (csidstr != null) {
040: csid = new CSID(csidstr);
041: if (csid.isValid() == false) {
042: System.out.println("Invalid CSID - Aborting");
043: System.exit(8);
044: }
045: }
046:
047: }
048:
049: public boolean doSearch(String scope) {
050:
051: /* Optionally manage HTML results look. */
052: Results results = new Results();
053: results.setDisplay("score,url,title,description");
054: results.setDisplayAsLink("title", true);
055: results.setScoreGif(0, 50, "tired.gif");
056: results.setScoreGif(51, 100, "wired.gif");
057:
058: /* The Search class encapsulates the search.
059: ** It's parameters are:
060: ** 1) the search string
061: ** 2) the attributes you want returned, comma delimited
062: ** 3) sort order, comma delimited, - descending, + ascending
063: ** 4) first hit
064: ** 5) number of view Hits
065: ** 6) query language, eg compass, taxonomy-basic, schema-basic, etc
066: ** 7) CSID (Compass Server ID) instance
067: ** 8) The rdm search interface, eg, http://compass_server/rdm/incoming
068: ** (if not supplied, it will be derived from the csid)
069: */
070: Search search = new Search(scope,
071: //results.getDisplay(),
072: "", "-score,+title", 1, 20, "compass", csid, RDMServer);
073:
074: /* Execute the query. */
075:
076: System.out.println("\nSearch results for \"" + scope + "\"");
077:
078: DataOutputStream sos = null;
079: if (SOIFOutputFile != null) {
080: try {
081: sos = new DataOutputStream(new FileOutputStream(
082: SOIFOutputFile));
083: } catch (Exception e1) {
084: System.out.println("<failed to create output file>");
085: }
086: }
087:
088: int pagenum = 1;
089: int pagesize = 10;
090: SOIF firstPageSOIF = null;
091:
092: for (; pagenum <= 5; pagenum++) {
093: int firstHit = (pagenum - 1) * pagesize + 1;
094:
095: try {
096: search.doQuery(firstHit, pagesize);
097: } catch (Exception ex) {
098: ex.printStackTrace();
099: break;
100: }
101:
102: /* Check the result count. -1 indicates an
103: ** error, which can also be discovered by
104: ** checking search.getStatus().
105: */
106:
107: if (search.getResultCount() <= 0)
108: break;
109:
110: System.out
111: .println("==========================================");
112: System.out
113: .println("page "
114: + pagenum
115: + ": hits "
116: + search.getFirstHit()
117: + " to "
118: + (search.getFirstHit()
119: + search.getResultCount() - 1)
120: + " out of " + search.getHitCount()
121: + " across " + search.getDocumentCount()
122: + " documents");
123: System.out
124: .println("==========================================");
125: System.out.println();
126:
127: SOIF soif = search.getSOIFResult();
128: if (pagenum == 1)
129: firstPageSOIF = soif;
130:
131: /* Examine the results of the search. The
132: ** results can also be returned as a string
133: ** using getResult(), but the SOIF version
134: ** parses the string for you. The following
135: ** block loops through a list of SOIF instances.
136: */
137: for (soif = search.getSOIFResult(); soif != null; soif = soif.next) {
138: /* use the getValue() method to get
139: ** a value associated w/ the specified
140: ** attribute.
141: */
142: String u = soif.getValue("url");
143: String t = soif.getValue("title");
144: String d = soif.getValue("description");
145: String sc = soif.getValue("score");
146:
147: /* do something with the results */
148: System.out
149: .println("TITLE: "
150: + t
151: + "\n"
152: + "URL: "
153: + u
154: + "\n"
155: + "SCORE: "
156: + sc
157: + "\n"
158: + "DESCRIPTION: "
159: + d
160: + "\n"
161: + "--------------------------------------------\n");
162: if (sos != null) {
163: try {
164: sos.writeBytes(soif.toString());
165: } catch (Exception e1) {
166: System.out
167: .println("<failed to write to output file>");
168: }
169: }
170:
171: }
172: }
173:
174: if (firstPageSOIF == null)
175: System.out.println("No matching documents found.");
176:
177: else if (!true) {
178:
179: /* Use results manager to format results. */
180: System.out
181: .println("Here is the first page as HTML-formatted text...");
182: System.out.println();
183: System.out.println(results.resultsToHTML(firstPageSOIF,
184: "<tr>\n", "</tr>\n\n", "<td>\n", "\n</td>\n"));
185: }
186:
187: return true;
188: }
189:
190: /**
191: * @param filename - a file to dump raw SOIF results into - only
192: * use if running from the comand line or an applet with file
193: * system access
194: */
195: public void setSOIFfile(String filename) {
196: SOIFOutputFile = filename;
197: }
198:
199: }
200:
201: /**
202: * A panel with a search box that calls the simple search class.
203: */
204: class SearchPanel extends Panel {
205: Button searchBtn;
206: TextField searchTF;
207: SimpleSearch ss;
208:
209: /** C'tor */
210: public SearchPanel(SimpleSearch ss) {
211: this .ss = ss;
212: searchBtn = new Button("Search");
213: searchTF = new TextField("java", 40);
214: setLayout(new FlowLayout(FlowLayout.CENTER));
215: add(searchTF);
216: add(searchBtn);
217: }
218:
219: public void processEvent(AWTEvent e) {
220: if (e.getID() == Event.ACTION_EVENT) {
221: searchTF.setEditable(false);
222: searchBtn.setEnabled(false);
223: ss.doSearch(searchTF.getText());
224: searchTF.setEditable(true);
225: searchBtn.setEnabled(true);
226: } else
227: super .processEvent(e);
228: }
229: }
230:
231: /**
232: * Applet/application for simple query interface. Can be used as an
233: * example for those who want to create their own java interface.
234: * This example demonstrates search only. Browse, determining
235: * the schema of the compass server and obtaining the taxonomy
236: * of the compass server will be demonstrated in other examples.
237: */
238: public class SearchDemo extends Applet {
239: /** Run as an applet. */
240: public void init() {
241: String rdm = getParameter("RDMServer");
242: String csidstr = getParameter("CSID");
243: SimpleSearch ss = new SimpleSearch(rdm, csidstr);
244: SearchPanel sp = new SearchPanel(ss);
245: setLayout(new FlowLayout(FlowLayout.CENTER));
246: add(sp);
247: }
248:
249: /** Run as an application. */
250: public static void main(String argv[]) {
251: int args = argv.length;
252: String SOIFOutputFile = null;
253:
254: if (args != 1 && args != 2 && args != 3) {
255: System.out
256: .println("args: RDMServer [query] [soif_output_file_name]");
257: return;
258: }
259:
260: String rdm = argv[0]; // rdm server, eg, http://compass:port
261: String csidstr = null; // server will use default csid
262:
263: SimpleSearch ss = new SimpleSearch(rdm, csidstr);
264:
265: if (args == 3) {
266: --args;
267: ss.setSOIFfile(argv[2]); // dump raw soif results to this file
268: }
269:
270: if (args == 1) {
271: // run from a search box
272: Frame f = new Frame();
273: SearchPanel sp = new SearchPanel(ss);
274: f.add(sp);
275: f.pack();
276: f.show();
277: } else {
278: // run from command line
279: String query = argv[1];
280: ss.doSearch(query);
281: }
282: }
283:
284: }
|