Co-authored-by: Adam Bem <adam.bem@zoho.eu> Reviewed-on: #179 Reviewed-by: Mikolaj Widla <widlam@noreply.example.com>
This commit is contained in:
		| @@ -8,6 +8,9 @@ import com.r11.tools.controller.XPathController; | ||||
| import com.r11.tools.controller.XsdController; | ||||
| import com.r11.tools.controller.XsltController; | ||||
| import com.r11.tools.controller.internal.RestControllerRegistry; | ||||
| import com.r11.tools.xml.Saxon; | ||||
| import com.r11.tools.xml.Xalan; | ||||
| import com.r11.tools.xml.XmlEngine; | ||||
| import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import spark.Spark; | ||||
| @@ -35,12 +38,15 @@ public class SparkApplication { | ||||
|         Gson jsongson = new GsonBuilder() | ||||
|             .disableHtmlEscaping() | ||||
|             .create(); | ||||
|         XmlEngine saxon = new Saxon(); | ||||
|         XmlEngine xalan = new Xalan(); | ||||
|  | ||||
|  | ||||
|         RestControllerRegistry registry = new RestControllerRegistry(); | ||||
|         registry.registerController(new ProcessorInfoController(logger)); | ||||
|         registry.registerController(new XsdController(gson, logger)); | ||||
|         registry.registerController(new XPathController(gson, logger)); | ||||
|         registry.registerController(new XsltController(gson, logger)); | ||||
|         registry.registerController(new ProcessorInfoController(logger, saxon, xalan)); | ||||
|         registry.registerController(new XsdController(gson, logger, saxon, xalan)); | ||||
|         registry.registerController(new XPathController(gson, logger, saxon, xalan)); | ||||
|         registry.registerController(new XsltController(gson, logger, saxon, xalan)); | ||||
|         registry.registerController(new JsonController(gson, jsongson, logger)); | ||||
|  | ||||
|         registry.register(); | ||||
|   | ||||
| @@ -5,6 +5,7 @@ import com.r11.tools.controller.internal.HandlerType; | ||||
| import com.r11.tools.controller.internal.RestController; | ||||
| import com.r11.tools.controller.internal.ScopedControllerManifest; | ||||
| import com.r11.tools.xml.Saxon; | ||||
| import com.r11.tools.xml.XmlEngine; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import spark.Request; | ||||
| import spark.Response; | ||||
| @@ -13,9 +14,13 @@ import spark.Response; | ||||
| public class ProcessorInfoController implements RestController { | ||||
|  | ||||
|     private final Logger logger; | ||||
|     private final XmlEngine saxon; | ||||
|     private final XmlEngine xalan; | ||||
|  | ||||
|     public ProcessorInfoController(Logger logger) { | ||||
|     public ProcessorInfoController(Logger logger, XmlEngine saxon, XmlEngine xalan) { | ||||
|         this.logger = logger; | ||||
|         this.saxon = saxon; | ||||
|         this.xalan = xalan; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -24,8 +29,8 @@ public class ProcessorInfoController implements RestController { | ||||
|     @ScopedControllerManifest(method = HandlerType.GET, path = "/procinfo") | ||||
|     public void processorInfo(Request request, Response response) { | ||||
|         try { | ||||
|             response.header("processor", "Saxon " + Saxon.getVersion() + " over s9api"); | ||||
|             response.body(Saxon.getVersion()); | ||||
|             response.header("processor", "Saxon " + saxon.getVersion() + " over s9api"); | ||||
|             response.body(saxon.getVersion()); | ||||
|         } catch (Exception ex) { | ||||
|             this.logger.error("Error on retrieving engine version. " + ex); | ||||
|             response.body(ex.getMessage()); | ||||
|   | ||||
| @@ -5,6 +5,7 @@ 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; | ||||
| import spark.Response; | ||||
| @@ -15,9 +16,14 @@ public class XPathController implements RestController { | ||||
|     private final Gson gson; | ||||
|     private final Logger logger; | ||||
|  | ||||
|     public XPathController(Gson gson, Logger logger) { | ||||
|     private final XmlEngine saxon; | ||||
|     private final XmlEngine xalan; | ||||
|  | ||||
|     public XPathController(Gson gson, Logger logger, XmlEngine saxon, XmlEngine xalan) { | ||||
|         this.gson = gson; | ||||
|         this.logger = logger; | ||||
|         this.saxon = saxon; | ||||
|         this.xalan = xalan; | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xpath") | ||||
| @@ -56,11 +62,11 @@ public class XPathController implements RestController { | ||||
|         JsonObject responseJson = new JsonObject(); | ||||
|         switch (processor) { | ||||
|             case "saxon": | ||||
|                 response.header("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api"); | ||||
|                 response.header("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api"); | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|  | ||||
|                 try { | ||||
|                     tmp = Saxon.processXPath(data, query, version).getData().trim(); | ||||
|                     tmp = saxon.processXPath(data, query, version).getData().trim(); | ||||
|  | ||||
|                     response.status(200); | ||||
|  | ||||
| @@ -78,18 +84,18 @@ public class XPathController implements RestController { | ||||
|                 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("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api"); | ||||
|                 responseJson.addProperty("time", duration); | ||||
|  | ||||
|                 response.body(this.gson.toJson(responseJson)); | ||||
|                 return; | ||||
|  | ||||
|             case "xalan": | ||||
|                 response.header("processor", Xalan.getVersion()); | ||||
|                 response.header("processor", xalan.getVersion()); | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|  | ||||
|                 try { | ||||
|                     XPathQueryResult xPathQueryResult = Xalan.processXPath(data, query); | ||||
|                     XPathQueryResult xPathQueryResult = xalan.processXPath(data, query, ""); | ||||
|  | ||||
|                     response.status(200); | ||||
|  | ||||
| @@ -108,7 +114,7 @@ public class XPathController implements RestController { | ||||
|                 duration = System.currentTimeMillis() - timeStart; | ||||
|                 this.logger.info("Request (XPath, Xalan) processed in " + duration + " ms."); | ||||
|  | ||||
|                 responseJson.addProperty("processor", Xalan.getVersion()); | ||||
|                 responseJson.addProperty("processor", xalan.getVersion()); | ||||
|                 responseJson.addProperty("time", duration); | ||||
|  | ||||
|                 response.body(this.gson.toJson(responseJson)); | ||||
|   | ||||
| @@ -7,6 +7,7 @@ import com.r11.tools.controller.internal.HandlerType; | ||||
| import com.r11.tools.controller.internal.RestController; | ||||
| import com.r11.tools.controller.internal.ScopedControllerManifest; | ||||
| import com.r11.tools.xml.Xalan; | ||||
| import com.r11.tools.xml.XmlEngine; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import spark.Request; | ||||
| import spark.Response; | ||||
| @@ -17,9 +18,14 @@ public class XsdController implements RestController { | ||||
|     private final Gson gson; | ||||
|     private final Logger logger; | ||||
|  | ||||
|     public XsdController(Gson gson, Logger logger) { | ||||
|     private final XmlEngine saxon; | ||||
|     private final XmlEngine xalan; | ||||
|  | ||||
|     public XsdController(Gson gson, Logger logger, XmlEngine saxon, XmlEngine xalan) { | ||||
|         this.gson = gson; | ||||
|         this.logger = logger; | ||||
|         this.saxon = saxon; | ||||
|         this.xalan = xalan; | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xsd") | ||||
| @@ -44,14 +50,14 @@ public class XsdController implements RestController { | ||||
|         String data = requestJson.get("data").getAsString(); | ||||
|         String xsd = requestJson.get("process").getAsString(); | ||||
|  | ||||
|         response.header("processor", Xalan.getVersion()); | ||||
|         response.header("processor", xalan.getVersion()); | ||||
|  | ||||
|         long timeStart = System.currentTimeMillis(); | ||||
|         String tmp; | ||||
|  | ||||
|         JsonObject responseJson = new JsonObject(); | ||||
|         try { | ||||
|             tmp = Xalan.validate(data, xsd).trim(); | ||||
|             tmp = xalan.validate(data, xsd).trim(); | ||||
|  | ||||
|             response.status(200); | ||||
|  | ||||
| @@ -69,7 +75,7 @@ public class XsdController implements RestController { | ||||
|         long duration = System.currentTimeMillis() - timeStart; | ||||
|         this.logger.info("Request (XSD, Xalan) processed in " + duration + " ms."); | ||||
|  | ||||
|         responseJson.addProperty("processor", Xalan.getVersion()); | ||||
|         responseJson.addProperty("processor", xalan.getVersion()); | ||||
|         responseJson.addProperty("time", duration); | ||||
|  | ||||
|         response.body(this.gson.toJson(responseJson)); | ||||
|   | ||||
| @@ -8,6 +8,7 @@ import com.r11.tools.controller.internal.RestController; | ||||
| import com.r11.tools.controller.internal.ScopedControllerManifest; | ||||
| 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; | ||||
| import spark.Response; | ||||
| @@ -18,9 +19,14 @@ public class XsltController implements RestController { | ||||
|     private final Gson gson; | ||||
|     private final Logger logger; | ||||
|  | ||||
|     public XsltController(Gson gson, Logger logger) { | ||||
|     private final XmlEngine saxon; | ||||
|     private final XmlEngine xalan; | ||||
|  | ||||
|     public XsltController(Gson gson, Logger logger, XmlEngine saxon, XmlEngine xalan) { | ||||
|         this.gson = gson; | ||||
|         this.logger = logger; | ||||
|         this.saxon = saxon; | ||||
|         this.xalan = xalan; | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xslt") | ||||
| @@ -61,7 +67,7 @@ public class XsltController implements RestController { | ||||
|             case "saxon": | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|                 try { | ||||
|                     tmp = Saxon.processXSLT(data, query); | ||||
|                     tmp = saxon.processXSLT(data, query); | ||||
|  | ||||
|                     response.status(200); | ||||
|  | ||||
| @@ -79,7 +85,7 @@ public class XsltController implements RestController { | ||||
|                 duration = System.currentTimeMillis() - timeStart; | ||||
|                 this.logger.info("Request (XSLT, Saxon) processed in " + duration + " ms."); | ||||
|  | ||||
|                 responseJson.addProperty("processor", "Saxon " + Saxon.getVersion() + " " + version); | ||||
|                 responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version); | ||||
|                 responseJson.addProperty("time", duration); | ||||
|  | ||||
|                 response.body(this.gson.toJson(responseJson)); | ||||
| @@ -88,7 +94,7 @@ public class XsltController implements RestController { | ||||
|             case "xalan": | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|                 try { | ||||
|                     tmp = Xalan.processXSLT(data, query); | ||||
|                     tmp = xalan.processXSLT(data, query); | ||||
|  | ||||
|                     response.status(200); | ||||
|  | ||||
| @@ -106,7 +112,7 @@ public class XsltController implements RestController { | ||||
|                 duration = System.currentTimeMillis() - timeStart; | ||||
|                 this.logger.info("Request (XSLT, Xalan) processed in " + duration + " ms."); | ||||
|  | ||||
|                 responseJson.addProperty("processor", Xalan.getVersion()); | ||||
|                 responseJson.addProperty("processor", xalan.getVersion()); | ||||
|                 responseJson.addProperty("time", duration); | ||||
|  | ||||
|                 response.body(this.gson.toJson(responseJson)); | ||||
|   | ||||
| @@ -11,7 +11,7 @@ import java.io.StringWriter; | ||||
|  * Handler for Saxon engine | ||||
|  * @author Wojciech Czop | ||||
|  */ | ||||
| public class Saxon { | ||||
| public class Saxon implements XmlEngine{ | ||||
|  | ||||
|     /** | ||||
|      * Transforms string containing xml document via xslt | ||||
| @@ -20,7 +20,7 @@ public class Saxon { | ||||
|      * @return transformed xml | ||||
|      * @throws SaxonApiException thrown on stylesheet or transformation errors | ||||
|      */ | ||||
|     public static String processXSLT(String data, String transform) throws SaxonApiException { | ||||
|     public String processXSLT(String data, String transform) throws SaxonApiException { | ||||
|         Processor processor = new Processor(false); | ||||
|         XsltCompiler compiler = processor.newXsltCompiler(); | ||||
|         XsltExecutable stylesheet = compiler.compile(new StreamSource(new StringReader(transform))); | ||||
| @@ -34,6 +34,11 @@ public class Saxon { | ||||
|         return sw.toString(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String validate(String data, String xsd) { | ||||
|         throw new UnsupportedOperationException(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Process xpath and return either node or wrapped atomic value | ||||
|      * @param data xml to be querried | ||||
| @@ -42,7 +47,7 @@ public class Saxon { | ||||
|      * @return string xml representation of the node | ||||
|      * @throws Exception thrown on node building errors or invalid xpath | ||||
|      */ | ||||
|     public static XPathQueryResult processXPath(String data, String query, String version) throws Exception { | ||||
|     public XPathQueryResult processXPath(String data, String query, String version) throws Exception { | ||||
|         Processor p = new Processor(false); | ||||
|         XPathCompiler compiler = p.newXPathCompiler(); | ||||
|         DocumentBuilder builder = p.newDocumentBuilder(); | ||||
| @@ -70,7 +75,7 @@ public class Saxon { | ||||
|      * Returns version of the processor | ||||
|      * @return version of the processor | ||||
|      */ | ||||
|     public static String getVersion() { | ||||
|     public String getVersion() { | ||||
|         return new Processor(false).getSaxonProductVersion(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -24,7 +24,7 @@ import java.io.*; | ||||
|  * Handler for Xalan engine | ||||
|  * @author Wojciech Czop | ||||
|  */ | ||||
| public class Xalan { | ||||
| public class Xalan implements XmlEngine{ | ||||
|  | ||||
|     /** | ||||
|      * Transforms string containing xml document via xslt | ||||
| @@ -33,7 +33,7 @@ public class Xalan { | ||||
|      * @return transformed xml | ||||
|      * @throws Exception thrown on stylesheet or transformation errors | ||||
|      */ | ||||
|     public static String processXSLT(String data, String transform) throws Exception{ | ||||
|     public String processXSLT(String data, String transform) throws Exception { | ||||
|         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||||
|         DocumentBuilder builder = factory.newDocumentBuilder(); | ||||
|         Document document = builder.parse(new InputSource(new StringReader(data))); | ||||
| @@ -51,7 +51,7 @@ public class Xalan { | ||||
|         return sw.toString(); | ||||
|     } | ||||
|  | ||||
|     private static boolean isTextNode(Node n) { | ||||
|     private boolean isTextNode(Node n) { | ||||
|         if (n == null) | ||||
|             return false; | ||||
|         short nodeType = n.getNodeType(); | ||||
| @@ -65,7 +65,7 @@ public class Xalan { | ||||
|      * @return xml processed using given xpath | ||||
|      * @throws Exception thrown on node building errors or invalid xpath | ||||
|      */ | ||||
|     public static XPathQueryResult processXPath(String data, String transform) throws Exception { | ||||
|     public XPathQueryResult processXPath(String data, String transform, String version) throws Exception { | ||||
|  | ||||
|         // Set up a DOM tree to query. | ||||
|         InputSource in = new InputSource(new StringReader(data)); | ||||
| @@ -112,7 +112,7 @@ public class Xalan { | ||||
|      * Returns version of the processor | ||||
|      * @return version of the processor | ||||
|      */ | ||||
|     public static String getVersion(){ | ||||
|     public String getVersion(){ | ||||
|         return org.apache.xalan.Version.getVersion(); | ||||
|     } | ||||
|  | ||||
| @@ -123,7 +123,7 @@ public class Xalan { | ||||
|      * @return statement of validity | ||||
|      * @throws Exception thrown on invalid xsd schema or xml | ||||
|      */ | ||||
|     public static String validate(String data, String xsd) throws Exception{ | ||||
|     public String validate(String data, String xsd) throws Exception { | ||||
|         Source dataSource = new StreamSource(new StringReader(data)); | ||||
|         Source xsdSource = new StreamSource(new StringReader(xsd)); | ||||
|         SchemaFactory schemaFactory = SchemaFactory | ||||
|   | ||||
| @@ -0,0 +1,12 @@ | ||||
| package com.r11.tools.xml; | ||||
|  | ||||
| import com.r11.tools.controller.internal.XPathQueryResult; | ||||
|  | ||||
| public interface XmlEngine { | ||||
|     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; | ||||
|  | ||||
|     public String getVersion(); | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user