Search in sources :

Example 1 with POIFSDocumentPath

use of org.apache.poi.poifs.filesystem.POIFSDocumentPath in project poi by apache.

the class POIFSReader method processProperties.

private void processProperties(final BlockList small_blocks, final BlockList big_blocks, final Iterator<Property> properties, final POIFSDocumentPath path) throws IOException {
    if (!properties.hasNext() && notifyEmptyDirectories) {
        Iterator<POIFSReaderListener> listeners = registry.getListeners(path, ".");
        while (listeners.hasNext()) {
            POIFSReaderListener pl = listeners.next();
            POIFSReaderEvent pe = new POIFSReaderEvent(null, path, null);
            pl.processPOIFSReaderEvent(pe);
        }
        return;
    }
    while (properties.hasNext()) {
        Property property = properties.next();
        String name = property.getName();
        if (property.isDirectory()) {
            POIFSDocumentPath new_path = new POIFSDocumentPath(path, new String[] { name });
            DirectoryProperty dp = (DirectoryProperty) property;
            processProperties(small_blocks, big_blocks, dp.getChildren(), new_path);
        } else {
            int startBlock = property.getStartBlock();
            Iterator<POIFSReaderListener> listeners = registry.getListeners(path, name);
            if (listeners.hasNext()) {
                int size = property.getSize();
                OPOIFSDocument document = null;
                if (property.shouldUseSmallBlocks()) {
                    document = new OPOIFSDocument(name, small_blocks.fetchBlocks(startBlock, -1), size);
                } else {
                    document = new OPOIFSDocument(name, big_blocks.fetchBlocks(startBlock, -1), size);
                }
                while (listeners.hasNext()) {
                    POIFSReaderListener listener = listeners.next();
                    listener.processPOIFSReaderEvent(new POIFSReaderEvent(new DocumentInputStream(document), path, name));
                }
            } else {
                // consume the document's data and discard it
                if (property.shouldUseSmallBlocks()) {
                    small_blocks.fetchBlocks(startBlock, -1);
                } else {
                    big_blocks.fetchBlocks(startBlock, -1);
                }
            }
        }
    }
}
Also used : POIFSDocumentPath(org.apache.poi.poifs.filesystem.POIFSDocumentPath) DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty) OPOIFSDocument(org.apache.poi.poifs.filesystem.OPOIFSDocument) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream) Property(org.apache.poi.poifs.property.Property) DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty) RootProperty(org.apache.poi.poifs.property.RootProperty)

Example 2 with POIFSDocumentPath

use of org.apache.poi.poifs.filesystem.POIFSDocumentPath in project poi by apache.

the class TestPOIFSReaderRegistry method testEmptyRegistry.

/**
     * Test empty registry
     */
public void testEmptyRegistry() {
    POIFSReaderRegistry registry = new POIFSReaderRegistry();
    for (POIFSDocumentPath path : paths) {
        for (String name : names) {
            Iterator<POIFSReaderListener> listeners = registry.getListeners(path, name);
            assertTrue(!listeners.hasNext());
        }
    }
}
Also used : POIFSDocumentPath(org.apache.poi.poifs.filesystem.POIFSDocumentPath)

Example 3 with POIFSDocumentPath

use of org.apache.poi.poifs.filesystem.POIFSDocumentPath in project poi by apache.

the class TreeReaderListener method getNode.

/**
     * <p>Locates the parent node for a document entry in the tree
     * model. If the parent node does not yet exist it will be
     * created, too. This is done recursively, if needed.</p>
     *
     * @param path The tree node for this path is located.
     *
     * @param fsName The name of the POI filesystem. This is just a
     * string which is displayed in the tree at the top lovel.
     *
     * @param root The root node.
     */
private MutableTreeNode getNode(final POIFSDocumentPath path, final String fsName, final MutableTreeNode root) {
    MutableTreeNode n = pathToNode.get(path);
    if (n != null) {
        /* Node found in map, just return it. */
        return n;
    }
    if (path.length() == 0) {
        /* This is the root path of the POI filesystem. Its tree
             * node is resp. must be located below the tree node of
             * the POI filesystem itself. This is a tree node with the
             * POI filesystem's name (this the operating system file's
             * name) as its key it the path-to-node map. */
        n = pathToNode.get(fsName);
        if (n == null) {
            /* A tree node for the POI filesystem does not yet
                 * exist. */
            n = new DefaultMutableTreeNode(fsName);
            pathToNode.put(fsName, n);
            root.insert(n, 0);
        }
        return n;
    }
    /* else - The path is somewhere down in the POI filesystem's
         * hierarchy. We need the tree node of this path's parent
         * and attach our new node to it. */
    final String name = path.getComponent(path.length() - 1);
    final POIFSDocumentPath parentPath = path.getParent();
    final MutableTreeNode parentNode = getNode(parentPath, fsName, root);
    n = new DefaultMutableTreeNode(name);
    pathToNode.put(path, n);
    parentNode.insert(n, 0);
    return n;
}
Also used : POIFSDocumentPath(org.apache.poi.poifs.filesystem.POIFSDocumentPath) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode)

