Back to index

moin  1.9.0~rc2
StorableOutput.java
Go to the documentation of this file.
00001 /*
00002  * @(#)StorableOutput.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 
00021 public  class StorableOutput extends Object {
00022 
00023     private PrintWriter     fStream;
00024     private Vector          fMap;
00025     private int             fIndent;
00026 
00030     public StorableOutput(OutputStream stream) {
00031         fStream = new PrintWriter(stream);
00032         fMap = new Vector();
00033         fIndent = 0;
00034     }
00035 
00039     public void writeStorable(Storable storable) {
00040         if (storable == null) {
00041             fStream.print("NULL");
00042             space();
00043             return;
00044         }
00045 
00046         if (mapped(storable)) {
00047             writeRef(storable);
00048             return;
00049         }
00050 
00051         incrementIndent();
00052         startNewLine();
00053         map(storable);
00054         fStream.print(storable.getClass().getName());
00055         space();
00056         storable.write(this);
00057         space();
00058         decrementIndent();
00059     }
00060 
00064     public void writeInt(int i) {
00065         fStream.print(i);
00066         space();
00067     }
00068 
00069     public void writeColor(Color c) {
00070         writeInt(c.getRed());
00071         writeInt(c.getGreen());
00072         writeInt(c.getBlue());
00073     }
00074 
00078     public void writeDouble(double d) {
00079         fStream.print(d);
00080         space();
00081     }
00082 
00086     public void writeBoolean(boolean b) {
00087         if (b)
00088             fStream.print(1);
00089         else
00090             fStream.print(0);
00091         space();
00092     }
00093 
00098     public void writeString(String s) {
00099         fStream.print('"');
00100         for(int i = 0; i < s.length(); i++) {
00101             char c = s.charAt(i);
00102             switch(c) {
00103                 case '\n': fStream.print('\\'); fStream.print('n'); break;
00104                 case '"' : fStream.print('\\'); fStream.print('"'); break;
00105                 case '\\': fStream.print('\\'); fStream.print('\\'); break;
00106                 case '\t': fStream.print('\\'); fStream.print('\t'); break;
00107                 default: fStream.print(c);
00108             }
00109 
00110         }
00111         fStream.print('"');
00112         space();
00113     }
00114 
00118     public void close() {
00119         fStream.close();
00120     }
00121 
00122     private boolean mapped(Storable storable) {
00123         return fMap.contains(storable);
00124     }
00125 
00126     private void map(Storable storable) {
00127         if (!fMap.contains(storable))
00128             fMap.addElement(storable);
00129     }
00130 
00131     private void writeRef(Storable storable) {
00132         int ref = fMap.indexOf(storable);
00133 
00134         fStream.print("REF");
00135         space();
00136         fStream.print(ref);
00137         space();
00138     }
00139 
00140     private void incrementIndent() {
00141         fIndent += 4;
00142     }
00143 
00144     private void decrementIndent() {
00145         fIndent -= 4;
00146         if (fIndent < 0) fIndent = 0;
00147     }
00148 
00149     private void startNewLine() {
00150         fStream.println();
00151         for (int i=0; i<fIndent; i++)
00152             space();
00153     }
00154 
00155     private void space() {
00156         fStream.print(' ');
00157     }
00158 
00159 }