/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation. All rights reserved. This program and
 * the accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Actuate Corporation - initial API and implementation
 ******************************************************************************/

package org.eclipse.birt.test.performance.reporting.xml;

import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;

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

import org.eclipse.birt.test.performance.reporting.model.PerformanceReport;
import org.w3c.dom.Document;

/**
 * 
 */

public class XMLReportReader
{

	private static XMLReportReader reader = new XMLReportReader( );

	
	private XMLReportReader( )
	{
	}

	public static void main( String[] args ) throws Exception
	{
		/*XMLReportReader reader = XMLReportReader.getInstance( );
		PerformanceReport report = reader.read( XMLReportReader.class
				.getResourceAsStream( "perf2.xml" ) );
*/
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance( );
		DocumentBuilder builder = factory.newDocumentBuilder( );
		Document document = builder.parse( XMLReportReader.class.getResourceAsStream( "perf2.xml" ) );
		
		DomWriter writer = new DomWriter( false );
		writer.setOutput( new FileWriter( new File( "domwriter.xml") ) );
		writer.write( document );

		
	}

	public static XMLReportReader getInstance( )
	{
		return reader;
	}

	public PerformanceReport read( InputStream is ) throws Exception
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance( );
		DocumentBuilder builder = factory.newDocumentBuilder( );
		Document document = builder.parse( is );

		PerformanceReport report = new PerformanceReport( );
		report.setNode( document );

		XMLReportVisitor visitor = new XMLReportVisitor( );
		report.accept( visitor );

		return report;
	}
}
