PageRenderTime 6701ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/record/FooterRecord.java

http://github.com/openmicroscopy/bioformats
Java | 236 lines | 124 code | 30 blank | 82 comment | 9 complexity | 1f3080b04ab3b5726623970a8e4634d7 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.hssf.record;
  38. import loci.poi.util.LittleEndian;
  39. import loci.poi.util.StringUtil;
  40. /**
  41. * Title: Footer Record <P>
  42. * Description: Specifies the footer for a sheet<P>
  43. * REFERENCE: PG 317 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  44. * @author Andrew C. Oliver (acoliver at apache dot org)
  45. * @author Shawn Laubach (slaubach at apache dot org) Modified 3/14/02
  46. * @author Jason Height (jheight at chariot dot net dot au)
  47. * @version 2.0-pre
  48. */
  49. public class FooterRecord
  50. extends Record
  51. {
  52. public final static short sid = 0x15;
  53. private byte field_1_footer_len;
  54. private byte field_2_reserved;
  55. private byte field_3_unicode_flag;
  56. private String field_4_footer;
  57. public FooterRecord()
  58. {
  59. }
  60. /**
  61. * Constructs a FooterRecord record and sets its fields appropriately.
  62. * @param in the RecordInputstream to read the record from
  63. */
  64. public FooterRecord(RecordInputStream in)
  65. {
  66. super(in);
  67. }
  68. protected void validateSid(short id)
  69. {
  70. if (id != sid)
  71. {
  72. throw new RecordFormatException("NOT A FooterRECORD");
  73. }
  74. }
  75. protected void fillFields(RecordInputStream in)
  76. {
  77. if (in.remaining() > 0)
  78. {
  79. field_1_footer_len = in.readByte();
  80. /** These two fields are a bit odd. They are not documented*/
  81. field_2_reserved = in.readByte();
  82. field_3_unicode_flag = in.readByte(); // unicode
  83. if(isMultibyte())
  84. {
  85. field_4_footer = in.readUnicodeLEString(LittleEndian.ubyteToInt( field_1_footer_len));
  86. }
  87. else
  88. {
  89. field_4_footer = in.readCompressedUnicode(LittleEndian.ubyteToInt( field_1_footer_len));
  90. }
  91. }
  92. }
  93. /**
  94. * see the unicode flag
  95. *
  96. * @return boolean flag
  97. * true:footer string has at least one multibyte character
  98. */
  99. public boolean isMultibyte() {
  100. return ((field_3_unicode_flag & 0xFF) == 1);
  101. }
  102. /**
  103. * set the length of the footer string
  104. *
  105. * @param len length of the footer string
  106. * @see #setFooter(String)
  107. */
  108. public void setFooterLength(byte len)
  109. {
  110. field_1_footer_len = len;
  111. }
  112. /**
  113. * set the footer string
  114. *
  115. * @param footer string to display
  116. * @see #setFooterLength(byte)
  117. */
  118. public void setFooter(String footer)
  119. {
  120. field_4_footer = footer;
  121. field_3_unicode_flag =
  122. (byte) (StringUtil.hasMultibyte(field_4_footer) ? 1 : 0);
  123. }
  124. /**
  125. * get the length of the footer string
  126. *
  127. * @return length of the footer string
  128. * @see #getFooter()
  129. */
  130. public short getFooterLength()
  131. {
  132. return (short)(0xFF & field_1_footer_len); // [Shawn] Fixed needing unsigned byte
  133. }
  134. /**
  135. * get the footer string
  136. *
  137. * @return footer string to display
  138. * @see #getFooterLength()
  139. */
  140. public String getFooter()
  141. {
  142. return field_4_footer;
  143. }
  144. public String toString()
  145. {
  146. StringBuffer buffer = new StringBuffer();
  147. buffer.append("[FOOTER]\n");
  148. buffer.append(" .footerlen = ")
  149. .append(Integer.toHexString(getFooterLength())).append("\n");
  150. buffer.append(" .footer = ").append(getFooter())
  151. .append("\n");
  152. buffer.append("[/FOOTER]\n");
  153. return buffer.toString();
  154. }
  155. public int serialize(int offset, byte [] data)
  156. {
  157. int len = 4;
  158. if (getFooterLength() > 0)
  159. {
  160. len+=3; // [Shawn] Fixed for two null bytes in the length
  161. }
  162. short bytelen = (short)(isMultibyte() ?
  163. getFooterLength()*2 : getFooterLength() );
  164. LittleEndian.putShort(data, 0 + offset, sid);
  165. LittleEndian.putShort(data, 2 + offset,
  166. ( short ) ((len - 4) + bytelen ));
  167. if (getFooterLength() > 0)
  168. {
  169. data[ 4 + offset ] = (byte)getFooterLength();
  170. data[ 6 + offset ] = field_3_unicode_flag;
  171. if(isMultibyte())
  172. {
  173. StringUtil.putUnicodeLE(getFooter(), data, 7 + offset);
  174. }
  175. else
  176. {
  177. StringUtil.putCompressedUnicode(getFooter(), data, 7 + offset); // [Shawn] Place the string in the correct offset
  178. }
  179. }
  180. return getRecordSize();
  181. }
  182. public int getRecordSize()
  183. {
  184. int retval = 4;
  185. if (getFooterLength() > 0)
  186. {
  187. retval+=3; // [Shawn] Fixed for two null bytes in the length
  188. }
  189. return (isMultibyte() ?
  190. (retval + getFooterLength()*2) : (retval + getFooterLength()));
  191. }
  192. public short getSid()
  193. {
  194. return sid;
  195. }
  196. public Object clone() {
  197. FooterRecord rec = new FooterRecord();
  198. rec.field_1_footer_len = field_1_footer_len;
  199. rec.field_2_reserved = field_2_reserved;
  200. rec.field_3_unicode_flag = field_3_unicode_flag;
  201. rec.field_4_footer = field_4_footer;
  202. return rec;
  203. }
  204. }