Search in sources :

Example 46 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XSLFPictureShape method getPictureData.

/**
     * Return the data on the (internal) picture.
     * For an external linked picture, will return null
     */
public XSLFPictureData getPictureData() {
    if (_data == null) {
        String blipId = getBlipId();
        if (blipId == null)
            return null;
        PackagePart p = getSheet().getPackagePart();
        PackageRelationship rel = p.getRelationship(blipId);
        if (rel != null) {
            try {
                PackagePart imgPart = p.getRelatedPart(rel);
                _data = new XSLFPictureData(imgPart);
            } catch (Exception e) {
                throw new POIXMLException(e);
            }
        }
    }
    return _data;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) XmlException(org.apache.xmlbeans.XmlException) POIXMLException(org.apache.poi.POIXMLException)

Example 47 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XSLFCommonSlideData method getDrawingText.

public List<DrawingTextBody> getDrawingText() {
    CTGroupShape gs = data.getSpTree();
    List<DrawingTextBody> out = new ArrayList<DrawingTextBody>();
    processShape(gs, out);
    for (CTGroupShape shape : gs.getGrpSpArray()) {
        processShape(shape, out);
    }
    for (CTGraphicalObjectFrame frame : gs.getGraphicFrameArray()) {
        CTGraphicalObjectData data = frame.getGraphic().getGraphicData();
        XmlCursor c = data.newCursor();
        c.selectPath("declare namespace pic='" + CTTable.type.getName().getNamespaceURI() + "' .//pic:tbl");
        while (c.toNextSelection()) {
            XmlObject o = c.getObject();
            if (o instanceof XmlAnyTypeImpl) {
                // Pesky XmlBeans bug - see Bugzilla #49934
                try {
                    o = CTTable.Factory.parse(o.toString(), DEFAULT_XML_OPTIONS);
                } catch (XmlException e) {
                    throw new POIXMLException(e);
                }
            }
            if (o instanceof CTTable) {
                DrawingTable table = new DrawingTable((CTTable) o);
                for (DrawingTableRow row : table.getRows()) {
                    for (DrawingTableCell cell : row.getCells()) {
                        DrawingTextBody textBody = cell.getTextBody();
                        out.add(textBody);
                    }
                }
            }
        }
        c.dispose();
    }
    return out;
}
Also used : CTGraphicalObjectFrame(org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame) ArrayList(java.util.ArrayList) POIXMLException(org.apache.poi.POIXMLException) CTGroupShape(org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape) XmlCursor(org.apache.xmlbeans.XmlCursor) CTGraphicalObjectData(org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData) XmlException(org.apache.xmlbeans.XmlException) CTTable(org.openxmlformats.schemas.drawingml.x2006.main.CTTable) XmlAnyTypeImpl(org.apache.xmlbeans.impl.values.XmlAnyTypeImpl) XmlObject(org.apache.xmlbeans.XmlObject)

Example 48 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XSLFGraphicFrame method copyDiagram.

// TODO should be moved to a sub-class
private void copyDiagram(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape) {
    String xpath = "declare namespace dgm='http://schemas.openxmlformats.org/drawingml/2006/diagram' $this//dgm:relIds";
    XmlObject[] obj = objData.selectPath(xpath);
    if (obj != null && obj.length == 1) {
        XmlCursor c = obj[0].newCursor();
        XSLFSheet sheet = srcShape.getSheet();
        try {
            String dm = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "dm"));
            PackageRelationship dmRel = sheet.getPackagePart().getRelationship(dm);
            PackagePart dmPart = sheet.getPackagePart().getRelatedPart(dmRel);
            getSheet().importPart(dmRel, dmPart);
            String lo = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "lo"));
            PackageRelationship loRel = sheet.getPackagePart().getRelationship(lo);
            PackagePart loPart = sheet.getPackagePart().getRelatedPart(loRel);
            getSheet().importPart(loRel, loPart);
            String qs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "qs"));
            PackageRelationship qsRel = sheet.getPackagePart().getRelationship(qs);
            PackagePart qsPart = sheet.getPackagePart().getRelatedPart(qsRel);
            getSheet().importPart(qsRel, qsPart);
            String cs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "cs"));
            PackageRelationship csRel = sheet.getPackagePart().getRelationship(cs);
            PackagePart csPart = sheet.getPackagePart().getRelatedPart(csRel);
            getSheet().importPart(csRel, csPart);
        } catch (InvalidFormatException e) {
            throw new POIXMLException(e);
        }
        c.dispose();
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) QName(javax.xml.namespace.QName) XmlObject(org.apache.xmlbeans.XmlObject) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 49 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class TestExtractorFactory method testFile.

