/[hyperestraier]/upstream/0.5.2/java/Call.java
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /upstream/0.5.2/java/Call.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (hide annotations)
Wed Aug 3 15:21:15 2005 UTC (18 years, 10 months ago) by dpavlin
File size: 22352 byte(s)
import upstream version 0.5.2

1 dpavlin 9 /*************************************************************************************************
2     * Java interface of Hyper Estraier
3     * Copyright (C) 2004-2005 Mikio Hirabayashi
4     * All rights reserved.
5     * This file is part of Hyper Estraier.
6     * Redistribution and use in source and binary forms, with or without modification, are
7     * permitted provided that the following conditions are met:
8     *
9     * * Redistributions of source code must retain the above copyright notice, this list of
10     * conditions and the following disclaimer.
11     * * Redistributions in binary form must reproduce the above copyright notice, this list of
12     * conditions and the following disclaimer in the documentation and/or other materials
13     * provided with the distribution.
14     * * Neither the name of Mikio Hirabayashi nor the names of its contributors may be used to
15     * endorse or promote products derived from this software without specific prior written
16     * permission.
17     *
18     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
19     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20     * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21     * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25     * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26     * OF THE POSSIBILITY OF SUCH DAMAGE.
27     *************************************************************************************************/
28    
29    
30     package estraier;
31    
32     import java.util.*;
33     import java.io.*;
34     import java.net.*;
35    
36    
37    
38     /**
39     * Command line utility for the node API.
40     */
41     public class Call {
42     //----------------------------------------------------------------
43     // class fields
44     //----------------------------------------------------------------
45     /* class constants */
46     private static final int SEARCHMAX = 10;
47     private static final PrintStream stdout = System.out;
48     private static final PrintStream stderr = System.err;
49     /* class variables */
50     private static String pxhost = null;
51     private static int pxport = 0;
52     private static int timeout = -1;
53     private static String authname = null;
54     private static String authpass = null;
55     private static int condopts = 0;
56     //----------------------------------------------------------------
57     // public static methods
58     //----------------------------------------------------------------
59     /**
60     * Main routine of the command.
61     * @param args command line arguments. The first argument specifies the name of the sub
62     * command. "put", "out", "get", "uriid", "inform", "search", "setuser", and "setlink" are
63     * is provided.
64     */
65     public static void main(String[] args){
66     int rv = 0;
67     if(args.length < 1) usage();
68     if(args[0].equals("put")){
69     rv = runput(args);
70     } else if(args[0].equals("out")){
71     rv = runout(args);
72     } else if(args[0].equals("get")){
73     rv = runget(args);
74     } else if(args[0].equals("uriid")){
75     rv = runuriid(args);
76     } else if(args[0].equals("inform")){
77     rv = runinform(args);
78     } else if(args[0].equals("search")){
79     rv = runsearch(args);
80     } else if(args[0].equals("setuser")){
81     rv = runsetuser(args);
82     } else if(args[0].equals("setlink")){
83     rv = runsetlink(args);
84     } else {
85     usage();
86     }
87     System.exit(rv);
88     }
89     //----------------------------------------------------------------
90     // private static methods
91     //----------------------------------------------------------------
92     /* print the usage and exit */
93     private static void usage(){
94     stderr.println("command line utility for the node API of Hyper Estraier");
95     stderr.println("");
96     stderr.println("synopsys: ");
97     stderr.println(" java " + Call.class.getName() + " arguments...");
98     stderr.println("");
99     stderr.println("arguments: ");
100     stderr.println(" put [-proxy host:port] [-tout num] [-auth user pass]" +
101     " nurl [file]");
102     stderr.println(" out [-proxy host:port] [-tout num] [-auth user pass]" +
103     " nurl expr");
104     stderr.println(" get [-proxy host:port] [-tout num] [-auth user pass]" +
105     " nurl expr [attr]");
106     stderr.println(" uriid [-proxy host:port] [-tout num] [-auth user pass]" +
107     " nurl uri");
108     stderr.println(" inform [-proxy host:port] [-tout num] [-auth user pass]" +
109     " nurl");
110     stderr.println(" search [-proxy host:port] [-tout num] [-auth user pass]" +
111     " [-sf] [-attr expr] [-ord expr] [-max num] [-dpt num] nurl [phrase]");
112     stderr.println(" setuser [-proxy host:port] [-tout num] [-auth user pass]" +
113     " nurl name mode");
114     stderr.println(" setlink [-proxy host:port] [-tout num] [-auth user pass]" +
115     " nurl url label credit");
116     stderr.println("");
117     System.exit(1);
118     }
119     /* print error string and flush the buffer */
120     private static void printerror(String msg){
121     stderr.println(Call.class.getName() + ": " + msg);
122     stderr.flush();
123     }
124     /* parse arguments of the put command */
125     private static int runput(String[] args){
126     String nurl = null;
127     String file = null;
128     for(int i = 1; i < args.length; i++){
129     String arg = args[i];
130     if(nurl == null && arg.startsWith("-")){
131     if(arg.equals("-proxy")){
132     if(++i >= args.length) usage();
133     pxhost = args[i];
134     if(++i >= args.length) usage();
135     pxport = Integer.parseInt(args[i]);
136     } else if(arg.equals("-tout")){
137     if(++i >= args.length) usage();
138     timeout = Integer.parseInt(args[i]);
139     } else if(arg.equals("-auth")){
140     if(++i >= args.length) usage();
141     authname = args[i];
142     if(++i >= args.length) usage();
143     authpass = args[i];
144     } else {
145     usage();
146     }
147     } else if(nurl == null){
148     nurl = arg;
149     } else if(file == null){
150     file = arg;
151     } else {
152     usage();
153     }
154     }
155     if(nurl == null) usage();
156     return procput(nurl, file);
157     }
158     /* parse arguments of the out command */
159     private static int runout(String[] args){
160     String nurl = null;
161     String expr = null;
162     for(int i = 1; i < args.length; i++){
163     String arg = args[i];
164     if(nurl == null && arg.startsWith("-")){
165     if(arg.equals("-proxy")){
166     if(++i >= args.length) usage();
167     pxhost = args[i];
168     if(++i >= args.length) usage();
169     pxport = Integer.parseInt(args[i]);
170     } else if(arg.equals("-tout")){
171     if(++i >= args.length) usage();
172     timeout = Integer.parseInt(args[i]);
173     } else if(arg.equals("-auth")){
174     if(++i >= args.length) usage();
175     authname = args[i];
176     if(++i >= args.length) usage();
177     authpass = args[i];
178     } else {
179     usage();
180     }
181     } else if(nurl == null){
182     nurl = arg;
183     } else if(expr == null){
184     expr = arg;
185     } else {
186     usage();
187     }
188     }
189     if(nurl == null || expr == null) usage();
190     return procout(nurl, expr);
191     }
192     /* parse arguments of the get command */
193     private static int runget(String[] args){
194     String nurl = null;
195     String expr = null;
196     String attr = null;
197     for(int i = 1; i < args.length; i++){
198     String arg = args[i];
199     if(nurl == null && arg.startsWith("-")){
200     if(arg.equals("-proxy")){
201     if(++i >= args.length) usage();
202     pxhost = args[i];
203     if(++i >= args.length) usage();
204     pxport = Integer.parseInt(args[i]);
205     } else if(arg.equals("-tout")){
206     if(++i >= args.length) usage();
207     timeout = Integer.parseInt(args[i]);
208     } else if(arg.equals("-auth")){
209     if(++i >= args.length) usage();
210     authname = args[i];
211     if(++i >= args.length) usage();
212     authpass = args[i];
213     } else {
214     usage();
215     }
216     } else if(nurl == null){
217     nurl = arg;
218     } else if(expr == null){
219     expr = arg;
220     } else if(attr == null){
221     attr = arg;
222     } else {
223     usage();
224     }
225     }
226     if(nurl == null || expr == null) usage();
227     return procget(nurl, expr, attr);
228     }
229     /* parse arguments of the uriid command */
230     private static int runuriid(String[] args){
231     String nurl = null;
232     String uri = null;
233     for(int i = 1; i < args.length; i++){
234     String arg = args[i];
235     if(nurl == null && arg.startsWith("-")){
236     if(arg.equals("-proxy")){
237     if(++i >= args.length) usage();
238     pxhost = args[i];
239     if(++i >= args.length) usage();
240     pxport = Integer.parseInt(args[i]);
241     } else if(arg.equals("-tout")){
242     if(++i >= args.length) usage();
243     timeout = Integer.parseInt(args[i]);
244     } else if(arg.equals("-auth")){
245     if(++i >= args.length) usage();
246     authname = args[i];
247     if(++i >= args.length) usage();
248     authpass = args[i];
249     } else {
250     usage();
251     }
252     } else if(nurl == null){
253     nurl = arg;
254     } else if(uri == null){
255     uri = arg;
256     } else {
257     usage();
258     }
259     }
260     if(nurl == null || uri == null) usage();
261     return procuriid(nurl, uri);
262     }
263     /* parse arguments of the inform command */
264     private static int runinform(String[] args){
265     String nurl = null;
266     for(int i = 1; i < args.length; i++){
267     String arg = args[i];
268     if(nurl == null && arg.startsWith("-")){
269     if(arg.equals("-proxy")){
270     if(++i >= args.length) usage();
271     pxhost = args[i];
272     if(++i >= args.length) usage();
273     pxport = Integer.parseInt(args[i]);
274     } else if(arg.equals("-tout")){
275     if(++i >= args.length) usage();
276     timeout = Integer.parseInt(args[i]);
277     } else if(arg.equals("-auth")){
278     if(++i >= args.length) usage();
279     authname = args[i];
280     if(++i >= args.length) usage();
281     authpass = args[i];
282     } else {
283     usage();
284     }
285     } else if(nurl == null){
286     nurl = arg;
287     } else {
288     usage();
289     }
290     }
291     if(nurl == null) usage();
292     return procinform(nurl);
293     }
294     /* parse arguments of the search command */
295     private static int runsearch(String[] args){
296     String nurl = null;
297     String phrase = null;
298     List attrs = new ArrayList(3);
299     String ord = null;
300     int max = SEARCHMAX;
301     int depth = 0;
302     for(int i = 1; i < args.length; i++){
303     String arg = args[i];
304     if(nurl == null && arg.startsWith("-")){
305     if(arg.equals("-proxy")){
306     if(++i >= args.length) usage();
307     pxhost = args[i];
308     if(++i >= args.length) usage();
309     pxport = Integer.parseInt(args[i]);
310     } else if(arg.equals("-tout")){
311     if(++i >= args.length) usage();
312     timeout = Integer.parseInt(args[i]);
313     } else if(arg.equals("-auth")){
314     if(++i >= args.length) usage();
315     authname = args[i];
316     if(++i >= args.length) usage();
317     authpass = args[i];
318     } else if(arg.equals("-sf")){
319     condopts |= Condition.CONDSIMPLE;
320     } else if(arg.equals("-attr")){
321     if(++i >= args.length) usage();
322     attrs.add(args[i]);
323     } else if(arg.equals("-ord")){
324     if(++i >= args.length) usage();
325     ord = args[i];
326     } else if(arg.equals("-max")){
327     if(++i >= args.length) usage();
328     max = Integer.parseInt(args[i]);
329     } else if(arg.equals("-dpt")){
330     if(++i >= args.length) usage();
331     depth = Integer.parseInt(args[i]);
332     } else {
333     usage();
334     }
335     } else if(nurl == null){
336     nurl = arg;
337     } else if(phrase == null){
338     phrase = arg;
339     } else {
340     phrase = phrase + " " + arg;
341     }
342     }
343     if(nurl == null) usage();
344     return procsearch(nurl, phrase, attrs, ord, max, depth);
345     }
346     /* parse arguments of the setuser command */
347     private static int runsetuser(String[] args){
348     String nurl = null;
349     String name = null;
350     String mstr = null;
351     for(int i = 1; i < args.length; i++){
352     String arg = args[i];
353     if(nurl == null && arg.startsWith("-")){
354     if(arg.equals("-proxy")){
355     if(++i >= args.length) usage();
356     pxhost = args[i];
357     if(++i >= args.length) usage();
358     pxport = Integer.parseInt(args[i]);
359     } else if(arg.equals("-tout")){
360     if(++i >= args.length) usage();
361     timeout = Integer.parseInt(args[i]);
362     } else if(arg.equals("-auth")){
363     if(++i >= args.length) usage();
364     authname = args[i];
365     if(++i >= args.length) usage();
366     authpass = args[i];
367     } else {
368     usage();
369     }
370     } else if(nurl == null){
371     nurl = arg;
372     } else if(name == null){
373     name = arg;
374     } else if(mstr == null){
375     mstr = arg;
376     } else {
377     usage();
378     }
379     }
380     if(nurl == null || name == null || mstr == null) usage();
381     int mode = Integer.parseInt(mstr);
382     if(mode < 0) usage();
383     return procsetuser(nurl, name, mode);
384     }
385     /* parse arguments of the setlink command */
386     private static int runsetlink(String[] args){
387     String nurl = null;
388     String url = null;
389     String label = null;
390     String cstr = null;
391     for(int i = 1; i < args.length; i++){
392     String arg = args[i];
393     if(nurl == null && arg.startsWith("-")){
394     if(arg.equals("-proxy")){
395     if(++i >= args.length) usage();
396     pxhost = args[i];
397     if(++i >= args.length) usage();
398     pxport = Integer.parseInt(args[i]);
399     } else if(arg.equals("-tout")){
400     if(++i >= args.length) usage();
401     timeout = Integer.parseInt(args[i]);
402     } else if(arg.equals("-auth")){
403     if(++i >= args.length) usage();
404     authname = args[i];
405     if(++i >= args.length) usage();
406     authpass = args[i];
407     } else {
408     usage();
409     }
410     } else if(nurl == null){
411     nurl = arg;
412     } else if(url == null){
413     url = arg;
414     } else if(label == null){
415     label = arg;
416     } else if(cstr == null){
417     cstr = arg;
418     } else {
419     usage();
420     }
421     }
422     if(nurl == null || url == null || label == null || cstr == null) usage();
423     int credit = Integer.parseInt(cstr);
424     return procsetlink(nurl, url, label, credit);
425     }
426     /* perform the put command */
427     private static int procput(String nurl, String file){
428     byte[] data = null;
429     if(file != null){
430     FileInputStream fis = null;
431     try {
432     fis = new FileInputStream(file);
433     data = Utility.readAll(fis);
434     } catch(IOException e){
435     printerror(e.toString());
436     return 1;
437     } finally {
438     try {
439     if(fis != null) fis.close();
440     } catch(IOException e){}
441     }
442     } else {
443     try {
444     data = Utility.readAll(System.in);
445     } catch(IOException e){
446     printerror(e.toString());
447     return 1;
448     }
449     }
450     String draft = null;
451     try {
452     draft = new String(data, "UTF-8");
453     } catch(UnsupportedEncodingException e){
454     printerror(e.toString());
455     return 1;
456     }
457     Document doc = new DocumentImpl(draft);
458     Node node = new NodeImpl();
459     node.set_url(nurl);
460     if(pxhost != null) node.set_proxy(pxhost, pxport);
461     if(timeout > 0) node.set_timeout(timeout);
462     if(authname != null) node.set_auth(authname, authpass);
463     if(!node.put_doc(doc)){
464     printerror("failed: " + node.status());
465     return 1;
466     }
467     return 0;
468     }
469     /* perform the out command */
470     private static int procout(String nurl, String expr){
471     int id = -1;
472     try {
473     id = Integer.parseInt(expr);
474     } catch(NumberFormatException e){
475     id = -1;
476     }
477     Node node = new NodeImpl();
478     node.set_url(nurl);
479     if(pxhost != null) node.set_proxy(pxhost, pxport);
480     if(timeout > 0) node.set_timeout(timeout);
481     if(authname != null) node.set_auth(authname, authpass);
482     if(id > 0){
483     if(!node.out_doc(id)){
484     printerror("failed: " + node.status());
485     return 1;
486     }
487     } else {
488     if(!node.out_doc_by_uri(expr)){
489     printerror("failed: " + node.status());
490     return 1;
491     }
492     }
493     return 0;
494     }
495     /* perform the get command */
496     private static int procget(String nurl, String expr, String attr){
497     int id = -1;
498     try {
499     id = Integer.parseInt(expr);
500     } catch(NumberFormatException e){
501     id = -1;
502     }
503     Node node = new NodeImpl();
504     node.set_url(nurl);
505     if(pxhost != null) node.set_proxy(pxhost, pxport);
506     if(timeout > 0) node.set_timeout(timeout);
507     if(authname != null) node.set_auth(authname, authpass);
508     if(attr != null){
509     String value = id > 0 ? node.get_doc_attr(id, attr) : node.get_doc_attr_by_uri(expr, attr);
510     if(value == null){
511     printerror("failed: " + node.status());
512     return 1;
513     }
514     stdout.println(value);
515     } else {
516     Document doc = id > 0 ? node.get_doc(id) : node.get_doc_by_uri(expr);
517     if(doc == null){
518     printerror("failed: " + node.status());
519     return 1;
520     }
521     stdout.print(doc.dump_draft());
522     }
523     return 0;
524     }
525     /* perform the uriid command */
526     private static int procuriid(String nurl, String uri){
527     Node node = new NodeImpl();
528     node.set_url(nurl);
529     if(pxhost != null) node.set_proxy(pxhost, pxport);
530     if(timeout > 0) node.set_timeout(timeout);
531     if(authname != null) node.set_auth(authname, authpass);
532     int id = node.uri_to_id(uri);
533     if(id == -1){
534     printerror("failed: " + node.status());
535     return 1;
536     }
537     stdout.println(id);
538     return 0;
539     }
540     /* perform the inform command */
541     private static int procinform(String nurl){
542     Node node = new NodeImpl();
543     node.set_url(nurl);
544     if(pxhost != null) node.set_proxy(pxhost, pxport);
545     if(timeout > 0) node.set_timeout(timeout);
546     if(authname != null) node.set_auth(authname, authpass);
547     String name = node.name();
548     String label = node.name();
549     int dnum = node.doc_num();
550     int wnum = node.word_num();
551     double size = node.size();
552     if(name == null || label == null || dnum < 0 || wnum < 0 || size < 0.0){
553     printerror("failed: " + node.status());
554     return 1;
555     }
556     stdout.println(name + "\t" + label + "\t" + dnum + "\t" + wnum + "\t" + (long)size);
557     return 0;
558     }
559     /* perform the search command */
560     private static int procsearch(String nurl, String phrase, List attrs, String ord,
561     int max, int depth){
562     Node node = new NodeImpl();
563     node.set_url(nurl);
564     if(pxhost != null) node.set_proxy(pxhost, pxport);
565     if(timeout > 0) node.set_timeout(timeout);
566     if(authname != null) node.set_auth(authname, authpass);
567     Condition cond = new ConditionImpl();
568     if(phrase != null) cond.set_phrase(phrase);
569     Iterator attrsit = attrs.iterator();
570     while(attrsit.hasNext()){
571     cond.add_attr((String)attrsit.next());
572     }
573     if(ord != null) cond.set_order(ord);
574     cond.set_max(max);
575     cond.set_options(condopts);
576     NodeResult nres = node.search(cond, depth);
577     Calendar cal = new GregorianCalendar();
578     String border = "--------[" + cal.getTimeInMillis() + "]--------";
579     if(nres != null){
580     stdout.println(border);
581     String value;
582     if((value = nres.hint("VERSION")) != null) stdout.println("VERSION\t" + value);
583     if((value = nres.hint("NODE")) != null) stdout.println("NODE\t" + value);
584     if((value = nres.hint("HIT")) != null) stdout.println("HIT\t" + value);
585     for(int i = 1;; i++){
586     String key = "HINT#" + i;
587     if((value = nres.hint(key)) != null){
588     stdout.println(key + "\t" + value);
589     } else {
590     break;
591     }
592     }
593     if((value = nres.hint("DOCNUM")) != null) stdout.println("DOCNUM\t" + value);
594     if((value = nres.hint("WORDNUM")) != null) stdout.println("WORDNUM\t" + value);
595     if((value = nres.hint("TIME")) != null) stdout.println("TIME\t" + value);
596     for(int i = 0;; i++){
597     String key = "LINK#" + i;
598     if((value = nres.hint(key)) != null){
599     stdout.println(key + "\t" + value);
600     } else {
601     break;
602     }
603     }
604     if((value = nres.hint("VIEW")) != null) stdout.println("VIEW\t" + value);
605     stdout.println("");
606     for(int i = 0; i < nres.doc_num(); i++){
607     stdout.println(border);
608     ResultDocument rdoc = nres.get_doc(i);
609     List names = rdoc.attr_names();
610     Iterator it = names.iterator();
611     while(it.hasNext()){
612     String name = (String)it.next();
613     stdout.println(name + "=" + rdoc.attr(name));
614     }
615     stdout.println("");
616     stdout.print(rdoc.snippet());
617     }
618     stdout.println(border + ":END");
619     } else {
620     printerror("failed: " + node.status());
621     return 1;
622     }
623     return 0;
624     }
625     /* perform the setuser command */
626     private static int procsetuser(String nurl, String name, int mode){
627     Node node = new NodeImpl();
628     node.set_url(nurl);
629     if(pxhost != null) node.set_proxy(pxhost, pxport);
630     if(timeout > 0) node.set_timeout(timeout);
631     if(authname != null) node.set_auth(authname, authpass);
632     if(!node.set_user(name, mode)){
633     printerror("failed: " + node.status());
634     return 1;
635     }
636     return 0;
637     }
638     /* perform the setlink command */
639     private static int procsetlink(String nurl, String url, String label, int credit){
640     Node node = new NodeImpl();
641     node.set_url(nurl);
642     if(pxhost != null) node.set_proxy(pxhost, pxport);
643     if(timeout > 0) node.set_timeout(timeout);
644     if(authname != null) node.set_auth(authname, authpass);
645     if(!node.set_link(url, label, credit)){
646     printerror("failed: " + node.status());
647     return 1;
648     }
649     return 0;
650     }
651     /* dummy method: hide the constructor */
652     private Call(){}
653     }
654    
655    
656    
657     /* END OF FILE */

  ViewVC Help
Powered by ViewVC 1.1.26