1 2 17 18 19 package org.apache.poi.poifs.property; 20 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 24 import java.util.*; 25 26 import org.apache.poi.poifs.common.POIFSConstants; 27 import org.apache.poi.poifs.filesystem.BATManaged; 28 import org.apache.poi.poifs.storage.BlockWritable; 29 import org.apache.poi.poifs.storage.PropertyBlock; 30 import org.apache.poi.poifs.storage.RawDataBlock; 31 import org.apache.poi.poifs.storage.RawDataBlockList; 32 33 40 41 public class PropertyTable 42 implements BATManaged, BlockWritable 43 { 44 private int _start_block; 45 private List _properties; 46 private BlockWritable[] _blocks; 47 48 51 52 public PropertyTable() 53 { 54 _start_block = POIFSConstants.END_OF_CHAIN; 55 _properties = new ArrayList(); 56 addProperty(new RootProperty()); 57 _blocks = null; 58 } 59 60 71 72 public PropertyTable(final int startBlock, 73 final RawDataBlockList blockList) 74 throws IOException 75 { 76 _start_block = POIFSConstants.END_OF_CHAIN; 77 _blocks = null; 78 _properties = 79 PropertyFactory 80 .convertToProperties(blockList.fetchBlocks(startBlock)); 81 populatePropertyTree(( DirectoryProperty ) _properties.get(0)); 82 } 83 84 89 90 public void addProperty(final Property property) 91 { 92 _properties.add(property); 93 } 94 95 100 101 public void removeProperty(final Property property) 102 { 103 _properties.remove(property); 104 } 105 106 111 112 public RootProperty getRoot() 113 { 114 115 return ( RootProperty ) _properties.get(0); 117 } 118 119 122 123 public void preWrite() 124 { 125 Property[] properties = 126 ( Property [] ) _properties.toArray(new Property[ 0 ]); 127 128 for (int k = 0; k < properties.length; k++) 130 { 131 properties[ k ].setIndex(k); 132 } 133 134 _blocks = PropertyBlock.createPropertyBlockArray(_properties); 136 137 for (int k = 0; k < properties.length; k++) 139 { 140 properties[ k ].preWrite(); 141 } 142 } 143 144 149 150 public int getStartBlock() 151 { 152 return _start_block; 153 } 154 155 private void populatePropertyTree(DirectoryProperty root) 156 throws IOException 157 { 158 int index = root.getChildIndex(); 159 160 if (!Property.isValidIndex(index)) 161 { 162 163 return; 165 } 166 Stack children = new Stack(); 167 168 children.push(_properties.get(index)); 169 while (!children.empty()) 170 { 171 Property property = ( Property ) children.pop(); 172 173 root.addChild(property); 174 if (property.isDirectory()) 175 { 176 populatePropertyTree(( DirectoryProperty ) property); 177 } 178 index = property.getPreviousChildIndex(); 179 if (Property.isValidIndex(index)) 180 { 181 children.push(_properties.get(index)); 182 } 183 index = property.getNextChildIndex(); 184 if (Property.isValidIndex(index)) 185 { 186 children.push(_properties.get(index)); 187 } 188 } 189 } 190 191 192 193 198 199 public int countBlocks() 200 { 201 return (_blocks == null) ? 0 202 : _blocks.length; 203 } 204 205 211 212 public void setStartBlock(final int index) 213 { 214 _start_block = index; 215 } 216 217 218 219 220 229 230 public void writeBlocks(final OutputStream stream) 231 throws IOException 232 { 233 if (_blocks != null) 234 { 235 for (int j = 0; j < _blocks.length; j++) 236 { 237 _blocks[ j ].writeBlocks(stream); 238 } 239 } 240 } 241 242 243 } 245 | Popular Tags |