1   /**
2    *	jline - Java console input library
3    *	Copyright (c) 2002, 2003, 2004, 2005, Marc Prud'hommeaux mwp1@cornell.edu
4    *	All rights reserved.
5    *
6    *	Redistribution and use in source and binary forms, with or
7    *	without modification, are permitted provided that the following
8    *	conditions are met:
9    *
10   *	Redistributions of source code must retain the above copyright
11   *	notice, this list of conditions and the following disclaimer.
12   *
13   *	Redistributions in binary form must reproduce the above copyright
14   *	notice, this list of conditions and the following disclaimer
15   *	in the documentation and/or other materials provided with
16   *	the distribution.
17   *
18   *	Neither the name of JLine nor the names of its contributors
19   *	may be used to endorse or promote products derived from this
20   *	software without specific prior written permission.
21   *
22   *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   *	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24   *	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25   *	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26   *	EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
27   *	FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28   *	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29   *	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30   *	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31   *	AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32   *	LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33   *	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34   *	OF THE POSSIBILITY OF SUCH DAMAGE.
35   */
36  package jline.example;
37  
38  import jline.*;
39  import java.io.*;
40  import java.util.*;
41  import java.util.zip.*;
42  
43  public class Example
44  {
45  	public static void usage ()
46  	{
47  		System.out.println ("Usage: java " + Example.class.getName ()
48  			+ " [none/simple/files/dictionary [trigger mask]]");
49  		System.out.println ("  none - no completors");
50  		System.out.println ("  simple - a simple completor that comples "
51  			+ "\"foo\", \"bar\", and \"baz\"");
52  		System.out.println ("  files - a completor that comples "
53  			+ "file names");
54  		System.out.println ("  dictionary - a completor that comples "
55  			+ "english dictionary words");
56  		System.out.println ("  classes - a completor that comples "
57  			+ "java class names");
58  		System.out.println ("  trigger - a special word which causes it to assume "
59  				+ "the next line is a password");
60  		System.out.println ("  mask - is the character to print in place of "
61  				+ "the actual password character");
62  		System.out.println ("\n  E.g - java Example simple su '*'\n"
63  				+ "will use the simple compleator with 'su' triggering\n"
64  				+ "the use of '*' as a password mask.");
65  	}
66  
67  
68  	public static void main (String [] args)
69  		throws IOException
70  	{
71  		Character mask = null;
72  		String trigger = null;
73  		
74  		ConsoleReader reader = new ConsoleReader ();
75  		reader.setDebug (new PrintWriter (new FileWriter ("writer.debug",
76  			true)));
77  
78  		if (args == null || args.length == 0)
79  		{
80  			usage ();
81  			return;
82  		}
83  
84  		List completors = new LinkedList ();
85  		if (args.length > 0) 
86  		{			
87  			if (args [0].equals ("none"))
88  			{
89  			}
90  			else if (args [0].equals ("files"))
91  			{
92  				completors.add (new FileNameCompletor ());
93  			}
94  			else if (args [0].equals ("classes"))
95  			{
96  				completors.add (new ClassNameCompletor ());
97  			}
98  			else if (args [0].equals ("dictionary"))
99  			{
100 				completors.add (
101 					new SimpleCompletor (new GZIPInputStream (
102 						Example.class.getResourceAsStream ("english.gz"))));
103 			}
104 			else if (args [0].equals ("simple"))
105 			{
106 				completors.add (
107  		 			new SimpleCompletor (
108 						new String [] { "foo", "bar", "baz"}));
109 			}
110 			else
111 			{
112 				usage ();
113 				return;
114 			}
115 		}
116 		
117 		if (args.length == 3) 
118 		{
119 			mask = new Character(args[2].charAt(0));
120 			trigger = args[1];
121 		}
122 		
123 		reader.addCompletor (new ArgumentCompletor (completors));
124 					
125 
126 		String line;
127 		PrintWriter out = new PrintWriter (System.out);
128 	
129 		while ((line = reader.readLine ("prompt> ")) != null)
130 		{
131 			out.println ("======>\"" + line + "\"");
132 			out.flush ();
133 			// If we input the special word then we will mask
134 			// the next line.
135 			if (trigger != null && line.compareTo(trigger) == 0) {
136 				line = reader.readLine("password> ", mask);
137 			}
138 		}
139 	}
140 }