package be.iminds.ilabt.jfed.groovy_dsl.impl;

import be.iminds.ilabt.jfed.groovy_dsl.DSLEventWriter;
import be.iminds.ilabt.jfed.groovy_dsl.DSLStreamException;
import be.iminds.ilabt.jfed.groovy_dsl.events.DSLEvent;
import be.iminds.ilabt.jfed.groovy_dsl.impl.events.DSLEventImpl;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.Writer;

/**
 * User: twalcari
 * Date: 5/18/2015
 * Time: 09:15
 */
public class DSLEventWriterImpl implements DSLEventWriter {

    private final Writer writer;

    public DSLEventWriterImpl(Writer writer) {
        this.writer = writer;
    }


    @Override
    public void add(DSLEvent event) throws DSLStreamException {
        assert(event instanceof DSLEventImpl);

        ((DSLEventImpl)event).write(writer);
    }

    @Override
    public void flush() throws XMLStreamException {
        try {
            writer.flush();
        } catch (IOException e) {
            throw new XMLStreamException(e);
        }
    }

    @Override
    public void close() throws XMLStreamException {
        try {
            writer.close();
        } catch (IOException e) {
            throw new XMLStreamException(e);
        }
    }
}
