package gov.nih.nci.integration.transformer;
import gov.nih.nci.integration.exception.IntegrationError;
import gov.nih.nci.integration.exception.IntegrationException;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author chandrasekaravr
 */
public class XSLTTransformer {

    /**
     * Transformer using XSLT.
     */
    private Transformer transformer = null;

    private TransformerFactory factory = null; //NOPMD
    
    private static final Logger LOG = LoggerFactory.getLogger(XSLTTransformer.class);

    /**
     * Constructor
     * 
     * @param transformer xslt transformer
     */
    public XSLTTransformer(Transformer transformer) {
        this.transformer = transformer;
        this.factory = null;//NOPMD
    }

    /**
     * Constructor
     * 
     * @param transformerFactory - xslt transformer factory
     */
    public XSLTTransformer(TransformerFactory transformerFactory) {
        this.factory = transformerFactory;
    }

    /**
     * Initializes the transformer to be used
     * 
     * @param xslFileName - xslFileName for the transformer
     * @param xslBasePath - Base xsl file path for resolver
     * @throws IntegrationException - exception thrown if any
     */
    public void initTransformer(String xslFileName, String xslBasePath)// NOPMD
            throws IntegrationException {
        if (factory == null) { //NOPMD
            LOG.error("XSLTTransformer..factory is NULL while init the transformer ");
            throw new IntegrationException(IntegrationError._1060);
        }
        if (StringUtils.isEmpty(xslFileName)) {
            LOG.error("XSLTTransformer..xslFileName is NULL while init the transformer ");
            throw new IntegrationException(IntegrationError._1061);
        }
        if (StringUtils.isEmpty(xslBasePath)) {
            // CHECKSTYLE:OFF
            xslBasePath = ".";
            // CHECKSTYLE:ON
        }

        try {
            transformer = factory.newTransformer(factory.getURIResolver().resolve(xslFileName, xslBasePath));
        } catch (TransformerConfigurationException e) {
            LOG.error("XSLTTransformer..TransformerConfigurationException while init the transformer ", e );
            throw new IntegrationException(IntegrationError._1025, e);
        } catch (TransformerException e) {
            LOG.error("XSLTTransformer..TransformerException while init the transformer ", e );
            throw new IntegrationException(IntegrationError._1026, e);
        }
    }

    /**
     * Performs xsl transformation on source XML
     * 
     * @param params the set of parameters to pass to the transformation, may be null
     * @param in XML input stream
     * @param out XML output stream
     * @throws IntegrationException exception
     */
    public void transform(Map<String, String> params, InputStream in, OutputStream out) throws IntegrationException {
        if (params != null) { //NOPMD
            for (Entry<String, String> entry : params.entrySet()) {
                transformer.setParameter(entry.getKey(), entry.getValue());
            }
        }

        try {
            transformer.transform(new StreamSource(in), new StreamResult(out));
        } catch (TransformerException e) {
            LOG.error("XSLTTransformer..TransformerException while transforming the XML ", e );
            throw new IntegrationException(IntegrationError._1027, e);
        }
    }
    
}


@@ -1803,16 +1803,8 @@
 ath)
-// NOPMD
 %0D%0A  
@@ -1836,32 +1836,72 @@
 ionException %7B%0D%0A
+        String basePath = xslBasePath;%0D%0A
         if (fact
@@ -1907,39 +1907,32 @@
 tory == null) %7B 
-//NOPMD
 %0D%0A            LO
@@ -2349,26 +2349,24 @@
 BasePath)) %7B
-%0D%0A
             
@@ -2367,27 +2367,8 @@
     
-  // CHECKSTYLE:OFF
 %0D%0A  
@@ -2377,20 +2377,17 @@
         
-xslB
+b
 asePath 
@@ -2392,19 +2392,16 @@
 h = %22.%22;
-%0D%0A 
         
@@ -2407,24 +2407,8 @@
     
-// CHECKSTYLE:ON
 %0D%0A  
@@ -2528,20 +2528,17 @@
 leName, 
-xslB
+b
 asePath)

Fix StaticAnalysis