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.

StringPtg.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.ss.formula.ptg;
  16. import java.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.util.GenericRecordUtil;
  19. import org.apache.poi.util.LittleEndianInput;
  20. import org.apache.poi.util.LittleEndianOutput;
  21. import org.apache.poi.util.StringUtil;
  22. /**
  23. * String Stores a String value in a formula value stored in the format
  24. * <length 2 bytes>char[]
  25. */
  26. public final class StringPtg extends ScalarConstantPtg {
  27. public static final byte sid = 0x17;
  28. /** the character (") used in formulas to delimit string literals */
  29. private static final char FORMULA_DELIMITER = '"';
  30. private final boolean _is16bitUnicode;
  31. /**
  32. * NOTE: OO doc says 16bit length, but BiffViewer says 8 Book says something
  33. * totally different, so don't look there!
  34. */
  35. private final String field_3_string;
  36. /** Create a StringPtg from a stream */
  37. public StringPtg(LittleEndianInput in) {
  38. int nChars = in.readUByte(); // Note - nChars is 8-bit
  39. _is16bitUnicode = (in.readByte() & 0x01) != 0;
  40. if (_is16bitUnicode) {
  41. field_3_string = StringUtil.readUnicodeLE(in, nChars);
  42. } else {
  43. field_3_string = StringUtil.readCompressedUnicode(in, nChars);
  44. }
  45. }
  46. /**
  47. * Create a StringPtg from a string representation of the number Number
  48. * format is not checked, it is expected to be validated in the parser that
  49. * calls this method.
  50. *
  51. * @param value :
  52. * String representation of a floating point number
  53. */
  54. public StringPtg(String value) {
  55. if (value.length() > 255) {
  56. throw new IllegalArgumentException(
  57. "String literals in formulas can't be bigger than 255 characters ASCII");
  58. }
  59. _is16bitUnicode = StringUtil.hasMultibyte(value);
  60. field_3_string = value;
  61. }
  62. public String getValue() {
  63. return field_3_string;
  64. }
  65. public void write(LittleEndianOutput out) {
  66. out.writeByte(sid + getPtgClass());
  67. out.writeByte(field_3_string.length()); // Note - nChars is 8-bit
  68. out.writeByte(_is16bitUnicode ? 0x01 : 0x00);
  69. if (_is16bitUnicode) {
  70. StringUtil.putUnicodeLE(field_3_string, out);
  71. } else {
  72. StringUtil.putCompressedUnicode(field_3_string, out);
  73. }
  74. }
  75. @Override
  76. public byte getSid() {
  77. return sid;
  78. }
  79. public int getSize() {
  80. return 3 + field_3_string.length() * (_is16bitUnicode ? 2 : 1);
  81. }
  82. public String toFormulaString() {
  83. String value = field_3_string;
  84. int len = value.length();
  85. StringBuilder sb = new StringBuilder(len + 4);
  86. sb.append(FORMULA_DELIMITER);
  87. for (int i = 0; i < len; i++) {
  88. char c = value.charAt(i);
  89. if (c == FORMULA_DELIMITER) {
  90. sb.append(FORMULA_DELIMITER);
  91. }
  92. sb.append(c);
  93. }
  94. sb.append(FORMULA_DELIMITER);
  95. return sb.toString();
  96. }
  97. @Override
  98. public StringPtg copy() {
  99. return this;
  100. }
  101. @Override
  102. public Map<String, Supplier<?>> getGenericProperties() {
  103. return GenericRecordUtil.getGenericProperties("value", this::getValue);
  104. }
  105. }