samer@0: /* samer@0: * X.java samer@0: * samer@0: * Copyright (c) 2001, 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; samer@0: import java.io.StringWriter; samer@0: import java.util.StringTokenizer; samer@0: import java.awt.Font; samer@0: import java.awt.Rectangle; samer@0: import java.awt.Color; samer@0: import java.awt.SystemColor; samer@0: samer@0: import samer.core.Environment.Datum; samer@0: import samer.core.Environment.Codec; samer@0: import samer.core.Environment.Autocoder; samer@0: samer@0: /** samer@0: Static methods for converting objects and extracting correctly typed samer@0: values from Environments. Short name samer@0: because of expected frequent use! Also, contains samer@0: several Codec classes. samer@0: @see samer.core.Environment samer@0: @see samer.core.Environment.Datum samer@0: @see samer.core.Environment.Autocoder samer@0: */ samer@0: samer@0: public class X samer@0: { samer@0: /** Tries to extract an Object from the given Datum, throws Exception in failure */ samer@0: public static Object object(Datum b) throws Exception { samer@0: Object ret=b.get(ObjectCodec,null); samer@0: if (ret==null) throw new Exception("no data"); samer@0: return ret; samer@0: } samer@0: samer@0: /** Tries to extract a String given Datum */ samer@0: public static String string(Datum b) throws Exception { samer@0: String ret=(String)b.get(StringCodec,null); samer@0: if (ret==null) throw new Exception("datum has no value"); samer@0: return ret; samer@0: } samer@0: samer@0: /** Tries to extract a Font given Datum. If this fails, tries samer@0: to decode def using FontCodec. def can be a Font or a String. */ samer@0: public static Font font(Datum b, Object def) { samer@0: return (Font)b.get(FontCodec,def); samer@0: } samer@0: samer@0: /** Tries to extract a Rectangle from given Datum. If this fails, tries samer@0: to decode def as a rectangle. */ samer@0: public static Rectangle rect(Datum b, Rectangle def) { samer@0: return (Rectangle)b.get(RectangleCodec,def); samer@0: } samer@0: samer@0: /** Get String from Datum, returning def if this fails */ samer@0: public static String string(Datum b,String def) { samer@0: return (String)b.get(StringCodec,def); samer@0: } samer@0: samer@0: /** Get Color from Datum, decoding def if this fails. def can samer@0: be a Color or a String. See ColorCodec. */ samer@0: public static Color color(Datum b, Object def) { samer@0: return (Color)b.get(ColorCodec,def); samer@0: } samer@0: samer@0: /** Extract int from Datum */ samer@0: public static int _int(Datum b,int def) { samer@0: return ((Integer)b.get(IntegerCodec,new Integer(def))).intValue(); samer@0: } samer@0: /** Extract double from Datum */ samer@0: public static double _double(Datum b,double def) { samer@0: return ((Double)b.get(DoubleCodec,new Double(def))).doubleValue(); samer@0: } samer@0: /** Extract boolean from Datum */ samer@0: public static boolean _bool(Datum b,boolean def) { samer@0: return ((Boolean)b.get(BooleanCodec,new Boolean(def))).booleanValue(); samer@0: } samer@0: samer@0: /** store given object to current environment */ samer@0: public static void store(String name, Object value) { samer@0: Shell.env().store(name,value,codec(value)); samer@0: } samer@0: samer@0: /** Extracts an integer array from a string. Valid strings are, eg samer@0: "(1,2,3)", "(4 5 6)", "7 8 9", "10, 11, 12" */ samer@0: public static int[] parseIntArray(String s, int n) samer@0: { samer@0: StringTokenizer token = new StringTokenizer(s,"(), "); samer@0: int[] a=new int[n]; samer@0: samer@0: for (int i=0; i0; i--) { samer@0: if (classes[i].isAssignableFrom(c)) return codecs[i]; samer@0: } samer@0: return codecs[0]; samer@0: } samer@0: samer@0: private static Class[] classes = { samer@0: Object.class, samer@0: Font.class, samer@0: Rectangle.class, samer@0: Color.class, samer@0: String.class, samer@0: Boolean.class, samer@0: Integer.class, samer@0: Double.class samer@0: }; samer@0: samer@0: private static Codec[] codecs = { samer@0: ObjectCodec, samer@0: FontCodec, samer@0: RectangleCodec, samer@0: ColorCodec, samer@0: StringCodec, samer@0: BooleanCodec, samer@0: IntegerCodec, samer@0: DoubleCodec samer@0: }; samer@0: } samer@0: