Split XPath process method
This commit is contained in:
@@ -3,8 +3,6 @@ package com.r11.tools.controller;
|
|||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.r11.tools.controller.internal.*;
|
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 com.r11.tools.xml.XmlEngine;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import spark.Request;
|
import spark.Request;
|
||||||
@@ -50,10 +48,6 @@ public class XPathController implements RestController {
|
|||||||
String processor = requestJson.get("processor").getAsString();
|
String processor = requestJson.get("processor").getAsString();
|
||||||
String version = requestJson.get("version").getAsString();
|
String version = requestJson.get("version").getAsString();
|
||||||
|
|
||||||
String tmp = "";
|
|
||||||
long timeStart;
|
|
||||||
long duration;
|
|
||||||
|
|
||||||
if (processor == null) {
|
if (processor == null) {
|
||||||
response.body("saxon, xalan");
|
response.body("saxon, xalan");
|
||||||
return;
|
return;
|
||||||
@@ -62,65 +56,77 @@ public class XPathController implements RestController {
|
|||||||
JsonObject responseJson = new JsonObject();
|
JsonObject responseJson = new JsonObject();
|
||||||
switch (processor) {
|
switch (processor) {
|
||||||
case "saxon":
|
case "saxon":
|
||||||
response.header("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api");
|
processWithSaxon(response, data, query, version, responseJson);
|
||||||
timeStart = System.currentTimeMillis();
|
break;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
case "xalan":
|
case "xalan":
|
||||||
response.header("processor", xalan.getVersion());
|
processWithXalan(response, data, query, responseJson);
|
||||||
timeStart = System.currentTimeMillis();
|
break;
|
||||||
|
|
||||||
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;
|
|
||||||
default:
|
default:
|
||||||
response.body("saxon, xalan");
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user