
class xmltojson {
    constructor(xml) {
        let parser = new DOMParser();
        let xmlDoc = parser.parseFromString(xml, "text/xml");

        this.xType = xmlDoc.documentElement;
        this.jType = {};

        this.setType(this.xType, this.jType);
    }
    toString(){
        return JSON.stringify(this.jType);
    }

    setType(xType, jType) {
        jType.n = xType.getAttribute("n");
        jType.d = xType.getAttribute("d");
        jType.ObjectID = xType.getAttribute("ObjectID");

        let nodeProperties = findNodeOnPath(xType, 'properties');
        if (nodeProperties != null) {
            jType.edit_object = {};
            jType.edit_object.width = nodeProperties.getAttribute("width");

            jType.edit_object.columns = [];
            let nodeProp = nodeProperties.firstChild;
            while (nodeProp != null) {
                if (nodeProp.nodeName == "prop") {
                    let jProp = {};
                    let column = this.setProp(nodeProp, jProp);
                    jType.edit_object.columns.push(column);
                }
                nodeProp = nodeProp.nextSibling;
            }
            let node;
            node = findFirstNodeOnAttribute(nodeProperties, 'sql-query', 't', 'i');
            if (node != null)
                jType.edit_object.insert_query = getCdataValue(node);
            node = findFirstNodeOnAttribute(nodeProperties, 'sql-query', 't', 'u');
            if (node != null)
                jType.edit_object.update_query = getCdataValue(node);
            node = findFirstNodeOnAttribute(nodeProperties, 'sql-query', 't', 'd');
            if (node != null)
                jType.edit_object.delete_query = getCdataValue(node);
            node = findFirstNodeOnAttribute(nodeProperties, 'sql-query', 't', 's');
            if (node != null)
                jType.edit_object.select_query = getCdataValue(node);
        }

        let nodeObjectsList = findNodeOnPath(xType, 'objects-list');
        if (nodeObjectsList != null) {
            jType.show_object = {};
            jType.show_object.width = nodeObjectsList.getAttribute("width");
            jType.show_object.d = nodeObjectsList.getAttribute("d");

            let node;
            node = findFirstNode(nodeObjectsList, 'sql-query');
            if (node != null)
                jType.show_object.select_query = getCdataValue(node);

            let nFilter = findFirstNode(nodeObjectsList, 'filter');
            if (nFilter != null) {
                jType.show_object.filter = {};
                if (nFilter.hasAttribute("height"))
                    jType.show_object.filter.height = nFilter.getAttribute("height");

                jType.show_object.filter.columns = [];
                let nextNode = nFilter.firstChild;
                while (nextNode != null) {
                    if (nextNode.nodeName == "column") {
                        let jColumn = {};
                        let column = this.setFilterColumn(nextNode, jColumn);
                        jType.show_object.filter.columns.push(column);
                    }
                    nextNode = nextNode.nextSibling;
                }
            }

            //jType.show_object.columns = [];
        }
        //this.win.setWidth(nodeProperties.getAttribute("width"));

    }

    //Переписываю колонки редактирования
    setProp(xProp, jProp) {

        if (xProp.hasAttribute("d"))
            jProp.d = xProp.getAttribute("d");
        if (xProp.hasAttribute("n"))
            jProp.n = xProp.getAttribute("n");
        if (xProp.hasAttribute("vt"))
            jProp.vt = xProp.getAttribute("vt");
        if (xProp.hasAttribute("maybenull"))
            jProp.maybenull = xProp.getAttribute("maybenull");
        if (xProp.hasAttribute("ot"))
            jProp.ot = xProp.getAttribute("ot");
        if (xProp.hasAttribute("FieldCaption"))
            jProp.FieldCaption = xProp.getAttribute("FieldCaption");
        if (xProp.hasAttribute("selector"))
            jProp.selector = xProp.getAttribute("selector");

        jProp.value = getCdataValue(xProp);


        /*
                            if (nodeFilter.getAttribute("n")==nodeProp.getAttribute("n"))
                            {
                                let cdata1=findFirstNode(nodeFilter,'#cdata-section');
                                if((cdata1!=null)&&(cdata1.nodeValue!=""))
                                {
                                    let cdata2=findFirstNode(nodeProp,'#cdata-section');
                                    if (cdata2==null)
                                    {
                                        cdata2 = nodeProp.ownerDocument.createCDATASection("");
                                        nodeProp.appendChild(cdata2);
                                    }
                                    cdata2.nodeValue=cdata1.nodeValue;
                                }
                            }
        * */
        return jProp;
    }

    //Переписать из "type->objects-list->filter"
    setFilterColumn(xColumn, jColumn) {

        if (xColumn.hasAttribute("d"))
            jColumn.d = xColumn.getAttribute("d");
        if (xColumn.hasAttribute("n"))
            jColumn.n = xColumn.getAttribute("n");
        if (xColumn.hasAttribute("vt"))
            jColumn.vt = xColumn.getAttribute("vt");
        if (xColumn.hasAttribute("object"))
            jColumn.object = xColumn.getAttribute("object");
        if (xColumn.hasAttribute("FieldCaption"))
            jColumn.FieldCaption = xColumn.getAttribute("FieldCaption");
        if (xColumn.hasAttribute("selector"))
            jColumn.selector = xColumn.getAttribute("selector");
        if (xColumn.hasAttribute("visible"))
            jColumn.visible = xColumn.getAttribute("visible");

        jColumn.value = getCdataValue(xColumn);

        return jColumn;
    }
}
