Connected frontend and backend in formatter
This commit is contained in:
		@@ -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:
 | 
			
		||||
 
 | 
			
		||||
@@ -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()
 | 
			
		||||
@@ -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 = "<empty/>";
 | 
			
		||||
 
 | 
			
		||||
@@ -25,9 +25,9 @@
 | 
			
		||||
                    onfocus="clearDefaultContent(this, 'Insert XML here');"></textarea>
 | 
			
		||||
                <br><br>
 | 
			
		||||
                <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"
 | 
			
		||||
                    onclick="performRequest('xsdpost', true, true)">Minimize XML</button>
 | 
			
		||||
                    onclick="performFormatRequest('minimizepost', true)">Minimize XML</button>
 | 
			
		||||
                <br><br>
 | 
			
		||||
 | 
			
		||||
                <label for="resultArea"><b>Result:<span id="procinfo"></span></b></label>
 | 
			
		||||
@@ -73,22 +73,17 @@
 | 
			
		||||
    </script>
 | 
			
		||||
    <script>
 | 
			
		||||
        function getProcessor() {
 | 
			
		||||
            return document.getElementById("processors").value;
 | 
			
		||||
            return "libxml";
 | 
			
		||||
        }
 | 
			
		||||
    </script>
 | 
			
		||||
    <script>
 | 
			
		||||
        function getVersion() {
 | 
			
		||||
            if (getProcInfo() == "xalan") {
 | 
			
		||||
            return "1.0";
 | 
			
		||||
            } else {
 | 
			
		||||
                return "3.0";
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    </script>
 | 
			
		||||
    <script>
 | 
			
		||||
        function getProcInfo() {
 | 
			
		||||
            var processVariables = document.getElementById("processors").value;// + "&version=" + document.getElementById("versions").value;
 | 
			
		||||
            return processVariables;
 | 
			
		||||
            return "libxml";
 | 
			
		||||
        }
 | 
			
		||||
    </script>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user