// Copyright 2000-2003, FreeHEP.
package hep.graphics.heprep.ref;

import java.io.*;
import java.util.*;
import java.util.zip.*;

import hep.graphics.heprep.*;
import hep.graphics.heprep.util.*;

/**
 * Writew out java serialized IO
 *
 * @author M.Donszelmann
 *
 * @version $Id: DefaultHepRepWriter.java,v 1.9 2004/05/27 22:08:43 duns Exp $
 */

public class DefaultHepRepWriter implements HepRepWriter {
    public static final String cvsId = "$Id: DefaultHepRepWriter.java,v 1.9 2004/05/27 22:08:43 duns Exp $";

    protected ObjectOutputStream out;
    protected ZipOutputStream zip;
    protected Map/*<String, String>*/ properties;

    protected DefaultHepRepWriter(OutputStream out, boolean randomAccess, boolean compress) throws IOException {
        super();
        if (randomAccess) {
            zip = new ZipOutputStream(out);
            zip.setLevel(compress ? Deflater.DEFAULT_COMPRESSION : Deflater.NO_COMPRESSION);
            // this.out is initialized later in write(HepRep)
        } else if (compress) {
            this.out = new ObjectOutputStream(new GZIPOutputStream(out));
        } else {
            this.out = new ObjectOutputStream(out);
        }
        properties = new HashMap();
    }

    public void addProperty(String key, String value) throws IOException {
        properties.put(key, value);
    }

    public void close() throws IOException {
        if (zip != null) {
            zip.putNextEntry(new ZipEntry("heprep.properties"));
            PrintStream ps = new PrintStream(zip);
            for (Iterator i=properties.keySet().iterator(); i.hasNext(); ) {
                String key = (String)i.next();
                ps.println(key+"="+(String)properties.get(key));
            }
            zip.closeEntry();
            zip.close();
        }
        if (out != null) {
            out.close();
        }
    }

    public void write(HepRep heprep, String name) throws IOException {
        if (zip != null) {
            zip.putNextEntry(new ZipEntry(name));
            out = new ObjectOutputStream(zip);
        }
        out.writeObject(heprep);
        if (zip != null) {
            zip.closeEntry();
        }
    }

    public void write(List/*<String>*/ layerOrder) throws IOException {
        out.writeObject(layerOrder);
    }

    public void write(HepRepTypeTree typeTree) throws IOException {
        out.writeObject(typeTree);
    }

    public void write(HepRepType type) throws IOException {
        out.writeObject(type);
    }

    public void write(HepRepTreeID treeID) throws IOException {
        out.writeObject(treeID);
    }

    public void write(HepRepAction action) throws IOException {
        out.writeObject(action);
    }

    public void write(HepRepInstanceTree instanceTree) throws IOException {
        out.writeObject(instanceTree);
    }

    public void write(HepRepInstance instance) throws IOException {
        out.writeObject(instance);
    }

    public void write(HepRepPoint point) throws IOException {
        out.writeObject(point);
    }

    public void write(HepRepAttribute attribute) throws IOException {
        out.writeObject(attribute);
    }

    public void write(HepRepDefinition definition) throws IOException {
        out.writeObject(definition);
    }

    public void write(HepRepAttValue attValue) throws IOException {
        out.writeObject(attValue);
    }

    public void write(HepRepAttDef attDef) throws IOException {
        out.writeObject(attDef);
    }
}

