Added early version of libxml backend
This commit is contained in:
		
							
								
								
									
										56
									
								
								Backend-libXML/Parser.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								Backend-libXML/Parser.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | |||||||
|  | from lxml import etree | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def xpath(source: str, xpath: str) -> str: | ||||||
|  |     """ | ||||||
|  |     Method used to get nodes from XML string using XPath | ||||||
|  |     :param source: XML string used for selection | ||||||
|  |     :param xpath: XPath query used for selection | ||||||
|  |     :return: Nodes selected using XPath | ||||||
|  |     """ | ||||||
|  |     root = etree.XML(source) | ||||||
|  |     nsmap = root.nsmap | ||||||
|  |  | ||||||
|  |     # LXML doesn't accept namespace with None key, | ||||||
|  |     # so it need to be deleted if exists | ||||||
|  |     try: | ||||||
|  |         nsmap.pop(None) | ||||||
|  |     except KeyError: | ||||||
|  |         print(end="") | ||||||
|  |  | ||||||
|  |     result = root.xpath(xpath, namespaces=nsmap) | ||||||
|  |     result_string = "" | ||||||
|  |     for e in result: | ||||||
|  |         result_string += etree.tostring(e, pretty_print=True).decode() + "\n" | ||||||
|  |     return result_string | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def xsd(source: str, xsd: str) -> bool: | ||||||
|  |     """ | ||||||
|  |     Method used to validate XML string against XSD schema | ||||||
|  |     :param source: XML string used for validation | ||||||
|  |     :param xsd: XSD schema to validate XML against | ||||||
|  |     :return: If the validation was successful or not | ||||||
|  |     """ | ||||||
|  |     xml_schema = etree.XMLSchema(etree.XML(xsd)) | ||||||
|  |  | ||||||
|  |     xml = etree.XML(source) | ||||||
|  |  | ||||||
|  |     return xml_schema.validate(xml) | ||||||
|  |      | ||||||
|  |      | ||||||
|  | def xslt(source: str, xslt: str) -> str: | ||||||
|  |     """ | ||||||
|  |     Method used to transformate XML string using XSLT | ||||||
|  |     :param source: Transformed XML string | ||||||
|  |     :param xslt: XSLT string used to transformate XML | ||||||
|  |     :return: Result of transformation | ||||||
|  |     """ | ||||||
|  |     xslt_transform = etree.XSLT(etree.XML(xslt)) | ||||||
|  |  | ||||||
|  |     xml = etree.XML(source) | ||||||
|  |  | ||||||
|  |     transformated = xslt_transform(xml) | ||||||
|  |     print(transformated) | ||||||
|  |     return str(transformated) | ||||||
							
								
								
									
										
											BIN
										
									
								
								Backend-libXML/__pycache__/Parser.cpython-310.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Backend-libXML/__pycache__/Parser.cpython-310.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								Backend-libXML/__pycache__/server.cpython-310.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Backend-libXML/__pycache__/server.cpython-310.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										17
									
								
								Backend-libXML/main.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								Backend-libXML/main.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | |||||||
|  | import Parser | ||||||
|  |  | ||||||
|  |  | ||||||
|  | if __name__ == '__main__': | ||||||
|  |     SOURCE = "sample/xslt.xml" | ||||||
|  |     XSLT = "sample/sample.xslt" | ||||||
|  |  | ||||||
|  |     file = open(SOURCE, "r") | ||||||
|  |     xml = file.read() | ||||||
|  |     file.close() | ||||||
|  |  | ||||||
|  |     file = open(XSLT, "r") | ||||||
|  |     xslt = file.read() | ||||||
|  |     print(Parser.xslt(xml, xslt)) | ||||||
|  |  | ||||||
|  |     file.close() | ||||||
|  |  | ||||||
							
								
								
									
										5
									
								
								Backend-libXML/sample/xpath.curl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								Backend-libXML/sample/xpath.curl
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | #url = "localhost:8081/xpathpost" | ||||||
|  | url = "localhost:5000/xpath" | ||||||
|  | request = "POST" | ||||||
|  | data = "@xpath.json" | ||||||
|  | header = "Content-Type: application/json" | ||||||
							
								
								
									
										6
									
								
								Backend-libXML/sample/xpath.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								Backend-libXML/sample/xpath.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | { | ||||||
|  |   "data": "<?xml version='1.0' encoding='utf-8'?><books><book id='1'><name>Hamlet</name><date>2001-05-04</date><authorId>1</authorId><availability>false</availability></book><book id='2'><name>Macbeth</name><date>2000-12-13</date><authorId>1</authorId><availability>false</availability></book><book id='3'><name>Harry Potter and the Sorcerer's Stone</name><date>2005-04-29</date><authorId>2</authorId><availability>true</availability></book><book id='4'><name>The Long Walk</name><date>2018-07-01</date><authorId>4</authorId><availability>true</availability></book><book id='5'><name>Misery</name><date>2018-01-31</date><authorId>4</authorId><availability>true</availability></book><book id='6'><name>Think and Grow Rich</name><date>2004-09-10</date><authorId>6</authorId><availability>true</availability></book><book id='7'><name>The Law of Success</name><date>1982-05-09</date><authorId>6</authorId><availability>false</availability></book><book id='8'><name>Patriot Games</name><date>1995-10-21</date><authorId>5</authorId><availability>false</availability></book><book id='9'><name>The Sum of All Fears</name><date>1992-09-19</date><authorId>5</authorId><availability>false</availability></book><book id='10'><name>The Alchemist</name><date>2017-02-20</date><authorId>3</authorId><availability>false</availability></book><book id='11'><name>Hamlet</name><date>1994-06-01</date><authorId>1</authorId><availability>false</availability></book><book id='12'><name>Measure for Measure</name><date>1990-03-23</date><authorId>1</authorId><availability>false</availability></book><book id='13'><name>Hamlet</name><date>1989-05-05</date><authorId>1</authorId><availability>true</availability></book><book id='14'><name>Hamlet</name><date>1999-05-30</date><authorId>1</authorId><availability>true</availability></book><book id='15'><name>The Law of Success</name><date>2004-11-26</date><authorId>6</authorId><availability>true</availability></book><book id='16'><name>Romeo and Juliet</name><date>1997-02-08</date><authorId>1</authorId><availability>true</availability></book><book id='17'><name>The Alchemist</name><date>2009-08-21</date><authorId>3</authorId><availability>true</availability></book></books>", | ||||||
|  |   "process": "/books/book[name = 'The Law of Success']", | ||||||
|  |   "processor": "saxon", | ||||||
|  |   "version": "2.0" | ||||||
|  | } | ||||||
							
								
								
									
										4
									
								
								Backend-libXML/sample/xsd.curl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								Backend-libXML/sample/xsd.curl
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | |||||||
|  | url = "http://localhost:5000/xsd" | ||||||
|  | data = "@xsd.json" | ||||||
|  | header = "Content-Type: application/json" | ||||||
|  | request = POST | ||||||
							
								
								
									
										6
									
								
								Backend-libXML/sample/xsd.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								Backend-libXML/sample/xsd.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | { | ||||||
|  |     "data": "<ns0:values xmlns:ns0 = \"http://www.tibco.com/schemas/test/Test/Resources/Schema.xsd\"><ns0:value>Test</ns0:value><ns0:value>Test3</ns0:value></ns0:values>", | ||||||
|  |     "process": "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://www.tibco.com/schemas/test/Test/Resources/Schema.xsd\" targetNamespace=\"http://www.tibco.com/schemas/test/Test/Resources/Schema.xsd\" elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\"> <xs:element name=\"values\"><xs:complexType><xs:sequence><xs:element name=\"value\" type=\"xs:string\" minOccurs=\"0\" maxOccurs=\"unbounded\"/></xs:sequence></xs:complexType></xs:element></xs:schema>", | ||||||
|  |     "processor": "saxon", | ||||||
|  |     "version": "1.0" | ||||||
|  | } | ||||||
							
								
								
									
										4
									
								
								Backend-libXML/sample/xslt.curl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								Backend-libXML/sample/xslt.curl
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | |||||||
|  | url = "http://localhost:5000/xslt" | ||||||
|  | data = "@xslt.json" | ||||||
|  | header = "Content-Type: application/json" | ||||||
|  | request = POST | ||||||
							
								
								
									
										6
									
								
								Backend-libXML/sample/xslt.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								Backend-libXML/sample/xslt.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | { | ||||||
|  |     "data": "<b:books xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.demo.com' xmlns:b='http://www.book.com' xmlns:a='http://www.author.com'><b:book id='1'><b:name>Hamlet</b:name><b:date>2001-05-04</b:date><a:authorId>1</a:authorId><b:availability>false</b:availability></b:book><b:book id='2'><b:name>Macbeth</b:name><b:date>2000-12-13</b:date><a:authorId>1</a:authorId><b:availability>false</b:availability></b:book><b:book id='3'><b:name>Harry Potter and the Sorcerer's Stone</b:name><b:date>2005-04-29</b:date><a:authorId>2</a:authorId><b:availability>true</b:availability></b:book><b:book id='4'><b:name>The Long Walk</b:name><b:date>2018-07-01</b:date><a:authorId>4</a:authorId><b:availability>true</b:availability></b:book><b:book id='5'><b:name>Misery</b:name><b:date>2018-01-31</b:date><a:authorId>4</a:authorId><b:availability>true</b:availability></b:book><b:book id='6'><b:name>Think and Grow Rich</b:name><b:date>2004-09-10</b:date><a:authorId>6</a:authorId><b:availability>true</b:availability></b:book><b:book id='7'><b:name>The Law of Success</b:name><b:date>1982-05-09</b:date><a:authorId>6</a:authorId><b:availability>false</b:availability></b:book><b:book id='8'><b:name>Patriot Games</b:name><b:date>1995-10-21</b:date><a:authorId>5</a:authorId><b:availability>false</b:availability></b:book><b:book id='9'><b:name>The Sum of All Fears</b:name><b:date>1992-09-19</b:date><a:authorId>5</a:authorId><b:availability>false</b:availability></b:book><b:book id='10'><b:name>The Alchemist</b:name><b:date>2017-02-20</b:date><a:authorId>3</a:authorId><b:availability>false</b:availability></b:book><b:book id='11'><b:name>Hamlet</b:name><b:date>1994-06-01</b:date><a:authorId>1</a:authorId><b:availability>false</b:availability></b:book><b:book id='12'><b:name>Measure for Measure</b:name><b:date>1990-03-23</b:date><a:authorId>1</a:authorId><b:availability>false</b:availability></b:book><b:book id='13'><b:name>Hamlet</b:name><b:date>1989-05-05</b:date><a:authorId>1</a:authorId><b:availability>true</b:availability></b:book><b:book id='14'><b:name>Hamlet</b:name><b:date>1999-05-30</b:date><a:authorId>1</a:authorId><b:availability>true</b:availability></b:book><b:book id='15'><b:name>The Law of Success</b:name><b:date>2004-11-26</b:date><a:authorId>6</a:authorId><b:availability>true</b:availability></b:book><b:book id='16'><b:name>Romeo and Juliet</b:name><b:date>1997-02-08</b:date><a:authorId>1</a:authorId><b:availability>true</b:availability></b:book><b:book id='17'><b:name>The Alchemist</b:name><b:date>2009-08-21</b:date><a:authorId>3</a:authorId><b:availability>true</b:availability></b:book></b:books>", | ||||||
|  |     "process": "<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:b='http://www.demo.com'><xsl:template match='b:books'><Library><BookCount><xsl:value-of select='count(b:book)' /></BookCount></Library></xsl:template></xsl:stylesheet>", | ||||||
|  |     "processor": "saxon", | ||||||
|  |     "version": "1.0" | ||||||
|  | } | ||||||
							
								
								
									
										69
									
								
								Backend-libXML/server.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								Backend-libXML/server.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,69 @@ | |||||||
|  | from flask import Flask | ||||||
|  | from flask import request | ||||||
|  | from lxml import etree | ||||||
|  | import json | ||||||
|  | import time | ||||||
|  | import Parser | ||||||
|  |  | ||||||
|  |  | ||||||
|  | app = Flask(__name__) | ||||||
|  |  | ||||||
|  | @app.route("/xpath", methods=["POST"]) | ||||||
|  | def xpath(): | ||||||
|  |     request_data = request.get_json() | ||||||
|  |     xml = request_data['data'] | ||||||
|  |     xpath = request_data['process'] | ||||||
|  |      | ||||||
|  |     response = dict() | ||||||
|  |     start = time.time_ns() | ||||||
|  |     try: | ||||||
|  |         response['result'] = Parser.xpath(xml, xpath) | ||||||
|  |         response['status'] = "OK" | ||||||
|  |     except BaseException as e: | ||||||
|  |         response['result'] = str(e) | ||||||
|  |         response['status'] = "ERR" | ||||||
|  |     finally: | ||||||
|  |         exec_time = (time.time_ns() - start) / 10**6 | ||||||
|  |         response['time'] = f"{exec_time:.03f}" | ||||||
|  |         response['processor'] = "libxml2 over lxml" | ||||||
|  |         return json.dumps(response) | ||||||
|  |  | ||||||
|  | @app.route("/xsd", methods=["POST"]) | ||||||
|  | def xsd(): | ||||||
|  |     request_data = request.get_json() | ||||||
|  |     xml = request_data['data'] | ||||||
|  |     xsd = request_data['process'] | ||||||
|  |  | ||||||
|  |     response = dict() | ||||||
|  |     start = time.time_ns() | ||||||
|  |     try: | ||||||
|  |         response['result'] = Parser.xsd(xml, xsd) | ||||||
|  |         response['status'] = "OK" | ||||||
|  |     except BaseException as e: | ||||||
|  |         response['result'] = str(e) | ||||||
|  |         response['status'] = "ERR" | ||||||
|  |     finally: | ||||||
|  |         exec_time = (time.time_ns() - start) / 10**6 | ||||||
|  |         response['time'] = f"{exec_time:.03f}" | ||||||
|  |         response['processor'] = "libxml2 over lxml" | ||||||
|  |         return json.dumps(response) | ||||||
|  |  | ||||||
|  | @app.route("/xslt", methods=["POST"]) | ||||||
|  | def xslt(): | ||||||
|  |     request_data = request.get_json() | ||||||
|  |     xml = request_data['data'] | ||||||
|  |     xslt = request_data['process'] | ||||||
|  |  | ||||||
|  |     response = dict() | ||||||
|  |     start = time.time_ns() | ||||||
|  |     try: | ||||||
|  |         response['result'] = Parser.xslt(xml, xslt) | ||||||
|  |         response['status'] = "OK" | ||||||
|  |     except BaseException as e: | ||||||
|  |         response['result'] = str(e) | ||||||
|  |         response['status'] = "ERR" | ||||||
|  |     finally: | ||||||
|  |         exec_time = (time.time_ns() - start) / 10**6 | ||||||
|  |         response['time'] = f"{exec_time:.03f}" | ||||||
|  |         response['processor'] = "libxml2 over lxml" | ||||||
|  |         return json.dumps(response) | ||||||
		Reference in New Issue
	
	Block a user