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