From 4955a7cc4f0644f78ed0ebe5fd57dbb1ee13ab54 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 11:02:16 +0100 Subject: [PATCH] Implemented new tool in backend --- Backend-libXML/Parser.py | 18 +++++++++--------- Backend-libXML/main.py | 8 +++++--- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py index c652212..2722386 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -1,16 +1,22 @@ from lxml import etree +def prettify(source: str) -> str: + xml = etree.XML(source) + return etree.tostring(xml, pretty_print=True) + +def minimize(source: str) -> str: + xml = etree.XML(source) + return etree.tostring(xml, pretty_print=False) + + 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 """ @@ -34,11 +40,8 @@ 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: Message saying, if the validation was successful or not - :rtype: str """ xml_schema = etree.XMLSchema(etree.XML(xsd)) @@ -54,11 +57,8 @@ 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)) diff --git a/Backend-libXML/main.py b/Backend-libXML/main.py index 0869a00..0996a1d 100644 --- a/Backend-libXML/main.py +++ b/Backend-libXML/main.py @@ -19,12 +19,9 @@ 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 @@ -70,5 +67,10 @@ def xsd(): def xslt(): return process_xml(request, "xslt") +@app.route("/prettifypost", methods=["POST"]) +def prettify(): + request_data = json.loads(request.get_data(as_text=True)) + return Parser.prettify(request_data['data']) + if __name__ == "__main__": app.run() \ No newline at end of file