Search in sources :

Example 1 with DataLoadingException

use of com.qaprosoft.carina.core.foundation.exception.DataLoadingException in project carina by qaprosoft.

the class XLSParser method parseCellLinks.

public static XLSChildTable parseCellLinks(Cell cell, Workbook wb, Sheet sheet) {
    if (cell == null)
        return null;
    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        if (cell.getCellFormula().contains("#This Row")) {
            if (cell.getCellFormula().contains("!")) {
                // Parse link to the cell with table name in the external doc([2]!Table1[[#This Row],[Header6]])
                List<String> paths = Arrays.asList(cell.getCellFormula().split("!"));
                int externalLinkNumber = Integer.valueOf(paths.get(0).replaceAll("\\D+", "")) - 1;
                String tableName = paths.get(1).split("\\[")[0];
                if (wb instanceof XSSFWorkbook) {
                    ExternalLinksTable link = ((XSSFWorkbook) wb).getExternalLinksTable().get(externalLinkNumber);
                    File file = new File(XLSCache.getWorkbookPath(wb));
                    XSSFWorkbook childWb = (XSSFWorkbook) XLSCache.getWorkbook(file.getParent() + "/" + link.getLinkedFileName());
                    if (childWb == null)
                        throw new DataLoadingException(String.format("WorkBook '%s' doesn't exist!", link.getLinkedFileName()));
                    for (int i = 0; i < childWb.getNumberOfSheets(); i++) {
                        XSSFSheet childSheet = childWb.getSheetAt(i);
                        for (XSSFTable table : childSheet.getTables()) {
                            if (table.getName().equals(tableName)) {
                                return createChildTable(childSheet, cell.getRowIndex());
                            }
                        }
                    }
                } else {
                    throw new DataLoadingException("Unsupported format. External links supports only for .xlsx documents.");
                }
            } else {
                // Parse link to the cell with table name in the same doc(=Table1[[#This Row],[Header6]])
                List<String> paths = Arrays.asList(cell.getCellFormula().replace("=", "").split("\\["));
                if (wb instanceof XSSFWorkbook) {
                    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
                        XSSFSheet childSheet = (XSSFSheet) wb.getSheetAt(i);
                        for (XSSFTable table : childSheet.getTables()) {
                            if (table.getName().equals(paths.get(0))) {
                                return createChildTable(childSheet, cell.getRowIndex());
                            }
                        }
                    }
                } else {
                    throw new DataLoadingException("Unsupported format. Links with table name supports only for .xlsx documents.");
                }
            }
        } else {
            String cellValue = cell.getCellFormula().replace("=", "").replace("[", "").replace("]", "!").replace("'", "");
            List<String> paths = Arrays.asList(cellValue.split("!"));
            int rowNumber = 0;
            Sheet childSheet = null;
            switch(paths.size()) {
                // Parse link to the cell in the same sheet(=A4)
                case 1:
                    rowNumber = Integer.valueOf(paths.get(0).replaceAll("\\D+", "")) - 1;
                    return createChildTable(sheet, rowNumber);
                // Parse link to the cell in another sheet in the same doc(=SheetName!A4)
                case 2:
                    childSheet = wb.getSheet(paths.get(0));
                    if (childSheet == null)
                        throw new DataLoadingException(String.format("Sheet '%s' doesn't exist!", paths.get(0)));
                    rowNumber = Integer.valueOf(paths.get(1).replaceAll("\\D+", "")) - 1;
                    return createChildTable(childSheet, rowNumber);
                // Parse link to the cell in another doc(=[2]SheetName!A4)
                case 3:
                    if (wb instanceof XSSFWorkbook) {
                        ExternalLinksTable link = ((XSSFWorkbook) wb).getExternalLinksTable().get(Integer.valueOf(paths.get(0)) - 1);
                        File file = new File(XLSCache.getWorkbookPath(wb));
                        XSSFWorkbook childWb = (XSSFWorkbook) XLSCache.getWorkbook(file.getParent() + "/" + link.getLinkedFileName());
                        if (childWb == null)
                            throw new DataLoadingException(String.format("WorkBook '%s' doesn't exist!", paths.get(0)));
                        childSheet = childWb.getSheet(paths.get(1));
                        if (childSheet == null)
                            throw new DataLoadingException(String.format("Sheet '%s' doesn't exist!", paths.get(0)));
                        rowNumber = Integer.valueOf(paths.get(2).replaceAll("\\D+", "")) - 1;
                        return createChildTable(childSheet, rowNumber);
                    } else {
                        throw new DataLoadingException("Unsupported format. External links supports only for .xlsx documents.");
                    }
                default:
                    return null;
            }
        }
    }
    return null;
}
Also used : DataLoadingException(com.qaprosoft.carina.core.foundation.exception.DataLoadingException) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ExternalLinksTable(org.apache.poi.xssf.model.ExternalLinksTable) File(java.io.File) XSSFTable(org.apache.poi.xssf.usermodel.XSSFTable) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet)

Aggregations

DataLoadingException (com.qaprosoft.carina.core.foundation.exception.DataLoadingException)1 File (java.io.File)1 ExternalLinksTable (org.apache.poi.xssf.model.ExternalLinksTable)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 XSSFTable (org.apache.poi.xssf.usermodel.XSSFTable)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1