Search in sources :

Example 26 with InvalidFormatException

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

the class SXSLFPowerPointExtractorDecorator method handleBasicRelatedParts.

/**
     * This should handle the comments, master, notes, etc
     *
     * @param contentType
     * @param xhtmlClassLabel
     * @param parentPart
     * @param contentHandler
     */
private void handleBasicRelatedParts(String contentType, String xhtmlClassLabel, PackagePart parentPart, ContentHandler contentHandler) throws SAXException {
    PackageRelationshipCollection relatedPartPRC = null;
    try {
        relatedPartPRC = parentPart.getRelationshipsByType(contentType);
    } catch (InvalidFormatException e) {
        metadata.add(TikaCoreProperties.TIKA_META_EXCEPTION_WARNING, ExceptionUtils.getStackTrace(e));
    }
    if (relatedPartPRC != null && relatedPartPRC.size() > 0) {
        AttributesImpl attributes = new AttributesImpl();
        attributes.addAttribute("", "class", "class", "CDATA", xhtmlClassLabel);
        contentHandler.startElement("", "div", "div", attributes);
        for (int i = 0; i < relatedPartPRC.size(); i++) {
            PackageRelationship relatedPartPackageRelationship = relatedPartPRC.getRelationship(i);
            try {
                PackagePart relatedPartPart = parentPart.getRelatedPart(relatedPartPackageRelationship);
                try (InputStream stream = relatedPartPart.getInputStream()) {
                    context.getSAXParser().parse(stream, new OfflineContentHandler(new EmbeddedContentHandler(contentHandler)));
                } catch (IOException | TikaException e) {
                    metadata.add(TikaCoreProperties.TIKA_META_EXCEPTION_WARNING, ExceptionUtils.getStackTrace(e));
                }
            } catch (InvalidFormatException e) {
                metadata.add(TikaCoreProperties.TIKA_META_EXCEPTION_WARNING, ExceptionUtils.getStackTrace(e));
            }
        }
        contentHandler.endElement("", "div", "div");
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) AttributesImpl(org.xml.sax.helpers.AttributesImpl) OfflineContentHandler(org.apache.tika.sax.OfflineContentHandler) TikaException(org.apache.tika.exception.TikaException) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) InputStream(java.io.InputStream) EmbeddedContentHandler(org.apache.tika.sax.EmbeddedContentHandler) IOException(java.io.IOException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 27 with InvalidFormatException

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

the class SXWPFWordExtractorDecorator method addRelatedParts.

private void addRelatedParts(PackagePart documentPart, List<PackagePart> relatedParts) {
    for (String relation : MAIN_PART_RELATIONS) {
        PackageRelationshipCollection prc = null;
        try {
            prc = documentPart.getRelationshipsByType(relation);
            if (prc != null) {
                for (int i = 0; i < prc.size(); i++) {
                    PackagePart packagePart = documentPart.getRelatedPart(prc.getRelationship(i));
                    relatedParts.add(packagePart);
                }
            }
        } catch (InvalidFormatException e) {
        }
    }
}
Also used : PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 28 with InvalidFormatException

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

the class XSLFPowerPointExtractorDecorator method addSlideParts.

private void addSlideParts(PackagePart slidePart, List<PackagePart> parts) {
    for (String relation : new String[] { XSLFRelation.VML_DRAWING.getRelation(), XSLFRelation.SLIDE_LAYOUT.getRelation(), XSLFRelation.NOTES_MASTER.getRelation(), XSLFRelation.NOTES.getRelation() }) {
        try {
            for (PackageRelationship packageRelationship : slidePart.getRelationshipsByType(relation)) {
                if (packageRelationship.getTargetMode() == TargetMode.INTERNAL) {
                    PackagePartName relName = PackagingURIHelper.createPartName(packageRelationship.getTargetURI());
                    parts.add(packageRelationship.getPackage().getPart(relName));
                }
            }
        } catch (InvalidFormatException e) {
        }
    }
    //and slide of course
    parts.add(slidePart);
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 29 with InvalidFormatException

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

the class GenerateData method readMappingFile.

private HashMap<String, SkosBean> readMappingFile(String skosPath) {
    log.debug("Init readSkosFile");
    HashMap<String, SkosBean> mapSkos = new HashMap<String, SkosBean>();
    File skosMappingg = new File(configDirectoryString + File.separator + skosPath);
    InputStream inp = null;
    Workbook wb = null;
    try {
        inp = new FileInputStream(skosMappingg);
        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());
    }
    if (wb != null) {
        Sheet sheet = wb.getSheetAt(0);
        for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);
            Cell cellId = row.getCell(0);
            Cell cellUri = row.getCell(1);
            SkosBean skosBean = new SkosBean();
            SkosBean skosBeanExtra = new SkosBean();
            String idCell = "";
            if (cellId.getCellType() == 0) {
                Double d = new Double(cellId.getNumericCellValue());
                idCell = d.intValue() + "";
            } else {
                idCell = cellId.getStringCellValue();
            }
            skosBean.setLabel(idCell);
            idCell = Utils.urlify(idCell);
            skosBean.setId(idCell);
            String uriCell = "";
            if (cellUri.getCellType() == 0) {
                Double d = new Double(cellUri.getNumericCellValue());
                uriCell = d.intValue() + "";
            } else {
                uriCell = cellUri.getStringCellValue();
                String id = uriCell.substring(uriCell.lastIndexOf("/") + 1, uriCell.length());
                if (!idCell.equals(id)) {
                    skosBeanExtra.setId(id);
                    skosBeanExtra.setLabel(id);
                    skosBeanExtra.setURI(uriCell);
                    mapSkos.put(id, skosBeanExtra);
                }
            }
            skosBean.setURI(uriCell);
            mapSkos.put(idCell, skosBean);
        }
    }
    log.debug("End readSkosFile");
    return mapSkos;
}
Also used : HashMap(java.util.HashMap) 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) SkosBean(com.localidata.process.bean.SkosBean) Row(org.apache.poi.ss.usermodel.Row) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 30 with InvalidFormatException

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

