Package org.apache.poi.poifs.filesystem

Examples of org.apache.poi.poifs.filesystem.POIFSFileSystem


        filename.deleteOnExit();

        /* Create a mutable property set with a section that does not have the
         * formatID set: */
        final OutputStream out = new FileOutputStream(filename);
        final POIFSFileSystem poiFs = new POIFSFileSystem();
        final MutablePropertySet ps = new MutablePropertySet();
        ps.clearSections();
        ps.addSection(new MutableSection());

        /* Write it to a POIFS and the latter to disk: */
        try
        {
            final ByteArrayOutputStream psStream = new ByteArrayOutputStream();
            ps.write(psStream);
            psStream.close();
            final byte[] streamData = psStream.toByteArray();
            poiFs.createDocument(new ByteArrayInputStream(streamData),
                                 SummaryInformation.DEFAULT_STREAM_NAME);
            poiFs.writeFilesystem(out);
            out.close();
            Assert.fail("Should have thrown a NoFormatIDException.");
        }
        catch (Exception ex)
        {
View Full Code Here


        final File filename = new File(dataDir, POI_FS);
        filename.deleteOnExit();

        /* Create a mutable property set and write it to a POIFS: */
        final OutputStream out = new FileOutputStream(filename);
        final POIFSFileSystem poiFs = new POIFSFileSystem();
        final MutablePropertySet ps = new MutablePropertySet();
        final MutableSection s = (MutableSection) ps.getSections().get(0);
        s.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);

        final ByteArrayOutputStream psStream = new ByteArrayOutputStream();
        ps.write(psStream);
        psStream.close();
        final byte[] streamData = psStream.toByteArray();
        poiFs.createDocument(new ByteArrayInputStream(streamData),
                             SummaryInformation.DEFAULT_STREAM_NAME);
        poiFs.writeFilesystem(out);
        out.close();

        /* Read the POIFS: */
        final POIFSReader r = new POIFSReader();
        r.registerListener(new MyPOIFSReaderListener(),
View Full Code Here

        final File dataDir =
            new File(System.getProperty("HPSF.testdata.path"));
        final File filename = new File(dataDir, POI_FS);
        filename.deleteOnExit();
        final OutputStream out = new FileOutputStream(filename);
        final POIFSFileSystem poiFs = new POIFSFileSystem();
   
        final MutablePropertySet ps = new MutablePropertySet();
        final MutableSection si = new MutableSection();
        si.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
        ps.getSections().set(0, si);
   
        final MutableProperty p = new MutableProperty();
        p.setID(PropertyIDMap.PID_AUTHOR);
        p.setType(Variant.VT_LPWSTR);
        p.setValue(AUTHOR);
        si.setProperty(p);
        si.setProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPSTR, TITLE);
   
        poiFs.createDocument(ps.toInputStream(),
                             SummaryInformation.DEFAULT_STREAM_NAME);
        poiFs.writeFilesystem(out);
        out.close();
   
        /* Read the POIFS: */
        final PropertySet[] psa = new PropertySet[1];
        final POIFSReader r = new POIFSReader();
View Full Code Here

            new File(System.getProperty("HPSF.testdata.path"));
        final File filename = new File(dataDir, POI_FS);
        filename.deleteOnExit();
        final OutputStream out = new FileOutputStream(filename);

        final POIFSFileSystem poiFs = new POIFSFileSystem();
        final MutablePropertySet ps = new MutablePropertySet();
        ps.clearSections();

        final ClassID formatID = new ClassID();
        formatID.setBytes(new byte[]{0, 1234567,
                                     8, 9, 10, 11, 12, 13, 14, 15});
        final MutableSection s1 = new MutableSection();
        s1.setFormatID(formatID);
        s1.setProperty(2, SECTION1);
        ps.addSection(s1);

        final MutableSection s2 = new MutableSection();
        s2.setFormatID(formatID);
        s2.setProperty(2, SECTION2);
        ps.addSection(s2);

        poiFs.createDocument(ps.toInputStream(), STREAM_NAME);
        poiFs.writeFilesystem(out);
        out.close();

        /* Read the POIFS: */
        final PropertySet[] psa = new PropertySet[1];
        final POIFSReader r = new POIFSReader();
