package bpelviz;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Creates the html5 visualization of a bpel file.
 */
public class BPELViz {

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

    public static final String BPELVIZ_CSS = "BPELviz.css";
    public static final String BPELVIZ_JS = "BPELviz.js";

    public void bpel2html(Path bpelFile, Path htmlFile) throws BPELVizException {
        try {
            createTransformer().transform(new StreamSource(bpelFile.toFile()), new StreamResult(htmlFile.toFile()));
        } catch (TransformerException e) {
            throw new BPELVizException(e);
        }

        try {
            Files.copy(BPELViz.class.getResourceAsStream("/" + BPELVIZ_CSS), htmlFile.getParent().resolve(BPELVIZ_CSS), StandardCopyOption.REPLACE_EXISTING);
            Files.copy(BPELViz.class.getResourceAsStream("/" + BPELVIZ_JS), htmlFile.getParent().resolve(BPELVIZ_JS), StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            throw new BPELVizException("Could not copy css/js file", e);
        }
    }

    private Transformer createTransformer() throws TransformerConfigurationException {
        Templates templates = createTemplates();
        Transformer trans = templates.newTransformer();
        trans.setErrorListener(new ErrorListener() {
            @Override
            public void warning(TransformerException exception) throws TransformerException {
                logger.warn("Transformation", exception);
            }

            @Override
            public void error(TransformerException exception) throws TransformerException {
                logger.error("Transformation", exception);
            }

            @Override
            public void fatalError(TransformerException exception) throws TransformerException {
                logger.error("Transformation", exception);
            }
        });
        return trans;
    }

    private Templates createTemplates() throws TransformerConfigurationException {
        InputStream is = BPELViz.class.getResourceAsStream("/BPELviz.xsl");
        if (is == null) {
            throw new IllegalStateException("Could not find BPELviz.xsl");
        }
        Source xsltSource = new StreamSource(is);
        TransformerFactory transFact = TransformerFactory.newInstance();
        transFact.setURIResolver(new URIResolver() {
            @Override
            public Source resolve(String href, String base) throws TransformerException {
                return new StreamSource(BPELViz.class.getResourceAsStream("/" + href));
            }
        });
        return transFact.newTemplates(xsltSource);
    }

}

@@ -11,16 +11,74 @@
 elviz;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import j
@@ -99,16 +99,16 @@
 form.*;%0A
-
 import j
@@ -351,64 +351,32 @@
 on;%0A
-%0A
 import 
-org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
+java.util.Objects
 ;%0A%0A/
@@ -997,32 +997,84 @@
      Files.copy(
+Objects.requireNonNull(%0A                            
 BPELViz.class.ge
@@ -1110,16 +1110,89 @@
 IZ_CSS),
+ %22Could not find /%22 + BPELVIZ_CSS + %22 in classpath%22),%0A                   
  htmlFil
@@ -1292,60 +1292,184 @@
 opy(
-BPELViz.class.getResourceAsStream(%22/%22 + BPELVIZ_JS),
+Objects.requireNonNull(%0A                            BPELViz.class.getResourceAsStream(%22/%22 + BPELVIZ_JS), %22Could not find /%22 + BPELVIZ_JS + %22 in classpath%22),%0A                   
  htm

Ensure that the results are not null