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.

StyleSheet.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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.io.IOException;
  17. import java.io.OutputStream;
  18. import org.apache.poi.hwpf.sprm.CharacterSprmUncompressor;
  19. import org.apache.poi.hwpf.sprm.ParagraphSprmUncompressor;
  20. import org.apache.poi.hwpf.usermodel.CharacterProperties;
  21. import org.apache.poi.hwpf.usermodel.ParagraphProperties;
  22. import org.apache.poi.util.Internal;
  23. import org.apache.poi.util.LittleEndian;
  24. import org.apache.poi.util.LittleEndianConsts;
  25. /**
  26. * Represents a document's stylesheet. A word documents formatting is stored as
  27. * compressed styles that are based on styles contained in the stylesheet. This
  28. * class also contains static utility functions to uncompress different
  29. * formatting properties.
  30. * <p>
  31. * Fields documentation is quotes from Microsoft Office Word 97-2007 Binary File
  32. * Format (.doc) Specification, page 36 of 210
  33. *
  34. * @author Ryan Ackley
  35. */
  36. @Internal
  37. public final class StyleSheet {
  38. public static final int NIL_STYLE = 4095;
  39. // private static final int PAP_TYPE = 1;
  40. // private static final int CHP_TYPE = 2;
  41. // private static final int SEP_TYPE = 4;
  42. // private static final int TAP_TYPE = 5;
  43. @Deprecated
  44. private final static ParagraphProperties NIL_PAP = new ParagraphProperties();
  45. @Deprecated
  46. private final static CharacterProperties NIL_CHP = new CharacterProperties();
  47. private final static byte[] NIL_CHPX = new byte[]{};
  48. private final static byte[] NIL_PAPX = new byte[]{0, 0};
  49. /**
  50. * Size of the STSHI structure
  51. */
  52. private int _cbStshi;
  53. /**
  54. * General information about a stylesheet
  55. */
  56. private Stshif _stshif;
  57. StyleDescription[] _styleDescriptions;
  58. /**
  59. * StyleSheet constructor. Loads a document's stylesheet information,
  60. *
  61. * @param tableStream A byte array containing a document's raw stylesheet
  62. * info. Found by using FileInformationBlock.getFcStshf() and
  63. * FileInformationBLock.getLcbStshf()
  64. */
  65. public StyleSheet(byte[] tableStream, int offset) {
  66. int startOffset = offset;
  67. _cbStshi = LittleEndian.getShort(tableStream, offset);
  68. offset += LittleEndianConsts.SHORT_SIZE;
  69. /*
  70. * Count of styles in stylesheet
  71. *
  72. * The number of styles in this style sheet. There will be stshi.cstd
  73. * (cbSTD, STD) pairs in the file following the STSHI. Note: styles can
  74. * be empty, i.e. cbSTD==0.
  75. */
  76. _stshif = new Stshif(tableStream, offset);
  77. // shall we discard cbLSD and mpstilsd?
  78. offset = startOffset + LittleEndianConsts.SHORT_SIZE + _cbStshi;
  79. _styleDescriptions = new StyleDescription[_stshif.getCstd()];
  80. for (int x = 0; x < _stshif.getCstd(); x++) {
  81. int stdSize = LittleEndian.getShort(tableStream, offset);
  82. //get past the size
  83. offset += 2;
  84. if (stdSize > 0) {
  85. //byte[] std = new byte[stdSize];
  86. StyleDescription aStyle = new StyleDescription(tableStream,
  87. _stshif.getCbSTDBaseInFile(), offset, true);
  88. _styleDescriptions[x] = aStyle;
  89. }
  90. offset += stdSize;
  91. }
  92. for (int x = 0; x < _styleDescriptions.length; x++) {
  93. if (_styleDescriptions[x] != null) {
  94. createPap(x);
  95. createChp(x);
  96. }
  97. }
  98. }
  99. public void writeTo(OutputStream out)
  100. throws IOException {
  101. int offset = 0;
  102. /*
  103. * we don't support 2003 Word extensions in STSHI (but may be we should
  104. * at least not delete them, shouldn't we?), so our structure is always
  105. * 18 bytes in length -- sergey
  106. */
  107. this._cbStshi = 18;
  108. // add two bytes so we can prepend the stylesheet w/ its size
  109. byte[] buf = new byte[_cbStshi + 2];
  110. LittleEndian.putUShort(buf, offset, (short) _cbStshi);
  111. offset += LittleEndianConsts.SHORT_SIZE;
  112. _stshif.setCstd(_styleDescriptions.length);
  113. _stshif.serialize(buf, offset);
  114. // offset += Stshif.getSize();
  115. out.write(buf);
  116. byte[] sizeHolder = new byte[2];
  117. for (StyleDescription styleDescription : _styleDescriptions) {
  118. if (styleDescription != null) {
  119. byte[] std = styleDescription.toByteArray();
  120. // adjust the size so it is always on a word boundary
  121. LittleEndian.putShort(sizeHolder, 0, (short) ((std.length) + (std.length % 2)));
  122. out.write(sizeHolder);
  123. out.write(std);
  124. // Must always start on a word boundary.
  125. if (std.length % 2 == 1) {
  126. out.write('\0');
  127. }
  128. } else {
  129. sizeHolder[0] = 0;
  130. sizeHolder[1] = 0;
  131. out.write(sizeHolder);
  132. }
  133. }
  134. }
  135. @Override
  136. public boolean equals(Object o) {
  137. if (!(o instanceof StyleSheet)) return false;
  138. StyleSheet ss = (StyleSheet) o;
  139. if (!ss._stshif.equals(this._stshif)
  140. || ss._cbStshi != this._cbStshi
  141. || ss._styleDescriptions.length != this._styleDescriptions.length
  142. ) return false;
  143. for (int i = 0; i < _styleDescriptions.length; i++) {
  144. StyleDescription tsd = this._styleDescriptions[i];
  145. StyleDescription osd = ss._styleDescriptions[i];
  146. if (tsd == null && osd == null) continue;
  147. if (osd == null || !osd.equals(tsd)) return false;
  148. }
  149. return true;
  150. }
  151. @Override
  152. public int hashCode() {
  153. assert false : "hashCode not designed";
  154. return 42; // any arbitrary constant will do
  155. }
  156. /**
  157. * Creates a PartagraphProperties object from a papx stored in the
  158. * StyleDescription at the index istd in the StyleDescription array. The PAP
  159. * is placed in the StyleDescription at istd after its been created. Not
  160. * every StyleDescription will contain a papx. In these cases this function
  161. * does nothing
  162. *
  163. * @param istd The index of the StyleDescription to create the
  164. * ParagraphProperties from (and also place the finished PAP in)
  165. */
  166. @Deprecated
  167. private void createPap(int istd) {
  168. StyleDescription sd = _styleDescriptions[istd];
  169. ParagraphProperties pap = sd.getPAP();
  170. byte[] papx = sd.getPAPX();
  171. int baseIndex = sd.getBaseStyle();
  172. if (pap == null && papx != null) {
  173. ParagraphProperties parentPAP = new ParagraphProperties();
  174. if (baseIndex != NIL_STYLE) {
  175. parentPAP = _styleDescriptions[baseIndex].getPAP();
  176. if (parentPAP == null) {
  177. if (baseIndex == istd) {
  178. // Oh dear, style claims that it is its own parent
  179. throw new IllegalStateException("Pap style " + istd + " claimed to have itself as its parent, which isn't allowed");
  180. }
  181. // Create the parent style
  182. createPap(baseIndex);
  183. parentPAP = _styleDescriptions[baseIndex].getPAP();
  184. }
  185. }
  186. if (parentPAP == null) {
  187. parentPAP = new ParagraphProperties();
  188. }
  189. pap = ParagraphSprmUncompressor.uncompressPAP(parentPAP, papx, 2);
  190. sd.setPAP(pap);
  191. }
  192. }
  193. /**
  194. * Creates a CharacterProperties object from a chpx stored in the
  195. * StyleDescription at the index istd in the StyleDescription array. The
  196. * CharacterProperties object is placed in the StyleDescription at istd after
  197. * its been created. Not every StyleDescription will contain a chpx. In these
  198. * cases this function does nothing.
  199. *
  200. * @param istd The index of the StyleDescription to create the
  201. * CharacterProperties object from.
  202. */
  203. @Deprecated
  204. private void createChp(int istd) {
  205. StyleDescription sd = _styleDescriptions[istd];
  206. CharacterProperties chp = sd.getCHP();
  207. byte[] chpx = sd.getCHPX();
  208. int baseIndex = sd.getBaseStyle();
  209. if (baseIndex == istd) {
  210. // Oh dear, this isn't allowed...
  211. // The word file seems to be corrupted
  212. // Switch to using the nil style so that
  213. // there's a chance we can read it
  214. baseIndex = NIL_STYLE;
  215. }
  216. // Build and decompress the Chp if required
  217. if (chp == null && chpx != null) {
  218. CharacterProperties parentCHP = new CharacterProperties();
  219. if (baseIndex != NIL_STYLE) {
  220. parentCHP = _styleDescriptions[baseIndex].getCHP();
  221. if (parentCHP == null) {
  222. createChp(baseIndex);
  223. parentCHP = _styleDescriptions[baseIndex].getCHP();
  224. }
  225. if (parentCHP == null) {
  226. parentCHP = new CharacterProperties();
  227. }
  228. }
  229. chp = CharacterSprmUncompressor.uncompressCHP(parentCHP, chpx, 0);
  230. sd.setCHP(chp);
  231. }
  232. }
  233. /**
  234. * Gets the number of styles in the style sheet.
  235. *
  236. * @return The number of styles in the style sheet.
  237. */
  238. public int numStyles() {
  239. return _styleDescriptions.length;
  240. }
  241. /**
  242. * Gets the StyleDescription at index x.
  243. *
  244. * @param styleIndex the index of the desired StyleDescription.
  245. */
  246. public StyleDescription getStyleDescription(int styleIndex) {
  247. return _styleDescriptions[styleIndex];
  248. }
  249. @Deprecated
  250. public CharacterProperties getCharacterStyle(int styleIndex) {
  251. if (styleIndex == NIL_STYLE) {
  252. return NIL_CHP;
  253. }
  254. if (styleIndex >= _styleDescriptions.length) {
  255. return NIL_CHP;
  256. }
  257. if (styleIndex == -1) {
  258. return NIL_CHP;
  259. }
  260. return (_styleDescriptions[styleIndex] != null ? _styleDescriptions[styleIndex]
  261. .getCHP() : NIL_CHP);
  262. }
  263. @Deprecated
  264. public ParagraphProperties getParagraphStyle(int styleIndex) {
  265. if (styleIndex == NIL_STYLE) {
  266. return NIL_PAP;
  267. }
  268. if (styleIndex >= _styleDescriptions.length) {
  269. return NIL_PAP;
  270. }
  271. if (styleIndex == -1) {
  272. return NIL_PAP;
  273. }
  274. if (_styleDescriptions[styleIndex] == null) {
  275. return NIL_PAP;
  276. }
  277. if (_styleDescriptions[styleIndex].getPAP() == null) {
  278. return NIL_PAP;
  279. }
  280. return _styleDescriptions[styleIndex].getPAP();
  281. }
  282. public byte[] getCHPX(int styleIndex) {
  283. if (styleIndex == NIL_STYLE) {
  284. return NIL_CHPX;
  285. }
  286. if (styleIndex >= _styleDescriptions.length) {
  287. return NIL_CHPX;
  288. }
  289. if (styleIndex == -1) {
  290. return NIL_CHPX;
  291. }
  292. if (_styleDescriptions[styleIndex] == null) {
  293. return NIL_CHPX;
  294. }
  295. if (_styleDescriptions[styleIndex].getCHPX() == null) {
  296. return NIL_CHPX;
  297. }
  298. return _styleDescriptions[styleIndex].getCHPX();
  299. }
  300. public byte[] getPAPX(int styleIndex) {
  301. if (styleIndex == NIL_STYLE) {
  302. return NIL_PAPX;
  303. }
  304. if (styleIndex >= _styleDescriptions.length) {
  305. return NIL_PAPX;
  306. }
  307. if (styleIndex == -1) {
  308. return NIL_PAPX;
  309. }
  310. if (_styleDescriptions[styleIndex] == null) {
  311. return NIL_PAPX;
  312. }
  313. if (_styleDescriptions[styleIndex].getPAPX() == null) {
  314. return NIL_PAPX;
  315. }
  316. return _styleDescriptions[styleIndex].getPAPX();
  317. }
  318. }