You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PropertyFactory.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.property;
  16. import java.io.IOException;
  17. import java.util.*;
  18. import org.apache.poi.poifs.common.POIFSConstants;
  19. import org.apache.poi.poifs.storage.ListManagedBlock;
  20. /**
  21. * Factory for turning an array of RawDataBlock instances containing
  22. * Property data into an array of proper Property objects.
  23. *
  24. * The array produced may be sparse, in that any portion of data that
  25. * should correspond to a Property, but which does not map to a proper
  26. * Property (i.e., a DirectoryProperty, DocumentProperty, or
  27. * RootProperty) will get mapped to a null Property in the array.
  28. *
  29. * @author Marc Johnson (mjohnson at apache dot org)
  30. */
  31. class PropertyFactory
  32. {
  33. // no need for an accessible constructor
  34. private PropertyFactory()
  35. {
  36. }
  37. /**
  38. * Convert raw data blocks to an array of Property's
  39. *
  40. * @param blocks to be converted
  41. *
  42. * @return the converted List of Property objects. May contain
  43. * nulls, but will not be null
  44. *
  45. * @exception IOException if any of the blocks are empty
  46. */
  47. static List<Property> convertToProperties(ListManagedBlock [] blocks)
  48. throws IOException
  49. {
  50. List<Property> properties = new ArrayList<Property>();
  51. for (int j = 0; j < blocks.length; j++) {
  52. byte[] data = blocks[ j ].getData();
  53. convertToProperties(data, properties);
  54. }
  55. return properties;
  56. }
  57. static void convertToProperties(byte[] data, List<Property> properties)
  58. throws IOException
  59. {
  60. int property_count = data.length / POIFSConstants.PROPERTY_SIZE;
  61. int offset = 0;
  62. for (int k = 0; k < property_count; k++) {
  63. switch (data[ offset + PropertyConstants.PROPERTY_TYPE_OFFSET ]) {
  64. case PropertyConstants.DIRECTORY_TYPE :
  65. properties.add(
  66. new DirectoryProperty(properties.size(), data, offset)
  67. );
  68. break;
  69. case PropertyConstants.DOCUMENT_TYPE :
  70. properties.add(
  71. new DocumentProperty(properties.size(), data, offset)
  72. );
  73. break;
  74. case PropertyConstants.ROOT_TYPE :
  75. properties.add(
  76. new RootProperty(properties.size(), data, offset)
  77. );
  78. break;
  79. default :
  80. properties.add(null);
  81. break;
  82. }
  83. offset += POIFSConstants.PROPERTY_SIZE;
  84. }
  85. }
  86. } // end package scope class PropertyFactory