Search in sources :

Example 71 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project local-data-aragopedia by aragonopendata.

the class GenerateRDF method readXlsxFile.

private void readXlsxFile(File file, ConfigBean configBean) {
    log.debug("Init readXlsxFile");
    InputStream inp = null;
    Workbook wb = null;
    try {
        inp = new FileInputStream(file);
        wb = WorkbookFactory.create(inp);
    } catch (FileNotFoundException e) {
        log.error(e.getMessage());
    } catch (InvalidFormatException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    }
    Sheet sheet = wb.getSheetAt(0);
    Row rowName = sheet.getRow(0);
    Row rowNameNormalized = sheet.getRow(1);
    Row rowNormalization = sheet.getRow(2);
    Row rowDimMesure = sheet.getRow(3);
    Row rowType = sheet.getRow(4);
    Row rowSkosfile = sheet.getRow(5);
    Row rowConstant = sheet.getRow(6);
    Row rowConstantValue = sheet.getRow(7);
    Row rowRelationKos = sheet.getRow(8);
    Row rowKosNameNormalized = sheet.getRow(9);
    Row rowKosName = sheet.getRow(10);
    boolean cont = true;
    int columnReaded = 0;
    while (cont) {
        Cell cellName = rowName.getCell(columnReaded);
        Cell cellNameNormalized = rowNameNormalized.getCell(columnReaded);
        Cell cellNormalization = rowNormalization.getCell(columnReaded);
        Cell cellDimMesure = rowDimMesure.getCell(columnReaded);
        Cell cellType = rowType.getCell(columnReaded);
        Cell cellSkosfile = null;
        if (rowSkosfile != null)
            cellSkosfile = rowSkosfile.getCell(columnReaded);
        Cell cellConstant = null;
        if (rowConstant != null)
            cellConstant = rowConstant.getCell(columnReaded);
        Cell cellConstantValue = null;
        if (rowConstantValue != null)
            cellConstantValue = rowConstantValue.getCell(columnReaded);
        Cell cellRelationKos = null;
        if (rowRelationKos != null)
            cellRelationKos = rowRelationKos.getCell(columnReaded);
        Cell cellKosName = null;
        if (rowKosName != null)
            cellKosName = rowKosName.getCell(columnReaded);
        Cell cellKosNameNormalized = null;
        if (rowKosNameNormalized != null)
            cellKosNameNormalized = rowKosNameNormalized.getCell(columnReaded);
        DataBean dataBean = new DataBean();
        if (cellName == null) {
            if (rowName.getCell((columnReaded + 1)) == null)
                cont = false;
            else
                columnReaded++;
        } else {
            dataBean.setName(cellName.getStringCellValue());
            if (cellNameNormalized != null) {
                dataBean.setNameNormalized(cellNameNormalized.getStringCellValue());
            } else {
                log.error("Error in config " + file.getName() + " in cell name normalized");
            }
            if (cellNormalization != null) {
                dataBean.setNormalizacion(cellNormalization.getStringCellValue());
            } else {
                log.error("Error in config " + file.getName() + " in cell normalization");
            }
            if (cellDimMesure != null) {
                dataBean.setDimensionMesure(cellDimMesure.getStringCellValue());
            } else {
                log.error("Error in config " + file.getName() + " in cell dim mesure");
            }
            dataBean.setIdConfig(configBean.getId());
            String type = "";
            if (cellType != null) {
                type = cellType.getStringCellValue();
            } else {
                type = "xsd:string";
            }
            dataBean.setType(type);
            if (cellSkosfile != null && !cellSkosfile.getStringCellValue().equals("")) {
                HashMap<String, SkosBean> mapSkos = readMappingFile(cellSkosfile.getStringCellValue());
                dataBean.setMapSkos(mapSkos);
                configBean.getMapData().put(dataBean.getNameNormalized(), dataBean);
                dataWithSkos.add(dataBean);
            } else {
                configBean.getMapData().put(dataBean.getNameNormalized(), dataBean);
            }
            if (Prop.addDataConstant && cellConstant != null && cellConstant.getStringCellValue().equals(Constants.constante)) {
                if (cellConstantValue != null) {
                    dataBean.setConstant(cellConstantValue.getStringCellValue() + "");
                    configBean.getListDataConstant().add(dataBean);
                }
            }
            if (cellRelationKos != null) {
                if (Utils.v(cellRelationKos.getStringCellValue())) {
                    dataBean.setRelationKos(cellRelationKos.getStringCellValue());
                    dataWithSkosHierarchical.add(dataBean);
                }
            }
            if (Utils.v(cellKosNameNormalized) && Utils.v(cellKosNameNormalized.getStringCellValue())) {
                dataBean.setKosNameNormalized(cellKosNameNormalized.getStringCellValue());
            } else {
                dataBean.setKosNameNormalized(dataBean.getNameNormalized());
            }
            if (Utils.v(cellKosName) && Utils.v(cellKosName.getStringCellValue())) {
                dataBean.setKosName(cellKosName.getStringCellValue());
            } else {
                dataBean.setKosName(dataBean.getName());
            }
            columnReaded++;
        }
    }
    log.debug("End readXlsxFile");
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Workbook(org.apache.poi.ss.usermodel.Workbook) FileInputStream(java.io.FileInputStream) DataBean(com.localidata.bean.DataBean) SkosBean(com.localidata.bean.SkosBean) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)71 IOException (java.io.IOException)24 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)22 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)18 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)17 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)15 InputStream (java.io.InputStream)12 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)11 Test (org.junit.Test)10 URI (java.net.URI)9 POIXMLException (org.apache.poi.POIXMLException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ArrayList (java.util.ArrayList)7 TikaException (org.apache.tika.exception.TikaException)7 XmlException (org.apache.xmlbeans.XmlException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 FileNotFoundException (java.io.FileNotFoundException)6 HashMap (java.util.HashMap)6 Workbook (org.apache.poi.ss.usermodel.Workbook)6