/***************************************************************************
 *   Copyright (C) 2008 by Fabrizio Montesi                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/

package joliex.util;


import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import jolie.net.CommMessage;
import jolie.runtime.FaultException;
import jolie.runtime.JavaService;
import jolie.runtime.Value;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XmlUtils extends JavaService
{
	final private DocumentBuilderFactory documentBuilder;

	public XmlUtils()
	{
		this.documentBuilder = DocumentBuilderFactory.newInstance();
	}

	public CommMessage xmlToValue( CommMessage request )
		throws FaultException
	{
		try {
			Value result = Value.create();
			DocumentBuilder builder = documentBuilder.newDocumentBuilder();
			InputSource src = new InputSource( new StringReader( request.value().strValue() ) );
			Document doc = builder.parse( src );
			jolie.xml.XmlUtils.documentToValue( doc, result );
			return CommMessage.createResponse( request, result );
		} catch( ParserConfigurationException e ) {
			e.printStackTrace();
			throw new FaultException( e );
		} catch( SAXException e ) {
			e.printStackTrace();
			throw new FaultException( e );
		} catch( IOException e ) {
			e.printStackTrace();
			throw new FaultException( e );
		}
	}

	public CommMessage transform( CommMessage request )
		throws FaultException
	{
		try {
			StreamSource source = new StreamSource( new StringReader( request.value().getFirstChild( "source" ).strValue() ) );
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer t = tFactory.newTransformer( new StreamSource( new StringReader( request.value().getFirstChild( "xslt" ).strValue() ) ) );
			StringWriter writer = new StringWriter();
			StreamResult result = new StreamResult( writer );
			t.transform( source, result );
			return CommMessage.createResponse( request, Value.create( writer.toString() ) );
		} catch( TransformerException e ) {
			e.printStackTrace();
			throw new FaultException( e );
		}
	}
}

@@ -1826,24 +1826,86 @@
 ransformer;%0A
+import javax.xml.transform.TransformerConfigurationException;%0A
 import javax
@@ -1980,32 +1980,74 @@
 sformerFactory;%0A
+import javax.xml.transform.dom.DOMSource;%0A
 import javax.xml
@@ -2447,16 +2447,23 @@
 tBuilder
+Factory
 ;%0A%0A%09publ
@@ -2501,16 +2501,23 @@
 tBuilder
+Factory
  = Docum
@@ -2549,24 +2549,1025 @@
 ance();%0A%09%7D%0A%0A
+%09public CommMessage valueToXml( CommMessage request )%0A%09%09throws FaultException%0A%09%7B%0A%09%09try %7B%0A%09%09%09Document doc = documentBuilderFactory.newDocumentBuilder().newDocument();%0A%09%09%09String root = request.value().getFirstChild( %22root%22 ).strValue();%0A%09%09%09jolie.xml.XmlUtils.valueToDocument(%0A%09%09%09%09request.value().getFirstChild( root ),%0A%09%09%09%09root,%0A%09%09%09%09doc%0A%09%09%09);%0A%09%09%09TransformerFactory tFactory = TransformerFactory.newInstance();%0A%09%09%09Transformer t = tFactory.newTransformer();%0A%09%09%09StringWriter writer = new StringWriter();%0A%09%09%09StreamResult result = new StreamResult( writer );%0A%09%09%09t.transform( new DOMSource( doc ), result );%0A%09%09%09return CommMessage.createResponse( request, Value.create( writer.toString() ) );%0A%09%09%7D catch( ParserConfigurationException e ) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%09throw new FaultException( e );%0A%09%09%7D catch( TransformerConfigurationException e ) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%09throw new FaultException( e );%0A%09%09%7D catch( TransformerException e ) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%09throw new FaultException( e );%0A%09%09%7D%0A%09%7D%0A%0A
 %09public Comm
@@ -3721,16 +3721,23 @@
 tBuilder
+Factory
 .newDocu

Support for converting structured JOLIE data to XML strings.