Finished backend feature and cleans code
This commit is contained in:
		| @@ -42,7 +42,7 @@ public class SparkApplication { | ||||
|         registry.registerController(new ProcessorInfoController(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.register(); | ||||
|   | ||||
| @@ -19,12 +19,12 @@ public class MultipleXMLController implements RestController { | ||||
|     private final Gson gson; | ||||
|     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.logger = logger; | ||||
|         this.saxon = saxon; | ||||
|         this.engine = engine; | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/multiple/xslt") | ||||
| @@ -40,11 +40,10 @@ public class MultipleXMLController implements RestController { | ||||
|             requestErrorResponse(response, e); | ||||
|             return; | ||||
|         } | ||||
|         processRequest(new MultipleXmlJob(response, requestBody, saxon, xmlJobType)); | ||||
|         processRequest(new MultipleXmlJob(response, requestBody, engine, xmlJobType)); | ||||
|     } | ||||
|  | ||||
|     private void processRequest(MultipleXmlJob xmlJob) { | ||||
|         logger.info(xmlJob); | ||||
|         XMLResponseBody responseBody = null; | ||||
|         long timeStart = System.currentTimeMillis(); | ||||
|         long duration; | ||||
| @@ -70,8 +69,8 @@ public class MultipleXMLController implements RestController { | ||||
|  | ||||
|     } | ||||
|  | ||||
|     private XMLResponseBody processData(MultipleXmlJob xmlJob) throws IOException, SaxonApiException, InterruptedException { | ||||
|         Saxon engine = xmlJob.getEngine(); | ||||
|     private XMLResponseBody processData(MultipleXmlJob xmlJob) throws Exception { | ||||
|         XmlEngine engine = xmlJob.getEngine(); | ||||
|         XMLMultipleFilesBody requestBody = xmlJob.getRequestBody(); | ||||
|         String result = engine.processXSLT(requestBody.getData(), requestBody.getProcessorData()); | ||||
|         return new XMLResponseBody(result, "OK", requestBody.getVersion()); | ||||
|   | ||||
| @@ -10,10 +10,10 @@ public class MultipleXmlJob { | ||||
|  | ||||
|     private final Response response; | ||||
|     private final XMLMultipleFilesBody requestBody; | ||||
|     private final Saxon engine; | ||||
|     private final XmlEngine engine; | ||||
|     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.requestBody = requestBody; | ||||
|         this.engine = engine; | ||||
| @@ -28,7 +28,7 @@ public class MultipleXmlJob { | ||||
|         return requestBody; | ||||
|     } | ||||
|  | ||||
|     public Saxon getEngine() { | ||||
|     public XmlEngine getEngine() { | ||||
|         return engine; | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -2,16 +2,14 @@ package com.r11.tools.xml; | ||||
|  | ||||
| import com.r11.tools.model.XMLMultipleFilesData; | ||||
| import com.r11.tools.model.XPathQueryResult; | ||||
| import net.sf.saxon.Configuration; | ||||
| import net.sf.saxon.lib.CatalogResourceResolver; | ||||
| import net.sf.saxon.s9api.*; | ||||
|  | ||||
| import javax.xml.transform.stream.StreamSource; | ||||
| import java.io.*; | ||||
| import java.nio.file.*; | ||||
| import java.util.ArrayList; | ||||
| import java.nio.file.Files; | ||||
| import java.nio.file.Path; | ||||
| import java.nio.file.Paths; | ||||
| import java.util.Comparator; | ||||
| import java.util.List; | ||||
| import java.util.UUID; | ||||
|  | ||||
| /** | ||||
| @@ -27,7 +25,7 @@ public class Saxon implements XmlEngine{ | ||||
|      * @return transformed xml | ||||
|      * @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); | ||||
|  | ||||
|         XsltCompiler compiler = processor.newXsltCompiler(); | ||||
| @@ -41,16 +39,19 @@ public class Saxon implements XmlEngine{ | ||||
|                 Path filePath = Files.createFile( Paths.get(filesPath + fileData.getFilename() ) ); | ||||
|                 try (FileWriter writer = new FileWriter(filePath.toFile())) { | ||||
|                     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(); | ||||
|             Serializer out = processor.newSerializer(sw); | ||||
|             out.setOutputProperty(Serializer.Property.METHOD, "xml"); | ||||
|             out.setOutputProperty(Serializer.Property.INDENT, "yes"); | ||||
|             Xslt30Transformer transformer = stylesheet.load30(); | ||||
|  | ||||
|             transformer.transform( new StreamSource( new File(filesPath+data[0].getFilename()) ) , out ); | ||||
|             return sw.toString(); | ||||
|  | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| package com.r11.tools.xml; | ||||
|  | ||||
| import com.r11.tools.model.XMLMultipleFilesData; | ||||
| import com.r11.tools.model.XPathQueryResult; | ||||
| import org.apache.xpath.XPathAPI; | ||||
| import org.w3c.dom.Document; | ||||
| @@ -17,7 +18,10 @@ import javax.xml.transform.stream.StreamSource; | ||||
| import javax.xml.validation.Schema; | ||||
| import javax.xml.validation.SchemaFactory; | ||||
| 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 | ||||
| @@ -57,6 +61,11 @@ public class Xalan implements XmlEngine{ | ||||
|         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 | ||||
|      * @param data xml | ||||
|   | ||||
| @@ -2,11 +2,10 @@ package com.r11.tools.xml; | ||||
|  | ||||
| import com.r11.tools.model.XMLMultipleFilesData; | ||||
| import com.r11.tools.model.XPathQueryResult; | ||||
| import net.sf.saxon.s9api.SaxonApiException; | ||||
|  | ||||
| import java.io.IOException; | ||||
|  | ||||
| public interface XmlEngine { | ||||
|  | ||||
|     String processXSLT(XMLMultipleFilesData[] data, String transform) throws Exception; | ||||
|     XPathQueryResult processXPath(String data, String query, String version) throws Exception; | ||||
|     String processXSLT(String data, String transform) throws Exception; | ||||
|     String validate(String data, String xsd) throws Exception; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user