the class GenerateRDF method readMappingFile.

private HashMap<String, SkosBean> readMappingFile(String skosPath) {
    log.debug("Init readSkosFile");
    HashMap<String, SkosBean> mapSkos = new HashMap<String, SkosBean>();
    File skosMappingg = new File(configDirectoryString + File.separator + skosPath);
    InputStream inp = null;
    Workbook wb = null;
    try {
        inp = new FileInputStream(skosMappingg);
        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());
    }
    if (wb != null) {
        Sheet sheet = wb.getSheetAt(0);
        for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);
            Cell cellId = row.getCell(0);
            Cell cellUri = row.getCell(1);
            if (cellId == null || cellUri == null) {
                continue;
            }
            SkosBean skosBean = new SkosBean();
            SkosBean skosBeanExtra = new SkosBean();
            String idCell = "";
            if (cellId.getCellType() == 0) {
                Double d = new Double(cellId.getNumericCellValue());
                idCell = d.intValue() + "";
            } else {
                idCell = cellId.getStringCellValue();
            }
            skosBean.setLabel(idCell);
            idCell = Utils.urlify(idCell);
            skosBean.setId(idCell);
            String uriCell = "";
            if (cellUri.getCellType() == 0) {
                Double d = new Double(cellUri.getNumericCellValue());
                uriCell = d.intValue() + "";
            } else {
                uriCell = cellUri.getStringCellValue();
                String id = uriCell.substring(uriCell.lastIndexOf("/") + 1, uriCell.length());
                if (!idCell.equals(id)) {
                    skosBeanExtra.setId(id);
                    skosBeanExtra.setLabel(id);
                    skosBeanExtra.setURI(uriCell);
                    mapSkos.put(id, skosBeanExtra);
                }
            }
            skosBean.setURI(uriCell);
            mapSkos.put(idCell, skosBean);
        }
    }
    log.debug("End readSkosFile");
    return mapSkos;
}
Also used : HashMap(java.util.HashMap) 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) SkosBean(com.localidata.bean.SkosBean) Row(org.apache.poi.ss.usermodel.Row) File(java.io.File) 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