view 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
line wrap: on
line source
/*
 *	ConsoleEnvironment.java	
 *
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS IS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.core.util;
import  samer.core.*;
import  java.io.*;

/**	
  *	Environment which provides access to the user
  *	using the standard input and output
  */

public class ConsoleEnvironment extends Environment
{
	BufferedReader	in;

	public ConsoleEnvironment(Environment parent) {
		super(parent,parent.node());
		in=new BufferedReader(new InputStreamReader(System.in));
	}

	public Datum datum() { return new Texton("",Null); } // ??
	public Datum datum(String name) {
		// should check if use is willing to supply value
		// if not return Null (or bad score)
		return new Texton(rel(name),parent.datum(abs(name))); 
	}

	private class Texton implements Datum 
	{
		String	name;			// name of this Binding
		Datum		inherited;	// parent's binding

		public Texton(String nm, Datum inh) { name=nm; inherited=inh; }

		public String name()  { return this.name; }
		public int	  score() { return 32; } // less than perfect match

		public Object get(Codec c, Object def)
		{
			if (def==null) def=inherited.get(c,def);

			Shell.push(parent);
			try {
				System.out.println("Enter value for "+name);
				if (def!=null) {
					System.out.println("Default value is: " + def);
				}
				String instring=in.readLine();

				if (instring.equals(".")) {
					System.out.println("returning: "+def);
					return def;
				}
				return c.decode(instring);
			} catch (Exception ex) { 
				Shell.trace(ex.toString());
				throw new Error("get "+name);
			} finally { Shell.pop(); }
		}

		public void get(Autocoder obj)
		{
			Shell.push(parent);
			try {
				System.out.println("Enter value for "+name);
				System.out.println("Current value is: " + obj.string());
				String instring=in.readLine();
				if (!instring.equals(".")) obj.decode(instring);
			} catch (Exception ex) { 
				Shell.trace(ex.toString());
				throw new Error("get "+name);
			} finally { Shell.pop(); }
		}
	}
}