All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.xolite.utils.StreamXMLSerializer Maven / Gradle / Ivy

Go to download

This project provides a lightweight framework to serialize/deserialize (or marshall/unmarshall) java objects into XML. The implementation is based on standard SAX (Simple Api for Xml) but it follows a original approach based on the strong data encapsulation paradigm of Object Oriented (OO) programming.

The newest version!
/*-------------------------------------------------------------------------
 Copyright 2006 Olivier Berlanger

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 -------------------------------------------------------------------------*/
package net.sf.xolite.utils;


import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import net.sf.xolite.XMLSerializable;
import net.sf.xolite.XMLSerializeException;
import net.sf.xolite.impl.FlowXMLSerializer;


/**
 * A XMLSerializer implementation writing a tree of java XMLSerializable objects as XML into an output stream.
 * 
 * @author Olivier Berlanger
 * @see StreamXMLSerializer
 */
public class StreamXMLSerializer extends FlowXMLSerializer {


    private String writerEncoding;
    private BufferedWriter writer;
    private boolean autoCloseWriter = true;


    public StreamXMLSerializer(OutputStream stream) throws IOException {
        this(stream, "UTF-8");
    }


    public StreamXMLSerializer(OutputStream stream, String encoding) throws IOException {
        writerEncoding = encoding;
        writer = new BufferedWriter(new OutputStreamWriter(stream, encoding));
    }


    public void serializeObject(XMLSerializable serial) throws XMLSerializeException {
        if (serial == null) throw new XMLSerializeException("Cannot serialize null object");
        startDocument();
        serial.serialize(this);
        endDocument();
    }


    public boolean getAutoCloseWriter() {
        return autoCloseWriter;
    }


    public void setAutoCloseWriter(boolean newAutoCloseWriter) {
        autoCloseWriter = newAutoCloseWriter;
    }


    // -------------------------------------- Implementation ---------------------------------------------


    @Override
    protected void startDocumentImpl() throws IOException {
        writer.write("");
        if (isUsingLineBreaks()) writeNewLine();
    }


    @Override
    protected void endDocumentImpl() throws IOException {
        if (autoCloseWriter) writer.close();
        else writer.flush();
    }


    @Override
    protected void writeNewLine() throws IOException {
        writer.newLine();
    }


    @Override
    protected void writeString(String text) throws IOException {
        writer.write(text);
    }


    @Override
    protected void writeCharacter(char ch) throws IOException {
        writer.write(ch);
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy