samer@0: /* samer@0: * ConsoleEnvironment.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS IS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.core.util; samer@0: import samer.core.*; samer@0: import java.io.*; samer@0: samer@0: /** samer@0: * Environment which provides access to the user samer@0: * using the standard input and output samer@0: */ samer@0: samer@0: public class ConsoleEnvironment extends Environment samer@0: { samer@0: BufferedReader in; samer@0: samer@0: public ConsoleEnvironment(Environment parent) { samer@0: super(parent,parent.node()); samer@0: in=new BufferedReader(new InputStreamReader(System.in)); samer@0: } samer@0: samer@0: public Datum datum() { return new Texton("",Null); } // ?? samer@0: public Datum datum(String name) { samer@0: // should check if use is willing to supply value samer@0: // if not return Null (or bad score) samer@0: return new Texton(rel(name),parent.datum(abs(name))); samer@0: } samer@0: samer@0: private class Texton implements Datum samer@0: { samer@0: String name; // name of this Binding samer@0: Datum inherited; // parent's binding samer@0: samer@0: public Texton(String nm, Datum inh) { name=nm; inherited=inh; } samer@0: samer@0: public String name() { return this.name; } samer@0: public int score() { return 32; } // less than perfect match samer@0: samer@0: public Object get(Codec c, Object def) samer@0: { samer@0: if (def==null) def=inherited.get(c,def); samer@0: samer@0: Shell.push(parent); samer@0: try { samer@0: System.out.println("Enter value for "+name); samer@0: if (def!=null) { samer@0: System.out.println("Default value is: " + def); samer@0: } samer@0: String instring=in.readLine(); samer@0: samer@0: if (instring.equals(".")) { samer@0: System.out.println("returning: "+def); samer@0: return def; samer@0: } samer@0: return c.decode(instring); samer@0: } catch (Exception ex) { samer@0: Shell.trace(ex.toString()); samer@0: throw new Error("get "+name); samer@0: } finally { Shell.pop(); } samer@0: } samer@0: samer@0: public void get(Autocoder obj) samer@0: { samer@0: Shell.push(parent); samer@0: try { samer@0: System.out.println("Enter value for "+name); samer@0: System.out.println("Current value is: " + obj.string()); samer@0: String instring=in.readLine(); samer@0: if (!instring.equals(".")) obj.decode(instring); samer@0: } catch (Exception ex) { samer@0: Shell.trace(ex.toString()); samer@0: throw new Error("get "+name); samer@0: } finally { Shell.pop(); } samer@0: } samer@0: } samer@0: } samer@0: