Merge pull request 'bema/func/libxml_backend' (#20) from bema/func/libxml_backend into dev

Reviewed-on: R11/release11-tools-web#20
This commit is contained in:
2023-02-09 13:34:42 +01:00
18 changed files with 202 additions and 2 deletions

3
Backend-libXML/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.idea
__pycache**
venv

View File

@@ -0,0 +1,8 @@
FROM tiangolo/meinheld-gunicorn-flask:python3.9
COPY ./requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
COPY ./main.py /app/
COPY ./Parser.py /app/

67
Backend-libXML/Parser.py Normal file
View File

@@ -0,0 +1,67 @@
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
:type source: str
:param xpath: XPath query used for selection
:type xpath: str
:return: Nodes selected using XPath
:rtype: str
"""
root = etree.XML(source)
nsmap = root.nsmap
# LXML doesn't accept empty (None) namespace prefix,
# so it need to be deleted if exists
if None in nsmap:
nsmap.pop(None)
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
:type source: str
:param xsd: XSD schema to validate XML against
:type xsd: str
:return: If the validation was successful or not
:rtype: bool
"""
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: XML string to transform
:type source: str
:param xslt: XSLT string used to transformate XML
:type xslt: str
:return: Result of transformation
:rtype: str
"""
xslt_transform = etree.XSLT(etree.XML(xslt))
xml = etree.XML(source)
transformated = xslt_transform(xml)
print(transformated)
return str(transformated)

68
Backend-libXML/main.py Normal file
View File

@@ -0,0 +1,68 @@
from flask import Flask
from flask import request
from lxml import etree
import json
import time
import Parser
app = Flask(__name__)
def process_xml(request: request, type: str) -> str:
"""Function to process
:param request: Received request
:type request: request
:param type: Type of needed processing: xsd, xslt or xpath
:type type: str
:raises ValueError: is raised when type is different than those provided above
:return: response JSON converted to string and response code
:rtype: str, int
"""
start = time.time_ns()
code = 200
response_json = dict()
try:
request_data = json.loads(request.get_data(as_text=True))
data = request_data['data']
process = request_data['process']
if (type == "xsd"):
response_json['result'] = Parser.xsd(data, process)
elif (type == "xslt"):
response_json['result'] = Parser.xslt(data, process)
elif (type == "xpath"):
response_json['result'] = Parser.xpath(data, process)
else:
raise ValueError("Valid operation types are: xsd, xslt, xpath")
response_json['status'] = "OK"
except KeyError as e:
response_json['result'] = "Missing key: " + str(e)
response_json['status'] = "ERR"
code = 400
except Exception as e:
response_json['result'] = str(e)
response_json['status'] = "ERR"
code = 400
finally:
exec_time = (time.time_ns() - start) / 10**6
response_json['time'] = f"{exec_time:.03f}"
response_json['processor'] = "libxml2 over lxml"
return json.dumps(response_json), code
@app.route("/xpathpost", methods=["POST"])
def xpath():
return process_xml(request, "xpath")
@app.route("/xsdpost", methods=["POST"])
def xsd():
return process_xml(request, "xsd")
@app.route("/xsltpost", methods=["POST"])
def xslt():
return process_xml(request, "xslt")
if __name__ == "__main__":
app.run()

View File

@@ -0,0 +1,2 @@
lxml
flask

View File

@@ -0,0 +1,6 @@
{
"data": "<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"
}

View 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": "/b:books/b:book[b:name = 'The Law of Success']",
"processor": "saxon",
"version": "2.0"
}

View File

@@ -0,0 +1,4 @@
#url = "localhost:8081/xpathpost"
url = "localhost:5000/xpath"
request = "POST"
data = "@data.json"

View File

@@ -0,0 +1,4 @@
#url = "localhost:8081/xpathpost"
url = "localhost:5000/xpath"
request = "POST"
data = "@dataNS.json"

View File

@@ -0,0 +1,4 @@
#url = "http://localhost:8082/xsd"
url = "http://localhost:5000/xsd"
data = "@xsd.json"
request = POST

View 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"
}

View File

@@ -0,0 +1,3 @@
url = "http://localhost:5000/xslt"
data = "@xslt.json"
request = POST

View 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"
}

View File

@@ -24,7 +24,7 @@
<label for="processors">Select XPath processor:</label>
<select name="processors" id="processors">
<option value="saxon">Saxon</option>
<!-- <option value="xalan">Xalan</option> -->
<option value="libxml">libXML</option>
</select>
<label for="versions">XPath version:</label>
<select name="versions" id="versions">

View File

@@ -18,8 +18,8 @@
</div>
<label for="processors">Select XSLT processor:</label>
<select name="processors" id="processors">
<!-- <option value="saxon">Saxon</option> -->
<option value="xalan">Xalan</option>
<option value="libxml">libXML</option>
</select>
<!-- <span id="processorTooltipInfo">procInfo</span><br> -->
<br>

View File

@@ -19,6 +19,7 @@
<select name="processors" id="processors">
<option value="saxon">Saxon</option>
<option value="xalan">Xalan</option>
<option value="libxml">libXML</option>
</select>
<span id="processorTooltipInfo">procInfo</span><br>
<br>

View File

@@ -19,6 +19,13 @@ services:
ports:
- 8081:8081
xmltools-libxml-backend:
build: ./Backend-libXML
container_name: xmltools-libxml-backend
image: xmltools-libxml-backend
ports:
- 8082:80
xmltools-mocked-services:
build:
context: ./Backend/mocked-services

View File

@@ -17,6 +17,11 @@ openapi.yml
Rest API accepts xml documents, given querry and processor version to call requested xml engine to perform given
operation and finally returns outcome in response body.
## Flask Python backend
This is module providing support for processing XMLs using libxml library. It consumes same JSON as Java backend.
## Mocked services
MockedServices is a tool that allows developer to create, in easy and simple way, http server mocked endpoints for integration tests