@Test
public void testFile() throws Exception {
    // Excel
    POITextExtractor xlsExtractor = ExtractorFactory.createExtractor(xls);
    assertNotNull("Had empty extractor for " + xls, xlsExtractor);
    assertTrue("Expected instanceof ExcelExtractor, but had: " + xlsExtractor.getClass(), xlsExtractor instanceof ExcelExtractor);
    assertTrue(xlsExtractor.getText().length() > 200);
    xlsExtractor.close();
    POITextExtractor extractor = ExtractorFactory.createExtractor(xlsx);
    assertTrue(extractor.getClass().getName(), extractor instanceof XSSFExcelExtractor);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(xlsx);
    assertTrue(extractor.getText().length() > 200);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(xltx);
    assertTrue(extractor.getClass().getName(), extractor instanceof XSSFExcelExtractor);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(xlsb);
    assertContains(extractor.getText(), "test");
    extractor.close();
    extractor = ExtractorFactory.createExtractor(xltx);
    assertContains(extractor.getText(), "test");
    extractor.close();
    // TODO Support OOXML-Strict, see bug #57699
    try {
        /*extractor =*/
        ExtractorFactory.createExtractor(xlsxStrict);
        fail("OOXML-Strict isn't yet supported");
    } catch (POIXMLException e) {
    // Expected, for now
    }
    //        extractor = ExtractorFactory.createExtractor(xlsxStrict);
    //        assertTrue(
    //                extractor
    //                instanceof XSSFExcelExtractor
    //        );
    //        extractor.close();
    //
    //        extractor = ExtractorFactory.createExtractor(xlsxStrict);
    //        assertTrue(
    //                extractor.getText().contains("test")
    //        );
    //        extractor.close();
    // Word
    extractor = ExtractorFactory.createExtractor(doc);
    assertTrue(extractor instanceof WordExtractor);
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(doc6);
    assertTrue(extractor instanceof Word6Extractor);
    assertTrue(extractor.getText().length() > 20);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(doc95);
    assertTrue(extractor instanceof Word6Extractor);
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(docx);
    assertTrue(extractor instanceof XWPFWordExtractor);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(docx);
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(dotx);
    assertTrue(extractor instanceof XWPFWordExtractor);
    extractor.close();
    extractor = ExtractorFactory.createExtractor(dotx);
    assertContains(extractor.getText(), "Test");
    extractor.close();
    // PowerPoint (PPT)
    extractor = ExtractorFactory.createExtractor(ppt);
    assertTrue(extractor instanceof PowerPointExtractor);
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    // PowerPoint (PPTX)
    extractor = ExtractorFactory.createExtractor(pptx);
    assertTrue(extractor instanceof XSLFPowerPointExtractor);
    assertTrue(extractor.getText().length() > 120);
    extractor.close();
    // Visio - binary
    extractor = ExtractorFactory.createExtractor(vsd);
    assertTrue(extractor instanceof VisioTextExtractor);
    assertTrue(extractor.getText().length() > 50);
    extractor.close();
    // Visio - vsdx
    extractor = ExtractorFactory.createExtractor(vsdx);
    assertTrue(extractor instanceof XDGFVisioExtractor);
    assertTrue(extractor.getText().length() > 20);
    extractor.close();
    // Publisher
    extractor = ExtractorFactory.createExtractor(pub);
    assertTrue(extractor instanceof PublisherTextExtractor);
    assertTrue(extractor.getText().length() > 50);
    extractor.close();
    // Outlook msg
    extractor = ExtractorFactory.createExtractor(msg);
    assertTrue(extractor instanceof OutlookTextExtactor);
    assertTrue(extractor.getText().length() > 50);
    extractor.close();
    // Text
    try {
        ExtractorFactory.createExtractor(txt);
        fail();
    } catch (IllegalArgumentException e) {
    // Good
    }
}
Also used : XDGFVisioExtractor(org.apache.poi.xdgf.extractor.XDGFVisioExtractor) XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) Word6Extractor(org.apache.poi.hwpf.extractor.Word6Extractor) PowerPointExtractor(org.apache.poi.hslf.extractor.PowerPointExtractor) XSLFPowerPointExtractor(org.apache.poi.xslf.extractor.XSLFPowerPointExtractor) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) PublisherTextExtractor(org.apache.poi.hpbf.extractor.PublisherTextExtractor) POIXMLException(org.apache.poi.POIXMLException) WordExtractor(org.apache.poi.hwpf.extractor.WordExtractor) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) OutlookTextExtactor(org.apache.poi.hsmf.extractor.OutlookTextExtactor) XSLFPowerPointExtractor(org.apache.poi.xslf.extractor.XSLFPowerPointExtractor) POITextExtractor(org.apache.poi.POITextExtractor) XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) ExcelExtractor(org.apache.poi.hssf.extractor.ExcelExtractor) XSSFEventBasedExcelExtractor(org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor) EventBasedExcelExtractor(org.apache.poi.hssf.extractor.EventBasedExcelExtractor) VisioTextExtractor(org.apache.poi.hdgf.extractor.VisioTextExtractor) Test(org.junit.Test)

