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.

PropertyNode.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.hwpf.model;
  16. import java.util.Arrays;
  17. import java.util.Comparator;
  18. import java.util.Objects;
  19. import org.apache.logging.log4j.LogManager;
  20. import org.apache.logging.log4j.Logger;
  21. import org.apache.poi.common.Duplicatable;
  22. import org.apache.poi.util.Internal;
  23. import static org.apache.logging.log4j.util.Unbox.box;
  24. /**
  25. * Represents a lightweight node in the Trees used to store content
  26. * properties.
  27. * This only ever works in characters. For the few odd cases when
  28. * the start and end aren't in characters (eg PAPX and CHPX), use
  29. * {@link BytePropertyNode} between you and this.
  30. */
  31. @Internal
  32. public abstract class PropertyNode<T extends PropertyNode<T>> implements Comparable<T>, Duplicatable {
  33. public static final Comparator<PropertyNode<?>> EndComparator = Comparator.comparingInt(PropertyNode::getEnd);
  34. public static final Comparator<PropertyNode<?>> StartComparator = Comparator.comparingInt(PropertyNode::getStart);
  35. private static final Logger LOG = LogManager.getLogger(PropertyNode.class);
  36. protected Object _buf;
  37. /**
  38. * The start, in characters
  39. */
  40. private int _cpStart;
  41. /**
  42. * The end, in characters
  43. */
  44. private int _cpEnd;
  45. protected PropertyNode(PropertyNode<T> other) {
  46. // TODO: clone _buf?
  47. _buf = other._buf;
  48. _cpStart = other._cpStart;
  49. _cpEnd = other._cpEnd;
  50. }
  51. /**
  52. * @param fcStart The start of the text for this property, in characters.
  53. * @param fcEnd The end of the text for this property, in characters.
  54. * @param buf FIXME: Old documentation is: "grpprl The property description in compressed form."
  55. */
  56. protected PropertyNode(int fcStart, int fcEnd, Object buf) {
  57. _cpStart = fcStart;
  58. _cpEnd = fcEnd;
  59. _buf = buf;
  60. if (_cpStart < 0) {
  61. LOG.atWarn().log("A property claimed to start before zero, at {}! Resetting it to zero, and hoping for the best", box(_cpStart));
  62. _cpStart = 0;
  63. }
  64. if (_cpEnd < _cpStart) {
  65. LOG.atWarn().log("A property claimed to end ({}) before start! Resetting end to start, and hoping for the best", box(_cpEnd));
  66. _cpEnd = _cpStart;
  67. }
  68. }
  69. /**
  70. * @return The start offset of this property's text.
  71. */
  72. public int getStart() {
  73. return _cpStart;
  74. }
  75. public void setStart(int start) {
  76. _cpStart = start;
  77. }
  78. /**
  79. * @return The offset of the end of this property's text.
  80. */
  81. public int getEnd() {
  82. return _cpEnd;
  83. }
  84. public void setEnd(int end) {
  85. _cpEnd = end;
  86. }
  87. /**
  88. * Adjust for a deletion that can span multiple PropertyNodes.
  89. */
  90. public void adjustForDelete(int start, int length) {
  91. int end = start + length;
  92. if (_cpEnd > start) {
  93. // The start of the change is before we end
  94. if (_cpStart < end) {
  95. // The delete was somewhere in the middle of us
  96. _cpEnd = end >= _cpEnd ? start : _cpEnd - length;
  97. _cpStart = Math.min(start, _cpStart);
  98. } else {
  99. // The delete was before us
  100. _cpEnd -= length;
  101. _cpStart -= length;
  102. }
  103. }
  104. }
  105. protected boolean limitsAreEqual(Object o) {
  106. return ((PropertyNode<?>) o).getStart() == _cpStart &&
  107. ((PropertyNode<?>) o).getEnd() == _cpEnd;
  108. }
  109. @Override
  110. public int hashCode() {
  111. return Objects.hash(_cpStart,_buf);
  112. }
  113. public boolean equals(Object o) {
  114. if (!(o instanceof PropertyNode)) return false;
  115. if (limitsAreEqual(o)) {
  116. Object testBuf = ((PropertyNode<?>) o)._buf;
  117. if (testBuf instanceof byte[] && _buf instanceof byte[]) {
  118. return Arrays.equals((byte[]) testBuf, (byte[]) _buf);
  119. }
  120. return _buf.equals(testBuf);
  121. }
  122. return false;
  123. }
  124. @Override
  125. public abstract PropertyNode<?> copy();
  126. /**
  127. * Used for sorting in collections.
  128. */
  129. public int compareTo(T o) {
  130. return Integer.compare(_cpEnd, o.getEnd());
  131. }
  132. }