Search in sources :

Example 1 with POIFSReader

use of org.apache.poi.poifs.eventfilesystem.POIFSReader in project poi by apache.

the class WriteAuthorAndTitle method main.

/**
     * <p>Runs the example program.</p>
     *
     * @param args Command-line arguments. The first command-line argument must
     * be the name of a POI filesystem to read.
     * @throws IOException if any I/O exception occurs.
     */
public static void main(final String[] args) throws IOException {
    /* Check whether we have exactly two command-line arguments. */
    if (args.length != 2) {
        System.err.println("Usage: " + WriteAuthorAndTitle.class.getName() + " originPOIFS destinationPOIFS");
        System.exit(1);
    }
    /* Read the names of the origin and destination POI filesystems. */
    final String srcName = args[0];
    final String dstName = args[1];
    /* Read the origin POIFS using the eventing API. The real work is done
         * in the class ModifySICopyTheRest which is registered here as a
         * POIFSReader. */
    final POIFSReader r = new POIFSReader();
    final ModifySICopyTheRest msrl = new ModifySICopyTheRest(dstName);
    r.registerListener(msrl);
    FileInputStream fis = new FileInputStream(srcName);
    r.read(fis);
    fis.close();
    /* Write the new POIFS to disk. */
    msrl.close();
}
Also used : POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) FileInputStream(java.io.FileInputStream)

Example 2 with POIFSReader

use of org.apache.poi.poifs.eventfilesystem.POIFSReader in project poi by apache.

the class POIBrowser method run.

protected void run(String[] args) {
    addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    /* Create the tree model with a root node. The latter is
         * invisible but it must be present because a tree model
         * always needs a root. */
    rootNode = new DefaultMutableTreeNode("POI Filesystems");
    DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
    /* Create the tree UI element. */
    final JTree treeUI = new JTree(treeModel);
    getContentPane().add(new JScrollPane(treeUI));
    /* Add the POI filesystems to the tree. */
    int displayedFiles = 0;
    for (int i = 0; i < args.length; i++) {
        final String filename = args[i];
        try {
            FileInputStream fis = new FileInputStream(filename);
            POIFSReader r = new POIFSReader();
            r.registerListener(new TreeReaderListener(filename, rootNode));
            r.read(fis);
            fis.close();
            displayedFiles++;
        } catch (IOException ex) {
            System.err.println(filename + ": " + ex);
        } catch (Exception t) {
            System.err.println("Unexpected exception while reading \"" + filename + "\":");
            t.printStackTrace(System.err);
        }
    }
    /* Exit if there is no file to display (none specified or only
         * files with problems). */
    if (displayedFiles == 0) {
        System.out.println("No POI filesystem(s) to display.");
        System.exit(0);
    }
    /* Make the tree UI element visible. */
    treeUI.setRootVisible(true);
    treeUI.setShowsRootHandles(true);
    ExtendableTreeCellRenderer etcr = new ExtendableTreeCellRenderer();
    etcr.register(DocumentDescriptor.class, new DocumentDescriptorRenderer());
    etcr.register(PropertySetDescriptor.class, new PropertySetDescriptorRenderer());
    treeUI.setCellRenderer(etcr);
    setSize(600, 450);
    setTitle("POI Browser 0.09");
    setVisible(true);
}
Also used : JScrollPane(javax.swing.JScrollPane) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) WindowAdapter(java.awt.event.WindowAdapter) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) JTree(javax.swing.JTree) WindowEvent(java.awt.event.WindowEvent) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader)

Example 3 with POIFSReader

use of org.apache.poi.poifs.eventfilesystem.POIFSReader in project poi by apache.

the class TestWrite method writeTwoSections.

/**
     * <p>Writes a simple property set with two sections to a POIFS and reads it
     * back in.</p>
     *
     * @exception IOException if an I/O exception occurs
     * @exception WritingNotSupportedException if HPSF does not yet support
     * a variant type to be written
     */
