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.

BitMaskTextProp.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.model.textproperties;
  16. import static org.apache.poi.util.GenericRecordUtil.getBitsAsString;
  17. import java.util.Map;
  18. import java.util.function.Supplier;
  19. import org.apache.logging.log4j.LogManager;
  20. import org.apache.logging.log4j.Logger;
  21. import org.apache.logging.log4j.message.SimpleMessage;
  22. import org.apache.poi.util.GenericRecordUtil;
  23. /**
  24. * Definition of a special kind of property of some text, or its
  25. * paragraph. For these properties, a flag in the "contains" header
  26. * field tells you the data property family will exist. The value
  27. * of the property is itself a mask, encoding several different
  28. * (but related) properties
  29. */
  30. public abstract class BitMaskTextProp extends TextProp {
  31. protected static final Logger LOG = LogManager.getLogger(BitMaskTextProp.class);
  32. private final String[] subPropNames;
  33. private final int[] subPropMasks;
  34. private final boolean[] subPropMatches;
  35. /** Fetch the list of the names of the sub properties */
  36. public String[] getSubPropNames() { return subPropNames; }
  37. /** Fetch the list of if the sub properties match or not */
  38. public boolean[] getSubPropMatches() { return subPropMatches; }
  39. protected BitMaskTextProp(BitMaskTextProp other) {
  40. super(other);
  41. subPropNames = (other.subPropNames == null) ? null : other.subPropNames.clone();
  42. subPropMasks = (other.subPropMasks == null) ? null : other.subPropMasks.clone();
  43. // The old clone implementation didn't carry over matches, but keep everything else as it was
  44. // this is failing unit tests
  45. // subPropMatches = (other.subPropMatches == null) ? null : new boolean[other.subPropMatches.length];
  46. subPropMatches = (other.subPropMatches == null) ? null : other.subPropMatches.clone();
  47. }
  48. protected BitMaskTextProp(int sizeOfDataBlock, int maskInHeader, String overallName, String... subPropNames) {
  49. super(sizeOfDataBlock,maskInHeader,overallName);
  50. this.subPropNames = subPropNames;
  51. subPropMasks = new int[subPropNames.length];
  52. subPropMatches = new boolean[subPropNames.length];
  53. int LSB = Integer.lowestOneBit(maskInHeader);
  54. // Initialise the masks list
  55. for(int i=0; i<subPropMasks.length; i++) {
  56. subPropMasks[i] = (LSB << i);
  57. }
  58. }
  59. /**
  60. * Calculate mask from the subPropMatches.
  61. */
  62. @Override
  63. public int getWriteMask() {
  64. /*
  65. * The dataValue can't be taken as a mask, as sometimes certain properties
  66. * are explicitly set to false, i.e. the mask says the property is defined
  67. * but in the actually nibble the property is set to false
  68. */
  69. int mask = 0, i = 0;
  70. for (int subMask : subPropMasks) {
  71. if (subPropMatches[i++]) mask |= subMask;
  72. }
  73. return mask;
  74. }
  75. /**
  76. * Sets the write mask, i.e. which defines the text properties to be considered
  77. *
  78. * @param writeMask the mask, bit values outside the property mask range will be ignored
  79. */
  80. public void setWriteMask(int writeMask) {
  81. int i = 0;
  82. for (int subMask : subPropMasks) {
  83. subPropMatches[i++] = ((writeMask & subMask) != 0);
  84. }
  85. }
  86. /**
  87. * Return the text property value.
  88. * Clears all bits of the value, which are marked as unset.
  89. *
  90. * @return the text property value.
  91. */
  92. @Override
  93. public int getValue() {
  94. return maskValue(super.getValue());
  95. }
  96. private int maskValue(int pVal) {
  97. int val = pVal, i = 0;
  98. for (int mask : subPropMasks) {
  99. if (!subPropMatches[i++]) {
  100. val &= ~mask;
  101. }
  102. }
  103. return val;
  104. }
  105. /**
  106. * Set the value of the text property, and recompute the sub
  107. * properties based on it, i.e. all unset subvalues will be cleared.
  108. * Use {@link #setSubValue(boolean, int)} to explicitly set subvalues to {@code false}.
  109. */
  110. @Override
  111. public void setValue(int val) {
  112. super.setValue(val);
  113. // Figure out the values of the sub properties
  114. int i = 0;
  115. for(int mask : subPropMasks) {
  116. subPropMatches[i++] = ((val & mask) != 0);
  117. }
  118. }
  119. /**
  120. * Convenience method to set a value with mask, without splitting it into the subvalues
  121. */
  122. public void setValueWithMask(int val, int writeMask) {
  123. setWriteMask(writeMask);
  124. super.setValue(maskValue(val));
  125. if (val != super.getValue()) {
  126. LOG.atWarn().log("Style properties of '{}' don't match mask - output will be sanitized", getName());
  127. LOG.atDebug().log(() -> {
  128. StringBuilder sb = new StringBuilder("The following style attributes of the '")
  129. .append(getName()).append("' property will be ignored:\n");
  130. int i=0;
  131. for (int mask : subPropMasks) {
  132. if (!subPropMatches[i] && (val & mask) != 0) {
  133. sb.append(subPropNames[i]).append(",");
  134. }
  135. i++;
  136. }
  137. return new SimpleMessage(sb);
  138. });
  139. }
  140. }
  141. /**
  142. * Fetch the true/false status of the subproperty with the given index
  143. */
  144. public boolean getSubValue(int idx) {
  145. return subPropMatches[idx] && ((super.getValue() & subPropMasks[idx]) != 0);
  146. }
  147. /**
  148. * Set the true/false status of the subproperty with the given index
  149. */
  150. public void setSubValue(boolean value, int idx) {
  151. subPropMatches[idx] = true;
  152. int newVal = super.getValue();
  153. if (value) {
  154. newVal |= subPropMasks[idx];
  155. } else {
  156. newVal &= ~subPropMasks[idx];
  157. }
  158. super.setValue(newVal);
  159. }
  160. /**
  161. * @return an identical copy of this, i.e. also the subPropMatches are copied
  162. */
  163. public BitMaskTextProp cloneAll(){
  164. BitMaskTextProp bmtp = copy();
  165. if (subPropMatches != null) {
  166. System.arraycopy(subPropMatches, 0, bmtp.subPropMatches, 0, subPropMatches.length);
  167. }
  168. return bmtp;
  169. }
  170. @Override
  171. public Map<String, Supplier<?>> getGenericProperties() {
  172. return GenericRecordUtil.getGenericProperties(
  173. "base", super::getGenericProperties,
  174. "flags", getBitsAsString(this::getValue, subPropMasks, subPropNames)
  175. );
  176. }
  177. @Override
  178. public abstract BitMaskTextProp copy();
  179. }