| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
class KeyboardInput |
| 162 |
{ |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
private final BufferedReader in = |
| 168 |
new BufferedReader(new InputStreamReader(System.in)); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 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 |
|
| 199 |
|
| 200 |
|
| 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 |
|
| 226 |
|
| 227 |
|
| 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 |
|
| 253 |
|
| 254 |
|
| 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 |
|
| 280 |
|
| 281 |
|
| 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 |
|
| 297 |
|
| 298 |
|
| 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 |
|