Debian Codegraph @

src/dr/xml/UserInput.java

raw symbols SCIP
1 /*
2 * UserInput.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.xml;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31
32 public class UserInput {
33
34 public static AbstractXMLObjectParser STRING_PARSER = new AbstractXMLObjectParser() {
35
36 public String getParserName() { return "string"; }
37
38 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
39
40 if (xo.hasAttribute("prompt")) {
41 String prompt = xo.getStringAttribute("prompt");
42 System.out.print(prompt+": ");
43 System.out.flush();
44 return input.readString();
45 } else {
46 return xo.getChild(String.class);
47 }
48 }
49
50 //************************************************************************
51 // AbstractXMLObjectParser implementation
52 //************************************************************************
53
54 public String getParserDescription() {
55 return "returns a String. If a prompt attribute exists then the user is prompted for input, otherwise the character contents of the element are returned.";
56 }
57
58 public Class getReturnType() { return String.class; }
59
60
61 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
62
63 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
64 new XORRule(
65 new StringAttributeRule(
66 "prompt",
67 "A message displayed to the user when entering a value for this string",
68 "Enter the name of a dinosaur:"),
69 new ElementRule(String.class))
70 };
71 };
72
73 public static AbstractXMLObjectParser DOUBLE_PARSER = new AbstractXMLObjectParser() {
74
75 public String getParserName() { return "double"; }
76
77 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
78
79 if (xo.hasAttribute("prompt")) {
80 String prompt = xo.getStringAttribute("prompt");
81 System.out.print(prompt+": ");
82 System.out.flush();
83 return new Double(input.readDouble());
84 } else {
85 return xo.getChild(Double.class);
86 }
87 }
88
89 //************************************************************************
90 // AbstractXMLObjectParser implementation
91 //************************************************************************
92
93 public String getParserDescription() {
94 return "returns a Double. If a prompt attribute exists then the user is prompted for input, otherwise the character contents of the element are returned as a Double.";
95 }
96
97 public Class getReturnType() { return Double.class; }
98
99 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
100
101 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
102 new XORRule(
103 new StringAttributeRule(
104 "prompt",
105 "A message displayed to the user when entering a value for this double",
106 "Enter the length of a piece of string (in metres):"),
107 new ElementRule(Double.class))
108 };
109 };
110
111 public static AbstractXMLObjectParser INTEGER_PARSER = new AbstractXMLObjectParser() {
112
113 public String getParserName() { return "integer"; }
114
115 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
116
117 if (xo.hasAttribute("prompt")) {
118 String prompt = xo.getStringAttribute("prompt");
119 System.out.print(prompt+": ");
120 System.out.flush();
121 return new Integer(input.readInteger());
122 } else {
123 return xo.getChild(Integer.class);
124 }
125 }
126
127 //************************************************************************
128 // AbstractXMLObjectParser implementation
129 //************************************************************************
130
131 public String getParserDescription() {
132 return "returns an Integer. If a prompt attribute exists then the user is prompted for input, otherwise the character contents of the element are returned as an Integer.";
133 }
134
135 public Class getReturnType() { return Integer.class; }
136
137 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
138
139 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
140 new XORRule(
141 new StringAttributeRule(
142 "prompt",
143 "A message displayed to the user when entering a value for this integer",
144 "Enter the number of categories:"),
145 new ElementRule(Integer.class))
146 };
147 };
148
149 static KeyboardInput input = new KeyboardInput();
150 }
151
152 /**
153 * A simple input class to read values typed at the command line. If an error
154 * occurs during input, any exceptions thrown are caught and a default value
155 * returned.
156 *
157 *@author Graham Roberts
158 *@author Russel Winder
159 *@version 1.2 Oct 2002
160 */
161 class KeyboardInput
162 {
163 /**
164 * The buffered stream that connects to the keyboard so that we can read
165 * from it sensibly.
166 */
167 private final BufferedReader in =
168 new BufferedReader(new InputStreamReader(System.in));
169
170 /**
171 * Read an <CODE>int</CODE> value from keyboard input.
172 *
173 *@return The integer value read in, or zero if the input was invalid.
174 */
175 public final synchronized int readInteger()
176 {
177 String input = "";
178 int value = 0;
179 try
180 {
181 input = in.readLine();
182 }
183 catch (IOException e)
184 {}
185 if (input != null)
186 {
187 try
188 {
189 value = Integer.parseInt(input);
190 }
191 catch (NumberFormatException e)
192 {}
193 }
194 return value;
195 }
196
197 /**
198 * Read a <CODE>long</CODE> value from keyboard input.
199 *
200 *@return The long value read in, or 0L if the input was invalid.
201 */
202 public final synchronized long readLong()
203 {
204 String input = "";
205 long value = 0L;
206 try
207 {
208 input = in.readLine();
209 }
210 catch (IOException e)
211 {}
212 if (input != null)
213 {
214 try
215 {
216 value = Long.parseLong(input);
217 }
218 catch (NumberFormatException e)
219 {}
220 }
221 return value;
222 }
223
224 /**
225 * Read a <CODE>double</CODE> value from keyboard input.
226 *
227 *@return The double value read in, or 0.0 if the input was invalid.
228 */
229 public final synchronized double readDouble()
230 {
231 String input = "";
232 double value = 0.0D;
233 try
234 {
235 input = in.readLine();
236 }
237 catch (IOException e)
238 {}
239 if (input != null)
240 {
241 try
242 {
243 value = Double.parseDouble(input);
244 }
245 catch (NumberFormatException e)
246 {}
247 }
248 return value;
249 }
250
251 /**
252 * Read a <CODE>float</CODE> value from keyboard input.
253 *
254 *@return The float value read in, or 0.0F if the input was invalid.
255 */
256 public final synchronized float readFloat()
257 {
258 String input = "";
259 float value = 0.0F;
260 try
261 {
262 input = in.readLine();
263 }
264 catch (IOException e)
265 {}
266 if (input != null)
267 {
268 try
269 {
270 value = Float.parseFloat(input);
271 }
272 catch (NumberFormatException e)
273 {}
274 }
275 return value;
276 }
277
278 /**
279 * Read a <CODE>char</CODE> value from keyboard input.
280 *
281 *@return The char value read in, or ' ' (space) if the input was invalid.
282 */
283 public final synchronized char readCharacter()
284 {
285 char c = ' ';
286 try
287 {
288 c = (char) in.read();
289 }
290 catch (IOException e)
291 {}
292 return c;
293 }
294
295 /**
296 * Read an <CODE>String</CODE> value from keyboard input.
297 *
298 *@return The String value read in.
299 */
300 public final synchronized String readString()
301 {
302 String s = "";
303 try
304 {
305 s = in.readLine();
306 }
307 catch (IOException e)
308 {}
309 if (s == null)
310 {
311 s = "";
312 }
313 return s;
314 }
315 }
316
317
318