Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ExtRst.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.common;
  16. import java.util.Arrays;
  17. import java.util.Map;
  18. import java.util.function.Supplier;
  19. import java.util.stream.Stream;
  20. import org.apache.poi.common.usermodel.GenericRecord;
  21. import org.apache.poi.hssf.record.cont.ContinuableRecordOutput;
  22. import org.apache.poi.util.GenericRecordUtil;
  23. import org.apache.poi.util.IOUtils;
  24. import org.apache.poi.util.Internal;
  25. import org.apache.poi.util.LittleEndianInput;
  26. import org.apache.poi.util.POILogFactory;
  27. import org.apache.poi.util.POILogger;
  28. import org.apache.poi.util.StringUtil;
  29. @Internal
  30. public class ExtRst implements Comparable<ExtRst>, GenericRecord {
  31. private static final POILogger LOG = POILogFactory.getLogger(ExtRst.class);
  32. //arbitrarily selected; may need to increase
  33. private static final int MAX_RECORD_LENGTH = 100_000;
  34. private short reserved;
  35. // This is a Phs (see page 881)
  36. private short formattingFontIndex;
  37. private short formattingOptions;
  38. // This is a RPHSSub (see page 894)
  39. private int numberOfRuns;
  40. private String phoneticText;
  41. // This is an array of PhRuns (see page 881)
  42. private PhRun[] phRuns;
  43. // Sometimes there's some cruft at the end
  44. private byte[] extraData;
  45. protected ExtRst() {
  46. populateEmpty();
  47. }
  48. protected ExtRst(ExtRst other) {
  49. this();
  50. reserved = other.reserved;
  51. formattingFontIndex = other.formattingFontIndex;
  52. formattingOptions = other.formattingOptions;
  53. numberOfRuns = other.numberOfRuns;
  54. phoneticText = other.phoneticText;
  55. phRuns = (other.phRuns == null) ? null : Stream.of(other.phRuns).map(PhRun::new).toArray(PhRun[]::new);
  56. }
  57. protected ExtRst(LittleEndianInput in, int expectedLength) {
  58. reserved = in.readShort();
  59. // Old style detection (Reserved = 0xFF)
  60. if(reserved == -1) {
  61. populateEmpty();
  62. return;
  63. }
  64. // Spot corrupt records
  65. if(reserved != 1) {
  66. LOG.log(POILogger.WARN, "Warning - ExtRst has wrong magic marker, expecting 1 but found ", reserved, " - ignoring");
  67. // Grab all the remaining data, and ignore it
  68. for(int i=0; i<expectedLength-2; i++) {
  69. in.readByte();
  70. }
  71. // And make us be empty
  72. populateEmpty();
  73. return;
  74. }
  75. // Carry on reading in as normal
  76. short stringDataSize = in.readShort();
  77. formattingFontIndex = in.readShort();
  78. formattingOptions = in.readShort();
  79. // RPHSSub
  80. numberOfRuns = in.readUShort();
  81. short length1 = in.readShort();
  82. // No really. Someone clearly forgot to read
  83. // the docs on their datastructure...
  84. short length2 = in.readShort();
  85. // And sometimes they write out garbage :(
  86. if(length1 == 0 && length2 > 0) {
  87. length2 = 0;
  88. }
  89. if(length1 != length2) {
  90. throw new IllegalStateException(
  91. "The two length fields of the Phonetic Text don't agree! " +
  92. length1 + " vs " + length2
  93. );
  94. }
  95. phoneticText = StringUtil.readUnicodeLE(in, length1);
  96. int runData = stringDataSize - 4 - 6 - (2*phoneticText.length());
  97. int numRuns = (runData / 6);
  98. phRuns = new PhRun[numRuns];
  99. for(int i=0; i<phRuns.length; i++) {
  100. phRuns[i] = new PhRun(in);
  101. }
  102. int extraDataLength = runData - (numRuns*6);
  103. if(extraDataLength < 0) {
  104. LOG.log( POILogger.WARN, "Warning - ExtRst overran by ", (0-extraDataLength), " bytes");
  105. extraDataLength = 0;
  106. }
  107. extraData = IOUtils.safelyAllocate(extraDataLength, MAX_RECORD_LENGTH);
  108. for(int i=0; i<extraData.length; i++) {
  109. extraData[i] = in.readByte();
  110. }
  111. }
  112. private void populateEmpty() {
  113. reserved = 1;
  114. phoneticText = "";
  115. phRuns = new PhRun[0];
  116. extraData = new byte[0];
  117. }
  118. /**
  119. * Returns our size, excluding our
  120. * 4 byte header
  121. */
  122. protected int getDataSize() {
  123. return 4 + 6 + (2*phoneticText.length()) +
  124. (6*phRuns.length) + extraData.length;
  125. }
  126. protected void serialize(ContinuableRecordOutput out) {
  127. int dataSize = getDataSize();
  128. out.writeContinueIfRequired(8);
  129. out.writeShort(reserved);
  130. out.writeShort(dataSize);
  131. out.writeShort(formattingFontIndex);
  132. out.writeShort(formattingOptions);
  133. out.writeContinueIfRequired(6);
  134. out.writeShort(numberOfRuns);
  135. out.writeShort(phoneticText.length());
  136. out.writeShort(phoneticText.length());
  137. out.writeContinueIfRequired(phoneticText.length()*2);
  138. StringUtil.putUnicodeLE(phoneticText, out);
  139. for (PhRun phRun : phRuns) {
  140. phRun.serialize(out);
  141. }
  142. out.write(extraData);
  143. }
  144. public boolean equals(Object obj) {
  145. if(! (obj instanceof ExtRst)) {
  146. return false;
  147. }
  148. ExtRst other = (ExtRst)obj;
  149. return (compareTo(other) == 0);
  150. }
  151. public int compareTo(ExtRst o) {
  152. int result;
  153. result = reserved - o.reserved;
  154. if (result != 0) {
  155. return result;
  156. }
  157. result = formattingFontIndex - o.formattingFontIndex;
  158. if (result != 0) {
  159. return result;
  160. }
  161. result = formattingOptions - o.formattingOptions;
  162. if (result != 0) {
  163. return result;
  164. }
  165. result = numberOfRuns - o.numberOfRuns;
  166. if (result != 0) {
  167. return result;
  168. }
  169. result = phoneticText.compareTo(o.phoneticText);
  170. if (result != 0) {
  171. return result;
  172. }
  173. result = phRuns.length - o.phRuns.length;
  174. if (result != 0) {
  175. return result;
  176. }
  177. for(int i=0; i<phRuns.length; i++) {
  178. result = phRuns[i].phoneticTextFirstCharacterOffset - o.phRuns[i].phoneticTextFirstCharacterOffset;
  179. if (result != 0) {
  180. return result;
  181. }
  182. result = phRuns[i].realTextFirstCharacterOffset - o.phRuns[i].realTextFirstCharacterOffset;
  183. if (result != 0) {
  184. return result;
  185. }
  186. result = phRuns[i].realTextLength - o.phRuns[i].realTextLength;
  187. if (result != 0) {
  188. return result;
  189. }
  190. }
  191. result = Arrays.hashCode(extraData)-Arrays.hashCode(o.extraData);
  192. return result;
  193. }
  194. @Override
  195. public int hashCode() {
  196. return Arrays.deepHashCode(new Object[]{reserved, formattingFontIndex, formattingOptions, numberOfRuns, phoneticText, phRuns});
  197. }
  198. public ExtRst copy() {
  199. return new ExtRst(this);
  200. }
  201. public short getFormattingFontIndex() {
  202. return formattingFontIndex;
  203. }
  204. public short getFormattingOptions() {
  205. return formattingOptions;
  206. }
  207. public int getNumberOfRuns() {
  208. return numberOfRuns;
  209. }
  210. public String getPhoneticText() {
  211. return phoneticText;
  212. }
  213. public PhRun[] getPhRuns() {
  214. return phRuns;
  215. }
  216. @Override
  217. public Map<String, Supplier<?>> getGenericProperties() {
  218. return GenericRecordUtil.getGenericProperties(
  219. "reserved", () -> reserved,
  220. "formattingFontIndex", this::getFormattingFontIndex,
  221. "formattingOptions", this::getFormattingOptions,
  222. "numberOfRuns", this::getNumberOfRuns,
  223. "phoneticText", this::getPhoneticText,
  224. "phRuns", this::getPhRuns,
  225. "extraData", () -> extraData
  226. );
  227. }
  228. }