@Test
public void writeTwoSections() throws WritingNotSupportedException, IOException {
    final String STREAM_NAME = "PropertySetStream";
    final String SECTION1 = "Section 1";
    final String SECTION2 = "Section 2";
    final File dataDir = _samples.getFile("");
    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, 1, 2, 3, 4, 5, 6, 7, 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);
    poiFs.close();
    out.close();
    /* Read the POIFS: */
    final PropertySet[] psa = new PropertySet[1];
    final POIFSReader r = new POIFSReader();
    r.registerListener(new POIFSReaderListener() {

        @Override
        public void processPOIFSReaderEvent(final POIFSReaderEvent event) {
            try {
                psa[0] = PropertySetFactory.create(event.getStream());
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }, STREAM_NAME);
    FileInputStream stream = new FileInputStream(filename);
    try {
        r.read(stream);
    } finally {
        stream.close();
    }
    assertNotNull(psa[0]);
    Section s = (psa[0].getSections().get(0));
    assertEquals(s.getFormatID(), formatID);
    Object p = s.getProperty(2);
    assertEquals(SECTION1, p);
    s = (psa[0].getSections().get(1));
    p = s.getProperty(2);
    assertEquals(SECTION2, p);
}
Also used : MutableSection(org.apache.poi.hpsf.MutableSection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NDocumentOutputStream(org.apache.poi.poifs.filesystem.NDocumentOutputStream) FileOutputStream(java.io.FileOutputStream) ClassID(org.apache.poi.hpsf.ClassID) POIFSReaderListener(org.apache.poi.poifs.eventfilesystem.POIFSReaderListener) MutableSection(org.apache.poi.hpsf.MutableSection) Section(org.apache.poi.hpsf.Section) NoPropertySetStreamException(org.apache.poi.hpsf.NoPropertySetStreamException) WritingNotSupportedException(org.apache.poi.hpsf.WritingNotSupportedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IllegalPropertySetDataException(org.apache.poi.hpsf.IllegalPropertySetDataException) UnsupportedVariantTypeException(org.apache.poi.hpsf.UnsupportedVariantTypeException) IOException(java.io.IOException) NoFormatIDException(org.apache.poi.hpsf.NoFormatIDException) ReadingNotSupportedException(org.apache.poi.hpsf.ReadingNotSupportedException) HPSFException(org.apache.poi.hpsf.HPSFException) FileInputStream(java.io.FileInputStream) POIFSReaderEvent(org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent) 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) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) Test(org.junit.Test)

Example 4 with POIFSReader

use of org.apache.poi.poifs.eventfilesystem.POIFSReader in project poi by apache.

the class ReaderWriter method main.

/**
     * Method main
     *
     *
     * @param args
     *
     * @exception IOException
     *
     */
public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        System.err.println("two arguments required: one input file name and one output file name");
    } else {
        POIFSReader reader = new POIFSReader();
        POIFSFileSystem filesystem = new POIFSFileSystem();
        reader.registerListener(new ReaderWriter(filesystem));
        FileInputStream istream = new FileInputStream(args[0]);
        reader.read(istream);
        istream.close();
        FileOutputStream ostream = new FileOutputStream(args[1]);
        filesystem.writeFilesystem(ostream);
        ostream.close();
        filesystem.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) FileInputStream(java.io.FileInputStream)

Example 5 with POIFSReader

use of org.apache.poi.poifs.eventfilesystem.POIFSReader in project poi by apache.

the class Util method readPropertySets.

/**
     * <p>Read all files from a POI filesystem which are property set streams
     * and returns them as an array of {@link org.apache.poi.hpsf.PropertySet}
     * instances.</p>
     * 
     * @param poiFs The name of the POI filesystem as seen by the
     * operating system. (This is the "filename".)
     *
     * @return The property sets. The elements are ordered in the same way
     * as the files in the POI filesystem.
     * 
     * @exception FileNotFoundException if the file containing the POI 
     * filesystem does not exist
     * 
     * @exception IOException if an I/O exception occurs
     */
public static List<POIFile> readPropertySets(final File poiFs) throws IOException {
    final List<POIFile> files = new ArrayList<POIFile>(7);
    final POIFSReader r = new POIFSReader();
    POIFSReaderListener pfl = new POIFSReaderListener() {

        @Override
        public void processPOIFSReaderEvent(final POIFSReaderEvent event) {
            try {
                final POIFile f = new POIFile();
                f.setName(event.getName());
                f.setPath(event.getPath());
                final InputStream in = event.getStream();
                if (PropertySet.isPropertySetStream(in)) {
                    f.setBytes(IOUtils.toByteArray(in));
                    files.add(f);
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    };
    /* Register the listener for all POI files. */
    r.registerListener(pfl);
    /* Read the POI filesystem. */
    InputStream is = new FileInputStream(poiFs);
    try {
        r.read(is);
    } finally {
        is.close();
    }
    return files;
}
Also used : POIFSReaderEvent(org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) POIFSReaderListener(org.apache.poi.poifs.eventfilesystem.POIFSReaderListener) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Aggregations

FileInputStream (java.io.FileInputStream)11 POIFSReader (org.apache.poi.poifs.eventfilesystem.POIFSReader)11 IOException (java.io.IOException)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 POIFSReaderEvent (org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent)4 POIFSReaderListener (org.apache.poi.poifs.eventfilesystem.POIFSReaderListener)4 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)4 TempFile (org.apache.poi.util.TempFile)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)3 MutableSection (org.apache.poi.hpsf.MutableSection)3 NDocumentOutputStream (org.apache.poi.poifs.filesystem.NDocumentOutputStream)3 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)3 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2