Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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