]> source.dussan.org Git - poi.git/commitdiff
POIFS Property refactoring ready for NIO support
authorNick Burch <nick@apache.org>
Mon, 20 Dec 2010 09:06:47 +0000 (09:06 +0000)
committerNick Burch <nick@apache.org>
Mon, 20 Dec 2010 09:06:47 +0000 (09:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1051025 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java
src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java
src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
src/java/org/apache/poi/poifs/property/PropertyFactory.java
src/java/org/apache/poi/poifs/property/PropertyTable.java
src/testcases/org/apache/poi/poifs/property/TestPropertyTable.java
src/testcases/org/apache/poi/poifs/storage/TestSmallBlockTableReader.java

index cd183b3ba4d641c8b1660382cba5d889182c577f..25a28d40cb01b4e80b4308adc290a1ae35d08d80 100644 (file)
@@ -83,10 +83,7 @@ public class POIFSHeaderDumper {
 
       // Properties Table
       PropertyTable properties =
-         new PropertyTable(
-               header_block.getBigBlockSize(),
-               header_block.getPropertyStart(),
-               data_blocks);
+         new PropertyTable(header_block, data_blocks);
       
       // Mini Fat
       BlockList sbat = 
index 450422622794d66d5e0a678b52b73179f8ec04b1..567527523e7cb0beddef4850145dd295c5b0682c 100644 (file)
@@ -78,7 +78,7 @@ public class POIFSReader
         HeaderBlock header_block = new HeaderBlock(stream);
 
         // read the rest of the stream into blocks
-        RawDataBlockList  data_blocks         = new RawDataBlockList(stream, header_block.getBigBlockSize());
+        RawDataBlockList data_blocks = new RawDataBlockList(stream, header_block.getBigBlockSize());
 
         // set up the block allocation table (necessary for the
         // data_blocks to be manageable
@@ -91,9 +91,7 @@ public class POIFSReader
 
         // get property table from the document
         PropertyTable properties =
-            new PropertyTable(header_block.getBigBlockSize(),
-                              header_block.getPropertyStart(),
-                              data_blocks);
+            new PropertyTable(header_block, data_blocks);
 
         // process documents
         processProperties(SmallBlockTableReader
index aa8e2966db37f0dfeedceada1e2895245b554e6d..c217f025f43cf7725ca140a67594533d8e10520e 100644 (file)
@@ -156,9 +156,7 @@ public class POIFSFileSystem
 
         // get property table from the document
         PropertyTable properties =
-            new PropertyTable(bigBlockSize,
-                              header_block.getPropertyStart(),
-                              data_blocks);
+            new PropertyTable(header_block, data_blocks);
 
         // init documents
         processProperties(
index 7dc92df2e6080ddf509f00945536d60db324f460..38533b71a7b8771b98923ab441324e55feeae550 100644 (file)
@@ -40,7 +40,6 @@ import org.apache.poi.poifs.storage.ListManagedBlock;
 
 class PropertyFactory
 {
-
     // no need for an accessible constructor
     private PropertyFactory()
     {
@@ -56,48 +55,52 @@ class PropertyFactory
      *
      * @exception IOException if any of the blocks are empty
      */
-
-    static List convertToProperties(ListManagedBlock [] blocks)
+    static List<Property> convertToProperties(ListManagedBlock [] blocks)
         throws IOException
     {
-        List properties = new ArrayList();
-
-        for (int j = 0; j < blocks.length; j++)
-        {
-            byte[] data           = blocks[ j ].getData();
-            int    property_count = data.length
-                                    / POIFSConstants.PROPERTY_SIZE;
-            int    offset         = 0;
-
-            for (int k = 0; k < property_count; k++)
-            {
-                switch (data[ offset + PropertyConstants.PROPERTY_TYPE_OFFSET ])
-                {
-
-                    case PropertyConstants.DIRECTORY_TYPE :
-                        properties
-                            .add(new DirectoryProperty(properties.size(),
-                                                       data, offset));
-                        break;
-
-                    case PropertyConstants.DOCUMENT_TYPE :
-                        properties.add(new DocumentProperty(properties.size(),
-                                                            data, offset));
-                        break;
-
-                    case PropertyConstants.ROOT_TYPE :
-                        properties.add(new RootProperty(properties.size(),
-                                                        data, offset));
-                        break;
-
-                    default :
-                        properties.add(null);
-                        break;
-                }
-                offset += POIFSConstants.PROPERTY_SIZE;
-            }
+        List<Property> properties = new ArrayList<Property>();
+
+        for (int j = 0; j < blocks.length; j++) {
+            byte[] data = blocks[ j ].getData();
+            convertToProperties(data, properties);
         }
         return properties;
     }
+    
+    static void convertToProperties(byte[] data, List<Property> properties)
+        throws IOException
+    {
+       int property_count = data.length / POIFSConstants.PROPERTY_SIZE;
+       int offset         = 0;
+
+       for (int k = 0; k < property_count; k++) {
+          switch (data[ offset + PropertyConstants.PROPERTY_TYPE_OFFSET ]) {
+          case PropertyConstants.DIRECTORY_TYPE :
+             properties.add(
+                   new DirectoryProperty(properties.size(), data, offset)
+             );
+             break;
+
+          case PropertyConstants.DOCUMENT_TYPE :
+             properties.add(
+                   new DocumentProperty(properties.size(), data, offset)
+             );
+             break;
+
+          case PropertyConstants.ROOT_TYPE :
+             properties.add(
+                   new RootProperty(properties.size(), data, offset)
+             );
+             break;
+
+          default :
+             properties.add(null);
+             break;
+          }
+          
+          offset += POIFSConstants.PROPERTY_SIZE;
+       }
+    }
+    
 }   // end package scope class PropertyFactory
 
index cce2a5a16e96157f9bd0cbe5c3f3ef2e0eace603..03475bd3a9cf9807b3c95c9f5e4c58517cd87714 100644 (file)
@@ -27,6 +27,7 @@ import org.apache.poi.poifs.common.POIFSBigBlockSize;
 import org.apache.poi.poifs.common.POIFSConstants;
 import org.apache.poi.poifs.filesystem.BATManaged;
 import org.apache.poi.poifs.storage.BlockWritable;
+import org.apache.poi.poifs.storage.HeaderBlock;
 import org.apache.poi.poifs.storage.PropertyBlock;
 import org.apache.poi.poifs.storage.RawDataBlockList;
 
@@ -63,17 +64,16 @@ public final class PropertyTable implements BATManaged, BlockWritable {
      * @exception IOException if anything goes wrong (which should be
      *            a result of the input being NFG)
      */
-    public PropertyTable(final POIFSBigBlockSize bigBlockSize,
-                         final int startBlock,
+    public PropertyTable(final HeaderBlock headerBlock,
                          final RawDataBlockList blockList)
         throws IOException
     {
-        _bigBigBlockSize = bigBlockSize;
+        _bigBigBlockSize = headerBlock.getBigBlockSize();
         _start_block = POIFSConstants.END_OF_CHAIN;
         _blocks      = null;
-        _properties  =
-            PropertyFactory
-                .convertToProperties(blockList.fetchBlocks(startBlock, -1));
+        _properties  = PropertyFactory.convertToProperties(
+              blockList.fetchBlocks(headerBlock.getPropertyStart(), -1)
+        );
         populatePropertyTree(( DirectoryProperty ) _properties.get(0));
     }
 
@@ -114,7 +114,7 @@ public final class PropertyTable implements BATManaged, BlockWritable {
      */
     public void preWrite()
     {
-        Property[] properties = _properties.toArray(new Property[ 0 ]);
+        Property[] properties = _properties.toArray(new Property[_properties.size()]);
 
         // give each property its index
         for (int k = 0; k < properties.length; k++)
index 0e404d48816f74635f662b84c691b69a707363ee..8865eb3f57a877bbf83409fed614d81e2916c020 100644 (file)
@@ -27,6 +27,7 @@ import junit.framework.TestCase;
 
 import org.apache.poi.poifs.common.POIFSConstants;
 import org.apache.poi.poifs.storage.BlockAllocationTableReader;
+import org.apache.poi.poifs.storage.HeaderBlock;
 import org.apache.poi.poifs.storage.RawDataBlockList;
 import org.apache.poi.poifs.storage.RawDataUtil;
 
@@ -438,9 +439,12 @@ public final class TestPropertyTable extends TestCase {
                new BlockAllocationTableReader(
                      POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, 1, bat_array, 0, -2, data_blocks);
 
+      // Fake up a header
+      HeaderBlock header_block = new HeaderBlock(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);
+      header_block.setPropertyStart(0);
+      
                // get property table from the document
-               PropertyTable table = new PropertyTable(
-                     POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, 0, data_blocks);
+               PropertyTable table = new PropertyTable(header_block, data_blocks);
 
                assertEquals(30 * 64, table.getRoot().getSize());
                int count = 0;
index 3d254f82616b17769e41d386a79e0a737bd13dc3..ee8ac39d594373eb5f025af38a67a11c70e45a74 100644 (file)
@@ -302,9 +302,13 @@ public final class TestSmallBlockTableReader extends TestCase {
                // need to initialize the block list with a block allocation
                // table
                new BlockAllocationTableReader(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, 1, bat_array, 0, -2, data_blocks);
+               
+               // Fake up a header
+               HeaderBlock header_block = new HeaderBlock(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);
+               header_block.setPropertyStart(0);
 
                // get property table from the document
-               PropertyTable properties = new PropertyTable(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, 0, data_blocks);
+               PropertyTable properties = new PropertyTable(header_block, data_blocks);
                RootProperty root = properties.getRoot();
                BlockList bl = SmallBlockTableReader.getSmallDocumentBlocks(
                      POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, data_blocks, root, 14);