Package org.apache.poi.hpsf

Examples of org.apache.poi.hpsf.SummaryInformation


            DocumentEntry dsiEntry = (DocumentEntry)
                dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            DocumentInputStream dis = new DocumentInputStream(dsiEntry);
            PropertySet ps = new PropertySet(dis);
            dis.close();
            si = new SummaryInformation(ps);
         
           
        }
        catch (FileNotFoundException ex)
        {
View Full Code Here


        p.add(text);

        if (ps instanceof SummaryInformation)
        {
            /* Use the convenience methods. */
            final SummaryInformation si = (SummaryInformation) ps;
            text.append("\n");
            text.append("\nTitle:               " + si.getTitle());
            text.append("\nSubject:             " + si.getSubject());
            text.append("\nAuthor:              " + si.getAuthor());
            text.append("\nKeywords:            " + si.getKeywords());
            text.append("\nComments:            " + si.getComments());
            text.append("\nTemplate:            " + si.getTemplate());
            text.append("\nLast Author:         " + si.getLastAuthor());
            text.append("\nRev. Number:         " + si.getRevNumber());
            text.append("\nEdit Time:           " + si.getEditTime());
            text.append("\nLast Printed:        " + si.getLastPrinted());
            text.append("\nCreate Date/Time:    " + si.getCreateDateTime());
            text.append("\nLast Save Date/Time: " + si.getLastSaveDateTime());
            text.append("\nPage Count:          " + si.getPageCount());
            text.append("\nWord Count:          " + si.getWordCount());
            text.append("\nChar Count:          " + si.getCharCount());
            // text.append("\nThumbnail:           " + si.getThumbnail());
            text.append("\nApplication Name:    " + si.getApplicationName());
            text.append("\nSecurity:            " + si.getSecurity());
        }

        if (selected)
            Util.invert(text);
        return p;
View Full Code Here

    static class MyPOIFSReaderListener implements POIFSReaderListener
    {
        public void processPOIFSReaderEvent(final POIFSReaderEvent event)
        {
            SummaryInformation si = null;
            try
            {
                si = (SummaryInformation)
                    PropertySetFactory.create(event.getStream());
            }
            catch (Exception ex)
            {
                throw new RuntimeException
                    ("Property set stream \"" +
                     event.getPath() + event.getName() + "\": " + ex);
            }
            final String title = si.getTitle();
            if (title != null)
                System.out.println("Title: \"" + title + "\"");
            else
                System.out.println("Document has no title.");
        }
View Full Code Here

        POIFSFileSystem poifs = new POIFSFileSystem(is);
        is.close();

        /* Read the summary information. */
        DirectoryEntry dir = poifs.getRoot();
        SummaryInformation si;
        try
        {
            DocumentEntry siEntry = (DocumentEntry)
                dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            DocumentInputStream dis = new DocumentInputStream(siEntry);
            PropertySet ps = new PropertySet(dis);
            dis.close();
            si = new SummaryInformation(ps);
        }
        catch (FileNotFoundException ex)
        {
            /* There is no summary information yet. We have to create a new
             * one. */
            si = PropertySetFactory.newSummaryInformation();
        }

        /* Change the author to "Rainer Klute". Any former author value will
         * be lost. If there has been no author yet, it will be created. */
        si.setAuthor("Rainer Klute");
        System.out.println("Author changed to " + si.getAuthor() + ".");


        /* Handling the document summary information is analogous to handling
         * the summary information. An additional feature, however, are the
         * custom properties. */

        /* Read the document summary information. */
        DocumentSummaryInformation dsi;
        try
        {
            DocumentEntry dsiEntry = (DocumentEntry)
                dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            DocumentInputStream dis = new DocumentInputStream(dsiEntry);
            PropertySet ps = new PropertySet(dis);
            dis.close();
            dsi = new DocumentSummaryInformation(ps);
        }
        catch (FileNotFoundException ex)
        {
            /* There is no document summary information yet. We have to create a
             * new one. */
            dsi = PropertySetFactory.newDocumentSummaryInformation();
        }

        /* Change the category to "POI example". Any former category value will
         * be lost. If there has been no category yet, it will be created. */
        dsi.setCategory("POI example");
        System.out.println("Category changed to " + dsi.getCategory() + ".");

        /* Read the custom properties. If there are no custom properties yet,
         * the application has to create a new CustomProperties object. It will
         * serve as a container for custom properties. */
        CustomProperties customProperties = dsi.getCustomProperties();
        if (customProperties == null)
            customProperties = new CustomProperties();
       
        /* Insert some custom properties into the container. */
        customProperties.put("Key 1", "Value 1");
        customProperties.put("Schl�ssel 2", "Wert 2");
        customProperties.put("Sample Number", new Integer(12345));
        customProperties.put("Sample Boolean", new Boolean(true));
        customProperties.put("Sample Date", new Date());

        /* Read a custom property. */
        Object value = customProperties.get("Sample Number");

        /* Write the custom properties back to the document summary
         * information. */
        dsi.setCustomProperties(customProperties);

        /* Write the summary information and the document summary information
         * to the POI filesystem. */
        si.write(dir, SummaryInformation.DEFAULT_STREAM_NAME);
        dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);

        /* Write the POI filesystem back to the original file. Please note that
         * in production code you should never write directly to the origin
         * file! In case of a writing error everything would be lost. */
 
View Full Code Here

        // All done
        return text.toString();
    }
    public String getSummaryInformationText() {
        SummaryInformation si = document.getSummaryInformation();

        // Just normal properties
        return getPropertiesText(si);
    }