View Full Code Here

            /* Create a new POI filesystem containing the origin file's
             * property set streams: */
            final File copy = TempFile.createTempFile(f.getName(), "");
            copy.deleteOnExit();
            final OutputStream out = new FileOutputStream(copy);
            final POIFSFileSystem poiFs = new POIFSFileSystem();
            for (int i = 0; i < psf1.length; i++)
            {
                final InputStream in =
                    new ByteArrayInputStream(psf1[i].getBytes());
                final PropertySet psIn = PropertySetFactory.create(in);
                final MutablePropertySet psOut = new MutablePropertySet(psIn);
                final ByteArrayOutputStream psStream =
                    new ByteArrayOutputStream();
                psOut.write(psStream);
                psStream.close();
                final byte[] streamData = psStream.toByteArray();
                poiFs.createDocument(new ByteArrayInputStream(streamData),
                                     psf1[i].getName());
                poiFs.writeFilesystem(out);
            }
            out.close();


            /* Read the property set streams from the POI filesystem just
View Full Code Here

            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 m = new HashMap(3, 1.0f);
            m.put(new Long(1), "String 1");
            m.put(new Long(2), "String 2");
            m.put(new Long(3), "String 3");
            s.setDictionary(m);
            s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
            int codepage = Constants.CP_UNICODE;
            s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,
                          new Integer(codepage));
            poiFs.createDocument(ps1.toInputStream(), "Test");
            poiFs.writeFilesystem(out);
            out.close();

            /* Read back: */
            final POIFile[] psf = Util.readPropertySets(copy);
            Assert.assertEquals(1, psf.length);
