Search in sources :

Example 1 with XMLTableRow

use of com.twinsoft.convertigo.beans.common.XMLTableRow in project convertigo by convertigo.

the class XMLTableWizardPage method fillXMLTableDescription.

private void fillXMLTableDescription() {
    NodeList tables;
    try {
        String theXPath = (xpath.indexOf("//TABLE") != -1) ? xpath : (xpath.startsWith("/") ? xpath : "/" + xpath);
        tables = XPathAPI.eval(dom.getDocumentElement(), theXPath).nodelist();
        if (tables.getLength() > 0) {
            // ------------------------------------------------------------- For Bean XMLTable
            xDescription = new XMLVector<XMLVector<Object>>();
            Element table = (Element) tables.item(0);
            int iHeaderRow = Integer.parseInt(headerRow.getText().trim(), 10);
            int iHeaderCol = Integer.parseInt(headerCol.getText().trim(), 10);
            boolean bHeadersFromRow = checkBoxHeadersFromRow.getSelection();
            boolean bHeadersFromCol = checkBoxHeadersFromCol.getSelection();
            boolean hasTableBody = XPathAPI.eval(table, "./TBODY").nodelist().getLength() > 0;
            int iTR = iHeaderRow + 1;
            int iTD = iHeaderCol + 1;
            if (!bHeadersFromCol && !bHeadersFromRow) {
                XMLTableRow xRow = XMLTableRow.create();
                xRow.setXpath(hasTableBody ? "./TBODY/TR" : "./TR");
                xRow.addColumn("column", "./TD", false);
                xDescription.add(xRow.toXMLVector());
            } else {
                boolean bSameRowHeaderNames = false, bSameColHeaderNames = false;
                String oldRowName = null, oldColName = null;
                Vector<String> colNames = new Vector<String>();
                String dataXPath = (hasTableBody ? (bHeadersFromRow ? "(./TBODY/TR)[position()>" + iHeaderRow + "]" : "./TBODY/TR") : (bHeadersFromRow ? "(./TR)[position()>" + iHeaderRow + "]" : "./TR"));
                NodeList nlTR = XPathAPI.eval(table, dataXPath).nodelist();
                for (int i = 0; i < nlTR.getLength(); i++) {
                    int iRow = iTR + i;
                    XMLTableRow xRow = XMLTableRow.create();
                    String rowXPath = (hasTableBody ? (bHeadersFromRow ? "(./TBODY/TR)[position()>" + iTR + "]" : "./TBODY/TR") : (bHeadersFromRow ? "(./TR)[position()>" + iTR + "]" : "./TR"));
                    xRow.setXpath(bHeadersFromCol ? (hasTableBody ? "(./TBODY/TR)[" + iRow + "]" : "(./TR)[" + iRow + "]") : rowXPath);
                    NodeList nlTD = XPathAPI.eval(nlTR.item(i), "(./TD)[position()>" + iHeaderCol + "] | (./TH)[position()>" + iHeaderCol + "]").nodelist();
                    for (int j = 0; j < nlTD.getLength(); j++) {
                        NodeList nltxt = XPathAPI.eval(nlTD.item(j), ".//text()").nodelist();
                        String tmpVal = "";
                        for (int k = 0; k < nltxt.getLength(); k++) {
                            tmpVal += StringUtils.normalize(nltxt.item(k).getNodeValue());
                        }
                        tmpVal.trim();
                        tmpVal = tmpVal.equals("") ? "X" : tmpVal;
                        int iCol = iTD + j;
                        if (bHeadersFromRow) {
                            if (i == 0) {
                                colNames.add(tmpVal);
                            } else {
                                if (bHeadersFromCol) {
                                    if (j == 0)
                                        xRow.setName(tmpVal);
                                    else {
                                        xRow.addColumn(colNames.get(j), "(./TD|./TH)[" + iCol + "]", true);
                                        if (oldColName == null)
                                            oldColName = colNames.get(j);
                                        else
                                            bSameColHeaderNames = oldColName.equals(colNames.get(j));
                                    }
                                } else {
                                    xRow.addColumn(colNames.get(j), "(./TD|./TH)[" + iCol + "]", true);
                                    if (oldColName == null)
                                        oldColName = colNames.get(j);
                                    else
                                        bSameColHeaderNames = oldColName.equals(colNames.get(j));
                                }
                            }
                        } else if (bHeadersFromCol) {
                            if (j == 0)
                                xRow.setName(tmpVal);
                            else
                                xRow.addColumn("column" + j, "(./TD|./TH)[" + iCol + "]", true);
                        }
                    }
                    if (((i == 1) && bHeadersFromRow && !bHeadersFromCol) || ((i >= 1) && bHeadersFromRow && bHeadersFromCol) || (!bHeadersFromRow && bHeadersFromCol)) {
                        if (bSameColHeaderNames) {
                            // optimization for table's columns with same names
                            int last;
                            xRow.setColumnXPath(0, "(./TD|./TH)[position()>" + (bHeadersFromCol ? iTD : iHeaderCol) + "]");
                            while ((last = xRow.getColumns().size()) > 1) xRow.getColumns().remove(last - 1);
                        }
                        xDescription.add(xRow.toXMLVector());
                        if (oldRowName == null)
                            oldRowName = xRow.getName();
                        else
                            bSameRowHeaderNames = oldRowName.equals(xRow.getName());
                    }
                }
                if (bSameRowHeaderNames) {
                    // optimization for table's rows with same names
                    XMLTableRow xRow = new XMLTableRow(xDescription.get(0));
                    xRow.setXpath(hasTableBody ? "(./TBODY/TR)[position()>" + (bHeadersFromRow ? iTR : iHeaderRow) + "]" : "(./TR)[position()>" + (bHeadersFromRow ? iTR : iHeaderRow) + "]");
                    xDescription = new XMLVector<XMLVector<Object>>();
                    xDescription.add(xRow.toXMLVector());
                }
            }
            // ------------------------------------------------------------ For SWT Table
            data = new Vector<Vector<String>>();
            NodeList nlCol = XPathAPI.eval(table, (hasTableBody ? "./TBODY/TR" : "./TR")).nodelist();
            for (int i = 0; i < nlCol.getLength(); i++) {
                if (i < iHeaderRow)
                    continue;
                NodeList nltd = XPathAPI.eval(nlCol.item(i), "./TD|./TH").nodelist();
                Vector<String> row = new Vector<String>();
                for (int j = 0; j < nltd.getLength(); j++) {
                    NodeList nltxt = XPathAPI.eval(nltd.item(j), ".//text()").nodelist();
                    String tmpVal = "";
                    for (int k = 0; k < nltxt.getLength(); k++) {
                        tmpVal += StringUtils.normalize(nltxt.item(k).getNodeValue());
                    }
                    if (j < iHeaderCol)
                        continue;
                    row.add(tmpVal.trim());
                }
                data.add(row);
            }
            if (table != null) {
                setTableData();
            }
        }
    } catch (Exception e) {
        ;
    }
}
Also used : XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) XMLTableRow(com.twinsoft.convertigo.beans.common.XMLTableRow) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) Vector(java.util.Vector)

Aggregations

XMLTableRow (com.twinsoft.convertigo.beans.common.XMLTableRow)1 XMLVector (com.twinsoft.convertigo.beans.common.XMLVector)1 Vector (java.util.Vector)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1