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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 org.apache.poi.hwpf.usermodel.CharacterProperties;
  18. import org.apache.poi.hwpf.usermodel.ParagraphProperties;
  19. import org.apache.poi.util.IOUtils;
  20. import org.apache.poi.util.Internal;
  21. import org.apache.poi.util.LittleEndian;
  22. import org.apache.poi.util.LittleEndianConsts;
  23. import org.apache.poi.util.POILogFactory;
  24. import org.apache.poi.util.POILogger;
  25. import org.apache.poi.util.StringUtil;
  26. /**
  27. * Comment me
  28. *
  29. * @author Ryan Ackley
  30. */
  31. @Internal
  32. public final class StyleDescription {
  33. private static final POILogger logger = POILogFactory.getLogger(StyleDescription.class);
  34. //arbitrarily selected; may need to increase
  35. private static final int MAX_RECORD_LENGTH = 100_000;
  36. private final static int PARAGRAPH_STYLE = 1;
  37. private final static int CHARACTER_STYLE = 2;
  38. // private final static int TABLE_STYLE = 3;
  39. // private final static int NUMBERING_STYLE = 4;
  40. private int _baseLength;
  41. private StdfBase _stdfBase;
  42. private StdfPost2000 _stdfPost2000;
  43. UPX[] _upxs;
  44. String _name;
  45. @Deprecated
  46. ParagraphProperties _pap;
  47. @Deprecated
  48. CharacterProperties _chp;
  49. public StyleDescription() {
  50. // _pap = new ParagraphProperties();
  51. // _chp = new CharacterProperties();
  52. }
  53. public StyleDescription(byte[] std, int baseLength, int offset, boolean word9) {
  54. _baseLength = baseLength;
  55. int nameStart = offset + baseLength;
  56. boolean readStdfPost2000 = false;
  57. if (baseLength == 0x0012) {
  58. readStdfPost2000 = true;
  59. } else if (baseLength == 0x000A) {
  60. readStdfPost2000 = false;
  61. } else {
  62. logger.log(POILogger.WARN,
  63. "Style definition has non-standard size of ",
  64. Integer.valueOf(baseLength));
  65. }
  66. _stdfBase = new StdfBase(std, offset);
  67. offset += StdfBase.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. switch (_stdfBase.getStk()) {
  121. case PARAGRAPH_STYLE:
  122. return _upxs[0].getUPX();
  123. default:
  124. return null;
  125. }
  126. }
  127. @Deprecated
  128. public ParagraphProperties getPAP() {
  129. return _pap;
  130. }
  131. @Deprecated
  132. public CharacterProperties getCHP() {
  133. return _chp;
  134. }
  135. @Deprecated
  136. void setPAP(ParagraphProperties pap) {
  137. _pap = pap;
  138. }
  139. @Deprecated
  140. void setCHP(CharacterProperties chp) {
  141. _chp = chp;
  142. }
  143. public String getName() {
  144. return _name;
  145. }
  146. public byte[] toByteArray() {
  147. // size equals _baseLength bytes for known variables plus 2 bytes for name
  148. // length plus name length * 2 plus 2 bytes for null plus upx's preceded by
  149. // length
  150. int size = _baseLength + 2 + ((_name.length() + 1) * 2);
  151. // determine the size needed for the upxs. They always fall on word
  152. // boundaries.
  153. size += _upxs[0].size() + 2;
  154. for (int x = 1; x < _upxs.length; x++) {
  155. size += _upxs[x - 1].size() % 2;
  156. size += _upxs[x].size() + 2;
  157. }
  158. byte[] buf = new byte[size];
  159. _stdfBase.serialize(buf, 0);
  160. int offset = _baseLength;
  161. char[] letters = _name.toCharArray();
  162. LittleEndian.putShort(buf, _baseLength, (short) letters.length);
  163. offset += LittleEndianConsts.SHORT_SIZE;
  164. for (int x = 0; x < letters.length; x++) {
  165. LittleEndian.putShort(buf, offset, (short) letters[x]);
  166. offset += LittleEndianConsts.SHORT_SIZE;
  167. }
  168. // get past the null delimiter for the name.
  169. offset += LittleEndianConsts.SHORT_SIZE;
  170. for (int x = 0; x < _upxs.length; x++) {
  171. short upxSize = (short) _upxs[x].size();
  172. LittleEndian.putShort(buf, offset, upxSize);
  173. offset += LittleEndianConsts.SHORT_SIZE;
  174. System.arraycopy(_upxs[x].getUPX(), 0, buf, offset, upxSize);
  175. offset += upxSize + (upxSize % 2);
  176. }
  177. return buf;
  178. }
  179. @Override
  180. public int hashCode() {
  181. return Arrays.deepHashCode(new Object[]{_name,_stdfBase,_upxs});
  182. }
  183. @Override
  184. public boolean equals(Object obj) {
  185. if (this == obj)
  186. return true;
  187. if (obj == null)
  188. return false;
  189. if (getClass() != obj.getClass())
  190. return false;
  191. StyleDescription other = (StyleDescription) obj;
  192. if (_name == null) {
  193. if (other._name != null)
  194. return false;
  195. } else if (!_name.equals(other._name))
  196. return false;
  197. if (_stdfBase == null) {
  198. if (other._stdfBase != null)
  199. return false;
  200. } else if (!_stdfBase.equals(other._stdfBase))
  201. return false;
  202. if (!Arrays.equals(_upxs, other._upxs))
  203. return false;
  204. return true;
  205. }
  206. @Override
  207. public String toString() {
  208. StringBuilder result = new StringBuilder();
  209. result.append("[STD]: '");
  210. result.append(_name);
  211. result.append("'");
  212. result.append(("\nStdfBase:\t" + _stdfBase).replaceAll("\n",
  213. "\n "));
  214. result.append(("\nStdfPost2000:\t" + _stdfPost2000).replaceAll(
  215. "\n", "\n "));
  216. for (UPX upx : _upxs) {
  217. result.append(("\nUPX:\t" + upx).replaceAll("\n", "\n "));
  218. }
  219. return result.toString();
  220. }
  221. }