/*******************************************************************************
 * 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.report.tests.viewer;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 *  Xml utility
 */
public class XmlUtility
{

	/**
	 * @param args
	 * @throws FileNotFoundException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 */
	public static void main( String[] args ) throws FileNotFoundException,
			ParserConfigurationException, SAXException, IOException
	{
		String input = "<div id=\"bookmark\" style=\" text-align:left;\">Hello world</div>"; //$NON-NLS-1$
		Document doc = XmlUtility.readXML( input );
		Node node = doc.getElementsByTagName( "div" ).item( 0 );//$NON-NLS-1$
		
		System.out.println( node.getAttributes( ).getNamedItem( "id" ).getNodeValue( ) );//$NON-NLS-1$
		System.out.println( node.getAttributes( ).getNamedItem( "style" ).getNodeValue( ));//$NON-NLS-1$
		System.out.println( node.getFirstChild( ).getNodeValue( ) );
	}

	/**
	 * Read an xml content(even an sub-section) into a document.
	 * 
	 * @param content
	 * @return
	 * @throws ParserConfigurationException
	 * @throws FileNotFoundException
	 * @throws SAXException
	 * @throws IOException
	 */

	public static Document readXML( String content )
			throws ParserConfigurationException, FileNotFoundException,
			SAXException, IOException
	{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance( );
		dbf.setNamespaceAware( true );
		DocumentBuilder db = dbf.newDocumentBuilder( );
		InputStream is = new ByteArrayInputStream( content.getBytes( ) );
		Document doc = db.parse( is );
		return doc;
	}

	/**
	 * Write a doc to an outputstream
	 * 
	 * @param doc
	 * @param os
	 * @throws TransformerConfigurationException
	 * @throws TransformerException
	 */

	public static void writeXML( Document doc, OutputStream os )
			throws TransformerConfigurationException, TransformerException
	{
		TransformerFactory tf = TransformerFactory.newInstance( );
		Transformer transformer = tf.newTransformer( );
		transformer.transform( new DOMSource( doc ), new StreamResult( os ) );
	}
}