PageRenderTime 69ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/org/apache/poi/hwpf/model/StyleSheet.java

https://github.com/minstrelsy/SimpleAndroidDocView
Java | 404 lines | 248 code | 59 blank | 97 comment | 66 complexity | 77aa9fdbea42509b8b2733de8923350a MD5 | raw file
Possible License(s): Apache-2.0
  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 org.apache.poi.hwpf.model.io.HWPFOutputStream;
  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(HWPFOutputStream 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, (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. public boolean equals(Object o)
  147. {
  148. StyleSheet ss = (StyleSheet)o;
  149. if (ss._stshif.equals( this._stshif ) && ss._cbStshi == _cbStshi)
  150. {
  151. if (ss._styleDescriptions.length == _styleDescriptions.length)
  152. {
  153. for (int x = 0; x < _styleDescriptions.length; x++)
  154. {
  155. // check for null
  156. if (ss._styleDescriptions[x] != _styleDescriptions[x])
  157. {
  158. // check for equality
  159. if (!ss._styleDescriptions[x].equals(_styleDescriptions[x]))
  160. {
  161. return false;
  162. }
  163. }
  164. }
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. /**
  171. * Creates a PartagraphProperties object from a papx stored in the
  172. * StyleDescription at the index istd in the StyleDescription array. The PAP
  173. * is placed in the StyleDescription at istd after its been created. Not
  174. * every StyleDescription will contain a papx. In these cases this function
  175. * does nothing
  176. *
  177. * @param istd The index of the StyleDescription to create the
  178. * ParagraphProperties from (and also place the finished PAP in)
  179. */
  180. @Deprecated
  181. private void createPap(int istd)
  182. {
  183. StyleDescription sd = _styleDescriptions[istd];
  184. ParagraphProperties pap = sd.getPAP();
  185. byte[] papx = sd.getPAPX();
  186. int baseIndex = sd.getBaseStyle();
  187. if(pap == null && papx != null)
  188. {
  189. ParagraphProperties parentPAP = new ParagraphProperties();
  190. if(baseIndex != NIL_STYLE)
  191. {
  192. parentPAP = _styleDescriptions[baseIndex].getPAP();
  193. if(parentPAP == null) {
  194. if(baseIndex == istd) {
  195. // Oh dear, style claims that it is its own parent
  196. throw new IllegalStateException("Pap style " + istd + " claimed to have itself as its parent, which isn't allowed");
  197. }
  198. // Create the parent style
  199. createPap(baseIndex);
  200. parentPAP = _styleDescriptions[baseIndex].getPAP();
  201. }
  202. }
  203. if (parentPAP == null) {
  204. parentPAP = new ParagraphProperties();
  205. }
  206. pap = ParagraphSprmUncompressor.uncompressPAP(parentPAP, papx, 2);
  207. sd.setPAP(pap);
  208. }
  209. }
  210. /**
  211. * Creates a CharacterProperties object from a chpx stored in the
  212. * StyleDescription at the index istd in the StyleDescription array. The
  213. * CharacterProperties object is placed in the StyleDescription at istd after
  214. * its been created. Not every StyleDescription will contain a chpx. In these
  215. * cases this function does nothing.
  216. *
  217. * @param istd The index of the StyleDescription to create the
  218. * CharacterProperties object from.
  219. */
  220. @Deprecated
  221. private void createChp(int istd)
  222. {
  223. StyleDescription sd = _styleDescriptions[istd];
  224. CharacterProperties chp = sd.getCHP();
  225. byte[] chpx = sd.getCHPX();
  226. int baseIndex = sd.getBaseStyle();
  227. if(baseIndex == istd) {
  228. // Oh dear, this isn't allowed...
  229. // The word file seems to be corrupted
  230. // Switch to using the nil style so that
  231. // there's a chance we can read it
  232. baseIndex = NIL_STYLE;
  233. }
  234. // Build and decompress the Chp if required
  235. if(chp == null && chpx != null)
  236. {
  237. CharacterProperties parentCHP = new CharacterProperties();
  238. if(baseIndex != NIL_STYLE)
  239. {
  240. parentCHP = _styleDescriptions[baseIndex].getCHP();
  241. if(parentCHP == null)
  242. {
  243. createChp(baseIndex);
  244. parentCHP = _styleDescriptions[baseIndex].getCHP();
  245. }
  246. }
  247. chp = CharacterSprmUncompressor.uncompressCHP(parentCHP, chpx, 0);
  248. sd.setCHP(chp);
  249. }
  250. }
  251. /**
  252. * Gets the number of styles in the style sheet.
  253. * @return The number of styles in the style sheet.
  254. */
  255. public int numStyles() {
  256. return _styleDescriptions.length;
  257. }
  258. /**
  259. * Gets the StyleDescription at index x.
  260. *
  261. * @param styleIndex
  262. * the index of the desired StyleDescription.
  263. */
  264. public StyleDescription getStyleDescription( int styleIndex )
  265. {
  266. return _styleDescriptions[styleIndex];
  267. }
  268. @Deprecated
  269. public CharacterProperties getCharacterStyle( int styleIndex )
  270. {
  271. if ( styleIndex == NIL_STYLE )
  272. {
  273. return NIL_CHP;
  274. }
  275. if ( styleIndex >= _styleDescriptions.length )
  276. {
  277. return NIL_CHP;
  278. }
  279. return ( _styleDescriptions[styleIndex] != null ? _styleDescriptions[styleIndex]
  280. .getCHP() : NIL_CHP );
  281. }
  282. @Deprecated
  283. public ParagraphProperties getParagraphStyle( int styleIndex )
  284. {
  285. if ( styleIndex == NIL_STYLE )
  286. {
  287. return NIL_PAP;
  288. }
  289. if ( styleIndex >= _styleDescriptions.length )
  290. {
  291. return NIL_PAP;
  292. }
  293. if ( _styleDescriptions[styleIndex] == null )
  294. {
  295. return NIL_PAP;
  296. }
  297. if ( _styleDescriptions[styleIndex].getPAP() == null )
  298. {
  299. return NIL_PAP;
  300. }
  301. return _styleDescriptions[styleIndex].getPAP();
  302. }
  303. public byte[] getCHPX( int styleIndex )
  304. {
  305. if ( styleIndex == NIL_STYLE )
  306. {
  307. return NIL_CHPX;
  308. }
  309. if ( styleIndex >= _styleDescriptions.length )
  310. {
  311. return NIL_CHPX;
  312. }
  313. if ( _styleDescriptions[styleIndex] == null )
  314. {
  315. return NIL_CHPX;
  316. }
  317. if ( _styleDescriptions[styleIndex].getCHPX() == null )
  318. {
  319. return NIL_CHPX;
  320. }
  321. return _styleDescriptions[styleIndex].getCHPX();
  322. }
  323. public byte[] getPAPX( int styleIndex )
  324. {
  325. if ( styleIndex == NIL_STYLE )
  326. {
  327. return NIL_PAPX;
  328. }
  329. if ( styleIndex >= _styleDescriptions.length )
  330. {
  331. return NIL_PAPX;
  332. }
  333. if ( _styleDescriptions[styleIndex] == null )
  334. {
  335. return NIL_PAPX;
  336. }
  337. if ( _styleDescriptions[styleIndex].getPAPX() == null )
  338. {
  339. return NIL_PAPX;
  340. }
  341. return _styleDescriptions[styleIndex].getPAPX();
  342. }
  343. }