PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/apache/poi/hwpf/usermodel/Paragraph.java

https://github.com/minstrelsy/SimpleAndroidDocView
Java | 659 lines | 509 code | 92 blank | 58 comment | 24 complexity | d9e337622aaacee6d2790b7572b0af11 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.usermodel;
  16. import java.util.NoSuchElementException;
  17. import org.apache.poi.hwpf.HWPFDocumentCore;
  18. import org.apache.poi.hwpf.model.LFO;
  19. import org.apache.poi.hwpf.model.ListLevel;
  20. import org.apache.poi.hwpf.model.ListTables;
  21. import org.apache.poi.hwpf.model.PAPX;
  22. import org.apache.poi.hwpf.model.StyleSheet;
  23. import org.apache.poi.hwpf.sprm.ParagraphSprmUncompressor;
  24. import org.apache.poi.hwpf.sprm.SprmBuffer;
  25. import org.apache.poi.hwpf.sprm.TableSprmCompressor;
  26. import org.apache.poi.util.Internal;
  27. import org.apache.poi.util.POILogFactory;
  28. import org.apache.poi.util.POILogger;
  29. public class Paragraph extends Range implements Cloneable
  30. {
  31. private static POILogger log = POILogFactory.getLogger( Paragraph.class );
  32. public final static short SPRM_JC = 0x2403;
  33. public final static short SPRM_FSIDEBYSIDE = 0x2404;
  34. public final static short SPRM_FKEEP = 0x2405;
  35. public final static short SPRM_FKEEPFOLLOW = 0x2406;
  36. public final static short SPRM_FPAGEBREAKBEFORE = 0x2407;
  37. public final static short SPRM_BRCL = 0x2408;
  38. public final static short SPRM_BRCP = 0x2409;
  39. public final static short SPRM_ILVL = 0x260A;
  40. public final static short SPRM_ILFO = 0x460B;
  41. public final static short SPRM_FNOLINENUMB = 0x240C;
  42. public final static short SPRM_CHGTABSPAPX = (short)0xC60D;
  43. public final static short SPRM_DXARIGHT = (short)0x840E;
  44. public final static short SPRM_DXALEFT = (short)0x840F;
  45. public final static short SPRM_DXALEFT1 = (short)0x8411;
  46. public final static short SPRM_DYALINE = 0x6412;
  47. public final static short SPRM_DYABEFORE = (short)0xA413;
  48. public final static short SPRM_DYAAFTER = (short)0xA414;
  49. public final static short SPRM_CHGTABS = (short)0xC615;
  50. public final static short SPRM_FINTABLE = 0x2416;
  51. public final static short SPRM_FTTP = 0x2417;
  52. public final static short SPRM_DXAABS = (short)0x8418;
  53. public final static short SPRM_DYAABS = (short)0x8419;
  54. public final static short SPRM_DXAWIDTH = (short)0x841A;
  55. public final static short SPRM_PC = 0x261B;
  56. public final static short SPRM_WR = 0x2423;
  57. public final static short SPRM_BRCTOP = 0x6424;
  58. public final static short SPRM_BRCLEFT = 0x6425;
  59. public final static short SPRM_BRCBOTTOM = 0x6426;
  60. public final static short SPRM_BRCRIGHT = 0x6427;
  61. public final static short SPRM_BRCBAR = 0x6629;
  62. public final static short SPRM_FNOAUTOHYPH = 0x242A;
  63. public final static short SPRM_WHEIGHTABS = 0x442B;
  64. public final static short SPRM_DCS = 0x442C;
  65. public final static short SPRM_SHD80 = 0x442D;
  66. public final static short SPRM_SHD = (short)0xC64D;
  67. public final static short SPRM_DYAFROMTEXT = (short)0x842E;
  68. public final static short SPRM_DXAFROMTEXT = (short)0x842F;
  69. public final static short SPRM_FLOCKED = 0x2430;
  70. public final static short SPRM_FWIDOWCONTROL = 0x2431;
  71. public final static short SPRM_RULER = (short)0xC632;
  72. public final static short SPRM_FKINSOKU = 0x2433;
  73. public final static short SPRM_FWORDWRAP = 0x2434;
  74. public final static short SPRM_FOVERFLOWPUNCT = 0x2435;
  75. public final static short SPRM_FTOPLINEPUNCT = 0x2436;
  76. public final static short SPRM_AUTOSPACEDE = 0x2437;
  77. public final static short SPRM_AUTOSPACEDN = 0x2438;
  78. public final static short SPRM_WALIGNFONT = 0x4439;
  79. public final static short SPRM_FRAMETEXTFLOW = 0x443A;
  80. public final static short SPRM_ANLD = (short)0xC63E;
  81. public final static short SPRM_PROPRMARK = (short)0xC63F;
  82. public final static short SPRM_OUTLVL = 0x2640;
  83. public final static short SPRM_FBIDI = 0x2441;
  84. public final static short SPRM_FNUMRMLNS = 0x2443;
  85. public final static short SPRM_CRLF = 0x2444;
  86. public final static short SPRM_NUMRM = (short)0xC645;
  87. public final static short SPRM_USEPGSUSETTINGS = 0x2447;
  88. public final static short SPRM_FADJUSTRIGHT = 0x2448;
  89. @Internal
  90. static Paragraph newParagraph( Range parent, PAPX papx )
  91. {
  92. HWPFDocumentCore doc = parent._doc;
  93. ListTables listTables = doc.getListTables();
  94. StyleSheet styleSheet = doc.getStyleSheet();
  95. ParagraphProperties properties = new ParagraphProperties();
  96. properties.setIstd( papx.getIstd() );
  97. properties = newParagraph_applyStyleProperties( styleSheet, papx,
  98. properties );
  99. properties = ParagraphSprmUncompressor.uncompressPAP( properties,
  100. papx.getGrpprl(), 2 );
  101. if ( properties.getIlfo() != 0 && listTables != null )
  102. {
  103. LFO lfo = null;
  104. try
  105. {
  106. lfo = listTables.getLfo( properties.getIlfo() );
  107. }
  108. catch ( NoSuchElementException exc )
  109. {
  110. log.log( POILogger.WARN, "Paragraph refers to LFO #",
  111. properties.getIlfo(), " that does not exists" );
  112. }
  113. if ( lfo != null )
  114. {
  115. final ListLevel listLevel = listTables.getLevel( lfo.getLsid(),
  116. properties.getIlvl() );
  117. if ( listLevel != null && listLevel.getGrpprlPapx() != null )
  118. {
  119. properties = ParagraphSprmUncompressor.uncompressPAP(
  120. properties, listLevel.getGrpprlPapx(), 0 );
  121. // reapply style and local PAPX properties
  122. properties = newParagraph_applyStyleProperties( styleSheet,
  123. papx, properties );
  124. properties = ParagraphSprmUncompressor.uncompressPAP(
  125. properties, papx.getGrpprl(), 2 );
  126. }
  127. }
  128. }
  129. if ( properties.getIlfo() > 0 )
  130. return new ListEntry( papx, properties, parent );
  131. return new Paragraph( papx, properties, parent );
  132. }
  133. protected static ParagraphProperties newParagraph_applyStyleProperties(
  134. StyleSheet styleSheet, PAPX papx, ParagraphProperties properties )
  135. {
  136. if ( styleSheet == null )
  137. return properties;
  138. int style = papx.getIstd();
  139. byte[] grpprl = styleSheet.getPAPX( style );
  140. return ParagraphSprmUncompressor.uncompressPAP( properties, grpprl, 2 );
  141. }
  142. protected short _istd;
  143. protected ParagraphProperties _props;
  144. protected SprmBuffer _papx;
  145. @Deprecated
  146. protected Paragraph( int startIdxInclusive, int endIdxExclusive,
  147. Table parent )
  148. {
  149. super( startIdxInclusive, endIdxExclusive, parent );
  150. initAll();
  151. PAPX papx = _paragraphs.get( _parEnd - 1 );
  152. _props = papx.getParagraphProperties( _doc.getStyleSheet() );
  153. _papx = papx.getSprmBuf();
  154. _istd = papx.getIstd();
  155. }
  156. @Deprecated
  157. protected Paragraph( PAPX papx, Range parent )
  158. {
  159. super( Math.max( parent._start, papx.getStart() ), Math.min(
  160. parent._end, papx.getEnd() ), parent );
  161. _props = papx.getParagraphProperties( _doc.getStyleSheet() );
  162. _papx = papx.getSprmBuf();
  163. _istd = papx.getIstd();
  164. }
  165. @Deprecated
  166. protected Paragraph( PAPX papx, Range parent, int start )
  167. {
  168. super( Math.max( parent._start, start ), Math.min( parent._end,
  169. papx.getEnd() ), parent );
  170. _props = papx.getParagraphProperties( _doc.getStyleSheet() );
  171. _papx = papx.getSprmBuf();
  172. _istd = papx.getIstd();
  173. }
  174. @Internal
  175. Paragraph( PAPX papx, ParagraphProperties properties, Range parent )
  176. {
  177. super( Math.max( parent._start, papx.getStart() ), Math.min(
  178. parent._end, papx.getEnd() ), parent );
  179. _props = properties;
  180. _papx = papx.getSprmBuf();
  181. _istd = papx.getIstd();
  182. }
  183. public short getStyleIndex()
  184. {
  185. return _istd;
  186. }
  187. @Deprecated
  188. public int type()
  189. {
  190. return TYPE_PARAGRAPH;
  191. }
  192. public boolean isInTable()
  193. {
  194. return _props.getFInTable();
  195. }
  196. /**
  197. * @return <tt>true</tt>, if table trailer paragraph (last in table row),
  198. * <tt>false</tt> otherwise
  199. */
  200. public boolean isTableRowEnd()
  201. {
  202. return _props.getFTtp() || _props.getFTtpEmbedded();
  203. }
  204. public int getTableLevel()
  205. {
  206. return _props.getItap();
  207. }
  208. /**
  209. * @return <tt>true</tt>, if the end of paragraph mark is really an end of
  210. * cell mark for a nested table cell, <tt>false</tt> otherwise
  211. */
  212. public boolean isEmbeddedCellMark()
  213. {
  214. return _props.getFInnerTableCell();
  215. }
  216. public int getJustification()
  217. {
  218. return _props.getJc();
  219. }
  220. public void setJustification(byte jc)
  221. {
  222. _props.setJc(jc);
  223. _papx.updateSprm(SPRM_JC, jc);
  224. }
  225. public boolean keepOnPage()
  226. {
  227. return _props.getFKeep();
  228. }
  229. public void setKeepOnPage(boolean fKeep)
  230. {
  231. _props.setFKeep(fKeep);
  232. _papx.updateSprm(SPRM_FKEEP, fKeep);
  233. }
  234. public boolean keepWithNext()
  235. {
  236. return _props.getFKeepFollow();
  237. }
  238. public void setKeepWithNext(boolean fKeepFollow)
  239. {
  240. _props.setFKeepFollow(fKeepFollow);
  241. _papx.updateSprm(SPRM_FKEEPFOLLOW, fKeepFollow);
  242. }
  243. public boolean pageBreakBefore()
  244. {
  245. return _props.getFPageBreakBefore();
  246. }
  247. public void setPageBreakBefore(boolean fPageBreak)
  248. {
  249. _props.setFPageBreakBefore(fPageBreak);
  250. _papx.updateSprm(SPRM_FPAGEBREAKBEFORE, fPageBreak);
  251. }
  252. public boolean isLineNotNumbered()
  253. {
  254. return _props.getFNoLnn();
  255. }
  256. public void setLineNotNumbered(boolean fNoLnn)
  257. {
  258. _props.setFNoLnn(fNoLnn);
  259. _papx.updateSprm(SPRM_FNOLINENUMB, fNoLnn);
  260. }
  261. public boolean isSideBySide()
  262. {
  263. return _props.getFSideBySide();
  264. }
  265. public void setSideBySide(boolean fSideBySide)
  266. {
  267. _props.setFSideBySide(fSideBySide);
  268. _papx.updateSprm(SPRM_FSIDEBYSIDE, fSideBySide);
  269. }
  270. public boolean isAutoHyphenated()
  271. {
  272. return !_props.getFNoAutoHyph();
  273. }
  274. public void setAutoHyphenated(boolean autoHyph)
  275. {
  276. _props.setFNoAutoHyph(!autoHyph);
  277. _papx.updateSprm(SPRM_FNOAUTOHYPH, !autoHyph);
  278. }
  279. public boolean isWidowControlled()
  280. {
  281. return _props.getFWidowControl();
  282. }
  283. public void setWidowControl(boolean widowControl)
  284. {
  285. _props.setFWidowControl(widowControl);
  286. _papx.updateSprm(SPRM_FWIDOWCONTROL, widowControl);
  287. }
  288. public int getIndentFromRight()
  289. {
  290. return _props.getDxaRight();
  291. }
  292. public void setIndentFromRight(int dxaRight)
  293. {
  294. _props.setDxaRight(dxaRight);
  295. _papx.updateSprm(SPRM_DXARIGHT, (short)dxaRight);
  296. }
  297. public int getIndentFromLeft()
  298. {
  299. return _props.getDxaLeft();
  300. }
  301. public void setIndentFromLeft(int dxaLeft)
  302. {
  303. _props.setDxaLeft(dxaLeft);
  304. _papx.updateSprm(SPRM_DXALEFT, (short)dxaLeft);
  305. }
  306. public int getFirstLineIndent()
  307. {
  308. return _props.getDxaLeft1();
  309. }
  310. public void setFirstLineIndent(int first)
  311. {
  312. _props.setDxaLeft1(first);
  313. _papx.updateSprm(SPRM_DXALEFT1, (short)first);
  314. }
  315. public LineSpacingDescriptor getLineSpacing()
  316. {
  317. return _props.getLspd();
  318. }
  319. public void setLineSpacing(LineSpacingDescriptor lspd)
  320. {
  321. _props.setLspd(lspd);
  322. _papx.updateSprm(SPRM_DYALINE, lspd.toInt());
  323. }
  324. public int getSpacingBefore()
  325. {
  326. return _props.getDyaBefore();
  327. }
  328. public void setSpacingBefore(int before)
  329. {
  330. _props.setDyaBefore(before);
  331. _papx.updateSprm(SPRM_DYABEFORE, (short)before);
  332. }
  333. public int getSpacingAfter()
  334. {
  335. return _props.getDyaAfter();
  336. }
  337. public void setSpacingAfter(int after)
  338. {
  339. _props.setDyaAfter(after);
  340. _papx.updateSprm(SPRM_DYAAFTER, (short)after);
  341. }
  342. public boolean isKinsoku()
  343. {
  344. return _props.getFKinsoku();
  345. }
  346. public void setKinsoku(boolean kinsoku)
  347. {
  348. _props.setFKinsoku(kinsoku);
  349. _papx.updateSprm(SPRM_FKINSOKU, kinsoku);
  350. }
  351. public boolean isWordWrapped()
  352. {
  353. return _props.getFWordWrap();
  354. }
  355. public void setWordWrapped(boolean wrap)
  356. {
  357. _props.setFWordWrap(wrap);
  358. _papx.updateSprm(SPRM_FWORDWRAP, wrap);
  359. }
  360. public int getFontAlignment()
  361. {
  362. return _props.getWAlignFont();
  363. }
  364. public void setFontAlignment(int align)
  365. {
  366. _props.setWAlignFont(align);
  367. _papx.updateSprm(SPRM_WALIGNFONT, (short)align);
  368. }
  369. public boolean isVertical()
  370. {
  371. return _props.isFVertical();
  372. }
  373. public void setVertical(boolean vertical)
  374. {
  375. _props.setFVertical(vertical);
  376. _papx.updateSprm(SPRM_FRAMETEXTFLOW, getFrameTextFlow());
  377. }
  378. public boolean isBackward()
  379. {
  380. return _props.isFBackward();
  381. }
  382. public void setBackward(boolean bward)
  383. {
  384. _props.setFBackward(bward);
  385. _papx.updateSprm(SPRM_FRAMETEXTFLOW, getFrameTextFlow());
  386. }
  387. public BorderCode getTopBorder()
  388. {
  389. return _props.getBrcTop();
  390. }
  391. public void setTopBorder(BorderCode top)
  392. {
  393. _props.setBrcTop(top);
  394. _papx.updateSprm(SPRM_BRCTOP, top.toInt());
  395. }
  396. public BorderCode getLeftBorder()
  397. {
  398. return _props.getBrcLeft();
  399. }
  400. public void setLeftBorder(BorderCode left)
  401. {
  402. _props.setBrcLeft(left);
  403. _papx.updateSprm(SPRM_BRCLEFT, left.toInt());
  404. }
  405. public BorderCode getBottomBorder()
  406. {
  407. return _props.getBrcBottom();
  408. }
  409. public void setBottomBorder(BorderCode bottom)
  410. {
  411. _props.setBrcBottom(bottom);
  412. _papx.updateSprm(SPRM_BRCBOTTOM, bottom.toInt());
  413. }
  414. public BorderCode getRightBorder()
  415. {
  416. return _props.getBrcRight();
  417. }
  418. public void setRightBorder(BorderCode right)
  419. {
  420. _props.setBrcRight(right);
  421. _papx.updateSprm(SPRM_BRCRIGHT, right.toInt());
  422. }
  423. public BorderCode getBarBorder()
  424. {
  425. return _props.getBrcBar();
  426. }
  427. public void setBarBorder(BorderCode bar)
  428. {
  429. _props.setBrcBar(bar);
  430. _papx.updateSprm(SPRM_BRCBAR, bar.toInt());
  431. }
  432. public ShadingDescriptor getShading()
  433. {
  434. return _props.getShd();
  435. }
  436. public void setShading(ShadingDescriptor shd)
  437. {
  438. _props.setShd(shd);
  439. //TODO: remove old one
  440. _papx.addSprm( SPRM_SHD, shd.serialize() );
  441. }
  442. public DropCapSpecifier getDropCap()
  443. {
  444. return _props.getDcs();
  445. }
  446. public void setDropCap(DropCapSpecifier dcs)
  447. {
  448. _props.setDcs(dcs);
  449. _papx.updateSprm(SPRM_DCS, dcs.toShort());
  450. }
  451. /**
  452. * Returns the ilfo, an index to the document's hpllfo, which
  453. * describes the automatic number formatting of the paragraph.
  454. * A value of zero means it isn't numbered.
  455. */
  456. public int getIlfo()
  457. {
  458. return _props.getIlfo();
  459. }
  460. /**
  461. * Returns the multi-level indent for the paragraph. Will be
  462. * zero for non-list paragraphs, and the first level of any
  463. * list. Subsequent levels in hold values 1-8.
  464. */
  465. public int getIlvl()
  466. {
  467. return _props.getIlvl();
  468. }
  469. /**
  470. * Returns the heading level (1-8), or 9 if the paragraph
  471. * isn't in a heading style.
  472. */
  473. public int getLvl()
  474. {
  475. return _props.getLvl();
  476. }
  477. void setTableRowEnd(TableProperties props)
  478. {
  479. setTableRowEnd(true);
  480. byte[] grpprl = TableSprmCompressor.compressTableProperty(props);
  481. _papx.append(grpprl);
  482. }
  483. private void setTableRowEnd(boolean val)
  484. {
  485. _props.setFTtp(val);
  486. _papx.updateSprm(SPRM_FTTP, val);
  487. }
  488. /**
  489. * Returns number of tabs stops defined for paragraph. Must be >= 0 and <=
  490. * 64.
  491. *
  492. * @return number of tabs stops defined for paragraph. Must be >= 0 and <=
  493. * 64
  494. */
  495. public int getTabStopsNumber()
  496. {
  497. return _props.getItbdMac();
  498. }
  499. /**
  500. * Returns array of positions of itbdMac tab stops
  501. *
  502. * @return array of positions of itbdMac tab stops
  503. */
  504. public int[] getTabStopsPositions()
  505. {
  506. return _props.getRgdxaTab();
  507. }
  508. public HWPFList getList()
  509. {
  510. if ( getIlfo() == 0x000 || getIlfo() == 0xF801 )
  511. {
  512. throw new IllegalStateException( "Paragraph not in list" );
  513. }
  514. HWPFList hwpfList = new HWPFList( getDocument().getStyleSheet(),
  515. getDocument().getListTables(), getIlfo() );
  516. return hwpfList;
  517. }
  518. public boolean isInList()
  519. {
  520. return getIlfo() != 0x000 && getIlfo() != 0xF801;
  521. }
  522. /**
  523. * clone the ParagraphProperties object associated with this Paragraph so
  524. * that you can apply the same properties to another paragraph.
  525. *
  526. */
  527. public ParagraphProperties cloneProperties() {
  528. try {
  529. return (ParagraphProperties)_props.clone();
  530. } catch (Exception e) {
  531. throw new RuntimeException(e);
  532. }
  533. }
  534. public Object clone()
  535. throws CloneNotSupportedException
  536. {
  537. Paragraph p = (Paragraph)super.clone();
  538. p._props = (ParagraphProperties)_props.clone();
  539. //p._baseStyle = _baseStyle;
  540. p._papx = new SprmBuffer(0);
  541. return p;
  542. }
  543. private short getFrameTextFlow()
  544. {
  545. short retVal = 0;
  546. if (_props.isFVertical())
  547. {
  548. retVal |= 1;
  549. }
  550. if (_props.isFBackward())
  551. {
  552. retVal |= 2;
  553. }
  554. if (_props.isFRotateFont())
  555. {
  556. retVal |= 4;
  557. }
  558. return retVal;
  559. }
  560. @Override
  561. public String toString()
  562. {
  563. return "Paragraph [" + getStartOffset() + "; " + getEndOffset() + ")";
  564. }
  565. }