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.

UnicodeString.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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.ArrayList;
  17. import java.util.Collections;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import org.apache.poi.hssf.record.cont.ContinuableRecordInput;
  21. import org.apache.poi.hssf.record.RecordInputStream;
  22. import org.apache.poi.hssf.record.cont.ContinuableRecordOutput;
  23. import org.apache.poi.util.BitField;
  24. import org.apache.poi.util.BitFieldFactory;
  25. import org.apache.poi.util.LittleEndianInput;
  26. import org.apache.poi.util.LittleEndianOutput;
  27. import org.apache.poi.util.StringUtil;
  28. /**
  29. * Title: Unicode String<p/>
  30. * Description: Unicode String - just standard fields that are in several records.
  31. * It is considered more desirable then repeating it in all of them.<p/>
  32. * This is often called a XLUnicodeRichExtendedString in MS documentation.<p/>
  33. * REFERENCE: PG 264 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<p/>
  34. * REFERENCE: PG 951 Excel Binary File Format (.xls) Structure Specification v20091214
  35. */
  36. public class UnicodeString implements Comparable<UnicodeString> { // TODO - make this final when the compatibility version is removed
  37. private short field_1_charCount;
  38. private byte field_2_optionflags;
  39. private String field_3_string;
  40. private List<FormatRun> field_4_format_runs;
  41. private ExtRst field_5_ext_rst;
  42. private static final BitField highByte = BitFieldFactory.getInstance(0x1);
  43. // 0x2 is reserved
  44. private static final BitField extBit = BitFieldFactory.getInstance(0x4);
  45. private static final BitField richText = BitFieldFactory.getInstance(0x8);
  46. public static class FormatRun implements Comparable<FormatRun> {
  47. final short _character;
  48. short _fontIndex;
  49. public FormatRun(short character, short fontIndex) {
  50. this._character = character;
  51. this._fontIndex = fontIndex;
  52. }
  53. public FormatRun(LittleEndianInput in) {
  54. this(in.readShort(), in.readShort());
  55. }
  56. public short getCharacterPos() {
  57. return _character;
  58. }
  59. public short getFontIndex() {
  60. return _fontIndex;
  61. }
  62. public boolean equals(Object o) {
  63. if (!(o instanceof FormatRun)) {
  64. return false;
  65. }
  66. FormatRun other = ( FormatRun ) o;
  67. return _character == other._character && _fontIndex == other._fontIndex;
  68. }
  69. public int compareTo(FormatRun r) {
  70. if (_character == r._character && _fontIndex == r._fontIndex) {
  71. return 0;
  72. }
  73. if (_character == r._character) {
  74. return _fontIndex - r._fontIndex;
  75. }
  76. return _character - r._character;
  77. }
  78. public String toString() {
  79. return "character="+_character+",fontIndex="+_fontIndex;
  80. }
  81. public void serialize(LittleEndianOutput out) {
  82. out.writeShort(_character);
  83. out.writeShort(_fontIndex);
  84. }
  85. }
  86. // See page 681
  87. public static class ExtRst implements Comparable<ExtRst> {
  88. private short reserved;
  89. // This is a Phs (see page 881)
  90. private short formattingFontIndex;
  91. private short formattingOptions;
  92. // This is a RPHSSub (see page 894)
  93. private int numberOfRuns;
  94. private String phoneticText;
  95. // This is an array of PhRuns (see page 881)
  96. private PhRun[] phRuns;
  97. // Sometimes there's some cruft at the end
  98. private byte[] extraData;
  99. private void populateEmpty() {
  100. reserved = 1;
  101. phoneticText = "";
  102. phRuns = new PhRun[0];
  103. extraData = new byte[0];
  104. }
  105. protected ExtRst() {
  106. populateEmpty();
  107. }
  108. protected ExtRst(LittleEndianInput in, int expectedLength) {
  109. reserved = in.readShort();
  110. // Old style detection (Reserved = 0xFF)
  111. if(reserved == -1) {
  112. populateEmpty();
  113. return;
  114. }
  115. // Spot corrupt records
  116. if(reserved != 1) {
  117. System.err.println("Warning - ExtRst was has wrong magic marker, expecting 1 but found " + reserved + " - ignoring");
  118. // Grab all the remaining data, and ignore it
  119. for(int i=0; i<expectedLength-2; i++) {
  120. in.readByte();
  121. }
  122. // And make us be empty
  123. populateEmpty();
  124. return;
  125. }
  126. // Carry on reading in as normal
  127. short stringDataSize = in.readShort();
  128. formattingFontIndex = in.readShort();
  129. formattingOptions = in.readShort();
  130. // RPHSSub
  131. numberOfRuns = in.readUShort();
  132. short length1 = in.readShort();
  133. // No really. Someone clearly forgot to read
  134. // the docs on their datastructure...
  135. short length2 = in.readShort();
  136. // And sometimes they write out garbage :(
  137. if(length1 == 0 && length2 > 0) {
  138. length2 = 0;
  139. }
  140. if(length1 != length2) {
  141. throw new IllegalStateException(
  142. "The two length fields of the Phonetic Text don't agree! " +
  143. length1 + " vs " + length2
  144. );
  145. }
  146. phoneticText = StringUtil.readUnicodeLE(in, length1);
  147. int runData = stringDataSize - 4 - 6 - (2*phoneticText.length());
  148. int numRuns = (runData / 6);
  149. phRuns = new PhRun[numRuns];
  150. for(int i=0; i<phRuns.length; i++) {
  151. phRuns[i] = new PhRun(in);
  152. }
  153. int extraDataLength = runData - (numRuns*6);
  154. if(extraDataLength < 0) {
  155. System.err.println("Warning - ExtRst overran by " + (0-extraDataLength) + " bytes");
  156. extraDataLength = 0;
  157. }
  158. extraData = new byte[extraDataLength];
  159. for(int i=0; i<extraData.length; i++) {
  160. extraData[i] = in.readByte();
  161. }
  162. }
  163. /**
  164. * Returns our size, excluding our
  165. * 4 byte header
  166. */
  167. protected int getDataSize() {
  168. return 4 + 6 + (2*phoneticText.length()) +
  169. (6*phRuns.length) + extraData.length;
  170. }
  171. protected void serialize(ContinuableRecordOutput out) {
  172. int dataSize = getDataSize();
  173. out.writeContinueIfRequired(8);
  174. out.writeShort(reserved);
  175. out.writeShort(dataSize);
  176. out.writeShort(formattingFontIndex);
  177. out.writeShort(formattingOptions);
  178. out.writeContinueIfRequired(6);
  179. out.writeShort(numberOfRuns);
  180. out.writeShort(phoneticText.length());
  181. out.writeShort(phoneticText.length());
  182. out.writeContinueIfRequired(phoneticText.length()*2);
  183. StringUtil.putUnicodeLE(phoneticText, out);
  184. for(int i=0; i<phRuns.length; i++) {
  185. phRuns[i].serialize(out);
  186. }
  187. out.write(extraData);
  188. }
  189. public boolean equals(Object obj) {
  190. if(! (obj instanceof ExtRst)) {
  191. return false;
  192. }
  193. ExtRst other = (ExtRst)obj;
  194. return (compareTo(other) == 0);
  195. }
  196. public int compareTo(ExtRst o) {
  197. int result;
  198. result = reserved - o.reserved;
  199. if(result != 0) return result;
  200. result = formattingFontIndex - o.formattingFontIndex;
  201. if(result != 0) return result;
  202. result = formattingOptions - o.formattingOptions;
  203. if(result != 0) return result;
  204. result = numberOfRuns - o.numberOfRuns;
  205. if(result != 0) return result;
  206. result = phoneticText.compareTo(o.phoneticText);
  207. if(result != 0) return result;
  208. result = phRuns.length - o.phRuns.length;
  209. if(result != 0) return result;
  210. for(int i=0; i<phRuns.length; i++) {
  211. result = phRuns[i].phoneticTextFirstCharacterOffset - o.phRuns[i].phoneticTextFirstCharacterOffset;
  212. if(result != 0) return result;
  213. result = phRuns[i].realTextFirstCharacterOffset - o.phRuns[i].realTextFirstCharacterOffset;
  214. if(result != 0) return result;
  215. result = phRuns[i].realTextFirstCharacterOffset - o.phRuns[i].realTextLength;
  216. if(result != 0) return result;
  217. }
  218. result = extraData.length - o.extraData.length;
  219. if(result != 0) return result;
  220. // If we get here, it's the same
  221. return 0;
  222. }
  223. protected ExtRst clone() {
  224. ExtRst ext = new ExtRst();
  225. ext.reserved = reserved;
  226. ext.formattingFontIndex = formattingFontIndex;
  227. ext.formattingOptions = formattingOptions;
  228. ext.numberOfRuns = numberOfRuns;
  229. ext.phoneticText = phoneticText;
  230. ext.phRuns = new PhRun[phRuns.length];
  231. for(int i=0; i<ext.phRuns.length; i++) {
  232. ext.phRuns[i] = new PhRun(
  233. phRuns[i].phoneticTextFirstCharacterOffset,
  234. phRuns[i].realTextFirstCharacterOffset,
  235. phRuns[i].realTextLength
  236. );
  237. }
  238. return ext;
  239. }
  240. public short getFormattingFontIndex() {
  241. return formattingFontIndex;
  242. }
  243. public short getFormattingOptions() {
  244. return formattingOptions;
  245. }
  246. public int getNumberOfRuns() {
  247. return numberOfRuns;
  248. }
  249. public String getPhoneticText() {
  250. return phoneticText;
  251. }
  252. public PhRun[] getPhRuns() {
  253. return phRuns;
  254. }
  255. }
  256. public static class PhRun {
  257. private int phoneticTextFirstCharacterOffset;
  258. private int realTextFirstCharacterOffset;
  259. private int realTextLength;
  260. public PhRun(int phoneticTextFirstCharacterOffset,
  261. int realTextFirstCharacterOffset, int realTextLength) {
  262. this.phoneticTextFirstCharacterOffset = phoneticTextFirstCharacterOffset;
  263. this.realTextFirstCharacterOffset = realTextFirstCharacterOffset;
  264. this.realTextLength = realTextLength;
  265. }
  266. private PhRun(LittleEndianInput in) {
  267. phoneticTextFirstCharacterOffset = in.readUShort();
  268. realTextFirstCharacterOffset = in.readUShort();
  269. realTextLength = in.readUShort();
  270. }
  271. private void serialize(ContinuableRecordOutput out) {
  272. out.writeContinueIfRequired(6);
  273. out.writeShort(phoneticTextFirstCharacterOffset);
  274. out.writeShort(realTextFirstCharacterOffset);
  275. out.writeShort(realTextLength);
  276. }
  277. }
  278. private UnicodeString() {
  279. //Used for clone method.
  280. }
  281. public UnicodeString(String str)
  282. {
  283. setString(str);
  284. }
  285. public int hashCode()
  286. {
  287. int stringHash = 0;
  288. if (field_3_string != null)
  289. stringHash = field_3_string.hashCode();
  290. return field_1_charCount + stringHash;
  291. }
  292. /**
  293. * Our handling of equals is inconsistent with compareTo. The trouble is because we don't truely understand
  294. * rich text fields yet it's difficult to make a sound comparison.
  295. *
  296. * @param o The object to compare.
  297. * @return true if the object is actually equal.
  298. */
  299. public boolean equals(Object o)
  300. {
  301. if (!(o instanceof UnicodeString)) {
  302. return false;
  303. }
  304. UnicodeString other = (UnicodeString) o;
  305. //OK lets do this in stages to return a quickly, first check the actual string
  306. boolean eq = ((field_1_charCount == other.field_1_charCount)
  307. && (field_2_optionflags == other.field_2_optionflags)
  308. && field_3_string.equals(other.field_3_string));
  309. if (!eq) return false;
  310. //OK string appears to be equal but now lets compare formatting runs
  311. if ((field_4_format_runs == null) && (other.field_4_format_runs == null))
  312. //Strings are equal, and there are not formatting runs.
  313. return true;
  314. if (((field_4_format_runs == null) && (other.field_4_format_runs != null)) ||
  315. (field_4_format_runs != null) && (other.field_4_format_runs == null))
  316. //Strings are equal, but one or the other has formatting runs
  317. return false;
  318. //Strings are equal, so now compare formatting runs.
  319. int size = field_4_format_runs.size();
  320. if (size != other.field_4_format_runs.size())
  321. return false;
  322. for (int i=0;i<size;i++) {
  323. FormatRun run1 = field_4_format_runs.get(i);
  324. FormatRun run2 = other.field_4_format_runs.get(i);
  325. if (!run1.equals(run2))
  326. return false;
  327. }
  328. // Well the format runs are equal as well!, better check the ExtRst data
  329. if(field_5_ext_rst == null && other.field_5_ext_rst == null) {
  330. // Good
  331. } else if(field_5_ext_rst != null && other.field_5_ext_rst != null) {
  332. int extCmp = field_5_ext_rst.compareTo(other.field_5_ext_rst);
  333. if(extCmp == 0) {
  334. // Good
  335. } else {
  336. return false;
  337. }
  338. } else {
  339. return false;
  340. }
  341. //Phew!! After all of that we have finally worked out that the strings
  342. //are identical.
  343. return true;
  344. }
  345. /**
  346. * construct a unicode string record and fill its fields, ID is ignored
  347. * @param in the RecordInputstream to read the record from
  348. */
  349. public UnicodeString(RecordInputStream in) {
  350. field_1_charCount = in.readShort();
  351. field_2_optionflags = in.readByte();
  352. int runCount = 0;
  353. int extensionLength = 0;
  354. //Read the number of rich runs if rich text.
  355. if ( isRichText() )
  356. {
  357. runCount = in.readShort();
  358. }
  359. //Read the size of extended data if present.
  360. if ( isExtendedText() )
  361. {
  362. extensionLength = in.readInt();
  363. }
  364. boolean isCompressed = ((field_2_optionflags & 1) == 0);
  365. if (isCompressed) {
  366. field_3_string = in.readCompressedUnicode(getCharCount());
  367. } else {
  368. field_3_string = in.readUnicodeLEString(getCharCount());
  369. }
  370. if (isRichText() && (runCount > 0)) {
  371. field_4_format_runs = new ArrayList<FormatRun>(runCount);
  372. for (int i=0;i<runCount;i++) {
  373. field_4_format_runs.add(new FormatRun(in));
  374. }
  375. }
  376. if (isExtendedText() && (extensionLength > 0)) {
  377. field_5_ext_rst = new ExtRst(new ContinuableRecordInput(in), extensionLength);
  378. if(field_5_ext_rst.getDataSize()+4 != extensionLength) {
  379. System.err.println("ExtRst was supposed to be " + extensionLength + " bytes long, but seems to actually be " + (field_5_ext_rst.getDataSize()+4));
  380. }
  381. }
  382. }
  383. /**
  384. * get the number of characters in the string,
  385. * as an un-wrapped int
  386. *
  387. * @return number of characters
  388. */
  389. public int getCharCount() {
  390. if(field_1_charCount < 0) {
  391. return field_1_charCount + 65536;
  392. }
  393. return field_1_charCount;
  394. }
  395. /**
  396. * get the number of characters in the string,
  397. * wrapped as needed to fit within a short
  398. *
  399. * @return number of characters
  400. */
  401. public short getCharCountShort() {
  402. return field_1_charCount;
  403. }
  404. /**
  405. * set the number of characters in the string
  406. * @param cc - number of characters
  407. */
  408. public void setCharCount(short cc)
  409. {
  410. field_1_charCount = cc;
  411. }
  412. /**
  413. * get the option flags which among other things return if this is a 16-bit or
  414. * 8 bit string
  415. *
  416. * @return optionflags bitmask
  417. *
  418. */
  419. public byte getOptionFlags()
  420. {
  421. return field_2_optionflags;
  422. }
  423. /**
  424. * set the option flags which among other things return if this is a 16-bit or
  425. * 8 bit string
  426. *
  427. * @param of optionflags bitmask
  428. *
  429. */
  430. public void setOptionFlags(byte of)
  431. {
  432. field_2_optionflags = of;
  433. }
  434. /**
  435. * @return the actual string this contains as a java String object
  436. */
  437. public String getString()
  438. {
  439. return field_3_string;
  440. }
  441. /**
  442. * set the actual string this contains
  443. * @param string the text
  444. */
  445. public void setString(String string)
  446. {
  447. field_3_string = string;
  448. setCharCount((short)field_3_string.length());
  449. // scan for characters greater than 255 ... if any are
  450. // present, we have to use 16-bit encoding. Otherwise, we
  451. // can use 8-bit encoding
  452. boolean useUTF16 = false;
  453. int strlen = string.length();
  454. for ( int j = 0; j < strlen; j++ )
  455. {
  456. if ( string.charAt( j ) > 255 )
  457. {
  458. useUTF16 = true;
  459. break;
  460. }
  461. }
  462. if (useUTF16)
  463. //Set the uncompressed bit
  464. field_2_optionflags = highByte.setByte(field_2_optionflags);
  465. else field_2_optionflags = highByte.clearByte(field_2_optionflags);
  466. }
  467. public int getFormatRunCount() {
  468. if (field_4_format_runs == null)
  469. return 0;
  470. return field_4_format_runs.size();
  471. }
  472. public FormatRun getFormatRun(int index) {
  473. if (field_4_format_runs == null) {
  474. return null;
  475. }
  476. if (index < 0 || index >= field_4_format_runs.size()) {
  477. return null;
  478. }
  479. return field_4_format_runs.get(index);
  480. }
  481. private int findFormatRunAt(int characterPos) {
  482. int size = field_4_format_runs.size();
  483. for (int i=0;i<size;i++) {
  484. FormatRun r = field_4_format_runs.get(i);
  485. if (r._character == characterPos)
  486. return i;
  487. else if (r._character > characterPos)
  488. return -1;
  489. }
  490. return -1;
  491. }
  492. /** Adds a font run to the formatted string.
  493. *
  494. * If a font run exists at the current charcter location, then it is
  495. * replaced with the font run to be added.
  496. */
  497. public void addFormatRun(FormatRun r) {
  498. if (field_4_format_runs == null) {
  499. field_4_format_runs = new ArrayList<FormatRun>();
  500. }
  501. int index = findFormatRunAt(r._character);
  502. if (index != -1)
  503. field_4_format_runs.remove(index);
  504. field_4_format_runs.add(r);
  505. //Need to sort the font runs to ensure that the font runs appear in
  506. //character order
  507. Collections.sort(field_4_format_runs);
  508. //Make sure that we now say that we are a rich string
  509. field_2_optionflags = richText.setByte(field_2_optionflags);
  510. }
  511. public Iterator<FormatRun> formatIterator() {
  512. if (field_4_format_runs != null) {
  513. return field_4_format_runs.iterator();
  514. }
  515. return null;
  516. }
  517. public void removeFormatRun(FormatRun r) {
  518. field_4_format_runs.remove(r);
  519. if (field_4_format_runs.size() == 0) {
  520. field_4_format_runs = null;
  521. field_2_optionflags = richText.clearByte(field_2_optionflags);
  522. }
  523. }
  524. public void clearFormatting() {
  525. field_4_format_runs = null;
  526. field_2_optionflags = richText.clearByte(field_2_optionflags);
  527. }
  528. public ExtRst getExtendedRst() {
  529. return this.field_5_ext_rst;
  530. }
  531. void setExtendedRst(ExtRst ext_rst) {
  532. if (ext_rst != null) {
  533. field_2_optionflags = extBit.setByte(field_2_optionflags);
  534. } else {
  535. field_2_optionflags = extBit.clearByte(field_2_optionflags);
  536. }
  537. this.field_5_ext_rst = ext_rst;
  538. }
  539. /**
  540. * Swaps all use in the string of one font index
  541. * for use of a different font index.
  542. * Normally only called when fonts have been
  543. * removed / re-ordered
  544. */
  545. public void swapFontUse(short oldFontIndex, short newFontIndex) {
  546. for (FormatRun run : field_4_format_runs) {
  547. if(run._fontIndex == oldFontIndex) {
  548. run._fontIndex = newFontIndex;
  549. }
  550. }
  551. }
  552. /**
  553. * unlike the real records we return the same as "getString()" rather than debug info
  554. * @see #getDebugInfo()
  555. * @return String value of the record
  556. */
  557. public String toString()
  558. {
  559. return getString();
  560. }
  561. /**
  562. * return a character representation of the fields of this record
  563. *
  564. *
  565. * @return String of output for biffviewer etc.
  566. *
  567. */
  568. public String getDebugInfo()
  569. {
  570. StringBuffer buffer = new StringBuffer();
  571. buffer.append("[UNICODESTRING]\n");
  572. buffer.append(" .charcount = ")
  573. .append(Integer.toHexString(getCharCount())).append("\n");
  574. buffer.append(" .optionflags = ")
  575. .append(Integer.toHexString(getOptionFlags())).append("\n");
  576. buffer.append(" .string = ").append(getString()).append("\n");
  577. if (field_4_format_runs != null) {
  578. for (int i = 0; i < field_4_format_runs.size();i++) {
  579. FormatRun r = field_4_format_runs.get(i);
  580. buffer.append(" .format_run"+i+" = ").append(r.toString()).append("\n");
  581. }
  582. }
  583. if (field_5_ext_rst != null) {
  584. buffer.append(" .field_5_ext_rst = ").append("\n");
  585. buffer.append( field_5_ext_rst.toString() ).append("\n");
  586. }
  587. buffer.append("[/UNICODESTRING]\n");
  588. return buffer.toString();
  589. }
  590. /**
  591. * Serialises out the String. There are special rules
  592. * about where we can and can't split onto
  593. * Continue records.
  594. */
  595. public void serialize(ContinuableRecordOutput out) {
  596. int numberOfRichTextRuns = 0;
  597. int extendedDataSize = 0;
  598. if (isRichText() && field_4_format_runs != null) {
  599. numberOfRichTextRuns = field_4_format_runs.size();
  600. }
  601. if (isExtendedText() && field_5_ext_rst != null) {
  602. extendedDataSize = 4 + field_5_ext_rst.getDataSize();
  603. }
  604. // Serialise the bulk of the String
  605. // The writeString handles tricky continue stuff for us
  606. out.writeString(field_3_string, numberOfRichTextRuns, extendedDataSize);
  607. if (numberOfRichTextRuns > 0) {
  608. //This will ensure that a run does not split a continue
  609. for (int i=0;i<numberOfRichTextRuns;i++) {
  610. if (out.getAvailableSpace() < 4) {
  611. out.writeContinue();
  612. }
  613. FormatRun r = field_4_format_runs.get(i);
  614. r.serialize(out);
  615. }
  616. }
  617. if (extendedDataSize > 0) {
  618. field_5_ext_rst.serialize(out);
  619. }
  620. }
  621. public int compareTo(UnicodeString str) {
  622. int result = getString().compareTo(str.getString());
  623. //As per the equals method lets do this in stages
  624. if (result != 0)
  625. return result;
  626. //OK string appears to be equal but now lets compare formatting runs
  627. if ((field_4_format_runs == null) && (str.field_4_format_runs == null))
  628. //Strings are equal, and there are no formatting runs.
  629. return 0;
  630. if ((field_4_format_runs == null) && (str.field_4_format_runs != null))
  631. //Strings are equal, but one or the other has formatting runs
  632. return 1;
  633. if ((field_4_format_runs != null) && (str.field_4_format_runs == null))
  634. //Strings are equal, but one or the other has formatting runs
  635. return -1;
  636. //Strings are equal, so now compare formatting runs.
  637. int size = field_4_format_runs.size();
  638. if (size != str.field_4_format_runs.size())
  639. return size - str.field_4_format_runs.size();
  640. for (int i=0;i<size;i++) {
  641. FormatRun run1 = field_4_format_runs.get(i);
  642. FormatRun run2 = str.field_4_format_runs.get(i);
  643. result = run1.compareTo(run2);
  644. if (result != 0)
  645. return result;
  646. }
  647. //Well the format runs are equal as well!, better check the ExtRst data
  648. if ((field_5_ext_rst == null) && (str.field_5_ext_rst == null))
  649. return 0;
  650. if ((field_5_ext_rst == null) && (str.field_5_ext_rst != null))
  651. return 1;
  652. if ((field_5_ext_rst != null) && (str.field_5_ext_rst == null))
  653. return -1;
  654. result = field_5_ext_rst.compareTo(str.field_5_ext_rst);
  655. if (result != 0)
  656. return result;
  657. //Phew!! After all of that we have finally worked out that the strings
  658. //are identical.
  659. return 0;
  660. }
  661. private boolean isRichText()
  662. {
  663. return richText.isSet(getOptionFlags());
  664. }
  665. private boolean isExtendedText()
  666. {
  667. return extBit.isSet(getOptionFlags());
  668. }
  669. public Object clone() {
  670. UnicodeString str = new UnicodeString();
  671. str.field_1_charCount = field_1_charCount;
  672. str.field_2_optionflags = field_2_optionflags;
  673. str.field_3_string = field_3_string;
  674. if (field_4_format_runs != null) {
  675. str.field_4_format_runs = new ArrayList<FormatRun>();
  676. for (FormatRun r : field_4_format_runs) {
  677. str.field_4_format_runs.add(new FormatRun(r._character, r._fontIndex));
  678. }
  679. }
  680. if (field_5_ext_rst != null) {
  681. str.field_5_ext_rst = field_5_ext_rst.clone();
  682. }
  683. return str;
  684. }
  685. }