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.

SubRecord.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.hssf.record;
  16. import java.io.ByteArrayOutputStream;
  17. import org.apache.poi.hssf.record.formula.Area3DPtg;
  18. import org.apache.poi.hssf.record.formula.AreaPtg;
  19. import org.apache.poi.hssf.record.formula.Ptg;
  20. import org.apache.poi.hssf.record.formula.Ref3DPtg;
  21. import org.apache.poi.hssf.record.formula.RefPtg;
  22. import org.apache.poi.util.HexDump;
  23. import org.apache.poi.util.LittleEndianByteArrayInputStream;
  24. import org.apache.poi.util.LittleEndianInput;
  25. import org.apache.poi.util.LittleEndianOutput;
  26. import org.apache.poi.util.LittleEndianOutputStream;
  27. /**
  28. * Subrecords are part of the OBJ class.
  29. */
  30. public abstract class SubRecord {
  31. protected SubRecord() {
  32. // no fields to initialise
  33. }
  34. public static SubRecord createSubRecord(LittleEndianInput in) {
  35. int sid = in.readUShort();
  36. int secondUShort = in.readUShort(); // Often (but not always) the datasize for the sub-record
  37. switch (sid) {
  38. case CommonObjectDataSubRecord.sid:
  39. return new CommonObjectDataSubRecord(in, secondUShort);
  40. case EmbeddedObjectRefSubRecord.sid:
  41. return new EmbeddedObjectRefSubRecord(in, secondUShort);
  42. case GroupMarkerSubRecord.sid:
  43. return new GroupMarkerSubRecord(in, secondUShort);
  44. case EndSubRecord.sid:
  45. return new EndSubRecord(in, secondUShort);
  46. case NoteStructureSubRecord.sid:
  47. return new NoteStructureSubRecord(in, secondUShort);
  48. case LbsDataSubRecord.sid:
  49. return new LbsDataSubRecord(in, secondUShort);
  50. }
  51. return new UnknownSubRecord(in, sid, secondUShort);
  52. }
  53. /**
  54. * @return the size of the data for this record (which is always 4 bytes less than the total
  55. * record size). Note however, that ushort encoded after the record sid is usually but not
  56. * always the data size.
  57. */
  58. protected abstract int getDataSize();
  59. public byte[] serialize() {
  60. int size = getDataSize() + 4;
  61. ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
  62. serialize(new LittleEndianOutputStream(baos));
  63. if (baos.size() != size) {
  64. throw new RuntimeException("write size mismatch");
  65. }
  66. return baos.toByteArray();
  67. }
  68. public abstract void serialize(LittleEndianOutput out);
  69. public abstract Object clone();
  70. private static final class UnknownSubRecord extends SubRecord {
  71. private final int _sid;
  72. private final byte[] _data;
  73. public UnknownSubRecord(LittleEndianInput in, int sid, int size) {
  74. _sid = sid;
  75. byte[] buf = new byte[size];
  76. in.readFully(buf);
  77. _data = buf;
  78. }
  79. protected int getDataSize() {
  80. return _data.length;
  81. }
  82. public void serialize(LittleEndianOutput out) {
  83. out.writeShort(_sid);
  84. out.writeShort(_data.length);
  85. out.write(_data);
  86. }
  87. public Object clone() {
  88. return this;
  89. }
  90. public String toString() {
  91. StringBuffer sb = new StringBuffer(64);
  92. sb.append(getClass().getName()).append(" [");
  93. sb.append("sid=").append(HexDump.shortToHex(_sid));
  94. sb.append(" size=").append(_data.length);
  95. sb.append(" : ").append(HexDump.toHex(_data));
  96. sb.append("]\n");
  97. return sb.toString();
  98. }
  99. }
  100. // TODO make into a top level class
  101. // perhaps all SubRecord sublcasses could go to their own package
  102. private static final class LbsDataSubRecord extends SubRecord {
  103. public static final int sid = 0x0013;
  104. private int _unknownShort1;
  105. private int _unknownInt4;
  106. private Ptg _linkPtg;
  107. private Byte _unknownByte6;
  108. private int _nEntryCount;
  109. private int _selectedEntryIndex;
  110. private int _style;
  111. private int _unknownShort10;
  112. private int _comboStyle;
  113. private int _lineCount;
  114. private int _unknownShort13;
  115. public LbsDataSubRecord(LittleEndianInput in, int unknownShort1) {
  116. _unknownShort1 = unknownShort1;
  117. int linkSize = in.readUShort();
  118. if (linkSize > 0) {
  119. int formulaSize = in.readUShort();
  120. _unknownInt4 = in.readInt();
  121. byte[] buf = new byte[formulaSize];
  122. in.readFully(buf);
  123. _linkPtg = readRefPtg(buf);
  124. switch (linkSize - formulaSize - 6) {
  125. case 1:
  126. _unknownByte6 = new Byte(in.readByte());
  127. break;
  128. case 0:
  129. _unknownByte6 = null;
  130. break;
  131. default:
  132. throw new RecordFormatException("Unexpected leftover bytes");
  133. }
  134. } else {
  135. _unknownInt4 = 0;
  136. _linkPtg = null;
  137. _unknownByte6 = null;
  138. }
  139. _nEntryCount = in.readUShort();
  140. _selectedEntryIndex = in.readUShort();
  141. _style = in.readUShort();
  142. _unknownShort10 = in.readUShort();
  143. _comboStyle = in.readUShort();
  144. _lineCount = in.readUShort();
  145. _unknownShort13 = in.readUShort();
  146. }
  147. protected int getDataSize() {
  148. int result = 2; // 2 initial shorts
  149. // optional link formula
  150. if (_linkPtg != null) {
  151. result += 2; // encoded Ptg size
  152. result += 4; // unknown int
  153. result += _linkPtg.getSize();
  154. if (_unknownByte6 != null) {
  155. result += 1;
  156. }
  157. }
  158. result += 7 * 2; // 7 shorts
  159. return result;
  160. }
  161. public void serialize(LittleEndianOutput out) {
  162. out.writeShort(sid);
  163. out.writeShort(_unknownShort1); // note - this is *not* the size
  164. if (_linkPtg == null) {
  165. out.writeShort(0);
  166. } else {
  167. int formulaSize = _linkPtg.getSize();
  168. int linkSize = formulaSize + 6;
  169. if (_unknownByte6 != null) {
  170. linkSize++;
  171. }
  172. out.writeShort(linkSize);
  173. out.writeShort(formulaSize);
  174. out.writeInt(_unknownInt4);
  175. _linkPtg.write(out);
  176. if (_unknownByte6 != null) {
  177. out.writeByte(_unknownByte6.intValue());
  178. }
  179. }
  180. out.writeShort(_nEntryCount);
  181. out.writeShort(_selectedEntryIndex);
  182. out.writeShort(_style);
  183. out.writeShort(_unknownShort10);
  184. out.writeShort(_comboStyle);
  185. out.writeShort(_lineCount);
  186. out.writeShort(_unknownShort13);
  187. }
  188. private static Ptg readRefPtg(byte[] formulaRawBytes) {
  189. LittleEndianInput in = new LittleEndianByteArrayInputStream(formulaRawBytes);
  190. byte ptgSid = in.readByte();
  191. switch(ptgSid) {
  192. case AreaPtg.sid: return new AreaPtg(in);
  193. case Area3DPtg.sid: return new Area3DPtg(in);
  194. case RefPtg.sid: return new RefPtg(in);
  195. case Ref3DPtg.sid: return new Ref3DPtg(in);
  196. }
  197. return null;
  198. }
  199. public Object clone() {
  200. return this;
  201. }
  202. public String toString() {
  203. StringBuffer sb = new StringBuffer(256);
  204. sb.append("[ftLbsData]\n");
  205. sb.append(" .unknownShort1 =").append(HexDump.shortToHex(_unknownShort1)).append("\n");
  206. if (_linkPtg == null) {
  207. sb.append(" <no link formula>\n");
  208. } else {
  209. sb.append(" .unknownInt4 =").append(HexDump.intToHex(_unknownInt4)).append("\n");
  210. sb.append(" .linkPtg =").append(_linkPtg.toFormulaString()).append(" (").append(_linkPtg.getRVAType()).append(")").append("\n");
  211. if (_unknownByte6 != null) {
  212. sb.append(" .unknownByte6 =").append(HexDump.byteToHex(_unknownByte6.byteValue())).append("\n");
  213. }
  214. }
  215. sb.append(" .nEntryCount =").append(HexDump.shortToHex(_nEntryCount)).append("\n");
  216. sb.append(" .selEntryIx =").append(HexDump.shortToHex(_selectedEntryIndex)).append("\n");
  217. sb.append(" .style =").append(HexDump.shortToHex(_style)).append("\n");
  218. sb.append(" .unknownShort10=").append(HexDump.shortToHex(_unknownShort10)).append("\n");
  219. sb.append(" .comboStyle =").append(HexDump.shortToHex(_comboStyle)).append("\n");
  220. sb.append(" .lineCount =").append(HexDump.shortToHex(_lineCount)).append("\n");
  221. sb.append(" .unknownShort13=").append(HexDump.shortToHex(_unknownShort13)).append("\n");
  222. sb.append("[/ftLbsData]\n");
  223. return sb.toString();
  224. }
  225. }
  226. }