Search in sources :

Example 46 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class PackagePartName method throwExceptionIfPartNameHaveInvalidSegments.

/**
	 * Throws an exception if the part name has empty segments. [M1.3]
	 *
	 * Throws an exception if a segment any characters other than pchar
	 * characters. [M1.6]
	 *
	 * Throws an exception if a segment contain percent-encoded forward slash
	 * ('/'), or backward slash ('\') characters. [M1.7]
	 *
	 * Throws an exception if a segment contain percent-encoded unreserved
	 * characters. [M1.8]
	 *
	 * Throws an exception if the specified part name's segments end with a dot
	 * ('.') character. [M1.9]
	 *
	 * Throws an exception if a segment doesn't include at least one non-dot
	 * character. [M1.10]
	 *
	 * @param partUri
	 *            The part name to check.
	 * @throws InvalidFormatException
	 *             if the specified URI contain an empty segments or if one the
	 *             segments contained in the part name, ends with a dot ('.')
	 *             character.
	 */
private static void throwExceptionIfPartNameHaveInvalidSegments(URI partUri) throws InvalidFormatException {
    if (partUri == null) {
        throw new IllegalArgumentException("partUri");
    }
    // Split the URI into several part and analyze each
    String[] segments = partUri.toASCIIString().split("/");
    if (segments.length <= 1 || !segments[0].equals(""))
        throw new InvalidFormatException("A part name shall not have empty segments [M1.3]: " + partUri.getPath());
    for (int i = 1; i < segments.length; ++i) {
        String seg = segments[i];
        if (seg == null || "".equals(seg)) {
            throw new InvalidFormatException("A part name shall not have empty segments [M1.3]: " + partUri.getPath());
        }
        if (seg.endsWith(".")) {
            throw new InvalidFormatException("A segment shall not end with a dot ('.') character [M1.9]: " + partUri.getPath());
        }
        if ("".equals(seg.replaceAll("\\\\.", ""))) {
            // implementation rule [M1.9]
            throw new InvalidFormatException("A segment shall include at least one non-dot character. [M1.10]: " + partUri.getPath());
        }
        // Check for rule M1.6, M1.7, M1.8
        checkPCharCompliance(seg);
    }
}
Also used : InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 47 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project tika by apache.

the class AbstractOOXMLExtractor method loadLinkedRelationships.

/**
     * This is used by the SAX docx and pptx decorators to load hyperlinks and
     * other linked objects
     *
     * @param bodyPart
     * @return
     */
protected Map<String, String> loadLinkedRelationships(PackagePart bodyPart, boolean includeInternal, Metadata metadata) {
    Map<String, String> linkedRelationships = new HashMap<>();
    try {
        PackageRelationshipCollection prc = bodyPart.getRelationshipsByType(XWPFRelation.HYPERLINK.getRelation());
        for (int i = 0; i < prc.size(); i++) {
            PackageRelationship pr = prc.getRelationship(i);
            if (pr == null) {
                continue;
            }
            if (!includeInternal && TargetMode.INTERNAL.equals(pr.getTargetMode())) {
                continue;
            }
            String id = pr.getId();
            String url = (pr.getTargetURI() == null) ? null : pr.getTargetURI().toString();
            if (id != null && url != null) {
                linkedRelationships.put(id, url);
            }
        }
        for (String rel : EMBEDDED_RELATIONSHIPS) {
            prc = bodyPart.getRelationshipsByType(rel);
            for (int i = 0; i < prc.size(); i++) {
                PackageRelationship pr = prc.getRelationship(i);
                if (pr == null) {
                    continue;
                }
                String id = pr.getId();
                String uriString = (pr.getTargetURI() == null) ? null : pr.getTargetURI().toString();
                String fileName = uriString;
                if (pr.getTargetURI() != null) {
                    try {
                        fileName = FileHelper.getFilename(new File(fileName));
                    } catch (Exception e) {
                        fileName = uriString;
                    }
                }
                if (id != null) {
                    fileName = (fileName == null) ? "" : fileName;
                    linkedRelationships.put(id, fileName);
                }
            }
        }
    } catch (InvalidFormatException e) {
        EmbeddedDocumentUtil.recordEmbeddedStreamException(e, metadata);
    }
    return linkedRelationships;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) HashMap(java.util.HashMap) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) File(java.io.File) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Ole10NativeException(org.apache.poi.poifs.filesystem.Ole10NativeException) TikaException(org.apache.tika.exception.TikaException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlException(org.apache.xmlbeans.XmlException) SAXException(org.xml.sax.SAXException)

Example 48 with InvalidFormatException

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

the class GenerateData 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 rowKosName = sheet.getRow(9);
    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);
        DataBean dataBean = new DataBean();
        if (cellName == null) {
            if (rowName.getCell((columnReaded + 1)) == null)
                cont = false;
            else
                columnReaded++;
        } else {
            dataBean.setName(cellName.getStringCellValue());
            dataBean.setNameNormalized(cellNameNormalized.getStringCellValue());
            dataBean.setNormalizacion(cellNormalization.getStringCellValue());
            dataBean.setDimensionMesure(cellDimMesure.getStringCellValue());
            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) {
                dataBean.setRelationKos(cellRelationKos.getStringCellValue());
                dataWithSkosHierarchical.add(dataBean);
            }
            if (cellKosName != null) {
                dataBean.setKosName(cellKosName.getStringCellValue());
            } else {
                dataBean.setKosName(dataBean.getNameNormalized());
            }
            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.process.bean.DataBean) SkosBean(com.localidata.process.bean.SkosBean) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 49 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)

Example 50 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project tika by apache.

the class XSSFBExcelExtractorDecorator method extractHyperLinks.

private void extractHyperLinks(PackagePart sheetPart, XHTMLContentHandler xhtml) throws SAXException {
    try {
        for (PackageRelationship rel : sheetPart.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation())) {
            xhtml.startElement("a", "href", rel.getTargetURI().toString());
            xhtml.characters(rel.getTargetURI().toString());
            xhtml.endElement("a");
        }
    } catch (InvalidFormatException e) {
    //swallow
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Aggregations

InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)72 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