Connected frontend and backend in formatter
This commit is contained in:
@@ -3,11 +3,11 @@ from lxml import etree
|
|||||||
|
|
||||||
def prettify(source: str) -> str:
|
def prettify(source: str) -> str:
|
||||||
xml = etree.XML(source)
|
xml = etree.XML(source)
|
||||||
return etree.tostring(xml, pretty_print=True)
|
return etree.tostring(xml, pretty_print=True).decode()
|
||||||
|
|
||||||
def minimize(source: str) -> str:
|
def minimize(source: str) -> str:
|
||||||
xml = etree.XML(source)
|
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:
|
def xpath(source: str, xpath: str) -> str:
|
||||||
|
|||||||
@@ -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:
|
def process_xml(request: request, type: str) -> str:
|
||||||
"""Function to process
|
"""Function to process
|
||||||
|
|
||||||
@@ -36,6 +73,10 @@ def process_xml(request: request, type: str) -> str:
|
|||||||
response_json['result'] = Parser.xslt(data, process)
|
response_json['result'] = Parser.xslt(data, process)
|
||||||
elif (type == "xpath"):
|
elif (type == "xpath"):
|
||||||
response_json['result'] = Parser.xpath(data, process)
|
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:
|
else:
|
||||||
raise ValueError("Valid operation types are: xsd, xslt, xpath")
|
raise ValueError("Valid operation types are: xsd, xslt, xpath")
|
||||||
|
|
||||||
@@ -69,13 +110,11 @@ def xslt():
|
|||||||
|
|
||||||
@app.route("/prettifypost", methods=["POST"])
|
@app.route("/prettifypost", methods=["POST"])
|
||||||
def prettify():
|
def prettify():
|
||||||
request_data = json.loads(request.get_data(as_text=True))
|
return process_xml(request, "prettify")
|
||||||
return Parser.prettify(request_data['data'])
|
|
||||||
|
|
||||||
@app.route("/minimizepost", methods=["POST"])
|
@app.route("/minimizepost", methods=["POST"])
|
||||||
def minimize():
|
def minimize():
|
||||||
request_data = json.loads(request.get_data(as_text=True))
|
return process_xml(request, "minimize")
|
||||||
return Parser.prettify(request_data['data'])
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run()
|
app.run()
|
||||||
@@ -91,7 +91,7 @@ function refreshTooltip() {
|
|||||||
document.getElementById("xsltelementsheader").innerText = XSLTheader;
|
document.getElementById("xsltelementsheader").innerText = XSLTheader;
|
||||||
}
|
}
|
||||||
|
|
||||||
function performRequest(text, checkXML, checkTransform){
|
function performRequest(endpoint, checkXML, checkTransform){
|
||||||
var xmlData = document.getElementById("xmlArea").value.trim();
|
var xmlData = document.getElementById("xmlArea").value.trim();
|
||||||
var transformData = document.getElementById("transformArea").value.trim();
|
var transformData = document.getElementById("transformArea").value.trim();
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ function performRequest(text, checkXML, checkTransform){
|
|||||||
empty = true;
|
empty = true;
|
||||||
}
|
}
|
||||||
if (!empty) {
|
if (!empty) {
|
||||||
restRequest(text);
|
restRequest(endpoint, xmlData, transformData);
|
||||||
}else{
|
}else{
|
||||||
document.getElementById("resultArea").value = "No data provided!";
|
document.getElementById("resultArea").value = "No data provided!";
|
||||||
return false;
|
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
|
//Form REST request, send, receive and display in resultArea
|
||||||
async function restRequest(text) {
|
async function restRequest(endpoint, xmlData, transformData) {
|
||||||
const escapeChar = "specialEscapeChar";
|
const escapeChar = "specialEscapeChar";
|
||||||
var port = ":8081/"
|
var port = ":8081/"
|
||||||
if (getProcessor() == "libxml") {
|
if (getProcessor() == "libxml") {
|
||||||
port = ":8082/"
|
port = ":8082/"
|
||||||
}
|
}
|
||||||
const addr = window.location.protocol + "//" + window.location.hostname + port + text;
|
const addr = window.location.protocol + "//" + window.location.hostname + port + endpoint;
|
||||||
|
|
||||||
var xmlData = document.getElementById("xmlArea").value.trim();
|
|
||||||
var transformData = document.getElementById("transformArea").value.trim();
|
|
||||||
|
|
||||||
if(defaultStrings.includes(xmlData)){
|
if(defaultStrings.includes(xmlData)){
|
||||||
xmlData = "<empty/>";
|
xmlData = "<empty/>";
|
||||||
|
|||||||
@@ -25,9 +25,9 @@
|
|||||||
onfocus="clearDefaultContent(this, 'Insert XML here');"></textarea>
|
onfocus="clearDefaultContent(this, 'Insert XML here');"></textarea>
|
||||||
<br><br>
|
<br><br>
|
||||||
<button id="requestButton" class="max-width block-label action-button active"
|
<button id="requestButton" class="max-width block-label action-button active"
|
||||||
onclick="performRequest('xsdpost', true, true)">Prettify XML</button>
|
onclick="performFormatRequest('prettifypost', true)">Prettify XML</button>
|
||||||
<button id="requestButton" class="max-width block-label action-button active"
|
<button id="requestButton" class="max-width block-label action-button active"
|
||||||
onclick="performRequest('xsdpost', true, true)">Minimize XML</button>
|
onclick="performFormatRequest('minimizepost', true)">Minimize XML</button>
|
||||||
<br><br>
|
<br><br>
|
||||||
|
|
||||||
<label for="resultArea"><b>Result:<span id="procinfo"></span></b></label>
|
<label for="resultArea"><b>Result:<span id="procinfo"></span></b></label>
|
||||||
@@ -73,22 +73,17 @@
|
|||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
function getProcessor() {
|
function getProcessor() {
|
||||||
return document.getElementById("processors").value;
|
return "libxml";
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
function getVersion() {
|
function getVersion() {
|
||||||
if (getProcInfo() == "xalan") {
|
return "1.0";
|
||||||
return "1.0";
|
|
||||||
} else {
|
|
||||||
return "3.0";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
function getProcInfo() {
|
function getProcInfo() {
|
||||||
var processVariables = document.getElementById("processors").value;// + "&version=" + document.getElementById("versions").value;
|
return "libxml";
|
||||||
return processVariables;
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user