/* 
 * Licensed to the soi-toolkit project under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The soi-toolkit project licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.soitoolkit.commons.mule.smooks;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.milyn.Smooks;
import org.milyn.SmooksException;
import org.milyn.container.ExecutionContext;
import org.milyn.event.report.HtmlReportGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.soitoolkit.commons.mule.util.MiscUtil;
import org.xml.sax.SAXException;

/**
 * Various helper methods used for smookes
 * 
 * @author Magnus Larsson
 *
 */
public class SmooksUtil {

	private final static Logger logger = LoggerFactory.getLogger(SmooksUtil.class);

	/**
     * Hidden constructor.
     */
    private SmooksUtil() {
        throw new UnsupportedOperationException("Not allowed to create an instance of this class");
    }
 
    static public String runSmooksTransformer(String source, String smookesConfig) throws IOException, SAXException, SmooksException {

        Smooks smooks = new Smooks(smookesConfig);
        StringWriter writer = new StringWriter();

        try {
        	
            ExecutionContext executionContext = smooks.createExecutionContext();

            // Configure the execution context to generate a report...
            executionContext.setEventListener(new HtmlReportGenerator("target/smooks-report/report.html"));

            smooks.filterSource(executionContext, new StreamSource(new StringReader(source)), new StreamResult(writer));

            String result = writer.toString();
            
            // Remove any leading new lines
            result = MiscUtil.removeLeadingNewLines(result);
            
            // Remove any trailing new lines
            result = MiscUtil.removeTrailingNewLines(result);
            
            logger.debug("Result from smooks transformation [{}]", result);
            return result;

        } finally {
            smooks.close();
            writer.close();
        }
    }
}

@@ -931,16 +931,86 @@
 riter;%0A%0A
+import javax.xml.transform.Result;%0Aimport javax.xml.transform.Source;%0A
 import j
@@ -1127,42 +1127,8 @@
 ks;%0A
-import org.milyn.SmooksException;%0A
 impo
@@ -1727,16 +1727,19 @@
 %0A    %7D%0A 
+   
 %0A    sta
@@ -1749,22 +1749,20 @@
  public 
-String
+void
  runSmoo
@@ -1780,21 +1780,21 @@
 er(S
-tring
+ource
  source,
  Str
@@ -1789,16 +1789,31 @@
  source,
+ Result result,
  String 
@@ -1829,67 +1829,32 @@
 nfig
-) throws IOException, SAXException, SmooksException
+, String report)
  %7B%0A%0A    
     
@@ -1849,20 +1849,17 @@
  %7B%0A%0A    
-    
+%09
 Smooks s
@@ -1871,42 +1871,22 @@
  = n
-ew Smooks(smookesConfig)
+ull
 ;%0A
+%0A
         
 Stri
@@ -1885,74 +1885,72 @@
     
-S
 tr
-ingWriter writer = new StringWriter();%0A%0A        try %7B%0A        %09
+y %7B%0A        %09%0A            smooks = new Smooks(smookesConfig);%0A
 %0A   
@@ -2095,16 +2095,29 @@
 a report
+ if requested
 ...%0A    
@@ -2124,16 +2124,51 @@
         
+if (report != null) %7B%0A            %09
 executio
@@ -2217,16 +2217,482 @@
 nerator(
+report));%0A            %7D%0A            %0A            smooks.filterSource(executionContext, source, result);%0A%0A        %7D catch (IOException e) %7B%0A        %09throw new RuntimeException(e);%0A%09%09%7D catch (SAXException e) %7B%0A        %09throw new RuntimeException(e);%0A%09%09%7D finally %7B%0A            smooks.close();%0A        %7D%0A    %7D%0A    %0A    static public void runSmooksTransformer(Source source, Result result, String smookesConfig) %7B%0A    %09runSmooksTransformer(source, result, smookesConfig, 
 %22target/
@@ -2722,62 +2722,205 @@
 ml%22)
-);%0A
+;
 %0A    
+%7D%0A
     
+%0A
     s
-mooks.filterSource(executionContext, 
+tatic public String runSmooksTransformer(String source, String smookesConfig) %7B%0A%0A        StringWriter writer = new StringWriter();%0A%0A        try %7B%0A            runSmooksTransformer(
 new 
@@ -2983,16 +2983,31 @@
 (writer)
+, smookesConfig
 );%0A%0A    
@@ -3440,51 +3440,54 @@
     
-smooks.close();%0A            writer.close();
+try %7Bwriter.close();%7D catch (IOException e) %7B%7D
 %0A   

Update issue 78