Example 4 with POIFSDocumentPath

use of org.apache.poi.poifs.filesystem.POIFSDocumentPath in project poi by apache.

the class POIFSReader method read.

/**
     * Read from an InputStream and process the documents we get
     *
     * @param stream the InputStream from which to read the data
     *
     * @exception IOException on errors reading, or on invalid data
     */
public void read(final InputStream stream) throws IOException {
    registryClosed = true;
    // read the header block from the stream
    HeaderBlock header_block = new HeaderBlock(stream);
    // read the rest of the stream into blocks
    RawDataBlockList data_blocks = new RawDataBlockList(stream, header_block.getBigBlockSize());
    // set up the block allocation table (necessary for the
    // data_blocks to be manageable
    new BlockAllocationTableReader(header_block.getBigBlockSize(), header_block.getBATCount(), header_block.getBATArray(), header_block.getXBATCount(), header_block.getXBATIndex(), data_blocks);
    // get property table from the document
    PropertyTable properties = new PropertyTable(header_block, data_blocks);
    // process documents
    RootProperty root = properties.getRoot();
    processProperties(SmallBlockTableReader.getSmallDocumentBlocks(header_block.getBigBlockSize(), data_blocks, root, header_block.getSBATStart()), data_blocks, root.getChildren(), new POIFSDocumentPath());
}
Also used : BlockAllocationTableReader(org.apache.poi.poifs.storage.BlockAllocationTableReader) PropertyTable(org.apache.poi.poifs.property.PropertyTable) HeaderBlock(org.apache.poi.poifs.storage.HeaderBlock) POIFSDocumentPath(org.apache.poi.poifs.filesystem.POIFSDocumentPath) RootProperty(org.apache.poi.poifs.property.RootProperty) RawDataBlockList(org.apache.poi.poifs.storage.RawDataBlockList)

Example 5 with POIFSDocumentPath

use of org.apache.poi.poifs.filesystem.POIFSDocumentPath in project poi by apache.

the class TestPOIFSReaderRegistry method testMixedRegistrationOperations.

/**
     * Test mixed registration operations
     */
public void testMixedRegistrationOperations() {
    POIFSReaderRegistry registry = new POIFSReaderRegistry();
    for (int j = 0; j < listeners.length; j++) {
        for (int k = 0; k < paths.length; k++) {
            for (int n = 0; n < names.length; n++) {
                if ((j != k) && (k != n)) {
                    registry.registerListener(listeners[j], paths[k], names[n]);
                }
            }
        }
    }
    for (int k = 0; k < paths.length; k++) {
        for (int n = 0; n < names.length; n++) {
            Iterator<POIFSReaderListener> listeners = registry.getListeners(paths[k], names[n]);
            if (k == n) {
                assertTrue(!listeners.hasNext());
            } else {
                Set<POIFSReaderListener> registeredListeners = new HashSet<POIFSReaderListener>();
                while (listeners.hasNext()) {
                    registeredListeners.add(listeners.next());
                }
                assertEquals(this.listeners.length - 1, registeredListeners.size());
                for (int j = 0; j < this.listeners.length; j++) {
                    if (j == k) {
                        assertTrue(!registeredListeners.contains(this.listeners[j]));
                    } else {
                        assertTrue(registeredListeners.contains(this.listeners[j]));
                    }
                }
            }
        }
    }
    for (POIFSReaderListener listener : listeners) {
        registry.registerListener(listener);
    }
    for (POIFSDocumentPath path : paths) {
        for (String name : names) {
            Iterator<POIFSReaderListener> listeners = registry.getListeners(path, name);
            Set<POIFSReaderListener> registeredListeners = new HashSet<POIFSReaderListener>();
            while (listeners.hasNext()) {
                registeredListeners.add(listeners.next());
            }
            assertEquals(this.listeners.length, registeredListeners.size());
            for (POIFSReaderListener listener : this.listeners) {
                assertTrue(registeredListeners.contains(listener));
            }
        }
    }
}
Also used : POIFSDocumentPath(org.apache.poi.poifs.filesystem.POIFSDocumentPath) HashSet(java.util.HashSet)

Aggregations

POIFSDocumentPath (org.apache.poi.poifs.filesystem.POIFSDocumentPath)5 RootProperty (org.apache.poi.poifs.property.RootProperty)2 HashSet (java.util.HashSet)1 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)1 MutableTreeNode (javax.swing.tree.MutableTreeNode)1 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)1 OPOIFSDocument (org.apache.poi.poifs.filesystem.OPOIFSDocument)1 DirectoryProperty (org.apache.poi.poifs.property.DirectoryProperty)1 Property (org.apache.poi.poifs.property.Property)1 PropertyTable (org.apache.poi.poifs.property.PropertyTable)1 BlockAllocationTableReader (org.apache.poi.poifs.storage.BlockAllocationTableReader)1 HeaderBlock (org.apache.poi.poifs.storage.HeaderBlock)1 RawDataBlockList (org.apache.poi.poifs.storage.RawDataBlockList)1