Implemented showing returned type (#112) #148
| @@ -54,12 +54,12 @@ def xpath(source: str, xpath: str) -> str: | ||||
|     # root.xpath can return 4 types: float, string, bool and list. | ||||
|     # List is the only one that can't be simply converted to str | ||||
|     if type(result) is not list: | ||||
|         return str(result) | ||||
|         return str(result), type(result).__name__ | ||||
|     else: | ||||
|         result_string = "" | ||||
|         for e in result: | ||||
|             result_string += etree.tostring(e, pretty_print=True).decode() + "\n" | ||||
|         return result_string | ||||
|         return result_string, "node" | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -35,7 +35,7 @@ def process_xml(request: request, type: str) -> str: | ||||
|         elif (type == "xslt"): | ||||
|             response_json['result'] = Parser.xslt(data, process) | ||||
|         elif (type == "xpath"): | ||||
|             response_json['result'] = Parser.xpath(data, process) | ||||
|             response_json['result'], response_json['type'] = Parser.xpath(data, process) | ||||
|         elif (type == "prettify"): | ||||
|             response_json['result'] = Parser.formatXML(data, True) | ||||
|         elif (type == "minimize"): | ||||
|   | ||||
| @@ -2,10 +2,7 @@ package com.r11.tools.controller; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import com.google.gson.JsonObject; | ||||
| import com.r11.tools.controller.internal.GlobalControllerManifest; | ||||
| 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.controller.internal.*; | ||||
| import com.r11.tools.xml.Saxon; | ||||
| import com.r11.tools.xml.Xalan; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| @@ -63,7 +60,7 @@ public class XPathController implements RestController { | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|  | ||||
|                 try { | ||||
|                     tmp = Saxon.processXPath(data, query, version).trim(); | ||||
|                     tmp = Saxon.processXPath(data, query, version).getData().trim(); | ||||
|  | ||||
|                     response.status(200); | ||||
|  | ||||
| @@ -92,12 +89,13 @@ public class XPathController implements RestController { | ||||
|                 timeStart = System.currentTimeMillis(); | ||||
|  | ||||
|                 try { | ||||
|                     tmp = Xalan.processXPath(data, query).trim(); | ||||
|                     XPathQueryResult xPathQueryResult = Xalan.processXPath(data, query); | ||||
|  | ||||
|                     response.status(200); | ||||
|  | ||||
|                     responseJson.addProperty("result", tmp); | ||||
|                     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); | ||||
|  | ||||
|   | ||||
| @@ -0,0 +1,22 @@ | ||||
| package com.r11.tools.controller.internal; | ||||
|  | ||||
| /** | ||||
|  * Class used to store data received from parser and type of that data (node, string, etc.) | ||||
|  */ | ||||
| public class XPathQueryResult { | ||||
|     private String data; | ||||
|     private String type; | ||||
|  | ||||
|     public XPathQueryResult(String data, String type) { | ||||
|         this.data = data; | ||||
|         this.type = type; | ||||
|     } | ||||
|  | ||||
|     public String getData() { | ||||
|         return data; | ||||
|     } | ||||
|  | ||||
|     public String getType() { | ||||
|         return type; | ||||
|     } | ||||
| } | ||||
| @@ -1,5 +1,6 @@ | ||||
| package com.r11.tools.xml; | ||||
|  | ||||
| import com.r11.tools.controller.internal.XPathQueryResult; | ||||
| import net.sf.saxon.s9api.*; | ||||
|  | ||||
| import javax.xml.transform.stream.StreamSource; | ||||
| @@ -41,7 +42,7 @@ public class Saxon { | ||||
|      * @return string xml representation of the node | ||||
|      * @throws Exception thrown on node building errors or invalid xpath | ||||
|      */ | ||||
|     public static String processXPath(String data, String query, String version) throws Exception { | ||||
|     public static XPathQueryResult processXPath(String data, String query, String version) throws Exception { | ||||
|         Processor p = new Processor(false); | ||||
|         XPathCompiler compiler = p.newXPathCompiler(); | ||||
|         DocumentBuilder builder = p.newDocumentBuilder(); | ||||
| @@ -61,7 +62,7 @@ public class Saxon { | ||||
|             sb.append(xdmItem); | ||||
|             sb.append('\n'); | ||||
|         } | ||||
|         return sb.toString(); | ||||
|         return new XPathQueryResult(sb.toString(), "N/A"); | ||||
|  | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| package com.r11.tools.xml; | ||||
|  | ||||
| import com.r11.tools.controller.internal.XPathQueryResult; | ||||
| import org.apache.xpath.XPathAPI; | ||||
| import org.apache.xpath.objects.XObject; | ||||
| import org.w3c.dom.Document; | ||||
| @@ -64,7 +65,7 @@ public class Xalan { | ||||
|      * @return xml processed using given xpath | ||||
|      * @throws Exception thrown on node building errors or invalid xpath | ||||
|      */ | ||||
|     public static String processXPath(String data, String transform) throws Exception { | ||||
|     public static XPathQueryResult processXPath(String data, String transform) throws Exception { | ||||
|  | ||||
|         // Set up a DOM tree to query. | ||||
|         InputSource in = new InputSource(new StringReader(data)); | ||||
| @@ -81,7 +82,7 @@ public class Xalan { | ||||
|             NodeIterator nl = XPathAPI.selectNodeIterator(doc, transform); | ||||
|  | ||||
|             // Serialize the found nodes to result object. | ||||
|             StringBuilder result = new StringBuilder(); | ||||
|             StringBuilder resultString = new StringBuilder(); | ||||
|             Node n; | ||||
|             while ((n = nl.nextNode())!= null) { | ||||
|                 StringBuilder sb; | ||||
| @@ -90,18 +91,19 @@ public class Xalan { | ||||
|                     // single XPath text node.  Coalesce all contiguous text nodes | ||||
|                     // at this level | ||||
|                     for (Node nn = n.getNextSibling(); isTextNode(nn); nn = nn.getNextSibling()) { | ||||
|                         result.append(nn.getNodeValue()); | ||||
|                         resultString.append(nn.getNodeValue()); | ||||
|                     } | ||||
|                 } else { | ||||
|                     ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||||
|                     serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(outputStream))); | ||||
|                     result.append(outputStream); | ||||
|                     resultString.append(outputStream); | ||||
|                 } | ||||
|                 result.append("\n"); | ||||
|                 resultString.append("\n"); | ||||
|             } | ||||
|             return result.toString(); | ||||
|             return new XPathQueryResult(resultString.toString(), "node"); | ||||
|         } catch (TransformerException e) { | ||||
|             return XPathAPI.eval(doc, transform).toString(); | ||||
|             String returnData = XPathAPI.eval(doc, transform).toString(); | ||||
|             return new XPathQueryResult(data, "string"); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|   | ||||
| @@ -244,9 +244,12 @@ function performRequest(endpoint, checkXML, checkTransform) { | ||||
|     if (!empty) { | ||||
|         restRequest(port, endpoint, xmlData, transformData).then(function (result) { | ||||
|             document.getElementById("resultArea").value = result.result; | ||||
|             document.getElementById("procinfo").innerText = ' Computed using '.concat(" ", result.processor); | ||||
|             document.getElementById("procinfo").innerText = ' Computed using ' + result.processor | ||||
|             if (result.type) | ||||
|                 document.getElementById("procinfo").innerText += ". Returned: " + result.type; | ||||
|                  | ||||
|             if (result.status = "OK") { | ||||
|                 document.getElementById("procinfo").innerText = document.getElementById("procinfo").innerText.concat(" in ", result.time, "ms"); | ||||
|                 document.getElementById("procinfo").innerText += " in " + result.time + "ms"; | ||||
|                 procinfo.style.color = "#30aa58"; | ||||
|             } else { | ||||
|                 procinfo.style.color = "#aa3030"; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user