import java.io.FileInputStream;
import java.io.StringReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Properties;
//import org.apache.xerces.parsers.DOMParser;
import org.apache.xpath.XPathAPI;
//import org.apache.xml.utils.TreeWalker;
//import org.apache.xml.utils.DOMBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.SAXException;
import org.xml.sax.InputSource;

// Imported JAVA API for XML Parsing 1.0 classes
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; 

// Imported Serializer classes
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;



public class XSLTTest
{
  protected String xmlfile=null,xslfile= null;
  protected Element parent=null;
  /** Process input args and execute the XPath.  */
  public void doMain(String[] args)
    throws Exception
  {
    xmlfile = args[0];
    xslfile = args[1];

    if ((xmlfile != null) && (xmlfile.length() > 0)
        && (xslfile != null) && (xslfile.length() > 0))
    {
      // Tell that we're loading classes and parsing, so the time it 
      // takes to do this doesn't get confused with the time to do 
      // the actual query and serialization.
      System.out.println("Loading classes, parsing");
      
      // Set up a DOM tree to query.
      //InputSource in = new InputSource(new FileInputStream(filename));
	  StreamSource xml = new StreamSource(new StringReader(xmlfile));//new File(xmlfilename));
	  StreamSource xsl = new StreamSource(new StringReader(xslfile));
	  Document doc;	  
      	TransformerFactory tFactory = TransformerFactory.newInstance();
	      	//tFactory.setNamespaceAware( true );
	      Transformer transformer = tFactory.newTransformer(xsl);
	      System.out.println("JAXP XSLT transform : "+transformer.toString());
	      // Create an empty DOMResult for the Result.
	      DOMResult domResult = new DOMResult();
	  
	      // Perform the transformation, placing the output in the DOMResult.
	      transformer.transform(xml, domResult);
	      doc = (Document)domResult.getNode();

      // Set up an identity transformer to use as serializer.
      Transformer serializer = TransformerFactory.newInstance().newTransformer();
      serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
      serializer.setOutputProperty(OutputKeys.INDENT, "yes");
      serializer.setOutputProperty(OutputKeys.METHOD, "xml");
      // TODO: look the encoding in the document!
      serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); 
	  

	  // print out the transformed document
      //serializer.transform(new DOMSource(doc), new StreamResult(System.out));


       serializer.transform(new DOMSource(doc), new StreamResult(System.out));
    }
    else
    {
      System.out.println("Bad input args: " + xmlfile + ", " + xslfile);
    }
  }
  
  /** Main method to run from the command line.    */
  public static void main (String[] args)
    throws Exception
  {
    if (args.length != 2)
    {
	     if (args.length==0)
	     {
	     	String[] a=
	     	{
	     		"<?xml version=\"1.0\" standalone=\"no\"?><original>Original data</original>",
	     		"<?xml version=\"1.0\" standalone=\"no\"?><xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\"><xsl:template match=\"/\"><elem test=\"test\"><xsl:copy-of select=\".\"/></elem></xsl:template></xsl:stylesheet>"
	     	};
	     	
		    XSLTTest app = new XSLTTest();
		    app.doMain(a);
	     }
						 
      return;
    }
        
    XSLTTest app = new XSLTTest();
    app.doMain(args);
  }	
  
  
} 


