Back to index

moin  1.9.0~rc2
StorableInput.java
Go to the documentation of this file.
00001 /*
00002  * @(#)StorableInput.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.util;
00007 
00008 import java.util.*;
00009 import java.io.*;
00010 import java.awt.Color;
00011 
00020 public  class StorableInput
00021         extends Object {
00022 
00023     private StreamTokenizer fTokenizer;
00024     private Vector          fMap;
00025 
00029     public StorableInput(InputStream stream) {
00030         Reader r = new BufferedReader(new InputStreamReader(stream));
00031         fTokenizer = new StreamTokenizer(r);
00032         fMap = new Vector();
00033     }
00034 
00038     public Storable readStorable() throws IOException {
00039         Storable storable;
00040         String s = readString();
00041 
00042         if (s.equals("NULL"))
00043             return null;
00044 
00045         if (s.equals("REF")) {
00046             int ref = readInt();
00047             return (Storable) retrieve(ref);
00048         }
00049 
00050         storable = (Storable) makeInstance(s);
00051         map(storable);
00052         storable.read(this);
00053         return storable;
00054     }
00055 
00059     public boolean testForNumber() throws IOException {
00060         int token = fTokenizer.nextToken();
00061        fTokenizer.pushBack();
00062         return (token == StreamTokenizer.TT_NUMBER);
00063     }
00064 
00068     public String readString() throws IOException {
00069         int token = fTokenizer.nextToken();
00070         if (token == StreamTokenizer.TT_WORD || token == '"') {
00071             return fTokenizer.sval;
00072         }
00073 
00074         String msg = "String expected in line: " + fTokenizer.lineno();
00075         throw new IOException(msg);
00076     }
00077 
00081     public int readInt() throws IOException {
00082         int token = fTokenizer.nextToken();
00083         if (token == StreamTokenizer.TT_NUMBER)
00084             return (int) fTokenizer.nval;
00085 
00086         String msg = "Integer expected in line: " + fTokenizer.lineno();
00087         throw new IOException(msg);
00088     }
00089 
00093     public Color readColor() throws IOException {
00094         return new Color(readInt(), readInt(), readInt());
00095     }
00096 
00100     public double readDouble() throws IOException {
00101         int token = fTokenizer.nextToken();
00102         if (token == StreamTokenizer.TT_NUMBER)
00103             return fTokenizer.nval;
00104 
00105         String msg = "Double expected in line: " + fTokenizer.lineno();
00106         throw new IOException(msg);
00107     }
00108 
00112     public boolean readBoolean() throws IOException {
00113         int token = fTokenizer.nextToken();
00114         if (token == StreamTokenizer.TT_NUMBER)
00115             return ((int) fTokenizer.nval) == 1;
00116 
00117         String msg = "Integer expected in line: " + fTokenizer.lineno();
00118         throw new IOException(msg);
00119     }
00120 
00121     private Object makeInstance(String className) throws IOException {
00122         try {
00123             Class cl = Class.forName(className);
00124             return cl.newInstance();
00125         } catch (NoSuchMethodError e) {
00126             throw new IOException("Class " + className
00127                 + " does not seem to have a no-arg constructor");
00128         } catch (ClassNotFoundException e) {
00129             throw new IOException("No class: " + className);
00130         } catch (InstantiationException e) {
00131             throw new IOException("Cannot instantiate: " + className);
00132         } catch (IllegalAccessException e) {
00133             throw new IOException("Class (" + className + ") not accessible");
00134         }
00135     }
00136 
00137     private void map(Storable storable) {
00138         if (!fMap.contains(storable))
00139             fMap.addElement(storable);
00140     }
00141 
00142     private Storable retrieve(int ref) {
00143         return (Storable) fMap.elementAt(ref);
00144     }
00145 }