View Full Code Here

            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 m = new HashMap(3, 1.0f);
            m.put(new Long(1), "String 1");
            m.put(new Long(2), "String 2");
            m.put(new Long(3), "String 3");
            s.setDictionary(m);
            s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
            int codepage = 12345;
            s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,
                          new Integer(codepage));
            poiFs.createDocument(ps1.toInputStream(), "Test");
            poiFs.writeFilesystem(out);
            out.close();
            fail("This testcase did not detect the invalid codepage value.");
        }
        catch (IllegalPropertySetDataException ex)
        {
View Full Code Here

        {
            final File doc = docs[i];
            System.out.println("Reading file " + doc);

            /* Read a test document <em>doc</em> into a POI filesystem. */
            final POIFSFileSystem poifs = new POIFSFileSystem(new FileInputStream(doc));
            final DirectoryEntry dir = poifs.getRoot();
            DocumentEntry dsiEntry = null;
            try
            {
                dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            }
View Full Code Here

        final String dataDirName = System.getProperty("HPSF.testdata.path");
        final File dataDir = new File(dataDirName);
        final File doc1 = new File(dataDir, POI_FS);
   
        /* Read a test document <em>doc1</em> into a POI filesystem. */
        POIFSFileSystem poifs = new POIFSFileSystem(new FileInputStream(doc1));
        DirectoryEntry dir = poifs.getRoot();
        DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        DocumentEntry dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
   
        /*
         * Read the summary information stream and the document summary
         * information stream from the POI filesystem.
         *
         * Please note that the result consists of SummaryInformation and
         * DocumentSummaryInformation instances which are in memory only. To
         * make them permanent they have to be written to a POI filesystem
         * explicitly (overwriting the former contents). Then the POI filesystem
         * should be saved to a file.
         */
        DocumentInputStream dis = new DocumentInputStream(siEntry);
        PropertySet ps = new PropertySet(dis);
        SummaryInformation si = new SummaryInformation(ps);
        dis = new DocumentInputStream(dsiEntry);
        ps = new PropertySet(dis);
        DocumentSummaryInformation dsi = new DocumentSummaryInformation(ps);
   
        /*
         * Write all properties supported by HPSF to the summary information
         * (e.g. author, edit date, application name) and to the document
         * summary information (e.g. company, manager).
         */
        Calendar cal = new GregorianCalendar();
        cal.set(2000, 6, 6, 6, 6, 6);
        final long time1 = cal.getTimeInMillis();
        cal.set(2001, 7, 7, 7, 7, 7);
        final long time2 = cal.getTimeInMillis();
        cal.set(2002, 8, 8, 8, 8, 8);
        final long time3 = cal.getTimeInMillis();
   
        int nr = 4711;
        final String P_APPLICATION_NAME = "ApplicationName";
        final String P_AUTHOR = "Author";
        final int    P_CHAR_COUNT = ++nr;
        final String P_COMMENTS = "Comments";
        final Date   P_CREATE_DATE_TIME = new Date(time1);
        final long   P_EDIT_TIME = ++nr * 1000 * 10;
        final String P_KEYWORDS = "Keywords";
        final String P_LAST_AUTHOR = "LastAuthor";
        final Date   P_LAST_PRINTED = new Date(time2);
        final Date   P_LAST_SAVE_DATE_TIME = new Date(time3);
        final int    P_PAGE_COUNT = ++nr;
        final String P_REV_NUMBER = "RevNumber";
        final int    P_SECURITY = 1;
        final String P_SUBJECT = "Subject";
        final String P_TEMPLATE = "Template";
        // FIXME (byte array properties not yet implemented): final byte[] P_THUMBNAIL = new byte[123];
        final String P_TITLE = "Title";
        final int    P_WORD_COUNT = ++nr;
   
        final int     P_BYTE_COUNT = ++nr;
        final String  P_CATEGORY = "Category";
        final String  P_COMPANY = "Company";
        // FIXME (byte array properties not yet implemented): final byte[]  P_DOCPARTS = new byte[123];
        // FIXME (byte array properties not yet implemented): final byte[]  P_HEADING_PAIR = new byte[123];
        final int     P_HIDDEN_COUNT = ++nr;
        final int     P_LINE_COUNT = ++nr;
        final boolean P_LINKS_DIRTY = true;
        final String  P_MANAGER = "Manager";
        final int     P_MM_CLIP_COUNT = ++nr;
        final int     P_NOTE_COUNT = ++nr;
        final int     P_PAR_COUNT = ++nr;
        final String  P_PRESENTATION_FORMAT = "PresentationFormat";
        final boolean P_SCALE = false;
        final int     P_SLIDE_COUNT = ++nr;
        final Date    now = new Date();

        final Integer POSITIVE_INTEGER = new Integer(2222);
        final Long POSITIVE_LONG = new  Long(3333);
        final Double POSITIVE_DOUBLE = new  Double(4444);
        final Integer NEGATIVE_INTEGER = new Integer(2222);
        final Long NEGATIVE_LONG = new  Long(3333);
        final Double NEGATIVE_DOUBLE = new  Double(4444);

        final Integer MAX_INTEGER = new Integer(Integer.MAX_VALUE);
        final Integer MIN_INTEGER = new Integer(Integer.MIN_VALUE);
        final Long MAX_LONG = new Long(Long.MAX_VALUE);
        final Long MIN_LONG = new Long(Long.MIN_VALUE);
        final Double MAX_DOUBLE = new Double(Double.MAX_VALUE);
        final Double MIN_DOUBLE = new Double(Double.MIN_VALUE);
       
        si.setApplicationName(P_APPLICATION_NAME);
        si.setAuthor(P_AUTHOR);
        si.setCharCount(P_CHAR_COUNT);
        si.setComments(P_COMMENTS);
        si.setCreateDateTime(P_CREATE_DATE_TIME);
        si.setEditTime(P_EDIT_TIME);
        si.setKeywords(P_KEYWORDS);
        si.setLastAuthor(P_LAST_AUTHOR);
        si.setLastPrinted(P_LAST_PRINTED);
        si.setLastSaveDateTime(P_LAST_SAVE_DATE_TIME);
        si.setPageCount(P_PAGE_COUNT);
        si.setRevNumber(P_REV_NUMBER);
        si.setSecurity(P_SECURITY);
        si.setSubject(P_SUBJECT);
        si.setTemplate(P_TEMPLATE);
        // FIXME (byte array properties not yet implemented): si.setThumbnail(P_THUMBNAIL);
        si.setTitle(P_TITLE);
        si.setWordCount(P_WORD_COUNT);
   
        dsi.setByteCount(P_BYTE_COUNT);
        dsi.setCategory(P_CATEGORY);
        dsi.setCompany(P_COMPANY);
        // FIXME (byte array properties not yet implemented): dsi.setDocparts(P_DOCPARTS);
        // FIXME (byte array properties not yet implemented): dsi.setHeadingPair(P_HEADING_PAIR);
        dsi.setHiddenCount(P_HIDDEN_COUNT);
        dsi.setLineCount(P_LINE_COUNT);
        dsi.setLinksDirty(P_LINKS_DIRTY);
        dsi.setManager(P_MANAGER);
        dsi.setMMClipCount(P_MM_CLIP_COUNT);
        dsi.setNoteCount(P_NOTE_COUNT);
        dsi.setParCount(P_PAR_COUNT);
        dsi.setPresentationFormat(P_PRESENTATION_FORMAT);
        dsi.setScale(P_SCALE);
        dsi.setSlideCount(P_SLIDE_COUNT);

        CustomProperties customProperties = dsi.getCustomProperties();
        if (customProperties == null)
            customProperties = new CustomProperties();
        customProperties.put("Schl�ssel �",    "Wert �");
        customProperties.put("Schl�ssel ��",   "Wert ��");
        customProperties.put("Schl�ssel ���""Wert ���");
        customProperties.put("Schl�ssel ����", "Wert ����");
        customProperties.put("positive_Integer", POSITIVE_INTEGER);
        customProperties.put("positive_Long", POSITIVE_LONG);
        customProperties.put("positive_Double", POSITIVE_DOUBLE);
        customProperties.put("negative_Integer", NEGATIVE_INTEGER);
        customProperties.put("negative_Long", NEGATIVE_LONG);
        customProperties.put("negative_Double", NEGATIVE_DOUBLE);
        customProperties.put("Boolean", new Boolean(true));
        customProperties.put("Date", now);
        customProperties.put("max_Integer", MAX_INTEGER);
        customProperties.put("min_Integer", MIN_INTEGER);
        customProperties.put("max_Long", MAX_LONG);
        customProperties.put("min_Long", MIN_LONG);
        customProperties.put("max_Double", MAX_DOUBLE);
        customProperties.put("min_Double", MIN_DOUBLE);
        dsi.setCustomProperties(customProperties);

        /* Write the summary information stream and the document summary
         * information stream to the POI filesystem. */
        si.write(dir, siEntry.getName());
        dsi.write(dir, dsiEntry.getName());

        /* Write the POI filesystem to a (temporary) file <em>doc2</em>
         * and close the latter. */
        final File doc2 = File.createTempFile("POI_HPSF_Test.", ".tmp");
        doc2.deleteOnExit();
        OutputStream out = new FileOutputStream(doc2);
        poifs.writeFilesystem(out);
        out.close();

        /*
         * Open <em>doc2</em> for reading and check summary information and
         * document summary information. All properties written before must be
         * found in the property streams of <em>doc2</em> and have the correct
         * values.
         */
        poifs = new POIFSFileSystem(new FileInputStream(doc2));
        dir = poifs.getRoot();
        siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
   
        dis = new DocumentInputStream(siEntry);
        ps = new PropertySet(dis);
        si = new SummaryInformation(ps);
        dis = new DocumentInputStream(dsiEntry);
        ps = new PropertySet(dis);
        dsi = new DocumentSummaryInformation(ps);
   
        assertEquals(P_APPLICATION_NAME, si.getApplicationName());
        assertEquals(P_AUTHOR, si.getAuthor());
        assertEquals(P_CHAR_COUNT, si.getCharCount());
        assertEquals(P_COMMENTS, si.getComments());
        assertEquals(P_CREATE_DATE_TIME, si.getCreateDateTime());
        assertEquals(P_EDIT_TIME, si.getEditTime());
        assertEquals(P_KEYWORDS, si.getKeywords());
        assertEquals(P_LAST_AUTHOR, si.getLastAuthor());
        assertEquals(P_LAST_PRINTED, si.getLastPrinted());
        assertEquals(P_LAST_SAVE_DATE_TIME, si.getLastSaveDateTime());
        assertEquals(P_PAGE_COUNT, si.getPageCount());
        assertEquals(P_REV_NUMBER, si.getRevNumber());
        assertEquals(P_SECURITY, si.getSecurity());
        assertEquals(P_SUBJECT, si.getSubject());
        assertEquals(P_TEMPLATE, si.getTemplate());
        // FIXME (byte array properties not yet implemented): assertEquals(P_THUMBNAIL, si.getThumbnail());
        assertEquals(P_TITLE, si.getTitle());
        assertEquals(P_WORD_COUNT, si.getWordCount());
   
        assertEquals(P_BYTE_COUNT, dsi.getByteCount());
        assertEquals(P_CATEGORY, dsi.getCategory());
        assertEquals(P_COMPANY, dsi.getCompany());
        // FIXME (byte array properties not yet implemented): assertEquals(P_, dsi.getDocparts());
        // FIXME (byte array properties not yet implemented): assertEquals(P_, dsi.getHeadingPair());
        assertEquals(P_HIDDEN_COUNT, dsi.getHiddenCount());
        assertEquals(P_LINE_COUNT, dsi.getLineCount());
        assertEquals(P_LINKS_DIRTY, dsi.getLinksDirty());
        assertEquals(P_MANAGER, dsi.getManager());
        assertEquals(P_MM_CLIP_COUNT, dsi.getMMClipCount());
        assertEquals(P_NOTE_COUNT, dsi.getNoteCount());
        assertEquals(P_PAR_COUNT, dsi.getParCount());
        assertEquals(P_PRESENTATION_FORMAT, dsi.getPresentationFormat());
        assertEquals(P_SCALE, dsi.getScale());
        assertEquals(P_SLIDE_COUNT, dsi.getSlideCount());

        final CustomProperties cps = dsi.getCustomProperties();
        assertEquals(customProperties, cps);
        assertNull(cps.get("No value available"));
        assertEquals("Wert �", cps.get("Schl�ssel �"));
        assertEquals("Wert ��", cps.get("Schl�ssel ��"));
        assertEquals("Wert ���", cps.get("Schl�ssel ���"));
        assertEquals("Wert ����", cps.get("Schl�ssel ����"));
        assertEquals(POSITIVE_INTEGER, cps.get("positive_Integer"));
        assertEquals(POSITIVE_LONG, cps.get("positive_Long"));
        assertEquals(POSITIVE_DOUBLE, cps.get("positive_Double"));
        assertEquals(NEGATIVE_INTEGER, cps.get("negative_Integer"));
        assertEquals(NEGATIVE_LONG, cps.get("negative_Long"));
        assertEquals(NEGATIVE_DOUBLE, cps.get("negative_Double"));
        assertEquals(new Boolean(true), cps.get("Boolean"));
        assertEquals(now, cps.get("Date"));
        assertEquals(MAX_INTEGER, cps.get("max_Integer"));
        assertEquals(MIN_INTEGER, cps.get("min_Integer"));
        assertEquals(MAX_LONG, cps.get("max_Long"));
        assertEquals(MIN_LONG, cps.get("min_Long"));
        assertEquals(MAX_DOUBLE, cps.get("max_Double"));
        assertEquals(MIN_DOUBLE, cps.get("min_Double"));

        /* Remove all properties supported by HPSF from the summary
         * information (e.g. author, edit date, application name) and from the
         * document summary information (e.g. company, manager). */
        si.removeApplicationName();
        si.removeAuthor();
        si.removeCharCount();
        si.removeComments();
        si.removeCreateDateTime();
        si.removeEditTime();
        si.removeKeywords();
        si.removeLastAuthor();
        si.removeLastPrinted();
        si.removeLastSaveDateTime();
        si.removePageCount();
        si.removeRevNumber();
        si.removeSecurity();
        si.removeSubject();
        si.removeTemplate();
        si.removeThumbnail();
        si.removeTitle();
        si.removeWordCount();
   
        dsi.removeByteCount();
        dsi.removeCategory();
        dsi.removeCompany();
        dsi.removeCustomProperties();
        dsi.removeDocparts();
        dsi.removeHeadingPair();
        dsi.removeHiddenCount();
        dsi.removeLineCount();
        dsi.removeLinksDirty();
        dsi.removeManager();
        dsi.removeMMClipCount();
        dsi.removeNoteCount();
        dsi.removeParCount();
        dsi.removePresentationFormat();
        dsi.removeScale();
        dsi.removeSlideCount();
   
        /*
         * <li><p>Write the summary information stream and the document summary
         * information stream to the POI filesystem. */
        si.write(dir, siEntry.getName());
        dsi.write(dir, dsiEntry.getName());
   
        /*
         * <li><p>Write the POI filesystem to a (temporary) file <em>doc3</em>
         * and close the latter. */
        final File doc3 = File.createTempFile("POI_HPSF_Test.", ".tmp");
        doc3.deleteOnExit();
        out = new FileOutputStream(doc3);
        poifs.writeFilesystem(out);
        out.close();
   
        /*
         * Open <em>doc3</em> for reading and check summary information
         * and document summary information. All properties removed before must not
         * be found in the property streams of <em>doc3</em>.
         */
        poifs = new POIFSFileSystem(new FileInputStream(doc3));
        dir = poifs.getRoot();
        siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
   
        dis = new DocumentInputStream(siEntry);
        ps = new PropertySet(dis);
View Full Code Here

                    IOException, NoPropertySetStreamException,
                    MarkUnsupportedException,
                    UnexpectedPropertySetTypeException
            {
                /* Read a test document <em>doc</em> into a POI filesystem. */
                final POIFSFileSystem poifs = new POIFSFileSystem(new FileInputStream(file));
                final DirectoryEntry dir = poifs.getRoot();
                DocumentEntry dsiEntry = null;
                try
                {
                    dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
                }
View Full Code Here

TOP

Related Classes of org.apache.poi.poifs.filesystem.POIFSFileSystem

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.