From 8c132d12d34312b83fa6226ea1416de250ab7752 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 09:58:49 +0100 Subject: [PATCH 01/13] Added interface for formater rool --- Frontend/assets/css/tools/r11form.css | 4 + Frontend/tools/formatter.html | 157 ++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 Frontend/tools/formatter.html diff --git a/Frontend/assets/css/tools/r11form.css b/Frontend/assets/css/tools/r11form.css index f4af46e..47f635a 100644 --- a/Frontend/assets/css/tools/r11form.css +++ b/Frontend/assets/css/tools/r11form.css @@ -287,6 +287,10 @@ width: 100%; } +.half-width { + width: 50%; +} + .max-width.with-padding { width: 94%; } diff --git a/Frontend/tools/formatter.html b/Frontend/tools/formatter.html new file mode 100644 index 0000000..f7bbd1e --- /dev/null +++ b/Frontend/tools/formatter.html @@ -0,0 +1,157 @@ + + + + + + + + + + + + +
+
+
+
+

Online XML Formatter v0.21.37 BETA +

+
+ + + +

+ + +

+ + + + +
+
+
+

What is this?

+

This tool has 2 main functions: +

    +
  • Prettify XML to make it human-readable (add indentation etc.)
  • +
  • Minimize XML to make it more compact (exactly opposite to above)
  • +
+

