use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestVariantSupport method newNumberTypes.
@Test
public void newNumberTypes() throws Exception {
ClipboardData cd = new ClipboardData();
cd.setValue(new byte[10]);
Object[][] exp = { { Variant.VT_CF, cd.toByteArray() }, { Variant.VT_BOOL, true }, { Variant.VT_LPSTR, "codepagestring" }, { Variant.VT_LPWSTR, "widestring" }, // int, not short ... :(
{ Variant.VT_I2, -1 }, { Variant.VT_UI2, 0xFFFF }, { Variant.VT_I4, -1 }, { Variant.VT_UI4, 0xFFFFFFFFL }, { Variant.VT_I8, -1L }, { Variant.VT_UI8, BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN) }, { Variant.VT_R4, -999.99f }, { Variant.VT_R8, -999.99d } };
HSSFWorkbook wb = new HSSFWorkbook();
POIFSFileSystem poifs = new POIFSFileSystem();
DocumentSummaryInformation dsi = PropertySetFactory.newDocumentSummaryInformation();
CustomProperties cpList = new CustomProperties();
for (Object[] o : exp) {
int type = (Integer) o[0];
Property p = new Property(PropertyIDMap.PID_MAX + type, type, o[1]);
cpList.put("testprop" + type, new CustomProperty(p, "testprop" + type));
}
dsi.setCustomProperties(cpList);
dsi.write(poifs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
poifs.writeFilesystem(bos);
poifs.close();
poifs = new POIFSFileSystem(new ByteArrayInputStream(bos.toByteArray()));
dsi = (DocumentSummaryInformation) PropertySetFactory.create(poifs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
cpList = dsi.getCustomProperties();
int i = 0;
for (Object[] o : exp) {
Object obj = cpList.get("testprop" + o[0]);
if (o[1] instanceof byte[]) {
assertArrayEquals("property " + i, (byte[]) o[1], (byte[]) obj);
} else {
assertEquals("property " + i, o[1], obj);
}
i++;
}
poifs.close();
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestBugs method test57843.
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void test57843() throws IOException {
File f = POIDataSamples.getDocumentInstance().getFile("57843.doc");
POIFSFileSystem fs = new POIFSFileSystem(f, true);
try {
HWPFOldDocument doc = new HWPFOldDocument(fs);
assertNotNull(doc);
doc.close();
} finally {
fs.close();
}
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestWrite method dictionary.
/**
* <p>Tests writing and reading back a proper dictionary.</p>
* @throws IOException
* @throws HPSFException
*/
@Test
public void dictionary() throws IOException, HPSFException {
final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
copy.deleteOnExit();
/* Write: */
final OutputStream out = new FileOutputStream(copy);
final POIFSFileSystem poiFs = new POIFSFileSystem();
final MutablePropertySet ps1 = new MutablePropertySet();
final MutableSection s = (MutableSection) ps1.getSections().get(0);
final Map<Long, String> m = new HashMap<Long, String>(3, 1.0f);
m.put(Long.valueOf(1), "String 1");
m.put(Long.valueOf(2), "String 2");
m.put(Long.valueOf(3), "String 3");
s.setDictionary(m);
s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
int codepage = CodePageUtil.CP_UNICODE;
s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, codepage);
poiFs.createDocument(ps1.toInputStream(), "Test");
poiFs.writeFilesystem(out);
poiFs.close();
out.close();
/* Read back: */
final List<POIFile> psf = Util.readPropertySets(copy);
assertEquals(1, psf.size());
final byte[] bytes = psf.get(0).getBytes();
final InputStream in = new ByteArrayInputStream(bytes);
final PropertySet ps2 = PropertySetFactory.create(in);
/* Check if the result is a DocumentSummaryInformation stream, as
* specified. */
assertTrue(ps2.isDocumentSummaryInformation());
/* Compare the property set stream with the corresponding one
* from the origin file and check whether they are equal. */
assertEquals(ps1, ps2);
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class EmbeddedObjects method main.
@SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(args[0]));
HSSFWorkbook workbook = new HSSFWorkbook(fs);
for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
//the OLE2 Class Name of the object
String oleName = obj.getOLE2ClassName();
DirectoryNode dn = (obj.hasDirectoryEntry()) ? (DirectoryNode) obj.getDirectory() : null;
Closeable document = null;
if (oleName.equals("Worksheet")) {
document = new HSSFWorkbook(dn, fs, false);
} else if (oleName.equals("Document")) {
document = new HWPFDocument(dn);
} else if (oleName.equals("Presentation")) {
document = new HSLFSlideShow(dn);
} else {
if (dn != null) {
// The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is
for (Entry entry : dn) {
String name = entry.getName();
}
} else {
// There is no DirectoryEntry
// Recover the object's data from the HSSFObjectData instance.
byte[] objectData = obj.getObjectData();
}
}
if (document != null) {
document.close();
}
}
workbook.close();
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class HPSFFileHandler method handleFile.
@Override
public void handleFile(InputStream stream, String path) throws Exception {
Assume.assumeFalse(EXCLUDES_HANDLE_FILE.contains(path));
POIFSFileSystem poifs = new POIFSFileSystem(stream);
HPSFPropertiesOnlyDocument hpsf = new HPSFPropertiesOnlyDocument(poifs);
DocumentSummaryInformation dsi = hpsf.getDocumentSummaryInformation();
SummaryInformation si = hpsf.getSummaryInformation();
boolean hasDSI = hasPropertyStream(poifs, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
boolean hasSI = hasPropertyStream(poifs, SummaryInformation.DEFAULT_STREAM_NAME);
assertEquals(hasDSI, dsi != null);
assertEquals(hasSI, si != null);
handlePOIDocument(hpsf);
}
Aggregations