Search in sources :

Example 41 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestEncryptor method encryptPackageWithoutCoreProperties.

/**
     * Ensure we can encrypt a package that is missing the Core
     *  Properties, eg one from dodgy versions of Jasper Reports 
     * See https://github.com/nestoru/xlsxenc/ and
     * http://stackoverflow.com/questions/28593223
     */
@Test
public void encryptPackageWithoutCoreProperties() throws Exception {
    // Open our file without core properties
    File inp = POIDataSamples.getOpenXML4JInstance().getFile("OPCCompliance_NoCoreProperties.xlsx");
    OPCPackage pkg = OPCPackage.open(inp.getPath());
    // It doesn't have any core properties yet
    assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
    assertNotNull(pkg.getPackageProperties());
    assertNotNull(pkg.getPackageProperties().getLanguageProperty());
    assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
    // Encrypt it
    EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
    NPOIFSFileSystem fs = new NPOIFSFileSystem();
    Encryptor enc = info.getEncryptor();
    enc.confirmPassword("password");
    OutputStream os = enc.getDataStream(fs);
    pkg.save(os);
    os.close();
    pkg.revert();
    // Save the resulting OLE2 document, and re-open it
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    fs.writeFilesystem(baos);
    fs.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    NPOIFSFileSystem inpFS = new NPOIFSFileSystem(bais);
    // Check we can decrypt it
    info = new EncryptionInfo(inpFS);
    Decryptor d = Decryptor.getInstance(info);
    assertEquals(true, d.verifyPassword("password"));
    OPCPackage inpPkg = OPCPackage.open(d.getDataStream(inpFS));
    // Check it now has empty core properties
    assertEquals(1, inpPkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
    assertNotNull(inpPkg.getPackageProperties());
    assertNotNull(inpPkg.getPackageProperties().getLanguageProperty());
    assertNull(inpPkg.getPackageProperties().getLanguageProperty().getValue());
    inpPkg.close();
    inpFS.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) AgileDecryptor(org.apache.poi.poifs.crypt.agile.AgileDecryptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 42 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestPackagingURIHelper method testCreatePartNameRelativeString.

/**
	 * Test createPartName(String, y)
	 */
public void testCreatePartNameRelativeString() throws InvalidFormatException {
    PackagePartName partNameToValid = PackagingURIHelper.createPartName("/word/media/image1.gif");
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    // Base part
    PackagePartName nameBase = PackagingURIHelper.createPartName("/word/document.xml");
    PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
    // Relative part name
    PackagePartName relativeName = PackagingURIHelper.createPartName("media/image1.gif", partBase);
    assertTrue("The part name must be equal to " + partNameToValid.getName(), partNameToValid.equals(relativeName));
    pkg.revert();
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 43 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestOPCComplianceCoreProperties method testNoCoreProperties_saveInPlace.

/**
     * Document with no core properties - testing at the OPC level,
     *  from a temp-file, saving in-place
     */
@Test
public void testNoCoreProperties_saveInPlace() throws Exception {
    String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx";
    // Copy this into a temp file, so we can play with it
    File tmp = TempFile.createTempFile("poi-test", ".opc");
    FileOutputStream out = new FileOutputStream(tmp);
    InputStream in = POIDataSamples.getOpenXML4JInstance().openResourceAsStream(sampleFileName);
    IOUtils.copy(in, out);
    out.close();
    in.close();
    // Open it from that temp file
    OPCPackage pkg = OPCPackage.open(tmp);
    // Empty properties
    assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
    assertNotNull(pkg.getPackageProperties());
    assertNotNull(pkg.getPackageProperties().getLanguageProperty());
    assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
    // Save and close
    pkg.close();
    // Re-open and check
    pkg = OPCPackage.open(tmp);
    // An Empty Properties part has been added in the save/load
    assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
    assertNotNull(pkg.getPackageProperties());
    assertNotNull(pkg.getPackageProperties().getLanguageProperty());
    assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
    // Finish and tidy
    pkg.revert();
    assertTrue(tmp.delete());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 44 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestOPCCompliancePackageModel method testAddPackageAlreadyAddFailure.

/**
     * Rule M1.12 : Packages shall not contain equivalent part names and package
     * implementers shall neither create nor recognize packages with equivalent
     * part names.
     */
@Test
public void testAddPackageAlreadyAddFailure() throws Exception {
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    PackagePartName name1 = null;
    PackagePartName name2 = null;
    try {
        name1 = PackagingURIHelper.createPartName("/word/document.xml");
        name2 = PackagingURIHelper.createPartName("/word/document.xml");
    } catch (InvalidFormatException e) {
        throw new Exception(e.getMessage());
    }
    pkg.createPart(name1, ContentTypes.XML);
    try {
        pkg.createPart(name2, ContentTypes.XML);
    } catch (PartAlreadyExistsException e) {
        return;
    }
    fail("Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) PartAlreadyExistsException(org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException) PartAlreadyExistsException(org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException) Test(org.junit.Test)

Example 45 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestXSSFBSheetHyperlinkManager method testBasic.

@Test
public void testBasic() throws Exception {
    OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("hyperlink.xlsb"));
    XSSFBReader reader = new XSSFBReader(pkg);
    XSSFReader.SheetIterator it = (XSSFReader.SheetIterator) reader.getSheetsData();
    it.next();
    XSSFBHyperlinksTable manager = new XSSFBHyperlinksTable(it.getSheetPart());
    List<XSSFHyperlinkRecord> records = manager.getHyperLinks().get(new CellAddress(0, 0));
    assertNotNull(records);
    assertEquals(1, records.size());
    XSSFHyperlinkRecord record = records.get(0);
    assertEquals("http://tika.apache.org/", record.getLocation());
    assertEquals("rId2", record.getRelId());
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) XSSFBReader(org.apache.poi.xssf.eventusermodel.XSSFBReader) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader) Test(org.junit.Test)

Aggregations

OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)116 Test (org.junit.Test)54 InputStream (java.io.InputStream)29 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)21 File (java.io.File)19 IOException (java.io.IOException)17 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)13 SignatureConfig (org.apache.poi.poifs.crypt.dsig.SignatureConfig)10 SignatureInfo (org.apache.poi.poifs.crypt.dsig.SignatureInfo)10 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)10 OutputStream (java.io.OutputStream)9 ArrayList (java.util.ArrayList)9 FileOutputStream (java.io.FileOutputStream)8 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)8 XmlException (org.apache.xmlbeans.XmlException)8 FileInputStream (java.io.FileInputStream)7