/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-2011 The eXist-db Project
 *  http://exist-db.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 2
 *  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.
 *  
 *  $Id$
 */
package org.exist.util.serializer;

import java.io.IOException;
import java.io.Writer;

import javax.xml.transform.TransformerException;
import org.exist.Namespaces;

import org.exist.dom.QName;
import org.exist.util.hashtable.ObjectHashSet;

/**
 * @author wolf
 *
 */
public class XHTMLWriter extends IndentingXMLWriter {

    private final static ObjectHashSet<String> emptyTags = new ObjectHashSet<String>(31);
    
    static {
        emptyTags.add("area");
        emptyTags.add("base");
        emptyTags.add("br");
        emptyTags.add("col");
        emptyTags.add("hr");
        emptyTags.add("img");
        emptyTags.add("input");
        emptyTags.add("link");
        emptyTags.add("meta");
        emptyTags.add("basefont");
        emptyTags.add("frame");
        emptyTags.add("isindex");
        emptyTags.add("param");
    }
    
    private static boolean isEmptyTag(String tag) {
        return emptyTags.contains(tag);
    }
    
    private String currentTag;
    
    /**
     * 
     */
    public XHTMLWriter() {
        super();
    }

    /**
     * @param writer
     */
    public XHTMLWriter(Writer writer) {
        super(writer);
    }

    @Override
    public void startElement(QName qname) throws TransformerException {
        
        final QName xhtmlQName = removeXhtmlPrefix(qname);
        
        super.startElement(xhtmlQName);
        currentTag = xhtmlQName.getStringValue();
    }
    
    @Override
    public void endElement(QName qname) throws TransformerException {
        final QName xhtmlQName = removeXhtmlPrefix(qname);
        
        super.endElement(xhtmlQName);
    }
    
    private QName removeXhtmlPrefix(QName qname) {
        String prefix = qname.getPrefix();
        String namespaceURI = qname.getNamespaceURI();
        if(prefix != null && prefix.length() > 0 && namespaceURI != null && namespaceURI.equals(Namespaces.XHTML_NS)) {
            return new QName(qname.getLocalName(), namespaceURI);   
        }
        
        return qname;
    }

    @Override
    public void startElement(String namespaceURI, String localName, String qname) throws TransformerException {
        
        final String xhtmlQName = removeXhtmlPrefix(namespaceURI, qname);
        
        super.startElement(namespaceURI, localName, xhtmlQName);
        currentTag = xhtmlQName;
    }
    
    @Override
    public void endElement(String namespaceURI, String localName, String qname) throws TransformerException {
        
        final String xhtmlQName = removeXhtmlPrefix(namespaceURI, qname);
        
        super.endElement(namespaceURI, localName, xhtmlQName);
    }
    
    private String removeXhtmlPrefix(String namespaceURI, String qname) {
        
        int pos = qname.indexOf(':');
        if(pos > 0 && namespaceURI != null && namespaceURI.equals(Namespaces.XHTML_NS)) {
            return qname.substring(pos+1);
        }
        
        return qname;
    }
    
    @Override
    protected void closeStartTag(boolean isEmpty) throws TransformerException {
        try {
            if (tagIsOpen) {
                if (isEmpty) {
                    if (isEmptyTag(currentTag))
                        writer.write(" />");
                    else {
                        writer.write('>');
                        writer.write("</");
                        writer.write(currentTag);
                        writer.write('>');
                    }
                } else
                    writer.write('>');
                tagIsOpen = false;
            }
        } catch (IOException e) {
            throw new TransformerException(e.getMessage(), e);
        }
    }
}

@@ -2033,24 +2033,75 @@
 iter);%0A    %7D
+%0A    %0A    boolean haveCollapsedXhtmlPrefix = false;
 %0A%0A    @Overr
@@ -2536,32 +2536,83 @@
 nt(xhtmlQName);%0A
+        %0A        haveCollapsedXhtmlPrefix = false;%0A
     %7D%0A    %0A    p
@@ -2867,32 +2867,77 @@
 es.XHTML_NS)) %7B%0A
+            haveCollapsedXhtmlPrefix = true;%0A
             retu
@@ -3639,24 +3639,75 @@
 htmlQName);%0A
+        %0A        haveCollapsedXhtmlPrefix = false;%0A
     %7D%0A    %0A 
@@ -3908,32 +3908,77 @@
 es.XHTML_NS)) %7B%0A
+            haveCollapsedXhtmlPrefix = true;%0A
             retu
@@ -4000,24 +4000,37 @@
 ing(pos+1);%0A
+            %0A
         %7D%0A  
@@ -4054,29 +4054,398 @@
 turn qname;%0A
-
     %7D
+%0A%0A    @Override%0A    public void namespace(String prefix, String nsURI) throws TransformerException %7B%0A        if(haveCollapsedXhtmlPrefix && prefix != null && prefix.length() %3E 0 && nsURI.equals(Namespaces.XHTML_NS)) %7B%0A            return; //dont output the xmlns:prefix for the collapsed nodes prefix%0A        %7D%0A        %0A        super.namespace(prefix, nsURI);%0A    %7D%0A    
 %0A    %0A    @O

Improve XHTML Serialization - Do not output the namespace for the node prefix if we have collapsed the prefix