samer@0
|
1 /*
|
samer@0
|
2 * ConsoleEnvironment.java
|
samer@0
|
3 *
|
samer@0
|
4 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
5 * All rights reserved.
|
samer@0
|
6 *
|
samer@0
|
7 * This software is provided AS IS and WITHOUT ANY WARRANTY;
|
samer@0
|
8 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
9 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
10 */
|
samer@0
|
11
|
samer@0
|
12 package samer.core.util;
|
samer@0
|
13 import samer.core.*;
|
samer@0
|
14 import java.io.*;
|
samer@0
|
15
|
samer@0
|
16 /**
|
samer@0
|
17 * Environment which provides access to the user
|
samer@0
|
18 * using the standard input and output
|
samer@0
|
19 */
|
samer@0
|
20
|
samer@0
|
21 public class ConsoleEnvironment extends Environment
|
samer@0
|
22 {
|
samer@0
|
23 BufferedReader in;
|
samer@0
|
24
|
samer@0
|
25 public ConsoleEnvironment(Environment parent) {
|
samer@0
|
26 super(parent,parent.node());
|
samer@0
|
27 in=new BufferedReader(new InputStreamReader(System.in));
|
samer@0
|
28 }
|
samer@0
|
29
|
samer@0
|
30 public Datum datum() { return new Texton("",Null); } // ??
|
samer@0
|
31 public Datum datum(String name) {
|
samer@0
|
32 // should check if use is willing to supply value
|
samer@0
|
33 // if not return Null (or bad score)
|
samer@0
|
34 return new Texton(rel(name),parent.datum(abs(name)));
|
samer@0
|
35 }
|
samer@0
|
36
|
samer@0
|
37 private class Texton implements Datum
|
samer@0
|
38 {
|
samer@0
|
39 String name; // name of this Binding
|
samer@0
|
40 Datum inherited; // parent's binding
|
samer@0
|
41
|
samer@0
|
42 public Texton(String nm, Datum inh) { name=nm; inherited=inh; }
|
samer@0
|
43
|
samer@0
|
44 public String name() { return this.name; }
|
samer@0
|
45 public int score() { return 32; } // less than perfect match
|
samer@0
|
46
|
samer@0
|
47 public Object get(Codec c, Object def)
|
samer@0
|
48 {
|
samer@0
|
49 if (def==null) def=inherited.get(c,def);
|
samer@0
|
50
|
samer@0
|
51 Shell.push(parent);
|
samer@0
|
52 try {
|
samer@0
|
53 System.out.println("Enter value for "+name);
|
samer@0
|
54 if (def!=null) {
|
samer@0
|
55 System.out.println("Default value is: " + def);
|
samer@0
|
56 }
|
samer@0
|
57 String instring=in.readLine();
|
samer@0
|
58
|
samer@0
|
59 if (instring.equals(".")) {
|
samer@0
|
60 System.out.println("returning: "+def);
|
samer@0
|
61 return def;
|
samer@0
|
62 }
|
samer@0
|
63 return c.decode(instring);
|
samer@0
|
64 } catch (Exception ex) {
|
samer@0
|
65 Shell.trace(ex.toString());
|
samer@0
|
66 throw new Error("get "+name);
|
samer@0
|
67 } finally { Shell.pop(); }
|
samer@0
|
68 }
|
samer@0
|
69
|
samer@0
|
70 public void get(Autocoder obj)
|
samer@0
|
71 {
|
samer@0
|
72 Shell.push(parent);
|
samer@0
|
73 try {
|
samer@0
|
74 System.out.println("Enter value for "+name);
|
samer@0
|
75 System.out.println("Current value is: " + obj.string());
|
samer@0
|
76 String instring=in.readLine();
|
samer@0
|
77 if (!instring.equals(".")) obj.decode(instring);
|
samer@0
|
78 } catch (Exception ex) {
|
samer@0
|
79 Shell.trace(ex.toString());
|
samer@0
|
80 throw new Error("get "+name);
|
samer@0
|
81 } finally { Shell.pop(); }
|
samer@0
|
82 }
|
samer@0
|
83 }
|
samer@0
|
84 }
|
samer@0
|
85
|