Co-authored-by: Adam Bem <adam.bem@zoho.eu> Reviewed-on: #196 Reviewed-by: Dariusz Augustyniak <augustyd@noreply.example.com>
This commit is contained in:
		| @@ -44,7 +44,7 @@ public class SparkApplication { | ||||
|  | ||||
|         RestControllerRegistry registry = new RestControllerRegistry(); | ||||
|         registry.registerController(new ProcessorInfoController(logger, saxon, xalan)); | ||||
|         registry.registerController(new XsdController(gson, logger, saxon, xalan)); | ||||
|         registry.registerController(new XsdController(gson, logger, xalan)); | ||||
|         registry.registerController(new XPathController(gson, logger, saxon, xalan)); | ||||
|         registry.registerController(new XsltController(gson, logger, saxon, xalan)); | ||||
|         registry.registerController(new JsonController(gson, jsongson, logger)); | ||||
|   | ||||
| @@ -3,8 +3,6 @@ package com.r11.tools.controller; | ||||
| import com.google.gson.Gson; | ||||
| import com.google.gson.JsonObject; | ||||
| import com.r11.tools.controller.internal.*; | ||||
| import com.r11.tools.xml.Saxon; | ||||
| import com.r11.tools.xml.Xalan; | ||||
| import com.r11.tools.xml.XmlEngine; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import spark.Request; | ||||
| @@ -50,10 +48,6 @@ public class XPathController implements RestController { | ||||
|         String processor = requestJson.get("processor").getAsString(); | ||||
|         String version = requestJson.get("version").getAsString(); | ||||
|  | ||||
|         String tmp = ""; | ||||
|         long timeStart; | ||||
|         long duration; | ||||
|  | ||||
|         if (processor == null) { | ||||
|             response.body("saxon, xalan"); | ||||
|             return; | ||||
| @@ -62,65 +56,77 @@ public class XPathController implements RestController { | ||||
|         JsonObject responseJson = new JsonObject(); | ||||
|         switch (processor) { | ||||
|             case "saxon": | ||||
|                 response.header("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api"); | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|  | ||||
|                 try { | ||||
|                     tmp = saxon.processXPath(data, query, version).getData().trim(); | ||||
|  | ||||
|                     response.status(200); | ||||
|  | ||||
|                     responseJson.addProperty("result", tmp); | ||||
|                     responseJson.addProperty("status", "OK"); | ||||
|                 } catch (Exception ex) { | ||||
|                     this.logger.error("Error on processing XPath using Saxon. " + ex); | ||||
|  | ||||
|                     response.status(400); | ||||
|  | ||||
|                     responseJson.addProperty("result", ex.getMessage()); | ||||
|                     responseJson.addProperty("status", "ERR"); | ||||
|                 } | ||||
|  | ||||
|                 duration = System.currentTimeMillis() - timeStart; | ||||
|                 this.logger.info("Request (XPath, Saxon) processed in " + duration + " ms."); | ||||
|  | ||||
|                 responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api"); | ||||
|                 responseJson.addProperty("time", duration); | ||||
|  | ||||
|                 response.body(this.gson.toJson(responseJson)); | ||||
|                 return; | ||||
|  | ||||
|                 processWithSaxon(response, data, query, version, responseJson); | ||||
|                 break; | ||||
|             case "xalan": | ||||
|                 response.header("processor", xalan.getVersion()); | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|  | ||||
|                 try { | ||||
|                     XPathQueryResult xPathQueryResult = xalan.processXPath(data, query, ""); | ||||
|  | ||||
|                     response.status(200); | ||||
|  | ||||
|                     responseJson.addProperty("result", xPathQueryResult.getData().trim()); | ||||
|                     responseJson.addProperty("status", "OK"); | ||||
|                     responseJson.addProperty("type", xPathQueryResult.getType()); | ||||
|                 } catch (Exception ex) { | ||||
|                     this.logger.error("Error on processing XPath using Xalan. " + ex); | ||||
|  | ||||
|                     response.status(400); | ||||
|  | ||||
|                     responseJson.addProperty("result", ex.getMessage()); | ||||
|                     responseJson.addProperty("status", "ERR"); | ||||
|                 } | ||||
|  | ||||
|                 duration = System.currentTimeMillis() - timeStart; | ||||
|                 this.logger.info("Request (XPath, Xalan) processed in " + duration + " ms."); | ||||
|  | ||||
|                 responseJson.addProperty("processor", xalan.getVersion()); | ||||
|                 responseJson.addProperty("time", duration); | ||||
|  | ||||
|                 response.body(this.gson.toJson(responseJson)); | ||||
|                 return; | ||||
|                 processWithXalan(response, data, query, responseJson); | ||||
|                 break; | ||||
|             default: | ||||
|                 response.body("saxon, xalan"); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void processWithXalan(Response response, String data, String query, JsonObject responseJson) { | ||||
|         long timeStart; | ||||
|         long duration; | ||||
|         response.header("processor", xalan.getVersion()); | ||||
|         timeStart = System.currentTimeMillis(); | ||||
|  | ||||
|         try { | ||||
|             XPathQueryResult xPathQueryResult = xalan.processXPath(data, query, ""); | ||||
|  | ||||
|             response.status(200); | ||||
|  | ||||
|             responseJson.addProperty("result", xPathQueryResult.getData().trim()); | ||||
|             responseJson.addProperty("status", "OK"); | ||||
|             responseJson.addProperty("type", xPathQueryResult.getType()); | ||||
|         } catch (Exception ex) { | ||||
|             this.logger.error("Error on processing XPath using Xalan. " + ex); | ||||
|  | ||||
|             response.status(400); | ||||
|  | ||||
|             responseJson.addProperty("result", ex.getMessage()); | ||||
|             responseJson.addProperty("status", "ERR"); | ||||
|         } | ||||
|  | ||||
|         duration = System.currentTimeMillis() - timeStart; | ||||
|         this.logger.info("Request (XPath, Xalan) processed in " + duration + " ms."); | ||||
|  | ||||
|         responseJson.addProperty("processor", xalan.getVersion()); | ||||
|         responseJson.addProperty("time", duration); | ||||
|  | ||||
|         response.body(this.gson.toJson(responseJson)); | ||||
|     } | ||||
|  | ||||
|     private void processWithSaxon(Response response, String data, String query, String version, JsonObject responseJson) { | ||||
|         long timeStart; | ||||
|         String tmp; | ||||
|         long duration; | ||||
|         response.header("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api"); | ||||
|         timeStart = System.currentTimeMillis(); | ||||
|  | ||||
|         try { | ||||
|             tmp = saxon.processXPath(data, query, version).getData().trim(); | ||||
|  | ||||
|             response.status(200); | ||||
|  | ||||
|             responseJson.addProperty("result", tmp); | ||||
|             responseJson.addProperty("status", "OK"); | ||||
|         } catch (Exception ex) { | ||||
|             this.logger.error("Error on processing XPath using Saxon. " + ex); | ||||
|  | ||||
|             response.status(400); | ||||
|  | ||||
|             responseJson.addProperty("result", ex.getMessage()); | ||||
|             responseJson.addProperty("status", "ERR"); | ||||
|         } | ||||
|  | ||||
|         duration = System.currentTimeMillis() - timeStart; | ||||
|         this.logger.info("Request (XPath, Saxon) processed in " + duration + " ms."); | ||||
|  | ||||
|         responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api"); | ||||
|         responseJson.addProperty("time", duration); | ||||
|  | ||||
|         response.body(this.gson.toJson(responseJson)); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -18,13 +18,11 @@ public class XsdController implements RestController { | ||||
|     private final Gson gson; | ||||
|     private final Logger logger; | ||||
|  | ||||
|     private final XmlEngine saxon; | ||||
|     private final XmlEngine xalan; | ||||
|  | ||||
|     public XsdController(Gson gson, Logger logger, XmlEngine saxon, XmlEngine xalan) { | ||||
|     public XsdController(Gson gson, Logger logger, XmlEngine xalan) { | ||||
|         this.gson = gson; | ||||
|         this.logger = logger; | ||||
|         this.saxon = saxon; | ||||
|         this.xalan = xalan; | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -57,69 +57,78 @@ public class XsltController implements RestController { | ||||
|             response.body("saxon, xalan"); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         String tmp; | ||||
|         long timeStart; | ||||
|         long duration; | ||||
|  | ||||
|         JsonObject responseJson = new JsonObject(); | ||||
|         switch (processor) { | ||||
|             case "saxon": | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|                 try { | ||||
|                     tmp = saxon.processXSLT(data, query); | ||||
|  | ||||
|                     response.status(200); | ||||
|  | ||||
|                     responseJson.addProperty("result", tmp); | ||||
|                     responseJson.addProperty("status", "OK"); | ||||
|                 } catch (Exception ex) { | ||||
|                     this.logger.error("Error on processing XSLT using Saxon. " + ex); | ||||
|  | ||||
|                     response.status(400); | ||||
|  | ||||
|                     responseJson.addProperty("result", ex.getMessage()); | ||||
|                     responseJson.addProperty("status", "ERR"); | ||||
|                 } | ||||
|  | ||||
|                 duration = System.currentTimeMillis() - timeStart; | ||||
|                 this.logger.info("Request (XSLT, Saxon) processed in " + duration + " ms."); | ||||
|  | ||||
|                 responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version); | ||||
|                 responseJson.addProperty("time", duration); | ||||
|  | ||||
|                 response.body(this.gson.toJson(responseJson)); | ||||
|                 processWithSaxon(response, data, query, version, responseJson); | ||||
|                 return; | ||||
|  | ||||
|             case "xalan": | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|                 try { | ||||
|                     tmp = xalan.processXSLT(data, query); | ||||
|  | ||||
|                     response.status(200); | ||||
|  | ||||
|                     responseJson.addProperty("result", tmp); | ||||
|                     responseJson.addProperty("status", "OK"); | ||||
|                 } catch (Exception ex) { | ||||
|                     this.logger.error("Error on processing XSLT using Xalan. " + ex); | ||||
|  | ||||
|                     response.status(400); | ||||
|  | ||||
|                     responseJson.addProperty("result", ex.getMessage()); | ||||
|                     responseJson.addProperty("status", "ERR"); | ||||
|                 } | ||||
|  | ||||
|                 duration = System.currentTimeMillis() - timeStart; | ||||
|                 this.logger.info("Request (XSLT, Xalan) processed in " + duration + " ms."); | ||||
|  | ||||
|                 responseJson.addProperty("processor", xalan.getVersion()); | ||||
|                 responseJson.addProperty("time", duration); | ||||
|  | ||||
|                 response.body(this.gson.toJson(responseJson)); | ||||
|                 processWithXalan(response, data, query, responseJson); | ||||
|                 return; | ||||
|  | ||||
|             default: | ||||
|                 response.body("saxon, xalan"); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void processWithXalan(Response response, String data, String query, JsonObject responseJson) { | ||||
|         long duration; | ||||
|         long timeStart; | ||||
|         String tmp; | ||||
|         timeStart = System.currentTimeMillis(); | ||||
|         try { | ||||
|             tmp = xalan.processXSLT(data, query); | ||||
|  | ||||
|             response.status(200); | ||||
|  | ||||
|             responseJson.addProperty("result", tmp); | ||||
|             responseJson.addProperty("status", "OK"); | ||||
|         } catch (Exception ex) { | ||||
|             this.logger.error("Error on processing XSLT using Xalan. " + ex); | ||||
|  | ||||
|             response.status(400); | ||||
|  | ||||
|             responseJson.addProperty("result", ex.getMessage()); | ||||
|             responseJson.addProperty("status", "ERR"); | ||||
|         } | ||||
|  | ||||
|         duration = System.currentTimeMillis() - timeStart; | ||||
|         this.logger.info("Request (XSLT, Xalan) processed in " + duration + " ms."); | ||||
|  | ||||
|         responseJson.addProperty("processor", xalan.getVersion()); | ||||
|         responseJson.addProperty("time", duration); | ||||
|  | ||||
|         response.body(this.gson.toJson(responseJson)); | ||||
|     } | ||||
|  | ||||
|     private void processWithSaxon(Response response, String data, String query, String version, JsonObject responseJson) { | ||||
|         long duration; | ||||
|         String tmp; | ||||
|         long timeStart; | ||||
|         timeStart = System.currentTimeMillis(); | ||||
|         try { | ||||
|             tmp = saxon.processXSLT(data, query); | ||||
|  | ||||
|             response.status(200); | ||||
|  | ||||
|             responseJson.addProperty("result", tmp); | ||||
|             responseJson.addProperty("status", "OK"); | ||||
|         } catch (Exception ex) { | ||||
|             this.logger.error("Error on processing XSLT using Saxon. " + ex); | ||||
|  | ||||
|             response.status(400); | ||||
|  | ||||
|             responseJson.addProperty("result", ex.getMessage()); | ||||
|             responseJson.addProperty("status", "ERR"); | ||||
|         } | ||||
|  | ||||
|         duration = System.currentTimeMillis() - timeStart; | ||||
|         this.logger.info("Request (XSLT, Saxon) processed in " + duration + " ms."); | ||||
|  | ||||
|         responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version); | ||||
|         responseJson.addProperty("time", duration); | ||||
|  | ||||
|         response.body(this.gson.toJson(responseJson)); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -103,7 +103,7 @@ public class Xalan implements XmlEngine{ | ||||
|             return new XPathQueryResult(resultString.toString(), "node"); | ||||
|         } catch (TransformerException e) { | ||||
|             String returnData = XPathAPI.eval(doc, transform).toString(); | ||||
|             return new XPathQueryResult(data, "string"); | ||||
|             return new XPathQueryResult(returnData, "string"); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user