/** * @file OutSerialize.java * @author Robert S Laramee * @version 1.0 * @see DrawCanvas.java *
* start date Friday, 27 Nov 98 * finish date Thursday, 03 Dec 98 *
* description This class contains the methods and data necessary for * saving user defined solid objects to a file (serializing) */ import java.util.*; // for Vector class //import matrix.*; // for Matrix4, Point4 class import java.io.*; // for FileReader, StreamTokenizer, PrintWriter public class OutSerialize { private PrintWriter output; private FileOutputStream fos; private String fileName; /** * constructor * We'll create one instance of the OutSerialize object along with the * one instance of the DrawCanavas object. This seems to make sense * so we do not have more than one instance with I/O to the same file. * Although, we could just make the methods synchronized. */ public OutSerialize() { output = null; fos = null; fileName = new String("solid.dat"); } /** * All we have to do is go down the solid object list and have each * solid object definition and each instance print itself * @param SOlist -a reference to the list of solid objects */ public void print (SolidObjectList SOlist) { for (int i = 0; i < SOlist.size(); i++) { /* print the definition */ SolidObject so = (SolidObject)SOlist.getSolidObject(i); so.print(); System.out.println("e"); for(int j = 0; j < so.getInstances().size(); j++) { /* print the instances */ Instance instance = (Instance)so.getInstances().elementAt(j); instance.print(); } } } /** * This method saves the solid object list to a file. Really what it * does is just obtain a handle to an output stream. The printToFile() * method is what actually saves the info in the file. * @param SOlist -a reference to the solid object list * @param CTinstances -a reference to the solid object list */ public void save (SolidObjectList SOlist, Vector CTinstances) { if(SOlist.size() == 0) { System.out.println("\n** Error: No objects to save."); return; } if (fileName != null) { try { fos = new FileOutputStream (fileName); output = new PrintWriter(fos, false); // don't need to flush } catch ( IOException e ) { System.err.println ("** Error: File printing to file: " + fileName); System.err.println ("** Error: " + e); System.err.println ("Using standard output"); output = new PrintWriter( new OutputStreamWriter(System.out), true); } } else { output = new PrintWriter(new OutputStreamWriter(System.out), true); } printToFile(output, SOlist, CTinstances); output.close(); // It won't work without this! System.out.println("\nSolid object list printed to file: " + fileName); } /** * printToFile() prints the all the solid objects to a file, definitions * and instances * @param output -a reference to an output file * @param SOlist -a reference to the solid object list * @param CTinstances -a reference to the solid object list */ private void printToFile(PrintWriter output, SolidObjectList SOlist, Vector CTinstances) { /* FOR EACH solid object in the solid object list */ for (int i = 0; i < SOlist.size(); i++) { /* print the definition */ SolidObject so = (SolidObject)SOlist.getSolidObject(i); so.printToFile(output); output.println("e"); for(int j = 0; j < so.getInstances().size(); j++) { /* print the instances */ Instance instance = (Instance)so.getInstances().elementAt(j); instance.printToFile(output); } } /* FOR EACH cube and tetrahedron in their instance list */ for (int i = 0; i < CTinstances.size(); i++) { /* print the instances */ Instance instance = (Instance)CTinstances.elementAt(i); instance.printToFile(output); } } }