Search in sources :

Example 21 with POIFSFileSystem

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();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) BigInteger(java.math.BigInteger) LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) Test(org.junit.Test)

Example 22 with POIFSFileSystem

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();
    }
}
Also used : HWPFOldDocument(org.apache.poi.hwpf.HWPFOldDocument) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) File(java.io.File) Test(org.junit.Test)

Example 23 with POIFSFileSystem

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);
}
Also used : HashMap(java.util.HashMap) MutableSection(org.apache.poi.hpsf.MutableSection) NDocumentInputStream(org.apache.poi.poifs.filesystem.NDocumentInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NDocumentOutputStream(org.apache.poi.poifs.filesystem.NDocumentOutputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) FileOutputStream(java.io.FileOutputStream) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) PropertySet(org.apache.poi.hpsf.PropertySet) TempFile(org.apache.poi.util.TempFile) File(java.io.File) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) Test(org.junit.Test)

Example 24 with POIFSFileSystem

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();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Entry(org.apache.poi.poifs.filesystem.Entry) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) Closeable(java.io.Closeable) HSSFObjectData(org.apache.poi.hssf.usermodel.HSSFObjectData) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) FileInputStream(java.io.FileInputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 25 with POIFSFileSystem

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);
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) HPSFPropertiesOnlyDocument(org.apache.poi.hpsf.HPSFPropertiesOnlyDocument)

Aggregations

POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)121 Test (org.junit.Test)58 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)38 InputStream (java.io.InputStream)36 ByteArrayInputStream (java.io.ByteArrayInputStream)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 FileInputStream (java.io.FileInputStream)31 File (java.io.File)25 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)15 FileOutputStream (java.io.FileOutputStream)14 OutputStream (java.io.OutputStream)14 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)13 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)13 TempFile (org.apache.poi.util.TempFile)13 IOException (java.io.IOException)12 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)7 MutableSection (org.apache.poi.hpsf.MutableSection)7 HashMap (java.util.HashMap)6 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)6 NDocumentOutputStream (org.apache.poi.poifs.filesystem.NDocumentOutputStream)6