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.

StringRecord.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.hssf.record.cont.ContinuableRecord;
  19. import org.apache.poi.hssf.record.cont.ContinuableRecordOutput;
  20. import org.apache.poi.util.GenericRecordUtil;
  21. import org.apache.poi.util.StringUtil;
  22. /**
  23. * STRING (0x0207)<p>
  24. *
  25. * Stores the cached result of a text formula
  26. */
  27. public final class StringRecord extends ContinuableRecord {
  28. public static final short sid = 0x0207;
  29. private boolean _is16bitUnicode;
  30. private String _text;
  31. public StringRecord() {}
  32. public StringRecord(StringRecord other) {
  33. _is16bitUnicode = other._is16bitUnicode;
  34. _text = other._text;
  35. }
  36. /**
  37. * @param in the RecordInputStream to read the record from
  38. */
  39. public StringRecord( RecordInputStream in) {
  40. int field_1_string_length = in.readUShort();
  41. _is16bitUnicode = in.readByte() != 0x00;
  42. if (_is16bitUnicode){
  43. _text = in.readUnicodeLEString(field_1_string_length);
  44. } else {
  45. _text = in.readCompressedUnicode(field_1_string_length);
  46. }
  47. }
  48. protected void serialize(ContinuableRecordOutput out) {
  49. out.writeShort(_text.length());
  50. out.writeStringData(_text);
  51. }
  52. public short getSid()
  53. {
  54. return sid;
  55. }
  56. /**
  57. * @return The string represented by this record.
  58. */
  59. public String getString()
  60. {
  61. return _text;
  62. }
  63. /**
  64. * Sets the string represented by this record.
  65. *
  66. * @param string The string-value for this record
  67. */
  68. public void setString(String string) {
  69. _text = string;
  70. _is16bitUnicode = StringUtil.hasMultibyte(string);
  71. }
  72. public StringRecord copy() {
  73. return new StringRecord(this);
  74. }
  75. @Override
  76. public HSSFRecordTypes getGenericRecordType() {
  77. return HSSFRecordTypes.STRING;
  78. }
  79. @Override
  80. public Map<String, Supplier<?>> getGenericProperties() {
  81. return GenericRecordUtil.getGenericProperties(
  82. "is16bitUnicode", () -> _is16bitUnicode,
  83. "text", this::getString
  84. );
  85. }
  86. }