comparison src/samer/core_/util/ConsoleEnvironment.java @ 0:bf79fb79ee13

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