PageRenderTime 240ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/thirdparties-extension/org.apache.poi.xwpf.converter.core/src/main/java/org/apache/poi/xwpf/converter/core/styles/XWPFStylesDocument.java

https://github.com/minstrelsy/xdocreport
Java | 1317 lines | 1015 code | 200 blank | 102 comment | 62 complexity | 18cc8374432d7dd6cf1b2a24c157d76a MD5 | raw file
  1. /**
  2. * Copyright (C) 2011-2012 The XDocReport Team <xdocreport@googlegroups.com>
  3. *
  4. * All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. package org.apache.poi.xwpf.converter.core.styles;
  26. import java.awt.Color;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.math.BigInteger;
  30. import java.util.ArrayList;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. import org.apache.poi.POIXMLDocumentPart;
  35. import org.apache.poi.xwpf.converter.core.BorderSide;
  36. import org.apache.poi.xwpf.converter.core.ParagraphLineSpacing;
  37. import org.apache.poi.xwpf.converter.core.TableCellBorder;
  38. import org.apache.poi.xwpf.converter.core.TableHeight;
  39. import org.apache.poi.xwpf.converter.core.TableWidth;
  40. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphAlignmentValueProvider;
  41. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphBackgroundColorValueProvider;
  42. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphBorderBottomValueProvider;
  43. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphBorderLeftValueProvider;
  44. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphBorderRightValueProvider;
  45. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphBorderTopValueProvider;
  46. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphIndentationFirstLineValueProvider;
  47. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphIndentationHangingValueProvider;
  48. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphIndentationLeftValueProvider;
  49. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphIndentationRightValueProvider;
  50. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphLineSpacingValueProvider;
  51. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphNumPrValueProvider;
  52. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphSpacingAfterValueProvider;
  53. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphSpacingBeforeValueProvider;
  54. import org.apache.poi.xwpf.converter.core.styles.paragraph.ParagraphTabsValueProvider;
  55. import org.apache.poi.xwpf.converter.core.styles.run.RunBackgroundColorValueProvider;
  56. import org.apache.poi.xwpf.converter.core.styles.run.RunFontColorValueProvider;
  57. import org.apache.poi.xwpf.converter.core.styles.run.RunFontFamilyAsciiValueProvider;
  58. import org.apache.poi.xwpf.converter.core.styles.run.RunFontFamilyEastAsiaValueProvider;
  59. import org.apache.poi.xwpf.converter.core.styles.run.RunFontFamilyHAnsiValueProvider;
  60. import org.apache.poi.xwpf.converter.core.styles.run.RunFontSizeValueProvider;
  61. import org.apache.poi.xwpf.converter.core.styles.run.RunFontStyleBoldValueProvider;
  62. import org.apache.poi.xwpf.converter.core.styles.run.RunFontStyleItalicValueProvider;
  63. import org.apache.poi.xwpf.converter.core.styles.run.RunFontStyleStrikeValueProvider;
  64. import org.apache.poi.xwpf.converter.core.styles.run.RunTextHighlightingValueProvider;
  65. import org.apache.poi.xwpf.converter.core.styles.run.RunUnderlineValueProvider;
  66. import org.apache.poi.xwpf.converter.core.styles.table.TableAlignmentValueProvider;
  67. import org.apache.poi.xwpf.converter.core.styles.table.TableBorderBottomValueProvider;
  68. import org.apache.poi.xwpf.converter.core.styles.table.TableBorderInsideHValueProvider;
  69. import org.apache.poi.xwpf.converter.core.styles.table.TableBorderInsideVValueProvider;
  70. import org.apache.poi.xwpf.converter.core.styles.table.TableBorderLeftValueProvider;
  71. import org.apache.poi.xwpf.converter.core.styles.table.TableBorderRightValueProvider;
  72. import org.apache.poi.xwpf.converter.core.styles.table.TableBorderTopValueProvider;
  73. import org.apache.poi.xwpf.converter.core.styles.table.TableIndentationValueProvider;
  74. import org.apache.poi.xwpf.converter.core.styles.table.TableMarginBottomValueProvider;
  75. import org.apache.poi.xwpf.converter.core.styles.table.TableMarginLeftValueProvider;
  76. import org.apache.poi.xwpf.converter.core.styles.table.TableMarginRightValueProvider;
  77. import org.apache.poi.xwpf.converter.core.styles.table.TableMarginTopValueProvider;
  78. import org.apache.poi.xwpf.converter.core.styles.table.TableWidthValueProvider;
  79. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellBackgroundColorValueProvider;
  80. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellBorderBottomValueProvider;
  81. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellBorderInsideHValueProvider;
  82. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellBorderInsideVValueProvider;
  83. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellBorderLeftValueProvider;
  84. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellBorderRightValueProvider;
  85. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellBorderTopValueProvider;
  86. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellGridSpanValueProvider;
  87. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellMarginBottomValueProvider;
  88. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellMarginLeftValueProvider;
  89. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellMarginRightValueProvider;
  90. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellMarginTopValueProvider;
  91. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellNoWrapValueProvider;
  92. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellTextDirectionValueProvider;
  93. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellVMergeValueProvider;
  94. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellVerticalAlignmentValueProvider;
  95. import org.apache.poi.xwpf.converter.core.styles.table.cell.TableCellWidthValueProvider;
  96. import org.apache.poi.xwpf.converter.core.styles.table.row.TableRowHeaderValueProvider;
  97. import org.apache.poi.xwpf.converter.core.styles.table.row.TableRowHeightValueProvider;
  98. import org.apache.poi.xwpf.converter.core.styles.table.row.TableRowMarginBottomValueProvider;
  99. import org.apache.poi.xwpf.converter.core.styles.table.row.TableRowMarginLeftValueProvider;
  100. import org.apache.poi.xwpf.converter.core.styles.table.row.TableRowMarginRightValueProvider;
  101. import org.apache.poi.xwpf.converter.core.styles.table.row.TableRowMarginTopValueProvider;
  102. import org.apache.poi.xwpf.converter.core.utils.DxaUtil;
  103. import org.apache.poi.xwpf.converter.core.utils.StringUtils;
  104. import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
  105. import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
  106. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  107. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  108. import org.apache.poi.xwpf.usermodel.XWPFRelation;
  109. import org.apache.poi.xwpf.usermodel.XWPFRun;
  110. import org.apache.poi.xwpf.usermodel.XWPFSettings;
  111. import org.apache.poi.xwpf.usermodel.XWPFTable;
  112. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  113. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  114. import org.apache.xmlbeans.XmlException;
  115. import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
  116. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
  117. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocDefaults;
  118. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFont;
  119. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumPr;
  120. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
  121. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
  122. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSettings;
  123. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
  124. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
  125. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabs;
  126. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
  127. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPrBase;
  128. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPrEx;
  129. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblStylePr;
  130. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
  131. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTextDirection;
  132. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;
  133. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTwipsMeasure;
  134. import org.openxmlformats.schemas.wordprocessingml.x2006.main.FontsDocument;
  135. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
  136. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc.Enum;
  137. import org.openxmlformats.schemas.wordprocessingml.x2006.main.SettingsDocument;
  138. public class XWPFStylesDocument
  139. {
  140. public static final Object EMPTY_VALUE = new Object();
  141. private static final float DEFAULT_TAB_STOP_POINT = DxaUtil.dxa2points( 720f );
  142. private final XWPFDocument document;
  143. private final Map<String, CTStyle> stylesByStyleId;
  144. private CTStyle defaultParagraphStyle;
  145. private CTStyle defaultTableStyle;
  146. private final Map<String, Object> values;
  147. private CTStyle defaultCharacterStyle;
  148. private CTStyle defaultNumberingStyle;
  149. private Map<XWPFTable, TableInfo> tableInfos;
  150. private Float defaultTabStop;
  151. private XWPFSettings settings;
  152. private CTSettings ctSettings;
  153. private List<ThemeDocument> themeDocuments;
  154. private List<FontsDocument> fontsDocuments;
  155. private final Map<String, List<String>> fontsAltName;
  156. private final Map<String, String> fontsToUse;
  157. public XWPFStylesDocument( XWPFDocument document )
  158. throws XmlException, IOException
  159. {
  160. this( document, true );
  161. }
  162. public XWPFStylesDocument( XWPFDocument document, boolean lazyInitialization )
  163. throws XmlException, IOException
  164. {
  165. this.document = document;
  166. this.stylesByStyleId = new HashMap<String, CTStyle>();
  167. this.values = new HashMap<String, Object>();
  168. this.fontsAltName = new HashMap<String, List<String>>();
  169. this.fontsToUse = new HashMap<String, String>();
  170. if ( lazyInitialization )
  171. {
  172. initialize();
  173. }
  174. }
  175. protected void initialize()
  176. throws XmlException, IOException
  177. {
  178. List<CTStyle> styles = document.getStyle().getStyleList();
  179. for ( CTStyle style : styles )
  180. {
  181. org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff.Enum isDefault = style.getDefault();
  182. org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType.Enum type = style.getType();
  183. boolean isDefaultStyle =
  184. ( isDefault != null && isDefault.intValue() == org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff.INT_X_1 );
  185. if ( isDefaultStyle )
  186. {
  187. // default
  188. if ( type != null )
  189. {
  190. switch ( type.intValue() )
  191. {
  192. case org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType.INT_CHARACTER:
  193. defaultCharacterStyle = style;
  194. break;
  195. case org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType.INT_NUMBERING:
  196. defaultNumberingStyle = style;
  197. break;
  198. case org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType.INT_PARAGRAPH:
  199. defaultParagraphStyle = style;
  200. break;
  201. case org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType.INT_TABLE:
  202. defaultTableStyle = style;
  203. break;
  204. }
  205. }
  206. }
  207. visitStyle( style, isDefaultStyle );
  208. stylesByStyleId.put( style.getStyleId(), style );
  209. }
  210. }
  211. protected void visitStyle( CTStyle style, boolean defaultStyle )
  212. {
  213. }
  214. public CTStyle getDefaultParagraphStyle()
  215. {
  216. return defaultParagraphStyle;
  217. }
  218. public CTStyle getStyle( String styleId )
  219. {
  220. return stylesByStyleId.get( styleId );
  221. }
  222. public CTDocDefaults getDocDefaults()
  223. {
  224. try
  225. {
  226. return document.getStyle().getDocDefaults();
  227. }
  228. catch ( XmlException e )
  229. {
  230. // TODO Auto-generated catch block
  231. e.printStackTrace();
  232. }
  233. catch ( IOException e )
  234. {
  235. // TODO Auto-generated catch block
  236. e.printStackTrace();
  237. }
  238. return null;
  239. }
  240. // -------------------- Paragraph
  241. /**
  242. * @param docxParagraph
  243. * @return
  244. */
  245. public Float getSpacingBefore( XWPFParagraph docxParagraph )
  246. {
  247. return ParagraphSpacingBeforeValueProvider.INSTANCE.getValue( docxParagraph, this );
  248. }
  249. public Float getSpacingBefore( CTPPr pPr )
  250. {
  251. return ParagraphSpacingBeforeValueProvider.INSTANCE.getValue( pPr );
  252. }
  253. public Float getSpacingAfter( XWPFParagraph docxParagraph )
  254. {
  255. return ParagraphSpacingAfterValueProvider.INSTANCE.getValue( docxParagraph, this );
  256. }
  257. public Float getSpacingAfter( CTPPr pPr )
  258. {
  259. return ParagraphSpacingAfterValueProvider.INSTANCE.getValue( pPr );
  260. }
  261. public Float getIndentationLeft( XWPFParagraph paragraph )
  262. {
  263. return ParagraphIndentationLeftValueProvider.INSTANCE.getValue( paragraph, this );
  264. }
  265. public Float getIndentationLeft( CTPPr pPr )
  266. {
  267. return ParagraphIndentationLeftValueProvider.INSTANCE.getValue( pPr );
  268. }
  269. public Float getIndentationRight( XWPFParagraph paragraph )
  270. {
  271. return ParagraphIndentationRightValueProvider.INSTANCE.getValue( paragraph, this );
  272. }
  273. public Float getIndentationRight( CTPPr pPr )
  274. {
  275. return ParagraphIndentationRightValueProvider.INSTANCE.getValue( pPr );
  276. }
  277. public Float getIndentationFirstLine( XWPFParagraph paragraph )
  278. {
  279. return ParagraphIndentationFirstLineValueProvider.INSTANCE.getValue( paragraph, this );
  280. }
  281. public Float getIndentationFirstLine( CTPPr pPr )
  282. {
  283. return ParagraphIndentationFirstLineValueProvider.INSTANCE.getValue( pPr );
  284. }
  285. public Float getIndentationHanging( XWPFParagraph paragraph )
  286. {
  287. return ParagraphIndentationHangingValueProvider.INSTANCE.getValue( paragraph, this );
  288. }
  289. public Float getIndentationHanging( CTPPr pPr )
  290. {
  291. return ParagraphIndentationHangingValueProvider.INSTANCE.getValue( pPr );
  292. }
  293. public Color getBackgroundColor( XWPFParagraph paragraph )
  294. {
  295. return ParagraphBackgroundColorValueProvider.INSTANCE.getValue( paragraph, this );
  296. }
  297. public Color getBackgroundColor( CTPPr pPr )
  298. {
  299. return ParagraphBackgroundColorValueProvider.INSTANCE.getValue( pPr );
  300. }
  301. /**
  302. * @param paragraph
  303. * @return
  304. */
  305. public ParagraphAlignment getParagraphAlignment( XWPFParagraph paragraph )
  306. {
  307. return ParagraphAlignmentValueProvider.INSTANCE.getValue( paragraph, this );
  308. }
  309. public ParagraphAlignment getParagraphAlignment( CTPPr pPr )
  310. {
  311. return ParagraphAlignmentValueProvider.INSTANCE.getValue( pPr );
  312. }
  313. public CTBorder getBorderTop( XWPFParagraph docxParagraph )
  314. {
  315. return ParagraphBorderTopValueProvider.INSTANCE.getValue( docxParagraph, this );
  316. }
  317. public CTBorder getBorderTop( CTPPr pPr )
  318. {
  319. return ParagraphBorderTopValueProvider.INSTANCE.getValue( pPr );
  320. }
  321. public CTBorder getBorderBottom( XWPFParagraph docxParagraph )
  322. {
  323. return ParagraphBorderBottomValueProvider.INSTANCE.getValue( docxParagraph, this );
  324. }
  325. public CTBorder getBorderBottom( CTPPr pPr )
  326. {
  327. return ParagraphBorderBottomValueProvider.INSTANCE.getValue( pPr );
  328. }
  329. public CTBorder getBorderLeft( XWPFParagraph docxParagraph )
  330. {
  331. return ParagraphBorderLeftValueProvider.INSTANCE.getValue( docxParagraph, this );
  332. }
  333. public CTBorder getBorderLeft( CTPPr pPr )
  334. {
  335. return ParagraphBorderLeftValueProvider.INSTANCE.getValue( pPr );
  336. }
  337. public CTBorder getBorderRight( XWPFParagraph docxParagraph )
  338. {
  339. return ParagraphBorderRightValueProvider.INSTANCE.getValue( docxParagraph, this );
  340. }
  341. public CTBorder getBorderRight( CTPPr pPr )
  342. {
  343. return ParagraphBorderRightValueProvider.INSTANCE.getValue( pPr );
  344. }
  345. public CTTabs getParagraphTabs( XWPFParagraph docxParagraph )
  346. {
  347. return ParagraphTabsValueProvider.INSTANCE.getValue( docxParagraph, this );
  348. }
  349. public CTTabs getParagraphTabs( CTPPr pPr )
  350. {
  351. return ParagraphTabsValueProvider.INSTANCE.getValue( pPr );
  352. }
  353. public ParagraphLineSpacing getParagraphSpacing( XWPFParagraph docxParagraph )
  354. {
  355. return ParagraphLineSpacingValueProvider.INSTANCE.getValue( docxParagraph, this );
  356. }
  357. public ParagraphLineSpacing getParagraphSpacing( CTPPr pPr )
  358. {
  359. return ParagraphLineSpacingValueProvider.INSTANCE.getValue( pPr );
  360. }
  361. public CTNumPr getParagraphNumPr( XWPFParagraph docxParagraph )
  362. {
  363. return ParagraphNumPrValueProvider.INSTANCE.getValue( docxParagraph, this );
  364. }
  365. public CTNumPr getParagraphNumPr( CTPPr pPr )
  366. {
  367. return ParagraphNumPrValueProvider.INSTANCE.getValue( pPr );
  368. }
  369. // -------------------- Run
  370. /**
  371. * @param run
  372. * @return
  373. */
  374. public String getFontFamilyAscii( XWPFRun run )
  375. {
  376. return RunFontFamilyAsciiValueProvider.INSTANCE.getValue( run, this );
  377. }
  378. public String getFontFamilyAscii( CTRPr rPr )
  379. {
  380. return RunFontFamilyAsciiValueProvider.INSTANCE.getValue( rPr, this );
  381. }
  382. public String getFontFamilyEastAsia( XWPFRun run )
  383. {
  384. return RunFontFamilyEastAsiaValueProvider.INSTANCE.getValue( run, this );
  385. }
  386. public String getFontFamilyEastAsia( CTRPr rPr )
  387. {
  388. return RunFontFamilyAsciiValueProvider.INSTANCE.getValue( rPr, this );
  389. }
  390. public String getFontFamilyHAnsi( XWPFRun run )
  391. {
  392. return RunFontFamilyHAnsiValueProvider.INSTANCE.getValue( run, this );
  393. }
  394. public String getFontFamilyHAnsi( CTRPr rPr )
  395. {
  396. return RunFontFamilyHAnsiValueProvider.INSTANCE.getValue( rPr, this );
  397. }
  398. public Float getFontSize( XWPFRun run )
  399. {
  400. return RunFontSizeValueProvider.INSTANCE.getValue( run, this );
  401. }
  402. public Float getFontSize( CTRPr rPr )
  403. {
  404. return RunFontSizeValueProvider.INSTANCE.getValue( rPr, this );
  405. }
  406. public Boolean getFontStyleBold( XWPFRun run )
  407. {
  408. return RunFontStyleBoldValueProvider.INSTANCE.getValue( run, this );
  409. }
  410. public Boolean getFontStyleBold( CTRPr rPr )
  411. {
  412. return RunFontStyleBoldValueProvider.INSTANCE.getValue( rPr, this );
  413. }
  414. public Boolean getFontStyleItalic( XWPFRun run )
  415. {
  416. return RunFontStyleItalicValueProvider.INSTANCE.getValue( run, this );
  417. }
  418. public Boolean getFontStyleItalic( CTRPr rPr )
  419. {
  420. return RunFontStyleItalicValueProvider.INSTANCE.getValue( rPr, this );
  421. }
  422. public Boolean getFontStyleStrike( XWPFRun run )
  423. {
  424. return RunFontStyleStrikeValueProvider.INSTANCE.getValue( run, this );
  425. }
  426. public Boolean getFontStyleStrike( CTRPr rPr )
  427. {
  428. return RunFontStyleStrikeValueProvider.INSTANCE.getValue( rPr, this );
  429. }
  430. public Color getFontColor( XWPFRun run )
  431. {
  432. return RunFontColorValueProvider.INSTANCE.getValue( run, this );
  433. }
  434. public Color getFontColor( CTRPr rPr )
  435. {
  436. return RunFontColorValueProvider.INSTANCE.getValue( rPr, this );
  437. }
  438. public UnderlinePatterns getUnderline( CTRPr rPr )
  439. {
  440. return RunUnderlineValueProvider.INSTANCE.getValue( rPr, this );
  441. }
  442. public UnderlinePatterns getUnderline( XWPFRun run )
  443. {
  444. return RunUnderlineValueProvider.INSTANCE.getValue( run, this );
  445. }
  446. public Color getBackgroundColor( XWPFRun run )
  447. {
  448. return RunBackgroundColorValueProvider.INSTANCE.getValue( run, this );
  449. }
  450. public Color getBackgroundColor( CTRPr rPr )
  451. {
  452. return RunBackgroundColorValueProvider.INSTANCE.getValue( rPr, this );
  453. }
  454. public Color getTextHighlighting( XWPFRun run )
  455. {
  456. return RunTextHighlightingValueProvider.INSTANCE.getValue( run, this );
  457. }
  458. public Color getTextHighlighting( CTRPr rPr )
  459. {
  460. return RunTextHighlightingValueProvider.INSTANCE.getValue( rPr, this );
  461. }
  462. // ------------------------ Table
  463. /**
  464. * @param table
  465. * @return
  466. */
  467. public TableWidth getTableWidth( XWPFTable table )
  468. {
  469. return TableWidthValueProvider.INSTANCE.getValue( table, this );
  470. }
  471. public TableWidth getTableWidth( CTTblPr tblPr )
  472. {
  473. return TableWidthValueProvider.INSTANCE.getValue( tblPr );
  474. }
  475. public TableWidth getTableWidth( CTTblPrBase tblPr )
  476. {
  477. return TableWidthValueProvider.INSTANCE.getValue( tblPr );
  478. }
  479. public ParagraphAlignment getTableAlignment( XWPFTable table )
  480. {
  481. return TableAlignmentValueProvider.INSTANCE.getValue( table, this );
  482. }
  483. public ParagraphAlignment getTableAlignment( CTTblPr tblPr )
  484. {
  485. return TableAlignmentValueProvider.INSTANCE.getValue( tblPr );
  486. }
  487. public ParagraphAlignment getTableAlignment( CTTblPrBase tblPr )
  488. {
  489. return TableAlignmentValueProvider.INSTANCE.getValue( tblPr );
  490. }
  491. public Float getTableIndentation( XWPFTable table )
  492. {
  493. return TableIndentationValueProvider.INSTANCE.getValue( table, this );
  494. }
  495. public Float getTableIndentation( CTTblPr tblPr )
  496. {
  497. return TableIndentationValueProvider.INSTANCE.getValue( tblPr );
  498. }
  499. public Float getTableIndentation( CTTblPrBase tblPr )
  500. {
  501. return TableIndentationValueProvider.INSTANCE.getValue( tblPr );
  502. }
  503. public TableCellBorder getTableBorderTop( XWPFTable table )
  504. {
  505. return TableBorderTopValueProvider.INSTANCE.getValue( table, this );
  506. }
  507. public TableCellBorder getTableBorderTop( CTTblPr tblPr )
  508. {
  509. return TableBorderTopValueProvider.INSTANCE.getValue( tblPr );
  510. }
  511. public TableCellBorder getTableBorderTop( CTTblPrBase tblPr )
  512. {
  513. return TableBorderTopValueProvider.INSTANCE.getValue( tblPr );
  514. }
  515. public TableCellBorder getTableBorderBottom( XWPFTable table )
  516. {
  517. return TableBorderBottomValueProvider.INSTANCE.getValue( table, this );
  518. }
  519. public TableCellBorder getTableBorderBottom( CTTblPr tblPr )
  520. {
  521. return TableBorderBottomValueProvider.INSTANCE.getValue( tblPr );
  522. }
  523. public TableCellBorder getTableBorderBottom( CTTblPrBase tblPr )
  524. {
  525. return TableBorderBottomValueProvider.INSTANCE.getValue( tblPr );
  526. }
  527. public TableCellBorder getTableBorderLeft( XWPFTable table )
  528. {
  529. return TableBorderLeftValueProvider.INSTANCE.getValue( table, this );
  530. }
  531. public TableCellBorder getTableBorderLeft( CTTblPr tblPr )
  532. {
  533. return TableBorderLeftValueProvider.INSTANCE.getValue( tblPr );
  534. }
  535. public TableCellBorder getTableBorderLeft( CTTblPrBase tblPr )
  536. {
  537. return TableBorderLeftValueProvider.INSTANCE.getValue( tblPr );
  538. }
  539. public TableCellBorder getTableBorderRight( XWPFTable table )
  540. {
  541. return TableBorderRightValueProvider.INSTANCE.getValue( table, this );
  542. }
  543. public TableCellBorder getTableBorderRight( CTTblPr tblPr )
  544. {
  545. return TableBorderRightValueProvider.INSTANCE.getValue( tblPr );
  546. }
  547. public TableCellBorder getTableBorderRight( CTTblPrBase tblPr )
  548. {
  549. return TableBorderRightValueProvider.INSTANCE.getValue( tblPr );
  550. }
  551. public TableCellBorder getTableBorderInsideH( XWPFTable table )
  552. {
  553. return TableBorderInsideHValueProvider.INSTANCE.getValue( table, this );
  554. }
  555. public TableCellBorder getTableBorderInsideH( CTTblPr tblPr )
  556. {
  557. return TableBorderInsideHValueProvider.INSTANCE.getValue( tblPr );
  558. }
  559. public TableCellBorder getTableBorderInsideH( CTTblPrBase tblPr )
  560. {
  561. return TableBorderInsideHValueProvider.INSTANCE.getValue( tblPr );
  562. }
  563. public TableCellBorder getTableBorderInsideV( XWPFTable table )
  564. {
  565. return TableBorderInsideVValueProvider.INSTANCE.getValue( table, this );
  566. }
  567. public TableCellBorder getTableBorderInsideV( CTTblPr tblPr )
  568. {
  569. return TableBorderInsideVValueProvider.INSTANCE.getValue( tblPr );
  570. }
  571. public TableCellBorder getTableBorderInsideV( CTTblPrBase tblPr )
  572. {
  573. return TableBorderInsideVValueProvider.INSTANCE.getValue( tblPr );
  574. }
  575. public Float getTableMarginTop( XWPFTable table )
  576. {
  577. return TableMarginTopValueProvider.INSTANCE.getValue( table, this );
  578. }
  579. public Float getTableMarginTop( CTTblPrBase tcPr )
  580. {
  581. return TableMarginTopValueProvider.INSTANCE.getValue( tcPr );
  582. }
  583. public Float getTableMarginBottom( XWPFTable table )
  584. {
  585. return TableMarginBottomValueProvider.INSTANCE.getValue( table, this );
  586. }
  587. public Float getTableMarginBottom( CTTblPrBase tcPr )
  588. {
  589. return TableMarginBottomValueProvider.INSTANCE.getValue( tcPr );
  590. }
  591. public Float getTableMarginLeft( XWPFTable table )
  592. {
  593. return TableMarginLeftValueProvider.INSTANCE.getValue( table, this );
  594. }
  595. public Float getTableMarginLeft( CTTblPrBase tcPr )
  596. {
  597. return TableMarginLeftValueProvider.INSTANCE.getValue( tcPr );
  598. }
  599. public Float getTableMarginRight( XWPFTable table )
  600. {
  601. return TableMarginRightValueProvider.INSTANCE.getValue( table, this );
  602. }
  603. public Float getTableMarginRight( CTTblPrBase tcPr )
  604. {
  605. return TableMarginRightValueProvider.INSTANCE.getValue( tcPr );
  606. }
  607. // ------------------------ Table row
  608. /**
  609. * @param row
  610. * @return
  611. */
  612. public TableHeight getTableRowHeight( XWPFTableRow row )
  613. {
  614. return TableRowHeightValueProvider.INSTANCE.getValue( row, this );
  615. }
  616. /**
  617. * @param cell
  618. * @return
  619. */
  620. public TableHeight getTableRowHeight( CTTrPr trPr )
  621. {
  622. return TableRowHeightValueProvider.INSTANCE.getValue( trPr );
  623. }
  624. public Float getTableRowMarginTop( XWPFTableRow row )
  625. {
  626. return TableRowMarginTopValueProvider.INSTANCE.getValue( row, this );
  627. }
  628. public Float getTableRowMarginTop( CTTblPrEx tblPrEx )
  629. {
  630. return TableRowMarginTopValueProvider.INSTANCE.getValue( tblPrEx );
  631. }
  632. public Float getTableRowMarginBottom( XWPFTableRow row )
  633. {
  634. return TableRowMarginBottomValueProvider.INSTANCE.getValue( row, this );
  635. }
  636. public Float getTableRowMarginBottom( CTTblPrEx tblPrEx )
  637. {
  638. return TableRowMarginBottomValueProvider.INSTANCE.getValue( tblPrEx );
  639. }
  640. public Float getTableRowMarginLeft( XWPFTableRow row )
  641. {
  642. return TableRowMarginLeftValueProvider.INSTANCE.getValue( row, this );
  643. }
  644. public Float getTableRowMarginLeft( CTTblPrEx tblPrEx )
  645. {
  646. return TableRowMarginLeftValueProvider.INSTANCE.getValue( tblPrEx );
  647. }
  648. public Float getTableRowMarginRight( XWPFTableRow row )
  649. {
  650. return TableRowMarginRightValueProvider.INSTANCE.getValue( row, this );
  651. }
  652. public Float getTableRowMarginRight( CTTblPrEx tblPrEx )
  653. {
  654. return TableRowMarginRightValueProvider.INSTANCE.getValue( tblPrEx );
  655. }
  656. public boolean isTableRowHeader( XWPFTableRow row )
  657. {
  658. return TableRowHeaderValueProvider.INSTANCE.getValue( row, this );
  659. }
  660. // ------------------------ Table cell
  661. /**
  662. * @param cell
  663. * @return
  664. */
  665. public Enum getTableCellVerticalAlignment( XWPFTableCell cell )
  666. {
  667. return TableCellVerticalAlignmentValueProvider.INSTANCE.getValue( cell, this );
  668. }
  669. public Color getTableCellBackgroundColor( XWPFTableCell cell )
  670. {
  671. return TableCellBackgroundColorValueProvider.INSTANCE.getValue( cell, this );
  672. }
  673. public Color getTableCellBackgroundColor( CTTcPr tcPr )
  674. {
  675. return TableCellBackgroundColorValueProvider.INSTANCE.getValue( tcPr );
  676. }
  677. public BigInteger getTableCellGridSpan( XWPFTableCell cell )
  678. {
  679. return TableCellGridSpanValueProvider.INSTANCE.getValue( cell, this );
  680. }
  681. public BigInteger getTableCellGridSpan( CTTcPr tcPr )
  682. {
  683. return TableCellGridSpanValueProvider.INSTANCE.getValue( tcPr );
  684. }
  685. public TableWidth getTableCellWith( XWPFTableCell cell )
  686. {
  687. return TableCellWidthValueProvider.INSTANCE.getValue( cell, this );
  688. }
  689. public TableWidth getTableCellWith( CTTcPr tcPr )
  690. {
  691. return TableCellWidthValueProvider.INSTANCE.getValue( tcPr );
  692. }
  693. public CTTextDirection getTextDirection( XWPFTableCell cell )
  694. {
  695. return TableCellTextDirectionValueProvider.INSTANCE.getValue( cell, this );
  696. }
  697. public CTTextDirection getTextDirection( CTTcPr tcPr )
  698. {
  699. return TableCellTextDirectionValueProvider.INSTANCE.getValue( tcPr );
  700. }
  701. public STMerge.Enum getTableCellVMerge( XWPFTableCell cell )
  702. {
  703. return TableCellVMergeValueProvider.INSTANCE.getValue( cell, this );
  704. }
  705. public STMerge.Enum getTableCellVMerge( CTTcPr tcPr )
  706. {
  707. return TableCellVMergeValueProvider.INSTANCE.getValue( tcPr );
  708. }
  709. /**
  710. * Returns the table cell borders with conflicts.
  711. *
  712. * @param cell
  713. * @param borderSide
  714. * @return
  715. * @see http://officeopenxml.com/WPtableCellBorderConflicts.php
  716. */
  717. public TableCellBorder getTableCellBorderWithConflicts( XWPFTableCell cell, BorderSide borderSide )
  718. {
  719. /**
  720. * Conflicts between cell borders and table and table-level exception borders If the cell spacing is zero, then
  721. * there is a conflict. The following rules apply as between cell borders and table and table-level exception
  722. * (row) borders (Reference: ECMA-376, 3rd Edition (June, 2011), Fundamentals and Markup Language Reference
  723. * 17.4.40.):
  724. */
  725. /**
  726. * 1) If there is a cell border, then the cell border is displayed.
  727. */
  728. /**
  729. * 2) If there is no cell border but there is a table-level exception border on the row, then that table-level
  730. * exception border is displayed.
  731. */
  732. TableCellBorder border = getTableCellBorder( cell, borderSide );
  733. if ( border == null )
  734. {
  735. XWPFTable table = cell.getTableRow().getTable();
  736. boolean borderInside = isBorderInside( cell, borderSide );
  737. if ( borderInside )
  738. {
  739. border = getTableCellBorderInside( cell, borderSide );
  740. if ( border == null )
  741. {
  742. border = getTableBorderInside( table, borderSide );
  743. }
  744. }
  745. if ( border == null && !borderInside )
  746. {
  747. /**
  748. * 3) If there is no cell or table-level exception border, then the table border is displayed.
  749. */
  750. border = getTableBorder( table, borderSide );
  751. }
  752. }
  753. return border;
  754. }
  755. public boolean isBorderInside( XWPFTableCell cell, BorderSide borderSide )
  756. {
  757. TableCellInfo cellInfo = getTableCellInfo( cell );
  758. boolean borderInside = cellInfo.isInside( borderSide );
  759. return borderInside;
  760. }
  761. public TableCellBorder getTableBorder( XWPFTable table, BorderSide borderSide )
  762. {
  763. switch ( borderSide )
  764. {
  765. case TOP:
  766. return getTableBorderTop( table );
  767. case BOTTOM:
  768. return getTableBorderBottom( table );
  769. case LEFT:
  770. return getTableBorderLeft( table );
  771. case RIGHT:
  772. return getTableBorderRight( table );
  773. }
  774. return null;
  775. }
  776. public TableCellBorder getTableBorderInside( XWPFTable table, BorderSide borderSide )
  777. {
  778. switch ( borderSide )
  779. {
  780. case TOP:
  781. case BOTTOM:
  782. return getTableBorderInsideH( table );
  783. default:
  784. return getTableBorderInsideV( table );
  785. }
  786. }
  787. public TableCellBorder getTableCellBorderInside( XWPFTableCell cell, BorderSide borderSide )
  788. {
  789. switch ( borderSide )
  790. {
  791. case TOP:
  792. case BOTTOM:
  793. return getTableCellBorderInsideH( cell );
  794. default:
  795. return getTableCellBorderInsideV( cell );
  796. }
  797. }
  798. public TableCellBorder getTableCellBorder( XWPFTableCell cell, BorderSide borderSide )
  799. {
  800. switch ( borderSide )
  801. {
  802. case TOP:
  803. return getTableCellBorderTop( cell );
  804. case BOTTOM:
  805. return getTableCellBorderBottom( cell );
  806. case LEFT:
  807. return getTableCellBorderLeft( cell );
  808. case RIGHT:
  809. return getTableCellBorderRight( cell );
  810. }
  811. return null;
  812. }
  813. public TableCellBorder getTableCellBorderTop( XWPFTableCell cell )
  814. {
  815. return TableCellBorderTopValueProvider.INSTANCE.getValue( cell, this );
  816. }
  817. public TableCellBorder getTableCellBorderTop( CTTcPr tcPr )
  818. {
  819. return TableCellBorderTopValueProvider.INSTANCE.getValue( tcPr );
  820. }
  821. public TableCellBorder getTableCellBorderBottom( XWPFTableCell cell )
  822. {
  823. return TableCellBorderBottomValueProvider.INSTANCE.getValue( cell, this );
  824. }
  825. public TableCellBorder getTableCellBorderBottom( CTTcPr tcPr )
  826. {
  827. return TableCellBorderBottomValueProvider.INSTANCE.getValue( tcPr );
  828. }
  829. public TableCellBorder getTableCellBorderLeft( XWPFTableCell cell )
  830. {
  831. return TableCellBorderLeftValueProvider.INSTANCE.getValue( cell, this );
  832. }
  833. public TableCellBorder getTableCellBorderLeft( CTTcPr tcPr )
  834. {
  835. return TableCellBorderLeftValueProvider.INSTANCE.getValue( tcPr );
  836. }
  837. public TableCellBorder getTableCellBorderRight( XWPFTableCell cell )
  838. {
  839. return TableCellBorderRightValueProvider.INSTANCE.getValue( cell, this );
  840. }
  841. public TableCellBorder getTableCellBorderRight( CTTcPr tcPr )
  842. {
  843. return TableCellBorderRightValueProvider.INSTANCE.getValue( tcPr );
  844. }
  845. public TableCellBorder getTableCellBorderInsideH( XWPFTableCell cell )
  846. {
  847. return TableCellBorderInsideHValueProvider.INSTANCE.getValue( cell, this );
  848. }
  849. public TableCellBorder getTableCellBorderInsideH( CTTcPr tcPr )
  850. {
  851. return TableCellBorderInsideHValueProvider.INSTANCE.getValue( tcPr );
  852. }
  853. public TableCellBorder getTableCellBorderInsideV( XWPFTableCell cell )
  854. {
  855. return TableCellBorderInsideVValueProvider.INSTANCE.getValue( cell, this );
  856. }
  857. public TableCellBorder getTableCellBorderInsideV( CTTcPr tcPr )
  858. {
  859. return TableCellBorderInsideVValueProvider.INSTANCE.getValue( tcPr );
  860. }
  861. public Float getTableCellMarginTop( XWPFTableCell cell )
  862. {
  863. return TableCellMarginTopValueProvider.INSTANCE.getValue( cell, this );
  864. }
  865. public Float getTableCellMarginTop( CTTcPr tcPr )
  866. {
  867. return TableCellMarginTopValueProvider.INSTANCE.getValue( tcPr );
  868. }
  869. public Float getTableCellMarginBottom( XWPFTableCell cell )
  870. {
  871. return TableCellMarginBottomValueProvider.INSTANCE.getValue( cell, this );
  872. }
  873. public Float getTableCellMarginBottom( CTTcPr tcPr )
  874. {
  875. return TableCellMarginBottomValueProvider.INSTANCE.getValue( tcPr );
  876. }
  877. public Float getTableCellMarginLeft( XWPFTableCell cell )
  878. {
  879. return TableCellMarginLeftValueProvider.INSTANCE.getValue( cell, this );
  880. }
  881. public Float getTableCellMarginLeft( CTTcPr tcPr )
  882. {
  883. return TableCellMarginLeftValueProvider.INSTANCE.getValue( tcPr );
  884. }
  885. public Float getTableCellMarginRight( XWPFTableCell cell )
  886. {
  887. return TableCellMarginRightValueProvider.INSTANCE.getValue( cell, this );
  888. }
  889. public Float getTableCellMarginRight( CTTcPr tcPr )
  890. {
  891. return TableCellMarginRightValueProvider.INSTANCE.getValue( tcPr );
  892. }
  893. public Boolean getTableCellNoWrap( XWPFTableCell cell )
  894. {
  895. return TableCellNoWrapValueProvider.INSTANCE.getValue( cell, this );
  896. }
  897. public Boolean getTableCellNoWrap( CTTcPr tcPr )
  898. {
  899. return TableCellNoWrapValueProvider.INSTANCE.getValue( tcPr );
  900. }
  901. public CTStyle getDefaultCharacterStyle()
  902. {
  903. return defaultCharacterStyle;
  904. }
  905. public CTStyle getDefaultNumberingStyle()
  906. {
  907. return defaultNumberingStyle;
  908. }
  909. public CTStyle getDefaultTableStyle()
  910. {
  911. return defaultTableStyle;
  912. }
  913. public CTStyle getStyle( CTString basedOn )
  914. {
  915. if ( basedOn == null )
  916. {
  917. return null;
  918. }
  919. return getStyle( basedOn.getVal() );
  920. }
  921. public <T> T getValue( String key )
  922. {
  923. return (T) values.get( key );
  924. }
  925. public <T> void setValue( String key, T value )
  926. {
  927. values.put( key, value );
  928. }
  929. public TableCellInfo getTableCellInfo( XWPFTableCell cell )
  930. {
  931. XWPFTable table = cell.getTableRow().getTable();
  932. return getTableInfo( table ).getCellInfo( cell );
  933. }
  934. public TableInfo getTableInfo( XWPFTable table )
  935. {
  936. if ( tableInfos == null )
  937. {
  938. tableInfos = new HashMap<XWPFTable, TableInfo>();
  939. }
  940. TableInfo tableInfo = tableInfos.get( table );
  941. if ( tableInfo == null )
  942. {
  943. tableInfo = new TableInfo( table, this );
  944. tableInfos.put( table, tableInfo );
  945. }
  946. return tableInfo;
  947. }
  948. public CTTblStylePr getTableStyle( String tableStyleID,
  949. org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblStyleOverrideType.Enum type )
  950. {
  951. CTStyle style = getStyle( tableStyleID );
  952. if ( style == null )
  953. {
  954. return null;
  955. }
  956. // TODO cache it
  957. List<CTTblStylePr> tblStylePrs = style.getTblStylePrList();
  958. for ( CTTblStylePr tblStylePr : tblStylePrs )
  959. {
  960. if ( type.equals( tblStylePr.getType() ) )
  961. {
  962. return tblStylePr;
  963. }
  964. }
  965. return null;
  966. }
  967. /**
  968. * 17.15.1.25 defaultTabStop (Distance Between Automatic Tab Stops) This element specifies the value which shall be
  969. * used as the multiplier to generate automatic tab stops in this document. Automatic tab stops refer to the tab
  970. * stop locations which occur after all custom tab stops in the current paragraph have been surpassed. If this
  971. * element is omitted, then automatic tab stops should be generated at 720 twentieths of a point (0.5") intervals
  972. * across the displayed page. [Example: Consider a WordprocessingML document
  973. *
  974. * @return
  975. */
  976. public float getDefaultTabStop()
  977. {
  978. if ( defaultTabStop == null )
  979. {
  980. CTSettings settings = getCTSettings();
  981. if ( settings != null )
  982. {
  983. CTTwipsMeasure value = settings.getDefaultTabStop();
  984. if ( value != null )
  985. {
  986. if ( !value.isNil() )
  987. {
  988. this.defaultTabStop = DxaUtil.dxa2points( value.getVal() );
  989. }
  990. }
  991. }
  992. if ( defaultTabStop == null )
  993. {
  994. this.defaultTabStop = DEFAULT_TAB_STOP_POINT;
  995. }
  996. }
  997. return defaultTabStop;
  998. }
  999. public XWPFSettings getSettings()
  1000. {
  1001. if ( settings != null )
  1002. {
  1003. return settings;
  1004. }
  1005. for ( POIXMLDocumentPart p : document.getRelations() )
  1006. {
  1007. String relationshipType = p.getPackageRelationship().getRelationshipType();
  1008. if ( relationshipType.equals( XWPFRelation.SETTINGS.getRelation() ) )
  1009. {
  1010. settings = (XWPFSettings) p;
  1011. return settings;
  1012. }
  1013. }
  1014. return null;
  1015. }
  1016. public CTSettings getCTSettings()
  1017. {
  1018. if ( ctSettings != null )
  1019. {
  1020. return ctSettings;
  1021. }
  1022. XWPFSettings settings = getSettings();
  1023. if ( settings != null )
  1024. {
  1025. try
  1026. {
  1027. InputStream inputStream = settings.getPackagePart().getInputStream();
  1028. ctSettings = SettingsDocument.Factory.parse( inputStream ).getSettings();
  1029. }
  1030. catch ( Exception e )
  1031. {
  1032. }
  1033. }
  1034. return ctSettings;
  1035. }
  1036. public List<ThemeDocument> getThemeDocuments()
  1037. throws Exception
  1038. {
  1039. if ( themeDocuments == null )
  1040. {
  1041. themeDocuments = new ArrayList<ThemeDocument>();
  1042. for ( POIXMLDocumentPart p : document.getRelations() )
  1043. {
  1044. String relationshipType = p.getPackageRelationship().getRelationshipType();
  1045. if ( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme".equals( relationshipType ) )
  1046. {
  1047. InputStream inputStream = p.getPackagePart().getInputStream();
  1048. ThemeDocument theme = ThemeDocument.Factory.parse( inputStream );
  1049. themeDocuments.add( theme );
  1050. }
  1051. }
  1052. }
  1053. return themeDocuments;
  1054. }
  1055. public List<FontsDocument> getFontsDocument()
  1056. throws Exception
  1057. {
  1058. if ( fontsDocuments == null )
  1059. {
  1060. fontsDocuments = new ArrayList<FontsDocument>();
  1061. for ( POIXMLDocumentPart p : document.getRelations() )
  1062. {
  1063. String relationshipType = p.getPackageRelationship().getRelationshipType();
  1064. // "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable"
  1065. if ( XWPFRelation.FONT_TABLE.getRelation().equals( relationshipType ) )
  1066. {
  1067. InputStream inputStream = p.getPackagePart().getInputStream();
  1068. FontsDocument fontsDocument = FontsDocument.Factory.parse( inputStream );
  1069. fontsDocuments.add( fontsDocument );
  1070. // Compute fonts alt name
  1071. // see spec 17.8.3.1 altName (Alternate Names for Font)
  1072. CTString altName = null;
  1073. List<CTFont> fonts = fontsDocument.getFonts().getFontList();
  1074. for ( CTFont font : fonts )
  1075. {
  1076. altName = font.getAltName();
  1077. if ( altName != null && StringUtils.isNotEmpty( altName.getVal() ) )
  1078. {
  1079. List<String> altNames = new ArrayList<String>();
  1080. // This element specifies a set of alternative names which can be used to locate the font
  1081. // specified by the parent
  1082. // element. This set of alternative names is stored in a comma-delimited list, with all
  1083. // adjacent commas ignored (i.e.
  1084. // a value of Name A, Name B is equivalent to Name A,,,,,,,,, Name B).
  1085. String[] names = altName.getVal().split( "," );
  1086. String name = null;
  1087. for ( int i = 0; i < names.length; i++ )
  1088. {
  1089. name = names[i];
  1090. if ( StringUtils.isNotEmpty( name ) )
  1091. {
  1092. altNames.add( name );
  1093. }
  1094. }
  1095. fontsAltName.put( font.getName(), altNames );
  1096. }
  1097. }
  1098. }
  1099. }
  1100. }
  1101. return fontsDocuments;
  1102. }
  1103. public List<String> getFontsAltName( String fontName )
  1104. throws Exception
  1105. {
  1106. getFontsDocument();
  1107. return fontsAltName.get( fontName );
  1108. }
  1109. public String getFontNameToUse( String fontName )
  1110. {
  1111. return fontsToUse.get( fontName );
  1112. }
  1113. public void setFontNameToUse( String fontName, String altFfontName )
  1114. {
  1115. fontsToUse.put( fontName, altFfontName );
  1116. }
  1117. }