use of org.apache.poi.openxml4j.opc.PackageRelationshipCollection in project poi by apache.
the class TestXSSFWorkbook method testSetVBAProject.
/**
* Tests that we can save a workbook with macros and reload it.
*/
@Test
public void testSetVBAProject() throws Exception {
File file;
final byte[] allBytes = new byte[256];
for (int i = 0; i < 256; i++) {
allBytes[i] = (byte) (i - 128);
}
XSSFWorkbook wb1 = new XSSFWorkbook();
wb1.createSheet();
wb1.setVBAProject(new ByteArrayInputStream(allBytes));
file = TempFile.createTempFile("poi-", ".xlsm");
OutputStream out = new FileOutputStream(file);
wb1.write(out);
out.close();
wb1.close();
// Check the package contains what we'd expect it to
OPCPackage pkg = OPCPackage.open(file.toString());
PackagePart wbPart = pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
assertTrue(wbPart.hasRelationships());
final PackageRelationshipCollection relationships = wbPart.getRelationships().getRelationships(XSSFRelation.VBA_MACROS.getRelation());
assertEquals(1, relationships.size());
assertEquals(XSSFRelation.VBA_MACROS.getDefaultFileName(), relationships.getRelationship(0).getTargetURI().toString());
PackagePart vbaPart = pkg.getPart(PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName()));
assertNotNull(vbaPart);
assertFalse(vbaPart.isRelationshipPart());
assertEquals(XSSFRelation.VBA_MACROS.getContentType(), vbaPart.getContentType());
final byte[] fromFile = IOUtils.toByteArray(vbaPart.getInputStream());
assertArrayEquals(allBytes, fromFile);
// Load back the XSSFWorkbook just to check nothing explodes
@SuppressWarnings("resource") XSSFWorkbook wb2 = new XSSFWorkbook(pkg);
assertEquals(1, wb2.getNumberOfSheets());
assertEquals(XSSFWorkbookType.XLSM, wb2.getWorkbookType());
pkg.close();
}
use of org.apache.poi.openxml4j.opc.PackageRelationshipCollection in project tika by apache.
the class ZipContainerDetector method detectOfficeOpenXML.
/**
* Detects the type of an OfficeOpenXML (OOXML) file from
* opened Package
*/
public static MediaType detectOfficeOpenXML(OPCPackage pkg) {
// Check for the normal Office core document
PackageRelationshipCollection core = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);
// Otherwise check for some other Office core document types
if (core.size() == 0) {
core = pkg.getRelationshipsByType(STRICT_CORE_DOCUMENT);
}
if (core.size() == 0) {
core = pkg.getRelationshipsByType(VISIO_DOCUMENT);
}
// If we didn't find a single core document of any type, skip detection
if (core.size() != 1) {
// Invalid OOXML Package received
return null;
}
// Get the type of the core document part
PackagePart corePart = pkg.getPart(core.getRelationship(0));
String coreType = corePart.getContentType();
// Turn that into the type of the overall document
String docType = coreType.substring(0, coreType.lastIndexOf('.'));
// The Macro Enabled formats are a little special
if (docType.toLowerCase(Locale.ROOT).endsWith("macroenabled")) {
docType = docType.toLowerCase(Locale.ROOT) + ".12";
}
if (docType.toLowerCase(Locale.ROOT).endsWith("macroenabledtemplate")) {
docType = MACRO_TEMPLATE_PATTERN.matcher(docType).replaceAll("macroenabled.12");
}
// Build the MediaType object and return
return MediaType.parse(docType);
}
use of org.apache.poi.openxml4j.opc.PackageRelationshipCollection in project tika by apache.
the class OOXMLExtractorFactory method trySXSLF.
private static POIXMLTextExtractor trySXSLF(OPCPackage pkg) throws XmlException, OpenXML4JException, IOException {
PackageRelationshipCollection packageRelationshipCollection = pkg.getRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");
if (packageRelationshipCollection.size() == 0) {
packageRelationshipCollection = pkg.getRelationshipsByType("http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument");
}
if (packageRelationshipCollection.size() == 0) {
return null;
}
PackagePart corePart = pkg.getPart(packageRelationshipCollection.getRelationship(0));
String targetContentType = corePart.getContentType();
XSLFRelation[] xslfRelations = org.apache.poi.xslf.extractor.XSLFPowerPointExtractor.SUPPORTED_TYPES;
for (int i = 0; i < xslfRelations.length; i++) {
XSLFRelation xslfRelation = xslfRelations[i];
if (xslfRelation.getContentType().equals(targetContentType)) {
return new XSLFEventBasedPowerPointExtractor(pkg);
}
}
if (XSLFRelation.THEME_MANAGER.getContentType().equals(targetContentType)) {
return new XSLFEventBasedPowerPointExtractor(pkg);
}
return null;
}
use of org.apache.poi.openxml4j.opc.PackageRelationshipCollection 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");
}
}
use of org.apache.poi.openxml4j.opc.PackageRelationshipCollection 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) {
}
}
}
Aggregations