/*
 *  Copyright (C) 2011 The Animo Project
 *  http://animotron.org
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 3
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.animotron.graph.handler;

import org.animotron.statement.Statement;
import org.animotron.statement.ml.*;
import org.neo4j.graphdb.Relationship;

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

/**
 * @author <a href="mailto:gazdovskyd@gmail.com">Evgeny Gazdovsky</a>
 * 
 */
public class StAXGraphHandler implements GraphHandler {
	
	private XMLStreamWriter writer;

	public StAXGraphHandler(XMLStreamWriter writer) {
		this.writer = writer;
	}
	
	@Override
	public void start(Statement statement, Relationship r, int level, boolean isOne) throws IOException {
	}

	@Override
	public void end(Statement statement, Relationship r, int level, boolean isOne) throws IOException {
	}

    @Override
    public void start(Statement statement, String[] param, int level, boolean isOne) throws IOException {
        try {
            if (statement instanceof ATTRIBUTE) {
                writer.writeAttribute(param[0], param[1]);
            } else if (statement instanceof ELEMENT) {
                writer.writeStartElement(param[0]);
            }
        } catch (XMLStreamException e) {
            throw new IOException(e);
        }
    }

    @Override
    public void end(Statement statement, String[] param, int level, boolean isOne) throws IOException {
        try {
            if (statement instanceof ELEMENT) {
                writer.writeEndElement();
            }
        } catch (XMLStreamException e) {
            throw new IOException(e);
        }
    }

    @Override
    public void start(Statement statement, String param, int level, boolean isOne) throws IOException {
        try {
            if (statement instanceof TEXT) {
                writer.writeCharacters(param);
            } else if (statement instanceof COMMENT){
                writer.writeComment(param);
            } else if (statement instanceof CDATA){
                writer.writeCData(param);
            }
        } catch (XMLStreamException e) {
            throw new IOException(e);
        }
    }

    @Override
    public void end(Statement statement, String param, int level, boolean isOne) throws IOException {
    }

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

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

}

@@ -1241,16 +1241,39 @@
  writer;
+%0A%09private long pos = 0;
 %0A%0A%09publi
@@ -1768,36 +1768,60 @@
 ) %7B%0A            
-    
+%09if (pos %3E 0)%0A            %09%09
 writer.writeAttr
@@ -1843,24 +1843,38 @@
  param%5B1%5D);%0A
+            %09%0A
             
@@ -1964,24 +1964,47 @@
 (param%5B0%5D);%0A
+                pos++;%0A
             
@@ -2323,16 +2323,39 @@
 ment();%0A
+                pos--;%0A
         
@@ -2580,32 +2580,58 @@
 %7B%0A        try %7B%0A
+        %09if (pos %3E 0) %7B %0A%09
             if (
@@ -2655,24 +2655,25 @@
 eof TEXT) %7B%0A
+%09
             
@@ -2699,32 +2699,33 @@
 racters(param);%0A
+%09
             %7D el
@@ -2762,16 +2762,17 @@
 MMENT)%7B%0A
+%09
         
@@ -2799,32 +2799,33 @@
 Comment(param);%0A
+%09
             %7D el
@@ -2860,16 +2860,17 @@
 CDATA)%7B%0A
+%09
         
@@ -2903,16 +2903,17 @@
 param);%0A
+%09
         
@@ -2910,32 +2910,43 @@
 %0A%09            %7D%0A
+        %09%7D%0A
         %7D catch 
@@ -3209,32 +3209,32 @@
 ption %7B%0A%09%09try %7B%0A
-
 %09%09%09writer.writeS
@@ -3241,32 +3241,44 @@
 tartDocument();%0A
+%09%09%09pos = 0;%0A
 %09%09%7D catch (XMLSt

make sure that root element is created