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;
}
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;
}
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();
}
}
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
}
}
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();
}
Aggregations