

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author
 * @version 1.0
 */

import java.io.*;

import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

public class TestXalan {

	public class work extends Thread {

		Templates m_template = null;
		String m_xml = null;

		public work( Templates template, String sXML ) {
			m_template = template;
			m_xml = sXML;
		}

		public void run() {
			try {
				System.out.println( "Start" );
				// obtain a new transformer, which should be thread safed...
				Transformer transformer = m_template.newTransformer();
				transformer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" );

				StreamSource xmlsrc = new StreamSource( new StringReader( m_xml ) );

				ByteArrayOutputStream x = new ByteArrayOutputStream();
				StreamResult result = new StreamResult( x );

				// transform...
				transformer.transform( xmlsrc, result );

				String fn = "F:\\temp\\x_" + Thread.currentThread().getName() + ".out";

				FileOutputStream out = new FileOutputStream( fn );
				x.writeTo( out );
				out.close();

				System.out.println( "Done" );

			}
			catch( Exception e ) {
				System.out.println( "Exception: " + e.getMessage() );
			}
		}
	}

	public void doit() {

		try {
			Templates cachedTemplate = null;
			int nByteRead = 0;
			byte [] buf = new byte[2048];

			FileInputStream xsl = new FileInputStream( "F:\\temp\\x.xsl" );
			ByteArrayOutputStream oxsl = new ByteArrayOutputStream();
			while( ( nByteRead = xsl.read( buf, 0, 2048 ) ) != -1 ) {
				oxsl.write( buf, 0, nByteRead );
			}
			String sXSL = new String( oxsl.toByteArray(), "UTF-8" );

			FileInputStream xml = new FileInputStream( "F:\\temp\\x.xml" );
			ByteArrayOutputStream oxml = new ByteArrayOutputStream();
			while( ( nByteRead = xml.read( buf, 0, 2048 ) ) != -1 ) {
				oxml.write( buf, 0, nByteRead );
			}
			String sXML = new String( oxml.toByteArray(), "UTF-8" );

			// Create a template first:
			try {
				StreamSource xsltsrc = new StreamSource( new StringReader( sXSL ) );
				// create a new instance of template...
				TransformerFactory factory  = TransformerFactory.newInstance();
				cachedTemplate = factory.newTemplates( xsltsrc );
			}
			catch( Exception e ) {
				System.out.println( "Exception: " + e.getMessage() );
			}

			System.out.println("Thread before transform:");
			ThreadGroup threadgroup = Thread.currentThread().getThreadGroup();
			Thread[] threads = new Thread[threadgroup.activeCount()];
			int count = threadgroup.enumerate(threads);
			for( int ti = 0; ti < count; ti++ ) {
				System.out.println("Thread " + ti + ": " + threads[ti].getName());
			}

			for( int i = 0; i < 5; i++ ) {
				work xxx = new work( cachedTemplate, sXML );
				xxx.start();
				xxx = null;
			}

			// wait 30 seconds to give threads a chance to exit
			Thread.currentThread().sleep(30000);


			System.out.println("Thread after transform:");
			ThreadGroup threadgroup1 = Thread.currentThread().getThreadGroup();
			Thread[] threads1 = new Thread[threadgroup1.activeCount()];
			int count1 = threadgroup1.enumerate(threads1);
			for( int ti = 0; ti < count1; ti++ ) {
				System.out.println("Thread " + ti + ": " + threads1[ti].getName());
			}
			threadgroup1 = null;
			threads1 = null;

			System.out.println( "GC" );
			System.gc();
			System.out.println( "GC DONE" );

			// wait 30 seconds to give user a chance to take a JProbe snapshot...
			Thread.currentThread().sleep(30000);

		}
		catch( Exception e ) {
			System.out.println( "Exception: " + e.getMessage() );
		}
	}


	public static void main(String[] args) {
		TestXalan xyz = new TestXalan();
		xyz.doit();

	}
}
