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.

EscherPropertyFactory.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.ddf;
  16. import org.apache.poi.util.LittleEndian;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. /**
  21. * Generates a property given a reference into the byte array storing that property.
  22. *
  23. * @author Glen Stampoultzis
  24. */
  25. public final class EscherPropertyFactory {
  26. /**
  27. * Create new properties from a byte array.
  28. *
  29. * @param data The byte array containing the property
  30. * @param offset The starting offset into the byte array
  31. * @return The new properties
  32. */
  33. public List<EscherProperty> createProperties(byte[] data, int offset, short numProperties) {
  34. List<EscherProperty> results = new ArrayList<EscherProperty>();
  35. int pos = offset;
  36. // while ( bytesRemaining >= 6 )
  37. for (int i = 0; i < numProperties; i++) {
  38. short propId;
  39. int propData;
  40. propId = LittleEndian.getShort( data, pos );
  41. propData = LittleEndian.getInt( data, pos + 2 );
  42. short propNumber = (short) ( propId & (short) 0x3FFF );
  43. boolean isComplex = ( propId & (short) 0x8000 ) != 0;
  44. boolean isBlipId = ( propId & (short) 0x4000 ) != 0;
  45. byte propertyType = EscherProperties.getPropertyType(propNumber);
  46. if ( propertyType == EscherPropertyMetaData.TYPE_BOOLEAN )
  47. results.add( new EscherBoolProperty( propId, propData ) );
  48. else if ( propertyType == EscherPropertyMetaData.TYPE_RGB )
  49. results.add( new EscherRGBProperty( propId, propData ) );
  50. else if ( propertyType == EscherPropertyMetaData.TYPE_SHAPEPATH )
  51. results.add( new EscherShapePathProperty( propId, propData ) );
  52. else
  53. {
  54. if ( !isComplex )
  55. results.add( new EscherSimpleProperty( propId, propData ) );
  56. else
  57. {
  58. if ( propertyType == EscherPropertyMetaData.TYPE_ARRAY)
  59. results.add( new EscherArrayProperty( propId, new byte[propData]) );
  60. else
  61. results.add( new EscherComplexProperty( propId, new byte[propData]) );
  62. }
  63. }
  64. pos += 6;
  65. // bytesRemaining -= 6 + complexBytes;
  66. }
  67. // Get complex data
  68. for (Iterator<EscherProperty> iterator = results.iterator(); iterator.hasNext();) {
  69. EscherProperty p = iterator.next();
  70. if (p instanceof EscherComplexProperty) {
  71. if (p instanceof EscherArrayProperty) {
  72. pos += ((EscherArrayProperty)p).setArrayData(data, pos);
  73. } else {
  74. byte[] complexData = ((EscherComplexProperty)p).getComplexData();
  75. int leftover = data.length-pos;
  76. if(leftover < complexData.length){
  77. throw new IllegalStateException("Could not read complex escher property, lenght was " + complexData.length + ", but had only " +
  78. leftover + " bytes left");
  79. }
  80. System.arraycopy(data, pos, complexData, 0, complexData.length);
  81. pos += complexData.length;
  82. }
  83. }
  84. }
  85. return results;
  86. }
  87. }