diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py index 14ce916..7dcb211 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 +def formatXML(source: str, prettify: bool) -> str: + """Method used to format XML - :param source: XML - :return: prettified XML + :param source: XML to format + :param prettify: sets if XML must be prettified + (added indentations 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: @@ -48,8 +40,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 +53,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 = "" @@ -78,9 +70,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: @@ -89,16 +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_transform = etree.XSLT(etree.XML(xslt)) + xslt_input = BytesIO(xslt.encode("utf-8")) + xslt_transform = etree.XSLT(etree.parse(xslt_input)) - 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 + 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 117b089..c0a962b 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 @@ -74,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.prettify(data) + response_json['result'] = Parser.formatXML(data, True) elif (type == "minimize"): - response_json['result'] = Parser.minimize(data) + response_json['result'] = Parser.formatXML(data, False) else: raise ValueError("Valid operation types are: xsd, xslt, xpath") 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 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" }