KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jline > example > Example


1 /**
2  * jline - Java console input library
3  * Copyright (c) 2002,2003 Marc Prud'hommeaux marc@apocalypse.org
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jline.example;
20
21 import jline.*;
22 import java.io.*;
23 import java.util.*;
24 import java.util.zip.*;
25
26 public class Example
27 {
28     public static void usage ()
29     {
30         System.out.println ("Usage: java " + Example.class.getName ()
31             + " [none/simple/files/dictionary]");
32         System.out.println (" none - no completors");
33         System.out.println (" simple - a simple completor that comples "
34             + "\"foo\", \"bar\", and \"baz\"");
35         System.out.println (" files - a completor that comples "
36             + "file names");
37         System.out.println (" dictionary - a completor that comples "
38             + "english dictionary words");
39         System.out.println (" classes - a completor that comples "
40             + "java class names");
41     }
42
43
44     public static void main (String JavaDoc [] args)
45         throws IOException
46     {
47         ConsoleReader reader = new ConsoleReader ();
48         reader.setDebug (new PrintWriter (new FileWriter ("writer.debug",
49             true)));
50
51         if (args == null || args.length == 0)
52         {
53             usage ();
54             return;
55         }
56
57         List completors = new LinkedList ();
58         for (int i = 0; args != null && i < args.length; i++)
59         {
60             if (args [i].equals ("none"))
61             {
62             }
63             else if (args [i].equals ("files"))
64             {
65                 completors.add (new FileNameCompletor ());
66             }
67             else if (args [i].equals ("classes"))
68             {
69                 completors.add (new ClassNameCompletor ());
70             }
71             else if (args [i].equals ("dictionary"))
72             {
73                 completors.add (
74                     new SimpleCompletor (new GZIPInputStream (
75                         Example.class.getResourceAsStream ("english.gz"))));
76             }
77             else if (args [i].equals ("simple"))
78             {
79                 completors.add (
80                     new SimpleCompletor (
81                         new String JavaDoc [] { "foo", "bar", "baz"}));
82             }
83             else
84             {
85                 usage ();
86                 return;
87             }
88         }
89
90         reader.addCompletor (new ArgumentCompletor (completors));
91                     
92
93         String JavaDoc line;
94         PrintWriter out = new PrintWriter (System.out);
95     
96         while ((line = reader.readLine ("prompt> ")) != null)
97         {
98             out.println ("======>\"" + line + "\"");
99             out.flush ();
100         }
101     }
102 }
103
Popular Tags