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.

StyleDescription.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.Objects;
  18. import org.apache.poi.hwpf.model.types.StdfBaseAbstractType;
  19. import org.apache.poi.hwpf.usermodel.CharacterProperties;
  20. import org.apache.poi.hwpf.usermodel.ParagraphProperties;
  21. import org.apache.poi.util.IOUtils;
  22. import org.apache.poi.util.Internal;
  23. import org.apache.poi.util.LittleEndian;
  24. import org.apache.poi.util.LittleEndianConsts;
  25. import org.apache.poi.util.POILogFactory;
  26. import org.apache.poi.util.POILogger;
  27. import org.apache.poi.util.StringUtil;
  28. /**
  29. * Comment me
  30. *
  31. * @author Ryan Ackley
  32. */
  33. @Internal
  34. public final class StyleDescription {
  35. private static final POILogger logger = POILogFactory.getLogger(StyleDescription.class);
  36. //arbitrarily selected; may need to increase
  37. private static final int MAX_RECORD_LENGTH = 100_000;
  38. private final static int PARAGRAPH_STYLE = 1;
  39. private final static int CHARACTER_STYLE = 2;
  40. // private final static int TABLE_STYLE = 3;
  41. // private final static int NUMBERING_STYLE = 4;
  42. private int _baseLength;
  43. private StdfBase _stdfBase;
  44. private StdfPost2000 _stdfPost2000;
  45. UPX[] _upxs;
  46. String _name;
  47. @Deprecated
  48. ParagraphProperties _pap;
  49. @Deprecated
  50. CharacterProperties _chp;
  51. public StyleDescription() {
  52. // _pap = new ParagraphProperties();
  53. // _chp = new CharacterProperties();
  54. }
  55. public StyleDescription(byte[] std, int baseLength, int offset, boolean word9) {
  56. _baseLength = baseLength;
  57. int nameStart = offset + baseLength;
  58. boolean readStdfPost2000 = false;
  59. if (baseLength == 0x0012) {
  60. readStdfPost2000 = true;
  61. } else if (baseLength == 0x000A) {
  62. readStdfPost2000 = false;
  63. } else {
  64. logger.log(POILogger.WARN, "Style definition has non-standard size of ", baseLength);
  65. }
  66. _stdfBase = new StdfBase(std, offset);
  67. offset += StdfBaseAbstractType.getSize();
  68. if (readStdfPost2000) {
  69. _stdfPost2000 = new StdfPost2000(std, offset);
  70. // offset += StdfPost2000.getSize();
  71. }
  72. //first byte(s) of variable length section of std is the length of the
  73. //style name and aliases string
  74. int nameLength = 0;
  75. int multiplier = 1;
  76. if (word9) {
  77. nameLength = LittleEndian.getShort(std, nameStart);
  78. multiplier = 2;
  79. nameStart += LittleEndianConsts.SHORT_SIZE;
  80. } else {
  81. nameLength = std[nameStart];
  82. }
  83. _name = StringUtil.getFromUnicodeLE(std, nameStart, (nameLength * multiplier) / 2);
  84. //length then null terminator.
  85. // the spec only refers to two possible upxs but it mentions
  86. // that more may be added in the future
  87. int varOffset = ((nameLength + 1) * multiplier) + nameStart;
  88. int countOfUPX = _stdfBase.getCupx();
  89. _upxs = new UPX[countOfUPX];
  90. for (int x = 0; x < countOfUPX; x++) {
  91. int upxSize = LittleEndian.getShort(std, varOffset);
  92. varOffset += LittleEndianConsts.SHORT_SIZE;
  93. byte[] upx = IOUtils.safelyAllocate(upxSize, Short.MAX_VALUE);
  94. System.arraycopy(std, varOffset, upx, 0, upxSize);
  95. _upxs[x] = new UPX(upx);
  96. varOffset += upxSize;
  97. // the upx will always start on a word boundary.
  98. if ((upxSize & 1) == 1) {
  99. ++varOffset;
  100. }
  101. }
  102. }
  103. public int getBaseStyle() {
  104. return _stdfBase.getIstdBase();
  105. }
  106. public byte[] getCHPX() {
  107. switch (_stdfBase.getStk()) {
  108. case PARAGRAPH_STYLE:
  109. if (_upxs.length > 1) {
  110. return _upxs[1].getUPX();
  111. }
  112. return null;
  113. case CHARACTER_STYLE:
  114. return _upxs[0].getUPX();
  115. default:
  116. return null;
  117. }
  118. }
  119. public byte[] getPAPX() {
  120. return _stdfBase.getStk() == PARAGRAPH_STYLE ? _upxs[0].getUPX() : null;
  121. }
  122. @Deprecated
  123. public ParagraphProperties getPAP() {
  124. return _pap;
  125. }
  126. @Deprecated
  127. public CharacterProperties getCHP() {
  128. return _chp;
  129. }
  130. @Deprecated
  131. void setPAP(ParagraphProperties pap) {
  132. _pap = pap;
  133. }
  134. @Deprecated
  135. void setCHP(CharacterProperties chp) {
  136. _chp = chp;
  137. }
  138. public String getName() {
  139. return _name;
  140. }
  141. public byte[] toByteArray() {
  142. // size equals _baseLength bytes for known variables plus 2 bytes for name
  143. // length plus name length * 2 plus 2 bytes for null plus upx's preceded by
  144. // length
  145. int size = _baseLength + 2 + ((_name.length() + 1) * 2);
  146. // determine the size needed for the upxs. They always fall on word
  147. // boundaries.
  148. size += _upxs[0].size() + 2;
  149. for (int x = 1; x < _upxs.length; x++) {
  150. size += _upxs[x - 1].size() % 2;
  151. size += _upxs[x].size() + 2;
  152. }
  153. byte[] buf = new byte[size];
  154. _stdfBase.serialize(buf, 0);
  155. int offset = _baseLength;
  156. char[] letters = _name.toCharArray();
  157. LittleEndian.putShort(buf, _baseLength, (short) letters.length);
  158. offset += LittleEndianConsts.SHORT_SIZE;
  159. for (char letter : letters) {
  160. LittleEndian.putShort(buf, offset, (short) letter);
  161. offset += LittleEndianConsts.SHORT_SIZE;
  162. }
  163. // get past the null delimiter for the name.
  164. offset += LittleEndianConsts.SHORT_SIZE;
  165. for (UPX upx : _upxs) {
  166. short upxSize = (short) upx.size();
  167. LittleEndian.putShort(buf, offset, upxSize);
  168. offset += LittleEndianConsts.SHORT_SIZE;
  169. System.arraycopy(upx.getUPX(), 0, buf, offset, upxSize);
  170. offset += upxSize + (upxSize % 2);
  171. }
  172. return buf;
  173. }
  174. @Override
  175. public int hashCode() {
  176. return Arrays.deepHashCode(new Object[]{_name,_stdfBase,_upxs});
  177. }
  178. @Override
  179. public boolean equals(Object obj) {
  180. if (this == obj)
  181. return true;
  182. if (!(obj instanceof StyleDescription))
  183. return false;
  184. StyleDescription other = (StyleDescription) obj;
  185. if (!Objects.equals(_name, other._name)) {
  186. return false;
  187. }
  188. if (!Objects.equals(_stdfBase, other._stdfBase)) {
  189. return false;
  190. }
  191. return Arrays.equals(_upxs, other._upxs);
  192. }
  193. @Override
  194. public String toString() {
  195. StringBuilder result = new StringBuilder();
  196. result.append("[STD]: '");
  197. result.append(_name);
  198. result.append("'");
  199. result.append(("\nStdfBase:\t" + _stdfBase).replaceAll("\n",
  200. "\n "));
  201. result.append(("\nStdfPost2000:\t" + _stdfPost2000).replaceAll(
  202. "\n", "\n "));
  203. for (UPX upx : _upxs) {
  204. result.append(("\nUPX:\t" + upx).replaceAll("\n", "\n "));
  205. }
  206. return result.toString();
  207. }
  208. }