/**
 * Copyright © 2015 digitalfondue (info@digitalfondue.ch)
 *
 * 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 ch.digitalfondue.jfiveparse;

import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class W3CDom {

    public static Document convert(ch.digitalfondue.jfiveparse.Document doc) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document d = builder.newDocument();
            doc.traverse(new W3CDNodeVisitor(d));
            return d;
        } catch (ParserConfigurationException e) {
            throw new IllegalStateException(e);
        }
    }

    public static class W3CDNodeVisitor implements NodesVisitor {

        protected Document document;

        public W3CDNodeVisitor(Document document) {
            this.document = document;
        }

        @Override
        public void start(Node node) {
            //FIXME
        }

        @Override
        public void end(Node node) {
            //FIXME
        }

        @Override
        public boolean complete() {
            //FIXME
            return false;
        }
    }
}

@@ -655,16 +655,41 @@
 parse;%0A%0A
+import org.w3c.dom.Attr;%0A
 import o
@@ -911,14 +911,20 @@
 ent 
-conver
+toW3CDocumen
 t(ch
@@ -1514,16 +1514,64 @@
 ocument;
+%0A        protected org.w3c.dom.Node currentNode;
 %0A%0A      
@@ -1646,32 +1646,73 @@
 ent = document;%0A
+            this.currentNode = document;%0A
         %7D%0A%0A     
@@ -1755,32 +1755,33 @@
 rt(Node node) %7B%0A
+%0A
             //FI
@@ -1780,101 +1780,1573 @@
     
-//FIXME%0A        %7D%0A%0A        @Override%0A        public void end(Node node) %7B%0A            //FIXME
+switch (node.getNodeType()) %7B%0A                case Node.ELEMENT_NODE:%0A                    Element elem = (Element) node;%0A                    org.w3c.dom.Element e = toElement(elem);%0A                    currentNode.appendChild(e);%0A                    currentNode = e;%0A                    break;%0A                case Node.TEXT_NODE:%0A                    currentNode.appendChild(document.createTextNode(((Text) node).getData()));%0A                    break;%0A                case Node.COMMENT_NODE:%0A                    currentNode.appendChild(document.createComment(((Comment) node).getData()));%0A                    break;%0A                case Node.DOCUMENT_TYPE_NODE:%0A                    break;%0A                default:%0A                    break;%0A            %7D%0A        %7D%0A%0A        protected org.w3c.dom.Element toElement(Element elem) %7B%0A            org.w3c.dom.Element e = document.createElementNS(elem.getNamespaceURI(), elem.getNodeName());%0A            for(String attrName : elem.getAttributes().keySet()) %7B%0A                AttributeNode attr = elem.getAttributeNode(attrName);%0A                Attr copiedAttr = document.createAttributeNS(attr.getNamespace(), attr.getName());%0A                copiedAttr.setValue(attr.getValue());%0A                copiedAttr.setPrefix(attr.getPrefix());%0A                e.setAttributeNodeNS(copiedAttr);%0A            %7D%0A            return e;%0A        %7D%0A%0A        @Override%0A        public void end(Node node) %7B%0A            if(node.getNodeType() == Node.ELEMENT_NODE) %7B%0A                currentNode = currentNode.getParentNode();%0A            %7D
 %0A   

work for supporting conversion from internal Document to org.w3c.dom.Document