/*
 *  Copyright (C) 2011-2012 The Animo Project
 *  http://animotron.org
 *  	
 *  This file is part of Animotron.
 *  
 *  Animotron is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 *  
 *  Animotron 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 Affero General Public License for more details.
 *  
 *  You should have received a copy of 
 *  the GNU Affero General Public License along with Animotron.  
 *  If not, see <http://www.gnu.org/licenses/>.
 */
package org.animotron.graph.handler;

import org.animotron.statement.Statement;
import org.animotron.statement.ml.ATTRIBUTE;
import org.animotron.statement.ml.COMMENT;
import org.animotron.statement.ml.ELEMENT;
import org.animotron.statement.value.VALUE;
import org.neo4j.graphdb.Relationship;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @author <a href="mailto:gazdovskyd@gmail.com">Evgeny Gazdovsky</a>
 * 
 */
public class HtmlPartGraphHandler extends AbstractTextGraphHandler {

    public HtmlPartGraphHandler(GraphHandler gh) {
        super(gh);
    }

    public HtmlPartGraphHandler(OutputStream out) {
        super(out);
    }

    public HtmlPartGraphHandler(StringBuilder out) {
        super(out);
    }

    @Override
	public void start(Statement statement, Statement parent, Relationship r, int level, boolean isOne, int pos, boolean isLast) throws IOException {
	}

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

    private boolean starting = false;

    private void close() throws IOException {
        if (starting) {
            write(">");
            starting = false;
        }
    }
    @Override
    public void start(Statement statement, Statement parent, Object[] param, int level, boolean isOne, int pos, boolean isLast) throws IOException {
        if (statement instanceof ATTRIBUTE) {
            write(" ");
            write(param[0].toString());
            if (param[1] != null) {
                write("=\"");
                write(param[1].toString().replace("\"", "&quot;"));
                write("\"");
            }
        } else if (statement instanceof ELEMENT) {
            close();
            write("<");
            write(param[0].toString());
            starting = true;
        }
    }

    @Override
    public void end(Statement statement, Statement parent, Object[] param, int level, boolean isOne, int pos, boolean isLast) throws IOException {
        if (statement instanceof ELEMENT) {
            String name = param[0].toString();
            if (starting) {
                starting = false;
                if (    // close void elements
                        name.equalsIgnoreCase("area")    ||
                        name.equalsIgnoreCase("base")    ||
                        name.equalsIgnoreCase("br")      ||
                        name.equalsIgnoreCase("col")     ||
                        name.equalsIgnoreCase("command") ||
                        name.equalsIgnoreCase("embed")   ||
                        name.equalsIgnoreCase("hr")      ||
                        name.equalsIgnoreCase("img")     ||
                        name.equalsIgnoreCase("input")   ||
                        name.equalsIgnoreCase("keygen")  ||
                        name.equalsIgnoreCase("link")    ||
                        name.equalsIgnoreCase("meta")    ||
                        name.equalsIgnoreCase("param")   ||
                        name.equalsIgnoreCase("source")  ||
                        name.equalsIgnoreCase("track")   ||
                        name.equalsIgnoreCase("wbr")
                   ) {
                    write(">");
                    return;
                } else if (name.contains(":")) {
                    write("/>");
                    return;
                } else {
                    close();
                }
            }
            write("</");
            write(name);
            write(">");
        }
    }

    @Override
    public void start(Statement statement, Statement parent, Object param, int level, boolean isOne, int pos, boolean isLast) throws IOException {
        if (statement instanceof COMMENT){
            close();
            write("<!--");
            write(param.toString());
            write("-->");
        } else if (statement instanceof VALUE) {
            close();
            write(param.toString());
        }
    }

    @Override
    public void end(Statement statement, Statement parent, Object param, int level, boolean isOne, int pos, boolean isLast) throws IOException {
    }

    @Override
    public void startGraph() throws IOException {
	}

    @Override
	public void endGraph() throws IOException {
	}

}
@@ -2979,32 +2979,196 @@
 arting = false;%0A
+                if (name.contains(%22:%22)) %7B%0A                    write(%22/%3E%22);%0A                    return;%0A                %7D else %7B%0A                    write(%22%3E%22);%0A    
                 
@@ -3198,16 +3198,20 @@
 lements%0A
+    
         
@@ -3254,32 +3254,36 @@
 e(%22area%22)    %7C%7C%0A
+    
                 
@@ -3342,32 +3342,36 @@
                 
+    
 name.equalsIgnor
@@ -3382,32 +3382,36 @@
 e(%22br%22)      %7C%7C%0A
+    
                 
@@ -3446,32 +3446,36 @@
 e(%22col%22)     %7C%7C%0A
+    
                 
@@ -3534,32 +3534,36 @@
                 
+    
 name.equalsIgnor
@@ -3574,32 +3574,36 @@
 e(%22embed%22)   %7C%7C%0A
+    
                 
@@ -3638,32 +3638,36 @@
 e(%22hr%22)      %7C%7C%0A
+    
                 
@@ -3726,32 +3726,36 @@
                 
+    
 name.equalsIgnor
@@ -3790,32 +3790,36 @@
                 
+   
+ 
 name.equalsIgnor
@@ -3834,24 +3834,28 @@
 eygen%22)  %7C%7C%0A
+    
             
@@ -3918,32 +3918,36 @@
                 
+ 
+   
 name.equalsIgnor
@@ -3958,32 +3958,36 @@
 e(%22meta%22)    %7C%7C%0A
+    
                 
@@ -4046,32 +4046,36 @@
                 
+    
 name.equalsIgnor
@@ -4110,32 +4110,36 @@
                 
+ 
+   
 name.equalsIgnor
@@ -4150,32 +4150,36 @@
 e(%22track%22)   %7C%7C%0A
+    
                 
@@ -4234,20 +4234,16 @@
         
-) %7B%0A
         
@@ -4247,222 +4247,65 @@
     
-           write(%22%3E%22);%0A                    return;%0A                %7D else if (name.contains(%22:%22)) %7B%0A                    write(%22/%3E%22);%0A                    return;%0A                %7D else %7B%0A                    close();
+) %7B%0A                        return;%0A                    %7D
 %0A   

fix the html handler