From b203bcc0b1bd1aca9337289e7778139653772f33 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Fri, 3 Mar 2023 09:00:37 +0100 Subject: [PATCH 01/10] Fixed for XPath --- Backend-libXML/Parser.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py index 14ce916..e019649 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -1,5 +1,5 @@ from lxml import etree - +from io import BytesIO def prettify(source: str) -> str: """Method used to pretty format given XML @@ -48,8 +48,8 @@ def xpath(source: str, xpath: str) -> str: :return: Nodes selected using XPath """ - - root = etree.XML(source) + byte_input = BytesIO(source.encode("utf-8")) + root = etree.parse(byte_input).getroot() nsmap = root.nsmap # LXML doesn't accept empty (None) namespace prefix, @@ -61,7 +61,7 @@ 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 result is not list: + if type(result) is not list: return str(result) else: result_string = "" -- 2.51.0 From 7554fc43befc61f439d8631a812fcbd27748ff71 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Fri, 3 Mar 2023 09:07:15 +0100 Subject: [PATCH 02/10] Fixed for XSD --- Backend-libXML/Parser.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py index e019649..046840a 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -78,9 +78,13 @@ def xsd(source: str, xsd: str) -> bool: :param xsd: XSD schema to validate XML against :return: Message saying, if the validation was successful or not """ - xml_schema = etree.XMLSchema(etree.XML(xsd)) - xml = etree.XML(source) + schema_input = BytesIO(xsd.encode("utf-8")) + xml_schema = etree.XMLSchema(etree.parse(schema_input).getroot()) + + document_input = BytesIO(source.encode("utf-8")) + xml = etree.parse(document_input).getroot() + if xml_schema.validate(xml): return "XML is valid." else: -- 2.51.0 From 8e5d1fe2feaae7ae33b2ee969141005601d75255 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Fri, 3 Mar 2023 09:12:03 +0100 Subject: [PATCH 03/10] Fixed for XSLT --- Backend-libXML/Parser.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py index 046840a..f991b70 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -99,10 +99,11 @@ def xslt(source: str, xslt: str) -> str: :param xslt: XSLT string used to transformate XML :return: Result of transformation """ - xslt_transform = etree.XSLT(etree.XML(xslt)) + xslt_input = BytesIO(xslt.encode("utf-8")) + xslt_transform = etree.XSLT(etree.parse(xslt_input).getroot()) - xml = etree.XML(source) + document_input = BytesIO(source.encode("utf-8")) + xml = etree.parse(document_input).getroot() transformated = xslt_transform(xml) - print(transformated) return str(transformated) \ No newline at end of file -- 2.51.0 From 8d8dccbbc4379338ebd2f8c1efb9c603d96cdb9d Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Fri, 3 Mar 2023 09:17:49 +0100 Subject: [PATCH 04/10] Updated sample files for curl --- Samples/xpath/data.json | 2 +- Samples/xpath/dataNS.json | 2 +- Samples/xpath/non-ns.curl | 4 ++-- Samples/xpath/ns.curl | 4 ++-- Samples/xsd/xsd.curl | 4 ++-- Samples/xsd/xsd.json | 4 ++-- Samples/xslt/xslt.curl | 3 ++- Samples/xslt/xslt.json | 4 ++-- 8 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Samples/xpath/data.json b/Samples/xpath/data.json index 2de1632..6216754 100644 --- a/Samples/xpath/data.json +++ b/Samples/xpath/data.json @@ -1,5 +1,5 @@ { - "data": "Hamlet2001-05-041falseMacbeth2000-12-131falseHarry Potter and the Sorcerer's Stone2005-04-292trueThe Long Walk2018-07-014trueMisery2018-01-314trueThink and Grow Rich2004-09-106trueThe Law of Success1982-05-096falsePatriot Games1995-10-215falseThe Sum of All Fears1992-09-195falseThe Alchemist2017-02-203falseHamlet1994-06-011falseMeasure for Measure1990-03-231falseHamlet1989-05-051trueHamlet1999-05-301trueThe Law of Success2004-11-266trueRomeo and Juliet1997-02-081trueThe Alchemist2009-08-213true", + "data": "\nHamlet2001-05-041falseMacbeth2000-12-131falseHarry Potter and the Sorcerer's Stone2005-04-292trueThe Long Walk2018-07-014trueMisery2018-01-314trueThink and Grow Rich2004-09-106trueThe Law of Success1982-05-096falsePatriot Games1995-10-215falseThe Sum of All Fears1992-09-195falseThe Alchemist2017-02-203falseHamlet1994-06-011falseMeasure for Measure1990-03-231falseHamlet1989-05-051trueHamlet1999-05-301trueThe Law of Success2004-11-266trueRomeo and Juliet1997-02-081trueThe Alchemist2009-08-213true", "process": "/books/book[name = 'The Law of Success']", "processor": "saxon", "version": "2.0" diff --git a/Samples/xpath/dataNS.json b/Samples/xpath/dataNS.json index ec5eb94..04cff85 100644 --- a/Samples/xpath/dataNS.json +++ b/Samples/xpath/dataNS.json @@ -1,5 +1,5 @@ { - "data": "Hamlet2001-05-041falseMacbeth2000-12-131falseHarry Potter and the Sorcerer's Stone2005-04-292trueThe Long Walk2018-07-014trueMisery2018-01-314trueThink and Grow Rich2004-09-106trueThe Law of Success1982-05-096falsePatriot Games1995-10-215falseThe Sum of All Fears1992-09-195falseThe Alchemist2017-02-203falseHamlet1994-06-011falseMeasure for Measure1990-03-231falseHamlet1989-05-051trueHamlet1999-05-301trueThe Law of Success2004-11-266trueRomeo and Juliet1997-02-081trueThe Alchemist2009-08-213true", + "data": "\nHamlet2001-05-041falseMacbeth2000-12-131falseHarry Potter and the Sorcerer's Stone2005-04-292trueThe Long Walk2018-07-014trueMisery2018-01-314trueThink and Grow Rich2004-09-106trueThe Law of Success1982-05-096falsePatriot Games1995-10-215falseThe Sum of All Fears1992-09-195falseThe Alchemist2017-02-203falseHamlet1994-06-011falseMeasure for Measure1990-03-231falseHamlet1989-05-051trueHamlet1999-05-301trueThe Law of Success2004-11-266trueRomeo and Juliet1997-02-081trueThe Alchemist2009-08-213true", "process": "/b:books/b:book[b:name = 'The Law of Success']", "processor": "saxon", "version": "2.0" diff --git a/Samples/xpath/non-ns.curl b/Samples/xpath/non-ns.curl index b059ae6..b54c5b6 100644 --- a/Samples/xpath/non-ns.curl +++ b/Samples/xpath/non-ns.curl @@ -1,4 +1,4 @@ -#url = "localhost:8081/xpathpost" -url = "localhost:5000/xpathpost" +#url = "localhost:8081/xpath" +url = "localhost:5000/xpath" request = "POST" data = "@data.json" diff --git a/Samples/xpath/ns.curl b/Samples/xpath/ns.curl index 90af030..46c6341 100644 --- a/Samples/xpath/ns.curl +++ b/Samples/xpath/ns.curl @@ -1,4 +1,4 @@ -#url = "localhost:8081/xpathpost" -url = "localhost:5000/xpathpost" +#url = "localhost:8081/xpath" +url = "localhost:5000/xpath" request = "POST" data = "@dataNS.json" diff --git a/Samples/xsd/xsd.curl b/Samples/xsd/xsd.curl index ae731a5..922856c 100644 --- a/Samples/xsd/xsd.curl +++ b/Samples/xsd/xsd.curl @@ -1,4 +1,4 @@ -#url = "http://localhost:8082/xsdpost" -url = "http://localhost:5000/xsdpost" +#url = "http://localhost:8081/xsd" +url = "http://localhost:5000/xsd" data = "@xsd.json" request = POST diff --git a/Samples/xsd/xsd.json b/Samples/xsd/xsd.json index 91dd2f2..cc367d0 100644 --- a/Samples/xsd/xsd.json +++ b/Samples/xsd/xsd.json @@ -1,6 +1,6 @@ { - "data": "TestTest3", - "process": " ", + "data": "\nTestTest3", + "process": "\n ", "processor": "saxon", "version": "1.0" } diff --git a/Samples/xslt/xslt.curl b/Samples/xslt/xslt.curl index a7a6f94..712c82c 100644 --- a/Samples/xslt/xslt.curl +++ b/Samples/xslt/xslt.curl @@ -1,3 +1,4 @@ -url = "http://localhost:5000/xsltpost" +#url = "http://localhost:8081/xslt" +url = "http://localhost:5000/xslt" data = "@xslt.json" request = POST diff --git a/Samples/xslt/xslt.json b/Samples/xslt/xslt.json index f833a40..47013d7 100644 --- a/Samples/xslt/xslt.json +++ b/Samples/xslt/xslt.json @@ -1,6 +1,6 @@ { - "data": "Hamlet2001-05-041falseMacbeth2000-12-131falseHarry Potter and the Sorcerer's Stone2005-04-292trueThe Long Walk2018-07-014trueMisery2018-01-314trueThink and Grow Rich2004-09-106trueThe Law of Success1982-05-096falsePatriot Games1995-10-215falseThe Sum of All Fears1992-09-195falseThe Alchemist2017-02-203falseHamlet1994-06-011falseMeasure for Measure1990-03-231falseHamlet1989-05-051trueHamlet1999-05-301trueThe Law of Success2004-11-266trueRomeo and Juliet1997-02-081trueThe Alchemist2009-08-213true", - "process": "", + "data": "City library3451237321AdamChoke5123LaurenWong6422Harry Potter75421234Macbeth51239556Romeo and Juliet", + "process": "", "processor": "saxon", "version": "1.0" } -- 2.51.0 From 456be34a40a96d3e5edc7b04c908326fa2c1b1a2 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Fri, 3 Mar 2023 09:50:54 +0100 Subject: [PATCH 05/10] Refactored formatter --- Backend-libXML/Parser.py | 40 ++++++++++++++++------------------------ Backend-libXML/main.py | 4 ++-- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py index f991b70..4cd5ac7 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -1,31 +1,18 @@ from lxml import etree from io import BytesIO -def prettify(source: str) -> str: - """Method used to pretty format given XML - :param source: XML - :return: prettified XML +def format(source: str, prettify: bool) -> str: + """Method used to format XML + + :param source: XML to format + :param prettify: sets if XML must be prettified + (have added intendations etc.) or not + :return: formatted XML """ - prolog = "" - prolog_start = source.find("") + 2 - prolog = source[prolog_start:prolog_end] + "\n" - source = source[prolog_end: ] - - parser = etree.XMLParser(remove_blank_text=True) - xml = etree.fromstring(source, parser=parser) - return prolog + etree.tostring(xml, pretty_print=True).decode() -def minimize(source: str) -> str: - """Method used to minimize XML by deleting not needed whitespaces. - - :param source: XML - :return: minimized XML - """ - + # Prolog is removed when XML is parsed + # so program has to copy it prolog = "" prolog_start = source.find(" str: prolog = source[prolog_start:prolog_end] source = source[prolog_end: ] + byte_input = BytesIO(source.encode("utf-8")) parser = etree.XMLParser(remove_blank_text=True) - xml = etree.fromstring(source, parser=parser) - return prolog + etree.tostring(xml, pretty_print=False).decode() + xml = etree.parse(byte_input, parser=parser) + + if prettify: + prolog += "\n" + + return prolog + etree.tostring(xml, pretty_print=prettify).decode() def xpath(source: str, xpath: str) -> str: diff --git a/Backend-libXML/main.py b/Backend-libXML/main.py index 117b089..d618dd2 100644 --- a/Backend-libXML/main.py +++ b/Backend-libXML/main.py @@ -74,9 +74,9 @@ def process_xml(request: request, type: str) -> str: elif (type == "xpath"): response_json['result'] = Parser.xpath(data, process) elif (type == "prettify"): - response_json['result'] = Parser.prettify(data) + response_json['result'] = Parser.format(data, True) elif (type == "minimize"): - response_json['result'] = Parser.minimize(data) + response_json['result'] = Parser.format(data, False) else: raise ValueError("Valid operation types are: xsd, xslt, xpath") -- 2.51.0 From 3fce2e1c62425db334bd6cde89952cd457dc13c0 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Fri, 3 Mar 2023 09:51:40 +0100 Subject: [PATCH 06/10] Removed unused code --- Backend-libXML/main.py | 37 ------------------------------------- 1 file changed, 37 deletions(-) diff --git a/Backend-libXML/main.py b/Backend-libXML/main.py index d618dd2..46fe1fc 100644 --- a/Backend-libXML/main.py +++ b/Backend-libXML/main.py @@ -15,43 +15,6 @@ cors = CORS(app, resource={ } }) -def format_xml(request: request, type: str) -> str: - """Function to format XML - - :param request: Received request - :param type: Type of needed processing: xsd, xslt or xpath - :raises ValueError: is raised when type is different than those provided above - :return: response JSON converted to string and response code - """ - 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 == "prettify"): - response_json['result'] = Parser.xsd(data, process) - elif (type == "minimize"): - response_json['result'] = Parser.xslt(data, process) - else: - raise ValueError("Valid operation types are: prettify, minimize") - - 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 - def process_xml(request: request, type: str) -> str: """Function to process -- 2.51.0 From dd4cc34e6cb57139c6a202b30b46a6535798429e Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Fri, 3 Mar 2023 09:53:33 +0100 Subject: [PATCH 07/10] Fixed typo --- Backend-libXML/Parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py index 4cd5ac7..73c303a 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -7,7 +7,7 @@ def format(source: str, prettify: bool) -> str: :param source: XML to format :param prettify: sets if XML must be prettified - (have added intendations etc.) or not + (added indentations etc.) or not :return: formatted XML """ -- 2.51.0 From 85beef19d8f4ef0e280aa1456997a9a7d7bc811f Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Fri, 3 Mar 2023 10:00:06 +0100 Subject: [PATCH 08/10] Fixed XSLT output being not formatted --- Backend-libXML/Parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py index 73c303a..1a93562 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -97,5 +97,5 @@ def xslt(source: str, xslt: str) -> str: document_input = BytesIO(source.encode("utf-8")) xml = etree.parse(document_input).getroot() - transformated = xslt_transform(xml) - return str(transformated) \ No newline at end of file + result = xml.xslt(xslt_transform).decode() + return result \ No newline at end of file -- 2.51.0 From 81737ed759463a261a68cf2c12b7a67a9e98328b Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Fri, 3 Mar 2023 10:05:42 +0100 Subject: [PATCH 09/10] Updated samples for formatting --- Samples/minimize/minimize.curl | 2 +- Samples/minimize/pretty | 104 --------------------------------- Samples/prettify/prettify.curl | 2 +- 3 files changed, 2 insertions(+), 106 deletions(-) delete mode 100644 Samples/minimize/pretty diff --git a/Samples/minimize/minimize.curl b/Samples/minimize/minimize.curl index f75d62a..3a74ae5 100644 --- a/Samples/minimize/minimize.curl +++ b/Samples/minimize/minimize.curl @@ -1,3 +1,3 @@ -url = "http://localhost:5000/minimizepost" +url = "http://localhost:5000/minimize" data = "@minimize.json" request = POST diff --git a/Samples/minimize/pretty b/Samples/minimize/pretty deleted file mode 100644 index 7690e96..0000000 --- a/Samples/minimize/pretty +++ /dev/null @@ -1,104 +0,0 @@ - - - Hamlet - 2001-05-04 - 1 - false - - - Macbeth - 2000-12-13 - 1 - false - - - Harry Potter and the Sorcerer's Stone - 2005-04-29 - 2 - true - - - The Long Walk - 2018-07-01 - 4 - true - - - Misery - 2018-01-31 - 4 - true - - - Think and Grow Rich - 2004-09-10 - 6 - true - - - The Law of Success - 1982-05-09 - 6 - false - - - Patriot Games - 1995-10-21 - 5 - false - - - The Sum of All Fears - 1992-09-19 - 5 - false - - - The Alchemist - 2017-02-20 - 3 - false - - - Hamlet - 1994-06-01 - 1 - false - - - Measure for Measure - 1990-03-23 - 1 - false - - - Hamlet - 1989-05-05 - 1 - true - - - Hamlet - 1999-05-30 - 1 - true - - - The Law of Success - 2004-11-26 - 6 - true - - - Romeo and Juliet - 1997-02-08 - 1 - true - - - The Alchemist - 2009-08-21 - 3 - true - - diff --git a/Samples/prettify/prettify.curl b/Samples/prettify/prettify.curl index 643474d..a10aa88 100644 --- a/Samples/prettify/prettify.curl +++ b/Samples/prettify/prettify.curl @@ -1,3 +1,3 @@ -url = "http://localhost:5000/prettifypost" +url = "http://localhost:5000/prettify" data = "@prettify.json" request = POST -- 2.51.0 From a46d7349a8d2bd60f7c694ba8de3e4e1b2b7c270 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Fri, 3 Mar 2023 10:30:15 +0100 Subject: [PATCH 10/10] XSLT now return prettified output --- Backend-libXML/Parser.py | 12 ++++++------ Backend-libXML/main.py | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py index 1a93562..7dcb211 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -2,7 +2,7 @@ from lxml import etree from io import BytesIO -def format(source: str, prettify: bool) -> str: +def formatXML(source: str, prettify: bool) -> str: """Method used to format XML :param source: XML to format @@ -85,17 +85,17 @@ def xsd(source: str, xsd: str) -> bool: def xslt(source: str, xslt: str) -> str: """ - Method used to transformate XML string using XSLT + Method used to transform XML string using XSLT :param source: XML string to transform - :param xslt: XSLT string used to transformate XML + :param xslt: XSLT string used to transform XML :return: Result of transformation """ xslt_input = BytesIO(xslt.encode("utf-8")) - xslt_transform = etree.XSLT(etree.parse(xslt_input).getroot()) + xslt_transform = etree.XSLT(etree.parse(xslt_input)) document_input = BytesIO(source.encode("utf-8")) xml = etree.parse(document_input).getroot() - result = xml.xslt(xslt_transform).decode() - return result \ No newline at end of file + transformed = str(xslt_transform(xml)) + return formatXML(transformed, True) \ No newline at end of file diff --git a/Backend-libXML/main.py b/Backend-libXML/main.py index 46fe1fc..c0a962b 100644 --- a/Backend-libXML/main.py +++ b/Backend-libXML/main.py @@ -37,9 +37,9 @@ def process_xml(request: request, type: str) -> str: elif (type == "xpath"): response_json['result'] = Parser.xpath(data, process) elif (type == "prettify"): - response_json['result'] = Parser.format(data, True) + response_json['result'] = Parser.formatXML(data, True) elif (type == "minimize"): - response_json['result'] = Parser.format(data, False) + response_json['result'] = Parser.formatXML(data, False) else: raise ValueError("Valid operation types are: xsd, xslt, xpath") -- 2.51.0