Example 50 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class TestXSSFBugs method bug54764.

@Test
public void bug54764() throws IOException, OpenXML4JException, XmlException {
    OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("54764.xlsx");
    // Check the core properties - will be found but empty, due
    //  to the expansion being too much to be considered valid
    POIXMLProperties props = new POIXMLProperties(pkg);
    assertEquals(null, props.getCoreProperties().getTitle());
    assertEquals(null, props.getCoreProperties().getSubject());
    assertEquals(null, props.getCoreProperties().getDescription());
    // Now check the spreadsheet itself
    try {
        new XSSFWorkbook(pkg).close();
        fail("Should fail as too much expansion occurs");
    } catch (POIXMLException e) {
    // Expected
    }
    pkg.close();
    // Try with one with the entities in the Content Types
    try {
        XSSFTestDataSamples.openSamplePackage("54764-2.xlsx").close();
        fail("Should fail as too much expansion occurs");
    } catch (Exception e) {
    // Expected
    }
    // Check we can still parse valid files after all that
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
    assertEquals(3, wb.getNumberOfSheets());
    wb.close();
}
Also used : POIXMLProperties(org.apache.poi.POIXMLProperties) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) POIXMLException(org.apache.poi.POIXMLException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) POIXMLException(org.apache.poi.POIXMLException) XLSBUnsupportedException(org.apache.poi.xssf.XLSBUnsupportedException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Aggregations

POIXMLException (org.apache.poi.POIXMLException)50 XmlException (org.apache.xmlbeans.XmlException)19 IOException (java.io.IOException)18 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)11 InputStream (java.io.InputStream)9 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)9 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)8 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 OutputStream (java.io.OutputStream)6 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)6 XmlObject (org.apache.xmlbeans.XmlObject)6 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)5 XmlCursor (org.apache.xmlbeans.XmlCursor)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)3 Workbook (org.apache.poi.ss.usermodel.Workbook)3