View Full Code Here

       DocumentInputStream dis;
       POIFSFileSystem fs =
               new POIFSFileSystem(_samples.openResourceAsStream("TestNon4ByteBoundary.doc"));
      
       dis = fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME);
       SummaryInformation si = (SummaryInformation)PropertySetFactory.create(dis);
      
       dis = fs.createDocumentInputStream(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
       DocumentSummaryInformation dsi = (DocumentSummaryInformation)PropertySetFactory.create(dis);
     
       // Test
       assertEquals("Microsoft Word 10.0", si.getApplicationName());
       assertEquals("", si.getTitle());
       assertEquals("", si.getAuthor());
       assertEquals("Cour de Justice", dsi.getCompany());
      
      
       // Write out and read back, should still be valid
       POIDocument doc = new HPSFPropertiesOnlyDocument(fs);
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       doc.write(baos);
       ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
       doc = new HPSFPropertiesOnlyDocument(new POIFSFileSystem(bais));
      
       // Check properties are still there
       assertEquals("Microsoft Word 10.0", si.getApplicationName());
       assertEquals("", si.getTitle());
       assertEquals("", si.getAuthor());
       assertEquals("Cour de Justice", dsi.getCompany());
   }
View Full Code Here

     * Writes out the standard Documment Information Properties (HPSF)
     * @param outFS the POIFSFileSystem to write the properties into
     * @param writtenEntries a list of POIFS entries to add the property names too
     */
    protected void writeProperties(POIFSFileSystem outFS, List<String> writtenEntries) throws IOException {
        SummaryInformation si = getSummaryInformation();
        if (si != null) {
            writePropertySet(SummaryInformation.DEFAULT_STREAM_NAME, si, outFS);
            if(writtenEntries != null) {
                writtenEntries.add(SummaryInformation.DEFAULT_STREAM_NAME);
            }
View Full Code Here

         * 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\u00fcssel \u00e4",    "Wert \u00e4");
        customProperties.put("Schl\u00fcssel \u00e4\u00f6",   "Wert \u00e4\u00f6");
        customProperties.put("Schl\u00fcssel \u00e4\u00f6\u00fc""Wert \u00e4\u00f6\u00fc");
        customProperties.put("Schl\u00fcssel \u00e4\u00f6\u00fc\u00d6", "Wert \u00e4\u00f6\u00fc\u00d6");
        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", 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);
       
        // Check the keys went in
        assertTrue(customProperties.containsKey("Schl\u00fcssel \u00e4"));
        assertTrue(customProperties.containsKey("Boolean"));
       
        // Check the values went in
        assertEquals("Wert \u00e4", customProperties.get("Schl\u00fcssel \u00e4"));
        assertEquals(Boolean.TRUE, customProperties.get("Boolean"));
        assertTrue(customProperties.containsValue(Boolean.TRUE));
        assertTrue(customProperties.containsValue("Wert \u00e4"));
       
        // Check that things that aren't in aren't in
        assertFalse(customProperties.containsKey("False Boolean"));
        assertFalse(customProperties.containsValue(Boolean.FALSE));

        // Save as our custom properties
        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 = TempFile.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 \u00e4", cps.get("Schl\u00fcssel \u00e4"));
        assertEquals("Wert \u00e4\u00f6", cps.get("Schl\u00fcssel \u00e4\u00f6"));
        assertEquals("Wert \u00e4\u00f6\u00fc", cps.get("Schl\u00fcssel \u00e4\u00f6\u00fc"));
        assertEquals("Wert \u00e4\u00f6\u00fc\u00d6", cps.get("Schl\u00fcssel \u00e4\u00f6\u00fc\u00d6"));
        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(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 = TempFile.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);
        si = new SummaryInformation(ps);
        dis = new DocumentInputStream(dsiEntry);
        ps = new PropertySet(dis);
        dsi = new DocumentSummaryInformation(ps);

        assertEquals(null, si.getApplicationName());
        assertEquals(null, si.getAuthor());
        assertEquals(0, si.getCharCount());
        assertTrue(si.wasNull());
        assertEquals(null, si.getComments());
        assertEquals(null, si.getCreateDateTime());
        assertEquals(0, si.getEditTime());
        assertTrue(si.wasNull());
        assertEquals(null, si.getKeywords());
        assertEquals(null, si.getLastAuthor());
        assertEquals(null, si.getLastPrinted());
        assertEquals(null, si.getLastSaveDateTime());
        assertEquals(0, si.getPageCount());
        assertTrue(si.wasNull());
        assertEquals(null, si.getRevNumber());
        assertEquals(0, si.getSecurity());
        assertTrue(si.wasNull());
        assertEquals(null, si.getSubject());
        assertEquals(null, si.getTemplate());
        assertEquals(null, si.getThumbnail());
        assertEquals(null, si.getTitle());
        assertEquals(0, si.getWordCount());
        assertTrue(si.wasNull());

        assertEquals(0, dsi.getByteCount());
        assertTrue(dsi.wasNull());
        assertEquals(null, dsi.getCategory());
        assertEquals(null, dsi.getCustomProperties());
View Full Code Here

        try {
            NPOIFSFileSystem fs = null;
            DirectoryEntry root = null;
            DocumentNode sinfDoc = null;
            DocumentNode dinfDoc = null;
            SummaryInformation sinf = null;
            DocumentSummaryInformation dinf = null;
           
            // We need to work on a File for in-place changes, so create a temp one
            final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
            copy.deleteOnExit();
           
            // Copy a test file over to our temp location
            InputStream inp = _samples.openResourceAsStream("TestShiftJIS.doc");
            FileOutputStream out = new FileOutputStream(copy);
            IOUtils.copy(inp, out);
            inp.close();
            out.close();
           
           
            // Open the copy in read/write mode
            fs = new NPOIFSFileSystem(copy, false);
            root = fs.getRoot();
           
           
            // Read the properties in there
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
   
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
           
           
            // Check they start as we expect
            assertEquals("Reiichiro Hori", sinf.getAuthor());
            assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
            assertEquals("\u7b2c1\u7ae0", sinf.getTitle());
           
            assertEquals("", dinf.getCompany());
            assertEquals(null, dinf.getManager());
           
           
            // Do an in-place replace via an InputStream
            new NPOIFSDocument(sinfDoc).replaceContents(sinf.toInputStream());
            new NPOIFSDocument(dinfDoc).replaceContents(dinf.toInputStream());
           
           
            // Check it didn't get changed
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
           
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
   
           
            // Start again!
            fs.close();
            inp = _samples.openResourceAsStream("TestShiftJIS.doc");
            out = new FileOutputStream(copy);
            IOUtils.copy(inp, out);
            inp.close();
            out.close();
           
            fs = new NPOIFSFileSystem(copy, false);
            root = fs.getRoot();
           
            // Read the properties in once more
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
   
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
           
           
            // Have them write themselves in-place with no changes, as an OutputStream
            sinf.write(new NDocumentOutputStream(sinfDoc));
            dinf.write(new NDocumentOutputStream(dinfDoc));
           
            // And also write to some bytes for checking
            ByteArrayOutputStream sinfBytes = new ByteArrayOutputStream();
            sinf.write(sinfBytes);
            ByteArrayOutputStream dinfBytes = new ByteArrayOutputStream();
            dinf.write(dinfBytes);
           
           
            // Check that the filesystem can give us back the same bytes
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
   
            byte[] sinfData = IOUtils.toByteArray(new NDocumentInputStream(sinfDoc));
            byte[] dinfData = IOUtils.toByteArray(new NDocumentInputStream(dinfDoc));
            assertThat(sinfBytes.toByteArray(), equalTo(sinfData));
            assertThat(dinfBytes.toByteArray(), equalTo(dinfData));
   
           
            // Read back in as-is
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
           
            assertEquals("Reiichiro Hori", sinf.getAuthor());
            assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
            assertEquals("\u7b2c1\u7ae0", sinf.getTitle());
           
            assertEquals("", dinf.getCompany());
            assertEquals(null, dinf.getManager());
           
   
            // Now alter a few of them
            sinf.setAuthor("Changed Author");
            sinf.setTitle("Le titre \u00e9tait chang\u00e9");
            dinf.setManager("Changed Manager");
           
           
            // Save this into the filesystem
            sinf.write(new NDocumentOutputStream(sinfDoc));
            dinf.write(new NDocumentOutputStream(dinfDoc));
           
           
            // Read them back in again
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
   
            assertEquals("Changed Author", sinf.getAuthor());
            assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
            assertEquals("Le titre \u00e9tait chang\u00e9", sinf.getTitle());
           
            assertEquals("", dinf.getCompany());
            assertEquals("Changed Manager", dinf.getManager());
   
           
            // Close the whole filesystem, and open it once more
            fs.writeFilesystem();
            fs.close();
           
            fs = new NPOIFSFileSystem(copy);
            root = fs.getRoot();
           
            // Re-check on load
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
   
            assertEquals("Changed Author", sinf.getAuthor());
            assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
            assertEquals("Le titre \u00e9tait chang\u00e9", sinf.getTitle());
           
            assertEquals("", dinf.getCompany());
            assertEquals("Changed Manager", dinf.getManager());
           
           
View Full Code Here

        DocumentEntry dsiEntry = (DocumentEntry)
            dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        DocumentInputStream dis = new DocumentInputStream(dsiEntry);
        PropertySet ps = new PropertySet(dis);
        dis.close();
        si = new SummaryInformation(ps);
     
       
    }
    catch (FileNotFoundException ex)
    {
View Full Code Here

TOP

Related Classes of org.apache.poi.hpsf.SummaryInformation

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.