+
+ + + +
+ + + + + + + + + + + + + + -- 2.51.0 From e2cf490f9da047c67a04be8cefbeda69f6f9fc38 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 09:59:48 +0100 Subject: [PATCH 02/13] Removed empty class --- Frontend/assets/css/tools/r11form.css | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Frontend/assets/css/tools/r11form.css b/Frontend/assets/css/tools/r11form.css index 47f635a..c917aac 100644 --- a/Frontend/assets/css/tools/r11form.css +++ b/Frontend/assets/css/tools/r11form.css @@ -441,10 +441,6 @@ content: "▼"; } -.content.active{ - -} - .hiddable { display: none; } -- 2.51.0 From 78cc13a6616c4db3d77381b3eb5631afbf3768b2 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 10:00:28 +0100 Subject: [PATCH 03/13] Added new tool to frame --- Frontend/index.html | 1 + 1 file changed, 1 insertion(+) diff --git a/Frontend/index.html b/Frontend/index.html index 162acd4..d9d0cd2 100644 --- a/Frontend/index.html +++ b/Frontend/index.html @@ -25,6 +25,7 @@
  • XPath
  • XSLT
  • XSD
  • +
  • Formatter
  • -- 2.51.0 From 4955a7cc4f0644f78ed0ebe5fd57dbb1ee13ab54 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 11:02:16 +0100 Subject: [PATCH 04/13] 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 -- 2.51.0 From 153a7313ef58db1f14417ef97fc42656df7927f8 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 11:02:30 +0100 Subject: [PATCH 05/13] Added new sample files --- Backend-libXML/sample/minimize/minimize.curl | 3 + Backend-libXML/sample/minimize/minimize.json | 5 + Backend-libXML/sample/minimize/pretty | 104 +++++++++++++++++++ Backend-libXML/sample/prettify/prettify.curl | 3 + Backend-libXML/sample/prettify/prettify.json | 5 + 5 files changed, 120 insertions(+) create mode 100644 Backend-libXML/sample/minimize/minimize.curl create mode 100644 Backend-libXML/sample/minimize/minimize.json create mode 100644 Backend-libXML/sample/minimize/pretty create mode 100644 Backend-libXML/sample/prettify/prettify.curl create mode 100644 Backend-libXML/sample/prettify/prettify.json diff --git a/Backend-libXML/sample/minimize/minimize.curl b/Backend-libXML/sample/minimize/minimize.curl new file mode 100644 index 0000000..f75d62a --- /dev/null +++ b/Backend-libXML/sample/minimize/minimize.curl @@ -0,0 +1,3 @@ +url = "http://localhost:5000/minimizepost" +data = "@minimize.json" +request = POST diff --git a/Backend-libXML/sample/minimize/minimize.json b/Backend-libXML/sample/minimize/minimize.json new file mode 100644 index 0000000..10bfc79 --- /dev/null +++ b/Backend-libXML/sample/minimize/minimize.json @@ -0,0 +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", + "processor": "libxml", + "version": "1.0" +} diff --git a/Backend-libXML/sample/minimize/pretty b/Backend-libXML/sample/minimize/pretty new file mode 100644 index 0000000..7690e96 --- /dev/null +++ b/Backend-libXML/sample/minimize/pretty @@ -0,0 +1,104 @@ + + + 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/Backend-libXML/sample/prettify/prettify.curl b/Backend-libXML/sample/prettify/prettify.curl new file mode 100644 index 0000000..643474d --- /dev/null +++ b/Backend-libXML/sample/prettify/prettify.curl @@ -0,0 +1,3 @@ +url = "http://localhost:5000/prettifypost" +data = "@prettify.json" +request = POST diff --git a/Backend-libXML/sample/prettify/prettify.json b/Backend-libXML/sample/prettify/prettify.json new file mode 100644 index 0000000..10bfc79 --- /dev/null +++ b/Backend-libXML/sample/prettify/prettify.json @@ -0,0 +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", + "processor": "libxml", + "version": "1.0" +} -- 2.51.0 From f5c891027728415a34a3867001e73aaaf35b7567 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 14:25:46 +0100 Subject: [PATCH 06/13] Added endpoints for formatter --- Backend-libXML/main.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Backend-libXML/main.py b/Backend-libXML/main.py index 0996a1d..fab863a 100644 --- a/Backend-libXML/main.py +++ b/Backend-libXML/main.py @@ -72,5 +72,10 @@ def prettify(): request_data = json.loads(request.get_data(as_text=True)) return Parser.prettify(request_data['data']) +@app.route("/minimizepost", methods=["POST"]) +def minimize(): + 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 -- 2.51.0 From 947e5d621e616fb9e64dbdfac5e4dd697a6a3a43 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 14:57:32 +0100 Subject: [PATCH 07/13] Connected frontend and backend in formatter --- Backend-libXML/Parser.py | 4 +- Backend-libXML/main.py | 47 ++++++++++++++++++++++-- Frontend/assets/scripts/tools/scripts.js | 30 +++++++++++---- Frontend/tools/formatter.html | 15 +++----- 4 files changed, 73 insertions(+), 23 deletions(-) diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py index 2722386..a694edd 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -3,11 +3,11 @@ from lxml import etree def prettify(source: str) -> str: xml = etree.XML(source) - return etree.tostring(xml, pretty_print=True) + return etree.tostring(xml, pretty_print=True).decode() def minimize(source: str) -> str: xml = etree.XML(source) - return etree.tostring(xml, pretty_print=False) + return etree.tostring(xml, pretty_print=False).decode() def xpath(source: str, xpath: str) -> str: diff --git a/Backend-libXML/main.py b/Backend-libXML/main.py index fab863a..55582cd 100644 --- a/Backend-libXML/main.py +++ b/Backend-libXML/main.py @@ -15,6 +15,43 @@ 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 @@ -36,6 +73,10 @@ def process_xml(request: request, type: str) -> str: response_json['result'] = Parser.xslt(data, process) elif (type == "xpath"): response_json['result'] = Parser.xpath(data, process) + elif (type == "prettify"): + response_json['result'] = Parser.prettify(data) + elif (type == "minimize"): + response_json['result'] = Parser.minimize(data) else: raise ValueError("Valid operation types are: xsd, xslt, xpath") @@ -69,13 +110,11 @@ def xslt(): @app.route("/prettifypost", methods=["POST"]) def prettify(): - request_data = json.loads(request.get_data(as_text=True)) - return Parser.prettify(request_data['data']) + return process_xml(request, "prettify") @app.route("/minimizepost", methods=["POST"]) def minimize(): - request_data = json.loads(request.get_data(as_text=True)) - return Parser.prettify(request_data['data']) + return process_xml(request, "minimize") if __name__ == "__main__": app.run() \ No newline at end of file diff --git a/Frontend/assets/scripts/tools/scripts.js b/Frontend/assets/scripts/tools/scripts.js index 8792bc5..6becf43 100644 --- a/Frontend/assets/scripts/tools/scripts.js +++ b/Frontend/assets/scripts/tools/scripts.js @@ -91,7 +91,7 @@ function refreshTooltip() { document.getElementById("xsltelementsheader").innerText = XSLTheader; } -function performRequest(text, checkXML, checkTransform){ +function performRequest(endpoint, checkXML, checkTransform){ var xmlData = document.getElementById("xmlArea").value.trim(); var transformData = document.getElementById("transformArea").value.trim(); @@ -106,7 +106,7 @@ function performRequest(text, checkXML, checkTransform){ empty = true; } if (!empty) { - restRequest(text); + restRequest(endpoint, xmlData, transformData); }else{ document.getElementById("resultArea").value = "No data provided!"; return false; @@ -114,17 +114,33 @@ function performRequest(text, checkXML, checkTransform){ } +function performFormatRequest(endpoint, checkXML){ + var xmlData = document.getElementById("xmlArea").value.trim(); + + var empty = false; + if (defaultStrings.includes(xmlData) && checkXML) { + document.getElementById("xmlArea").style.backgroundColor = color_red; + xmlData = ""; + empty = true; + } + if (!empty) { + restRequest(endpoint, xmlData, null); + }else{ + document.getElementById("resultArea").value = "No data provided!"; + return false; + } + +} + + //Form REST request, send, receive and display in resultArea -async function restRequest(text) { +async function restRequest(endpoint, xmlData, transformData) { const escapeChar = "specialEscapeChar"; var port = ":8081/" if (getProcessor() == "libxml") { port = ":8082/" } - const addr = window.location.protocol + "//" + window.location.hostname + port + text; - - var xmlData = document.getElementById("xmlArea").value.trim(); - var transformData = document.getElementById("transformArea").value.trim(); + const addr = window.location.protocol + "//" + window.location.hostname + port + endpoint; if(defaultStrings.includes(xmlData)){ xmlData = ""; diff --git a/Frontend/tools/formatter.html b/Frontend/tools/formatter.html index f7bbd1e..f583346 100644 --- a/Frontend/tools/formatter.html +++ b/Frontend/tools/formatter.html @@ -25,9 +25,9 @@ onfocus="clearDefaultContent(this, 'Insert XML here');">

    + onclick="performFormatRequest('prettifypost', true)">Prettify XML + onclick="performFormatRequest('minimizepost', true)">Minimize XML

    @@ -73,22 +73,17 @@ -- 2.51.0 From 935b1d01eacda58f6f022327514bd604a55666c6 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 14:58:50 +0100 Subject: [PATCH 08/13] Updated sample files --- Backend-libXML/sample/prettify/prettify.json | 1 + Backend-libXML/sample/xpath/non-ns.curl | 2 +- Backend-libXML/sample/xpath/ns.curl | 2 +- Backend-libXML/sample/xsd/xsd.curl | 4 ++-- Backend-libXML/sample/xslt/xslt.curl | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Backend-libXML/sample/prettify/prettify.json b/Backend-libXML/sample/prettify/prettify.json index 10bfc79..cb3a5ec 100644 --- a/Backend-libXML/sample/prettify/prettify.json +++ b/Backend-libXML/sample/prettify/prettify.json @@ -1,5 +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": "whatever", "processor": "libxml", "version": "1.0" } diff --git a/Backend-libXML/sample/xpath/non-ns.curl b/Backend-libXML/sample/xpath/non-ns.curl index 7d4219f..b059ae6 100644 --- a/Backend-libXML/sample/xpath/non-ns.curl +++ b/Backend-libXML/sample/xpath/non-ns.curl @@ -1,4 +1,4 @@ #url = "localhost:8081/xpathpost" -url = "localhost:5000/xpath" +url = "localhost:5000/xpathpost" request = "POST" data = "@data.json" diff --git a/Backend-libXML/sample/xpath/ns.curl b/Backend-libXML/sample/xpath/ns.curl index f439c0e..90af030 100644 --- a/Backend-libXML/sample/xpath/ns.curl +++ b/Backend-libXML/sample/xpath/ns.curl @@ -1,4 +1,4 @@ #url = "localhost:8081/xpathpost" -url = "localhost:5000/xpath" +url = "localhost:5000/xpathpost" request = "POST" data = "@dataNS.json" diff --git a/Backend-libXML/sample/xsd/xsd.curl b/Backend-libXML/sample/xsd/xsd.curl index 2481c02..ae731a5 100644 --- a/Backend-libXML/sample/xsd/xsd.curl +++ b/Backend-libXML/sample/xsd/xsd.curl @@ -1,4 +1,4 @@ -#url = "http://localhost:8082/xsd" -url = "http://localhost:5000/xsd" +#url = "http://localhost:8082/xsdpost" +url = "http://localhost:5000/xsdpost" data = "@xsd.json" request = POST diff --git a/Backend-libXML/sample/xslt/xslt.curl b/Backend-libXML/sample/xslt/xslt.curl index d50edd1..a7a6f94 100644 --- a/Backend-libXML/sample/xslt/xslt.curl +++ b/Backend-libXML/sample/xslt/xslt.curl @@ -1,3 +1,3 @@ -url = "http://localhost:5000/xslt" +url = "http://localhost:5000/xsltpost" data = "@xslt.json" request = POST -- 2.51.0 From cc7664519e5d352ebf3be82dfa86ae2ed2867ee9 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 15:01:54 +0100 Subject: [PATCH 09/13] Implemented Q&D minimalizer --- 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 a694edd..24f5967 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -6,8 +6,12 @@ def prettify(source: str) -> str: return etree.tostring(xml, pretty_print=True).decode() def minimize(source: str) -> str: - xml = etree.XML(source) - return etree.tostring(xml, pretty_print=False).decode() + result = source + result = result.replace(" ", "") + result = result.replace(" ", "") + result = result.replace("\t", "") + result = result.replace("\n", "") + return result def xpath(source: str, xpath: str) -> str: -- 2.51.0 From e37ce3c678df16eb59f5b68e3985e7ff38d23a14 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 15:04:57 +0100 Subject: [PATCH 10/13] Fixed bug in minimalizer --- 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 24f5967..ef2428e 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -7,8 +7,8 @@ def prettify(source: str) -> str: def minimize(source: str) -> str: result = source + result = result.replace(" ", "") result = result.replace(" ", "") - result = result.replace(" ", "") result = result.replace("\t", "") result = result.replace("\n", "") return result -- 2.51.0 From 91dc131fbad343a213a80d277757a3556a11959d Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Mon, 20 Feb 2023 15:18:20 +0100 Subject: [PATCH 11/13] Some refactor allowing to use same js code as everywhere --- Frontend/tools/formatter.html | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Frontend/tools/formatter.html b/Frontend/tools/formatter.html index f583346..c5d88f6 100644 --- a/Frontend/tools/formatter.html +++ b/Frontend/tools/formatter.html @@ -18,6 +18,10 @@ + +