/* 
 * (C) Copyright 2002-2003, Andy Clark.  All rights reserved.
 *
 * This file is distributed under an Apache style license. Please
 * refer to the LICENSE file for specific details.
 */

/*
 * The Apache Software License, Version 1.1
 *
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights
 * reserved.
 */


import org.cyberneko.html.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamResult;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;

/**
 * This program tests the NekoHTML parser's use of the HTML DOM
 * implementation by printing the class names of all the nodes in
 * the parsed document.
 *
 * @author Andy Clark
 * @author N. Afshartous  Modifed to transform the Document object 
 *                        returned by the Neko parser
 *
 * @version $Id$
 */
public class TestTransformDOM {

    public static void main(String[] argv) throws Exception {
        DOMParser parser = new DOMParser();
        for (int i = 0; i < argv.length; i++) {
            parser.parse(new org.xml.sax.InputSource(argv[i]));
            Document result = parser.getDocument();
            print(result, "");

            transform(result);
        }
    } 


    private static Document transform(Document document) {
        try {
            TransformerFactory tFactory = TransformerFactory.newInstance();
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            // And setNamespaceAware, which is required when parsing xsl files
            dFactory.setNamespaceAware(true);
      
            //Use the DocumentBuilderFactory to create a DocumentBuilder.
            DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
      
            //Use the DocumentBuilder to parse the XSL stylesheet.
            Document xslDoc = dBuilder.parse("birds.xsl");

            // Use the DOM Document to define a DOMSource object.
            DOMSource xslDomSource = new DOMSource(xslDoc);

            // Set the systemId: note this is actually a URL, not a local filename
            xslDomSource.setSystemId("birds.xsl");

            // Process the stylesheet DOMSource and generate a Transformer.
            Transformer transformer = tFactory.newTransformer(xslDomSource);

            // Use the DOM Document to define a DOMSource object.
            DOMSource xmlDomSource = new DOMSource(document);
      
            // Create an empty DOMResult for the Result.
            DOMResult domResult = new DOMResult();
            transformer.transform(xmlDomSource, domResult);

            return (Document) domResult.getNode();
  
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
        
        return null;
    }


    /** Prints a node's class name. */
    public static void print(Node node, String indent) {
        System.out.println(indent+node.getClass().getName());
        Node child = node.getFirstChild();
        while (child != null) {
            print(child, indent+" ");
            child = child.getNextSibling();
        }
    } // print(Node)

} // class TestHTMLDOM
