PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/thirdparties-extension/org.apache.poi.xwpf.converter/src/main/java/org/apache/poi/xwpf/converter/internal/XWPFElementVisitor.java

https://code.google.com/
Java | 305 lines | 212 code | 51 blank | 42 comment | 11 complexity | ac25279aef887d58ea179db27b375521 MD5 | raw file
  1. /**
  2. * Copyright (C) 2011 Angelo Zerr <angelo.zerr@gmail.com> and Pascal Leclercq <pascal.leclercq@gmail.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.internal;
  26. import java.io.IOException;
  27. import java.io.OutputStream;
  28. import java.util.Collection;
  29. import java.util.List;
  30. import java.util.logging.Logger;
  31. import org.apache.poi.openxml4j.opc.PackagePart;
  32. import org.apache.poi.xwpf.usermodel.IBodyElement;
  33. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  34. import org.apache.poi.xwpf.usermodel.XWPFFooter;
  35. import org.apache.poi.xwpf.usermodel.XWPFHeader;
  36. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  37. import org.apache.poi.xwpf.usermodel.XWPFPicture;
  38. import org.apache.poi.xwpf.usermodel.XWPFRun;
  39. import org.apache.poi.xwpf.usermodel.XWPFStyle;
  40. import org.apache.poi.xwpf.usermodel.XWPFTable;
  41. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  42. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  43. import org.apache.xmlbeans.XmlException;
  44. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocDefaults;
  45. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
  46. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef;
  47. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
  48. import org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument;
  49. import org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument;
  50. public abstract class XWPFElementVisitor<T>
  51. {
  52. /**
  53. * Logger for this class
  54. */
  55. private static final Logger LOGGER = Logger.getLogger( XWPFElementVisitor.class.getName() );
  56. protected final XWPFDocument document;
  57. protected CTDocDefaults defaults;
  58. public XWPFElementVisitor( XWPFDocument document )
  59. {
  60. this.document = document;
  61. try
  62. {
  63. this.defaults = document.getStyle().getDocDefaults();
  64. }
  65. catch ( XmlException e )
  66. {
  67. LOGGER.severe( e.getMessage() );
  68. }
  69. catch ( IOException e )
  70. {
  71. LOGGER.severe( e.getMessage() );
  72. }
  73. }
  74. // ------------------------------ Start/End document visitor -----------
  75. /**
  76. * Main entry for visit XWPFDocument.
  77. *
  78. * @param out
  79. * @throws Exception
  80. */
  81. public void visit( OutputStream out )
  82. throws Exception
  83. {
  84. T container = startVisitDocument( out );
  85. // Create Header/Footer
  86. CTSectPr sectPr = document.getDocument().getBody().getSectPr();
  87. visitHeadersFooters( sectPr, container );
  88. // Create IText element for each XWPF elements from the w:body
  89. List<IBodyElement> bodyElements = document.getBodyElements();
  90. visitBodyElements( bodyElements, container );
  91. // Save
  92. // Clean-up
  93. endVisitDocument();
  94. out.close();
  95. }
  96. protected abstract T startVisitDocument( OutputStream out )
  97. throws Exception;
  98. protected abstract void endVisitDocument()
  99. throws Exception;
  100. // ------------------------------ Header/Footer visitor -----------
  101. protected void visitHeadersFooters( CTSectPr sectPr, T container )
  102. throws Exception
  103. {
  104. Collection<CTHdrFtrRef> headersRef = sectPr.getHeaderReferenceList();
  105. Collection<CTHdrFtrRef> footersRef = sectPr.getFooterReferenceList();
  106. for ( CTHdrFtrRef headerRef : headersRef )
  107. {
  108. visitHeader( headerRef );
  109. }
  110. for ( CTHdrFtrRef footerRef : footersRef )
  111. {
  112. visitFooter( footerRef );
  113. }
  114. }
  115. protected XWPFHeader getXWPFHeader( CTHdrFtrRef headerRef )
  116. throws XmlException, IOException
  117. {
  118. PackagePart hdrPart = document.getPartById( headerRef.getId() );
  119. HdrDocument hdrDoc = HdrDocument.Factory.parse( hdrPart.getInputStream() );
  120. CTHdrFtr hdrFtr = hdrDoc.getHdr();
  121. XWPFHeader hdr = new XWPFHeader( document, hdrFtr);
  122. return hdr;
  123. }
  124. protected XWPFFooter getXWPFFooter( CTHdrFtrRef footerRef )
  125. throws XmlException, IOException
  126. {
  127. PackagePart hdrPart = document.getPartById( footerRef.getId() );
  128. FtrDocument hdrDoc = FtrDocument.Factory.parse( hdrPart.getInputStream() );
  129. CTHdrFtr hdrFtr = hdrDoc.getFtr();
  130. XWPFFooter ftr = new XWPFFooter(document, hdrFtr);
  131. return ftr;
  132. }
  133. protected abstract void visitHeader( CTHdrFtrRef headerRef )
  134. throws Exception;
  135. protected abstract void visitFooter( CTHdrFtrRef footerRef )
  136. throws Exception;
  137. // ------------------------------ XWPF Elements visitor -----------
  138. protected void visitBodyElements( List<IBodyElement> bodyElements, T container )
  139. throws Exception
  140. {
  141. for ( IBodyElement bodyElement : bodyElements )
  142. {
  143. visitBodyElement( bodyElement, container );
  144. }
  145. }
  146. protected void visitBodyElement( IBodyElement bodyElement, T container )
  147. throws Exception
  148. {
  149. switch ( bodyElement.getElementType() )
  150. {
  151. case PARAGRAPH:
  152. visitParagraph( (XWPFParagraph) bodyElement, container );
  153. break;
  154. case TABLE:
  155. visitTable( (XWPFTable) bodyElement, container );
  156. break;
  157. }
  158. }
  159. protected void visitParagraph( XWPFParagraph paragraph, T container )
  160. throws Exception
  161. {
  162. T paragraphContainer = startVisitPargraph( paragraph, container );
  163. visitParagraphBody( paragraph, paragraphContainer );
  164. endVisitPargraph( paragraph, container, paragraphContainer );
  165. }
  166. protected abstract T startVisitPargraph( XWPFParagraph paragraph, T parentContainer )
  167. throws Exception;
  168. protected abstract void endVisitPargraph( XWPFParagraph paragraph, T parentContainer, T paragraphContainer )
  169. throws Exception;
  170. protected void visitParagraphBody( XWPFParagraph paragraph, T paragraphContainer )
  171. throws Exception
  172. {
  173. List<XWPFRun> runs = paragraph.getRuns();
  174. if ( runs.isEmpty() )
  175. {
  176. visitEmptyRun( paragraphContainer );
  177. }
  178. else
  179. {
  180. for ( XWPFRun run : paragraph.getRuns() )
  181. {
  182. visitRun( run, paragraphContainer );
  183. }
  184. }
  185. }
  186. protected abstract void visitEmptyRun( T paragraphContainer )
  187. throws Exception;
  188. protected abstract void visitRun( XWPFRun run, T paragraphContainer )
  189. throws Exception;
  190. protected void visitTable( XWPFTable table, T container )
  191. throws Exception
  192. {
  193. T tableContainer = startVisitTable( table, container );
  194. visitTableBody( table, tableContainer );
  195. endVisitTable( table, container, tableContainer );
  196. }
  197. protected void visitTableBody( XWPFTable table, T tableContainer )
  198. throws Exception
  199. {
  200. // Proces Row
  201. List<XWPFTableRow> rows = table.getRows();
  202. for ( XWPFTableRow row : rows )
  203. {
  204. visitTableRow( row, tableContainer );
  205. }
  206. }
  207. protected abstract T startVisitTable( XWPFTable table, T tableContainer )
  208. throws Exception;
  209. protected abstract void endVisitTable( XWPFTable table, T parentContainer, T tableContainer )
  210. throws Exception;
  211. protected void visitTableRow( XWPFTableRow row, T tableContainer )
  212. throws Exception
  213. {
  214. // Process cell
  215. List<XWPFTableCell> cells = row.getTableCells();
  216. for ( XWPFTableCell cell : cells )
  217. {
  218. visitCell( cell, tableContainer );
  219. }
  220. }
  221. protected void visitCell( XWPFTableCell cell, T tableContainer )
  222. throws Exception
  223. {
  224. T tableCellContainer = startVisitTableCell( cell, tableContainer );
  225. visitTableCellBody( cell, tableCellContainer );
  226. endVisitTableCell( cell, tableContainer, tableCellContainer );
  227. }
  228. protected void visitTableCellBody( XWPFTableCell cell, T tableCellContainer )
  229. throws Exception
  230. {
  231. List<IBodyElement> bodyElements = cell.getBodyElements();
  232. visitBodyElements( bodyElements, tableCellContainer );
  233. }
  234. protected abstract T startVisitTableCell( XWPFTableCell cell, T tableContainer );
  235. protected abstract void endVisitTableCell( XWPFTableCell cell, T tableContainer, T tableCellContainer );
  236. protected void visitPictures( XWPFRun run, T parentContainer )
  237. throws Exception
  238. {
  239. List<XWPFPicture> embeddedPictures = run.getEmbeddedPictures();
  240. for ( XWPFPicture picture : embeddedPictures )
  241. {
  242. visitPicture( picture, parentContainer );
  243. }
  244. }
  245. protected abstract void visitPicture( XWPFPicture picture, T parentContainer )
  246. throws Exception;
  247. protected XWPFStyle getXWPFStyle( String styleID )
  248. {
  249. if ( styleID == null )
  250. return null;
  251. else
  252. return document.getStyles().getStyle( styleID );
  253. }
  254. }