Added backend support for multiple files in XSLT. #272
@@ -42,7 +42,7 @@ public class SparkApplication {
 | 
				
			|||||||
        registry.registerController(new ProcessorInfoController(logger, saxon, xalan));
 | 
					        registry.registerController(new ProcessorInfoController(logger, saxon, xalan));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        registry.registerController(new XmlController(gson, logger, saxon, xalan));
 | 
					        registry.registerController(new XmlController(gson, logger, saxon, xalan));
 | 
				
			||||||
        registry.registerController(new MultipleXMLController(gson,logger, (Saxon) saxon));
 | 
					        registry.registerController(new MultipleXMLController(gson,logger, saxon));
 | 
				
			||||||
        registry.registerController(new JsonController(gson, jsongson, logger));
 | 
					        registry.registerController(new JsonController(gson, jsongson, logger));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        registry.register();
 | 
					        registry.register();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,12 +19,12 @@ public class MultipleXMLController implements RestController {
 | 
				
			|||||||
    private final Gson gson;
 | 
					    private final Gson gson;
 | 
				
			||||||
    private final Logger logger;
 | 
					    private final Logger logger;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private final Saxon saxon;
 | 
					    private final XmlEngine engine;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public MultipleXMLController(Gson gson, Logger logger, Saxon saxon) {
 | 
					    public MultipleXMLController(Gson gson, Logger logger, XmlEngine engine) {
 | 
				
			||||||
        this.gson = gson;
 | 
					        this.gson = gson;
 | 
				
			||||||
        this.logger = logger;
 | 
					        this.logger = logger;
 | 
				
			||||||
        this.saxon = saxon;
 | 
					        this.engine = engine;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @ScopedControllerManifest(method = HandlerType.POST, path = "/multiple/xslt")
 | 
					    @ScopedControllerManifest(method = HandlerType.POST, path = "/multiple/xslt")
 | 
				
			||||||
@@ -40,11 +40,10 @@ public class MultipleXMLController implements RestController {
 | 
				
			|||||||
            requestErrorResponse(response, e);
 | 
					            requestErrorResponse(response, e);
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        processRequest(new MultipleXmlJob(response, requestBody, saxon, xmlJobType));
 | 
					        processRequest(new MultipleXmlJob(response, requestBody, engine, xmlJobType));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void processRequest(MultipleXmlJob xmlJob) {
 | 
					    private void processRequest(MultipleXmlJob xmlJob) {
 | 
				
			||||||
        logger.info(xmlJob);
 | 
					 | 
				
			||||||
        XMLResponseBody responseBody = null;
 | 
					        XMLResponseBody responseBody = null;
 | 
				
			||||||
        long timeStart = System.currentTimeMillis();
 | 
					        long timeStart = System.currentTimeMillis();
 | 
				
			||||||
        long duration;
 | 
					        long duration;
 | 
				
			||||||
@@ -70,8 +69,8 @@ public class MultipleXMLController implements RestController {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private XMLResponseBody processData(MultipleXmlJob xmlJob) throws IOException, SaxonApiException, InterruptedException {
 | 
					    private XMLResponseBody processData(MultipleXmlJob xmlJob) throws Exception {
 | 
				
			||||||
        Saxon engine = xmlJob.getEngine();
 | 
					        XmlEngine engine = xmlJob.getEngine();
 | 
				
			||||||
        XMLMultipleFilesBody requestBody = xmlJob.getRequestBody();
 | 
					        XMLMultipleFilesBody requestBody = xmlJob.getRequestBody();
 | 
				
			||||||
        String result = engine.processXSLT(requestBody.getData(), requestBody.getProcessorData());
 | 
					        String result = engine.processXSLT(requestBody.getData(), requestBody.getProcessorData());
 | 
				
			||||||
        return new XMLResponseBody(result, "OK", requestBody.getVersion());
 | 
					        return new XMLResponseBody(result, "OK", requestBody.getVersion());
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,10 +10,10 @@ public class MultipleXmlJob {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    private final Response response;
 | 
					    private final Response response;
 | 
				
			||||||
    private final XMLMultipleFilesBody requestBody;
 | 
					    private final XMLMultipleFilesBody requestBody;
 | 
				
			||||||
    private final Saxon engine;
 | 
					    private final XmlEngine engine;
 | 
				
			||||||
    private final XmlJobType xmlJobType;
 | 
					    private final XmlJobType xmlJobType;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public MultipleXmlJob(Response response, XMLMultipleFilesBody requestBody, Saxon engine, XmlJobType xmlJobType) {
 | 
					    public MultipleXmlJob(Response response, XMLMultipleFilesBody requestBody, XmlEngine engine, XmlJobType xmlJobType) {
 | 
				
			||||||
        this.response = response;
 | 
					        this.response = response;
 | 
				
			||||||
        this.requestBody = requestBody;
 | 
					        this.requestBody = requestBody;
 | 
				
			||||||
        this.engine = engine;
 | 
					        this.engine = engine;
 | 
				
			||||||
@@ -28,7 +28,7 @@ public class MultipleXmlJob {
 | 
				
			|||||||
        return requestBody;
 | 
					        return requestBody;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public Saxon getEngine() {
 | 
					    public XmlEngine getEngine() {
 | 
				
			||||||
        return engine;
 | 
					        return engine;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,16 +2,14 @@ package com.r11.tools.xml;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import com.r11.tools.model.XMLMultipleFilesData;
 | 
					import com.r11.tools.model.XMLMultipleFilesData;
 | 
				
			||||||
import com.r11.tools.model.XPathQueryResult;
 | 
					import com.r11.tools.model.XPathQueryResult;
 | 
				
			||||||
import net.sf.saxon.Configuration;
 | 
					 | 
				
			||||||
import net.sf.saxon.lib.CatalogResourceResolver;
 | 
					 | 
				
			||||||
import net.sf.saxon.s9api.*;
 | 
					import net.sf.saxon.s9api.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import javax.xml.transform.stream.StreamSource;
 | 
					import javax.xml.transform.stream.StreamSource;
 | 
				
			||||||
import java.io.*;
 | 
					import java.io.*;
 | 
				
			||||||
import java.nio.file.*;
 | 
					import java.nio.file.Files;
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.nio.file.Path;
 | 
				
			||||||
 | 
					import java.nio.file.Paths;
 | 
				
			||||||
import java.util.Comparator;
 | 
					import java.util.Comparator;
 | 
				
			||||||
import java.util.List;
 | 
					 | 
				
			||||||
import java.util.UUID;
 | 
					import java.util.UUID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@@ -27,7 +25,7 @@ public class Saxon implements XmlEngine{
 | 
				
			|||||||
     * @return transformed xml
 | 
					     * @return transformed xml
 | 
				
			||||||
     * @throws SaxonApiException thrown on stylesheet or transformation error
 | 
					     * @throws SaxonApiException thrown on stylesheet or transformation error
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public String processXSLT(XMLMultipleFilesData[] data, String transform) throws SaxonApiException, IOException, InterruptedException {
 | 
					    public String processXSLT(XMLMultipleFilesData[] data, String transform) throws SaxonApiException, IOException{
 | 
				
			||||||
        Processor processor = new Processor(false);
 | 
					        Processor processor = new Processor(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        XsltCompiler compiler = processor.newXsltCompiler();
 | 
					        XsltCompiler compiler = processor.newXsltCompiler();
 | 
				
			||||||
@@ -41,16 +39,19 @@ public class Saxon implements XmlEngine{
 | 
				
			|||||||
                Path filePath = Files.createFile( Paths.get(filesPath + fileData.getFilename() ) );
 | 
					                Path filePath = Files.createFile( Paths.get(filesPath + fileData.getFilename() ) );
 | 
				
			||||||
                try (FileWriter writer = new FileWriter(filePath.toFile())) {
 | 
					                try (FileWriter writer = new FileWriter(filePath.toFile())) {
 | 
				
			||||||
                    writer.write(fileData.getData());
 | 
					                    writer.write(fileData.getData());
 | 
				
			||||||
                    transform = transform.replace('\''+fileData.getFilename()+'\'',filesPath+fileData.getFilename());
 | 
					 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            XsltExecutable stylesheet = compiler.compile(new StreamSource( new StringReader(transform) ));
 | 
					            Path transformPath = Files.createFile( Paths.get(filesPath + "transform.xsl") );
 | 
				
			||||||
 | 
					            FileWriter writer = new FileWriter(transformPath.toFile());
 | 
				
			||||||
 | 
					            writer.write(transform);
 | 
				
			||||||
 | 
					            writer.close();
 | 
				
			||||||
 | 
					            XsltExecutable stylesheet = compiler.compile( new StreamSource( transformPath.toFile() ));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            StringWriter sw = new StringWriter();
 | 
					            StringWriter sw = new StringWriter();
 | 
				
			||||||
            Serializer out = processor.newSerializer(sw);
 | 
					            Serializer out = processor.newSerializer(sw);
 | 
				
			||||||
            out.setOutputProperty(Serializer.Property.METHOD, "xml");
 | 
					            out.setOutputProperty(Serializer.Property.METHOD, "xml");
 | 
				
			||||||
            out.setOutputProperty(Serializer.Property.INDENT, "yes");
 | 
					            out.setOutputProperty(Serializer.Property.INDENT, "yes");
 | 
				
			||||||
            Xslt30Transformer transformer = stylesheet.load30();
 | 
					            Xslt30Transformer transformer = stylesheet.load30();
 | 
				
			||||||
 | 
					 | 
				
			||||||
            transformer.transform( new StreamSource( new File(filesPath+data[0].getFilename()) ) , out );
 | 
					            transformer.transform( new StreamSource( new File(filesPath+data[0].getFilename()) ) , out );
 | 
				
			||||||
            return sw.toString();
 | 
					            return sw.toString();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,6 @@
 | 
				
			|||||||
package com.r11.tools.xml;
 | 
					package com.r11.tools.xml;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.r11.tools.model.XMLMultipleFilesData;
 | 
				
			||||||
import com.r11.tools.model.XPathQueryResult;
 | 
					import com.r11.tools.model.XPathQueryResult;
 | 
				
			||||||
import org.apache.xpath.XPathAPI;
 | 
					import org.apache.xpath.XPathAPI;
 | 
				
			||||||
import org.w3c.dom.Document;
 | 
					import org.w3c.dom.Document;
 | 
				
			||||||
@@ -17,7 +18,10 @@ import javax.xml.transform.stream.StreamSource;
 | 
				
			|||||||
import javax.xml.validation.Schema;
 | 
					import javax.xml.validation.Schema;
 | 
				
			||||||
import javax.xml.validation.SchemaFactory;
 | 
					import javax.xml.validation.SchemaFactory;
 | 
				
			||||||
import javax.xml.validation.Validator;
 | 
					import javax.xml.validation.Validator;
 | 
				
			||||||
import java.io.*;
 | 
					import java.io.ByteArrayOutputStream;
 | 
				
			||||||
 | 
					import java.io.OutputStreamWriter;
 | 
				
			||||||
 | 
					import java.io.StringReader;
 | 
				
			||||||
 | 
					import java.io.StringWriter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Handler for Xalan engine
 | 
					 * Handler for Xalan engine
 | 
				
			||||||
@@ -57,6 +61,11 @@ public class Xalan implements XmlEngine{
 | 
				
			|||||||
        return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
 | 
					        return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public String processXSLT(XMLMultipleFilesData[] data, String transform) throws Exception {
 | 
				
			||||||
 | 
					        throw new UnsupportedOperationException("Xalan does not support multiple files XSLT processing");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Process xpath and return either node or wrapped atomic value
 | 
					     * Process xpath and return either node or wrapped atomic value
 | 
				
			||||||
     * @param data xml
 | 
					     * @param data xml
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,11 +2,10 @@ package com.r11.tools.xml;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import com.r11.tools.model.XMLMultipleFilesData;
 | 
					import com.r11.tools.model.XMLMultipleFilesData;
 | 
				
			||||||
import com.r11.tools.model.XPathQueryResult;
 | 
					import com.r11.tools.model.XPathQueryResult;
 | 
				
			||||||
import net.sf.saxon.s9api.SaxonApiException;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import java.io.IOException;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
public interface XmlEngine {
 | 
					public interface XmlEngine {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    String processXSLT(XMLMultipleFilesData[] data, String transform) throws Exception;
 | 
				
			||||||
    XPathQueryResult processXPath(String data, String query, String version) throws Exception;
 | 
					    XPathQueryResult processXPath(String data, String query, String version) throws Exception;
 | 
				
			||||||
    String processXSLT(String data, String transform) throws Exception;
 | 
					    String processXSLT(String data, String transform) throws Exception;
 | 
				
			||||||
    String validate(String data, String xsd) throws Exception;
 | 
					    String validate(String data, String xsd) throws Exception;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user