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.

MasterTextPropAtom.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.hslf.record;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.function.Supplier;
  24. import org.apache.poi.hslf.model.textproperties.IndentProp;
  25. import org.apache.poi.util.GenericRecordUtil;
  26. import org.apache.poi.util.IOUtils;
  27. import org.apache.poi.util.LittleEndian;
  28. /**
  29. * Specifies the Indent Level for the text
  30. */
  31. public final class MasterTextPropAtom extends RecordAtom {
  32. //arbitrarily selected; may need to increase
  33. private static final int DEFAULT_MAX_RECORD_LENGTH = 100_000;
  34. private static int MAX_RECORD_LENGTH = DEFAULT_MAX_RECORD_LENGTH;
  35. /**
  36. * Record header.
  37. */
  38. private byte[] _header;
  39. /**
  40. * Record data.
  41. */
  42. private byte[] _data;
  43. // indent details
  44. private List<IndentProp> indents;
  45. /**
  46. * @param length the max record length allowed for MasterTextPropAtom
  47. */
  48. public static void setMaxRecordLength(int length) {
  49. MAX_RECORD_LENGTH = length;
  50. }
  51. /**
  52. * @return the max record length allowed for MasterTextPropAtom
  53. */
  54. public static int getMaxRecordLength() {
  55. return MAX_RECORD_LENGTH;
  56. }
  57. /**
  58. * Constructs a new empty master text prop atom.
  59. */
  60. public MasterTextPropAtom() {
  61. _header = new byte[8];
  62. _data = new byte[0];
  63. LittleEndian.putShort(_header, 2, (short)getRecordType());
  64. LittleEndian.putInt(_header, 4, _data.length);
  65. indents = new ArrayList<>();
  66. }
  67. /**
  68. * Constructs the ruler atom record from its
  69. * source data.
  70. *
  71. * @param source the source data as a byte array.
  72. * @param start the start offset into the byte array.
  73. * @param len the length of the slice in the byte array.
  74. */
  75. protected MasterTextPropAtom(byte[] source, int start, int len) {
  76. // Get the header.
  77. _header = Arrays.copyOfRange(source, start, start+8);
  78. // Get the record data.
  79. _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH);
  80. try {
  81. read();
  82. } catch (Exception e){
  83. LOG.atError().withThrowable(e).log("Failed to parse MasterTextPropAtom");
  84. }
  85. }
  86. /**
  87. * Gets the record type.
  88. *
  89. * @return the record type.
  90. */
  91. @Override
  92. public long getRecordType() {
  93. return RecordTypes.MasterTextPropAtom.typeID;
  94. }
  95. /**
  96. * Write the contents of the record back, so it can be written
  97. * to disk.
  98. *
  99. * @param out the output stream to write to.
  100. * @throws java.io.IOException if an error occurs.
  101. */
  102. @Override
  103. public void writeOut(OutputStream out) throws IOException {
  104. write();
  105. out.write(_header);
  106. out.write(_data);
  107. }
  108. /**
  109. * Write the internal variables to the record bytes
  110. */
  111. private void write() {
  112. int pos = 0;
  113. long newSize = Math.multiplyExact((long)indents.size(), (long)6);
  114. _data = IOUtils.safelyAllocate(newSize, MAX_RECORD_LENGTH);
  115. for (IndentProp prop : indents) {
  116. LittleEndian.putInt(_data, pos, prop.getCharactersCovered());
  117. LittleEndian.putShort(_data, pos+4, (short)prop.getIndentLevel());
  118. pos += 6;
  119. }
  120. }
  121. /**
  122. * Read the record bytes and initialize the internal variables
  123. */
  124. private void read() {
  125. int pos = 0;
  126. indents = new ArrayList<>(_data.length / 6);
  127. while (pos <= _data.length - 6) {
  128. int count = LittleEndian.getInt(_data, pos);
  129. short indent = LittleEndian.getShort(_data, pos+4);
  130. indents.add(new IndentProp(count, indent));
  131. pos += 6;
  132. }
  133. }
  134. /**
  135. * Returns the indent that applies at the given text offset
  136. */
  137. public int getIndentAt(int offset) {
  138. int charsUntil = 0;
  139. for (IndentProp prop : indents) {
  140. charsUntil += prop.getCharactersCovered();
  141. if (offset < charsUntil) {
  142. return prop.getIndentLevel();
  143. }
  144. }
  145. return -1;
  146. }
  147. public List<IndentProp> getIndents() {
  148. return Collections.unmodifiableList(indents);
  149. }
  150. @Override
  151. public Map<String, Supplier<?>> getGenericProperties() {
  152. return GenericRecordUtil.getGenericProperties(
  153. "indents", this::getIndents
  154. );
  155. }
  156. }