/***************************************************************************
 *   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 javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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
{
	public CommMessage xmlToValue( CommMessage request )
		throws FaultException
	{
		try {
			Value result = Value.create();
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().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 );
		}
	}
}

@@ -1611,24 +1611,53 @@
 ringReader;%0A
+import java.io.StringWriter;%0A
 import javax
@@ -1682,24 +1682,24 @@
 entBuilder;%0A
-
 import javax
@@ -1782,32 +1782,264 @@
 ationException;%0A
+import javax.xml.transform.Transformer;%0Aimport javax.xml.transform.TransformerException;%0Aimport javax.xml.transform.TransformerFactory;%0Aimport javax.xml.transform.stream.StreamResult;%0Aimport javax.xml.transform.stream.StreamSource;%0A
 import jolie.net
@@ -2290,16 +2290,161 @@
 rvice%0A%7B%0A
+%09final private DocumentBuilderFactory documentBuilder;%0A%0A%09public XmlUtils()%0A%09%7B%0A%09%09this.documentBuilder = DocumentBuilderFactory.newInstance();%0A%09%7D%0A%0A
 %09public 
@@ -2579,33 +2579,33 @@
 ilder builder = 
-D
+d
 ocumentBuilderFa
@@ -2602,37 +2602,16 @@
 tBuilder
-Factory.newInstance()
 .newDocu
@@ -3048,32 +3048,32 @@
 Exception( e );%0A
-
 %09%09%7D catch( IOExc
@@ -3149,11 +3149,743 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%0A%09public CommMessage transform( CommMessage request )%0A%09%09throws FaultException%0A%09%7B%0A%09%09try %7B%0A%09%09%09StreamSource source = new StreamSource( new StringReader( request.value().getFirstChild( %22source%22 ).strValue() ) );%0A%09%09%09TransformerFactory tFactory = TransformerFactory.newInstance();%0A%09%09%09Transformer t = tFactory.newTransformer( new StreamSource( new StringReader( request.value().getFirstChild( %22xslt%22 ).strValue() ) ) );%0A%09%09%09StringWriter writer = new StringWriter();%0A%09%09%09StreamResult result = new StreamResult( writer );%0A%09%09%09t.transform( source, result );%0A%09%09%09return CommMessage.createResponse( request, Value.create( writer.toString() ) );%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%7D%0A

Support for XSLT.