PageRenderTime 107ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/ddf/EscherOptRecord.java

http://github.com/openmicroscopy/bioformats
Java | 214 lines | 103 code | 22 blank | 89 comment | 4 complexity | 3308acafb0e4445385ac4670a7f43d75 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.ddf;
  38. import loci.poi.util.LittleEndian;
  39. import loci.poi.util.HexDump;
  40. import java.util.*;
  41. import java.io.IOException;
  42. /**
  43. * The opt record is used to store property values for a shape. It is the key to determining
  44. * the attributes of a shape. Properties can be of two types: simple or complex. Simple types
  45. * are fixed length. Complex properties are variable length.
  46. *
  47. * @author Glen Stampoultzis
  48. */
  49. public class EscherOptRecord
  50. extends EscherRecord
  51. {
  52. public static final short RECORD_ID = (short) 0xF00B;
  53. public static final String RECORD_DESCRIPTION = "msofbtOPT";
  54. private List properties = new ArrayList();
  55. /**
  56. * This method deserializes the record from a byte array.
  57. *
  58. * @param data The byte array containing the escher record information
  59. * @param offset The starting offset into <code>data</code>.
  60. * @param recordFactory May be null since this is not a container record.
  61. * @return The number of bytes read from the byte array.
  62. */
  63. public int fillFields( byte[] data, int offset, EscherRecordFactory recordFactory )
  64. {
  65. int bytesRemaining = readHeader( data, offset );
  66. int pos = offset + 8;
  67. EscherPropertyFactory f = new EscherPropertyFactory();
  68. properties = f.createProperties( data, pos, getInstance() );
  69. return bytesRemaining + 8;
  70. }
  71. /**
  72. * This method serializes this escher record into a byte array.
  73. *
  74. * @param offset The offset into <code>data</code> to start writing the record data to.
  75. * @param data The byte array to serialize to.
  76. * @param listener A listener to retrieve start and end callbacks. Use a <code>NullEscherSerailizationListener</code> to ignore these events.
  77. *
  78. * @return The number of bytes written.
  79. * @see NullEscherSerializationListener
  80. */
  81. public int serialize( int offset, byte[] data, EscherSerializationListener listener )
  82. {
  83. listener.beforeRecordSerialize( offset, getRecordId(), this );
  84. LittleEndian.putShort( data, offset, getOptions() );
  85. LittleEndian.putShort( data, offset + 2, getRecordId() );
  86. LittleEndian.putInt( data, offset + 4, getPropertiesSize() );
  87. int pos = offset + 8;
  88. for ( Iterator iterator = properties.iterator(); iterator.hasNext(); )
  89. {
  90. EscherProperty escherProperty = (EscherProperty) iterator.next();
  91. pos += escherProperty.serializeSimplePart( data, pos );
  92. }
  93. for ( Iterator iterator = properties.iterator(); iterator.hasNext(); )
  94. {
  95. EscherProperty escherProperty = (EscherProperty) iterator.next();
  96. pos += escherProperty.serializeComplexPart( data, pos );
  97. }
  98. listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
  99. return pos - offset;
  100. }
  101. /**
  102. * Returns the number of bytes that are required to serialize this record.
  103. *
  104. * @return Number of bytes
  105. */
  106. public int getRecordSize()
  107. {
  108. return 8 + getPropertiesSize();
  109. }
  110. /**
  111. * Automatically recalculate the correct option
  112. */
  113. public short getOptions()
  114. {
  115. setOptions( (short) ( ( properties.size() << 4 ) | 0x3 ) );
  116. return super.getOptions();
  117. }
  118. /**
  119. * The short name for this record
  120. */
  121. public String getRecordName()
  122. {
  123. return "Opt";
  124. }
  125. private int getPropertiesSize()
  126. {
  127. int totalSize = 0;
  128. for ( Iterator iterator = properties.iterator(); iterator.hasNext(); )
  129. {
  130. EscherProperty escherProperty = (EscherProperty) iterator.next();
  131. totalSize += escherProperty.getPropertySize();
  132. }
  133. return totalSize;
  134. }
  135. /**
  136. * Retrieve the string representation of this record.
  137. */
  138. public String toString()
  139. {
  140. String nl = System.getProperty( "line.separator" );
  141. StringBuffer propertiesBuf = new StringBuffer();
  142. for ( Iterator iterator = properties.iterator(); iterator.hasNext(); )
  143. propertiesBuf.append( " "
  144. + iterator.next().toString()
  145. + nl );
  146. return "loci.poi.ddf.EscherOptRecord:" + nl +
  147. " isContainer: " + isContainerRecord() + nl +
  148. " options: 0x" + HexDump.toHex( getOptions() ) + nl +
  149. " recordId: 0x" + HexDump.toHex( getRecordId() ) + nl +
  150. " numchildren: " + getChildRecords().size() + nl +
  151. " properties:" + nl +
  152. propertiesBuf.toString();
  153. }
  154. /**
  155. * The list of properties stored by this record.
  156. */
  157. public List getEscherProperties()
  158. {
  159. return properties;
  160. }
  161. /**
  162. * The list of properties stored by this record.
  163. */
  164. public EscherProperty getEscherProperty( int index )
  165. {
  166. return (EscherProperty) properties.get( index );
  167. }
  168. /**
  169. * Add a property to this record.
  170. */
  171. public void addEscherProperty( EscherProperty prop )
  172. {
  173. properties.add( prop );
  174. }
  175. /**
  176. * Records should be sorted by property number before being stored.
  177. */
  178. public void sortProperties()
  179. {
  180. Collections.sort( properties, new Comparator()
  181. {
  182. public int compare( Object o1, Object o2 )
  183. {
  184. EscherProperty p1 = (EscherProperty) o1;
  185. EscherProperty p2 = (EscherProperty) o2;
  186. return new Short( p1.getPropertyNumber() ).compareTo( new Short( p2.getPropertyNumber() ) );
  187. }
  188. } );
  189. }
  190. }