You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Kenneth Partlow <kp...@cysive.com> on 2001/05/25 21:24:29 UTC

DOMSource vs. StreamSource in Xalan

I'm having problems doing a transform with a DOMSource.
I'm using Xalan 2.1.0 and Xerces 1.4.

classpath=%lib%\xerces.jar;%lib%\xalan.jar for the test.

Below is my test file.  it is relatively small.

My DOM test vs. the Stream test gives very different results, though
Stream test gives the expected results.

If I'm doing something wrong, please let me know.

- Kenny


*************  Start Code **************************************************

//  no package for test
import java.io.PrintWriter;
import java.io.FileOutputStream;

import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.apache.xerces.parsers.DOMParser;

/**
*	class TestTransform
*/
public class TestTransform
{
	/**
	*  StreamSource test
	*/
	public void testStream(String xmlUrl, String xslURL, PrintWriter outputURL)
throws Throwable
	{
		StreamSource source = new StreamSource(xmlUrl);

		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer = tFactory.newTransformer(new
StreamSource(xslURL));

		transformer.transform(source, new StreamResult(outputURL));
	}

	/**
	*  DOMSource test
	*/
	public void testDom(String xmlURL, String xslURL, PrintWriter outputURL)
throws Throwable
	{
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer = tFactory.newTransformer(new
StreamSource(xslURL));

		transformer.transform(new DOMSource(getRootElement(xmlURL)), new
StreamResult(outputURL));
	}

	/**
	*  Retrieves root element
	*/
	public Element getRootElement(String xmlURL) throws Throwable
	{
		//  Parse
		DOMParser parser = new DOMParser();

		parser.setFeature( "http://xml.org/sax/features/validation", false);
		parser.parse(xmlURL);

		return parser.getDocument().getDocumentElement();
	}


	/**
	*  main
	*/
	public static void main(String argv[]) throws Throwable
	{
    	try
    	{
			// make sure we have enough arguments
			if (argv.length < 2)
			{
				System.out.println("usage:  java TestTransform file.xml file.xsl
[file.out]");
				System.exit(1);
			}

			//  print out our files
			System.out.println("xmlSource = " + argv[0]);
			System.out.println("xslSource = " + argv[1]);

			//  setup the print writer
			PrintWriter pw = null;

			if (argv.length < 3)
				pw = new PrintWriter(System.out);
			else
				pw = new PrintWriter(new FileOutputStream(argv[2]));

			//  transform
			TestTransform transformer = new TestTransform();
			pw.println("\n***  Starting Sream Test ***\n");
			transformer.testStream(argv[0], argv[1], pw);

			pw.println("\n***  Starting DOM Test ***\n");
			transformer.testDom(argv[0], argv[1], pw);
		}
		catch(Throwable t)
		{
			System.out.println("Exception:  " + t);
			t.printStackTrace();
		}
	}

}