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

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

https://github.com/minstrelsy/xdocreport
Java | 258 lines | 196 code | 29 blank | 33 comment | 27 complexity | d6d3c3a30f634d51c3155de9cc6c7667 MD5 | raw file
  1. package org.apache.poi.xwpf.converter.internal;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import java.util.logging.Logger;
  5. import org.apache.poi.openxml4j.opc.PackagePart;
  6. import org.apache.poi.xwpf.converter.styles.XWPFStylesDocument;
  7. import org.apache.poi.xwpf.usermodel.IBodyElement;
  8. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  9. import org.apache.poi.xwpf.usermodel.XWPFFooter;
  10. import org.apache.poi.xwpf.usermodel.XWPFHeader;
  11. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  12. import org.apache.poi.xwpf.usermodel.XWPFPictureData;
  13. import org.apache.xmlbeans.XmlCursor;
  14. import org.apache.xmlbeans.XmlException;
  15. import org.apache.xmlbeans.XmlObject;
  16. import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
  17. import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
  18. import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
  19. import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;
  20. import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
  21. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
  22. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
  23. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef;
  24. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
  25. import org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument;
  26. import org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument;
  27. /**
  28. * Visitor to visit elements from entry word/document.xml, word/header*.xml, word/footer*.xml
  29. *
  30. * @param <T>
  31. */
  32. public abstract class XWPFDocumentVisitor<T, E extends IXWPFMasterPage>
  33. extends XWPFElementVisitor<T>
  34. {
  35. /**
  36. * Logger for this class
  37. */
  38. private static final Logger LOGGER = Logger.getLogger( XWPFDocumentVisitor.class.getName() );
  39. private final MasterPageManager masterPageManager;
  40. private XWPFHeader currentHeader;
  41. private XWPFFooter currentFooter;
  42. protected final XWPFStylesDocument stylesDocument;
  43. public XWPFDocumentVisitor( XWPFDocument document )
  44. throws Exception
  45. {
  46. super( document );
  47. this.masterPageManager = new MasterPageManager( document, this );
  48. this.stylesDocument = new XWPFStylesDocument( document );
  49. }
  50. public MasterPageManager getMasterPageManager()
  51. {
  52. return masterPageManager;
  53. }
  54. protected void visitBodyElements( List<IBodyElement> bodyElements, T container )
  55. throws Exception
  56. {
  57. if ( !masterPageManager.isInitialized() )
  58. {
  59. // master page manager which hosts each <:w;sectPr declared in the word/document.xml
  60. // must be initialized. The initialisation loop for each
  61. // <w:p paragraph to compute a list of <w:sectPr which contains information
  62. // about header/footer declared in the <w:headerReference/<w:footerReference
  63. masterPageManager.initialize();
  64. }
  65. super.visitBodyElements( bodyElements, container );
  66. }
  67. protected void visitParagraph( XWPFParagraph paragraph, T container )
  68. throws Exception
  69. {
  70. if ( isWordDocumentPartParsing() )
  71. {
  72. // header/footer is not parsing.
  73. // It's the word/document.xml which is parsing
  74. // test if the current paragraph define a <w:sectPr
  75. // to update the header/footer declared in the <w:headerReference/<w:footerReference
  76. masterPageManager.update( paragraph );
  77. }
  78. super.visitParagraph( paragraph, container );
  79. }
  80. /**
  81. * Return true if word/document.xml is parsing and false otherwise.
  82. *
  83. * @return
  84. */
  85. protected boolean isWordDocumentPartParsing()
  86. {
  87. return currentHeader == null && currentFooter == null;
  88. }
  89. // ------------------------------ Header/Footer visitor -----------
  90. protected void visitHeaderRef( CTHdrFtrRef headerRef, CTSectPr sectPr, E masterPage )
  91. throws Exception
  92. {
  93. this.currentHeader = getXWPFHeader( headerRef );
  94. visitHeader( currentHeader, headerRef, sectPr, masterPage );
  95. this.currentHeader = null;
  96. }
  97. protected abstract void visitHeader( XWPFHeader header, CTHdrFtrRef headerRef, CTSectPr sectPr, E masterPage )
  98. throws Exception;
  99. protected void visitFooterRef( CTHdrFtrRef footerRef, CTSectPr sectPr, E masterPage )
  100. throws Exception
  101. {
  102. this.currentFooter = getXWPFFooter( footerRef );
  103. visitFooter( currentFooter, footerRef, sectPr, masterPage );
  104. this.currentFooter = null;
  105. }
  106. protected abstract void visitFooter( XWPFFooter footer, CTHdrFtrRef footerRef, CTSectPr sectPr, E masterPage )
  107. throws Exception;
  108. protected XWPFHeader getXWPFHeader( CTHdrFtrRef headerRef )
  109. throws XmlException, IOException
  110. {
  111. PackagePart hdrPart = document.getPartById( headerRef.getId() );
  112. List<XWPFHeader> headers = document.getHeaderList();
  113. for ( XWPFHeader header : headers )
  114. {
  115. if ( header.getPackagePart().equals( hdrPart ) )
  116. {
  117. return header;
  118. }
  119. }
  120. HdrDocument hdrDoc = HdrDocument.Factory.parse( hdrPart.getInputStream() );
  121. CTHdrFtr hdrFtr = hdrDoc.getHdr();
  122. XWPFHeader hdr = new XWPFHeader( document, hdrFtr );
  123. return hdr;
  124. }
  125. protected XWPFFooter getXWPFFooter( CTHdrFtrRef footerRef )
  126. throws XmlException, IOException
  127. {
  128. PackagePart hdrPart = document.getPartById( footerRef.getId() );
  129. List<XWPFFooter> footers = document.getFooterList();
  130. for ( XWPFFooter footer : footers )
  131. {
  132. if ( footer.getPackagePart().equals( hdrPart ) )
  133. {
  134. return footer;
  135. }
  136. }
  137. FtrDocument hdrDoc = FtrDocument.Factory.parse( hdrPart.getInputStream() );
  138. CTHdrFtr hdrFtr = hdrDoc.getFtr();
  139. XWPFFooter ftr = new XWPFFooter( document, hdrFtr );
  140. return ftr;
  141. }
  142. protected void visitDrawing( CTDrawing drawing, T parentContainer )
  143. throws Exception
  144. {
  145. List<CTInline> inlines = drawing.getInlineList();
  146. for ( CTInline inline : inlines )
  147. {
  148. visitInline( inline, parentContainer );
  149. }
  150. List<CTAnchor> anchors = drawing.getAnchorList();
  151. for ( CTAnchor anchor : anchors )
  152. {
  153. visitAnchor( anchor, parentContainer );
  154. }
  155. }
  156. protected void visitAnchor( CTAnchor anchor, T parentContainer )
  157. throws Exception
  158. {
  159. CTGraphicalObject graphic = anchor.getGraphic();
  160. if ( graphic != null )
  161. {
  162. CTGraphicalObjectData graphicData = graphic.getGraphicData();
  163. if ( graphicData != null )
  164. {
  165. XmlCursor c = graphicData.newCursor();
  166. c.selectPath( "./*" );
  167. while ( c.toNextSelection() )
  168. {
  169. XmlObject o = c.getObject();
  170. if ( o instanceof CTPicture )
  171. {
  172. visitPicture( (CTPicture) o, parentContainer );
  173. }
  174. }
  175. c.dispose();
  176. }
  177. }
  178. }
  179. protected void visitInline( CTInline inline, T parentContainer )
  180. throws Exception
  181. {
  182. CTGraphicalObject graphic = inline.getGraphic();
  183. if ( graphic != null )
  184. {
  185. CTGraphicalObjectData graphicData = graphic.getGraphicData();
  186. if ( graphicData != null )
  187. {
  188. XmlCursor c = graphicData.newCursor();
  189. c.selectPath( "./*" );
  190. while ( c.toNextSelection() )
  191. {
  192. XmlObject o = c.getObject();
  193. if ( o instanceof CTPicture )
  194. {
  195. visitPicture( (CTPicture) o, parentContainer );
  196. }
  197. }
  198. c.dispose();
  199. }
  200. }
  201. }
  202. protected abstract void visitPicture( CTPicture picture, T parentContainer )
  203. throws Exception;
  204. protected XWPFPictureData getPictureDataByID( String blipId )
  205. {
  206. if ( currentHeader != null )
  207. {
  208. return currentHeader.getPictureDataByID( blipId );
  209. }
  210. if ( currentFooter != null )
  211. {
  212. return currentFooter.getPictureDataByID( blipId );
  213. }
  214. return document.getPictureDataByID( blipId );
  215. }
  216. /**
  217. * Set active master page.
  218. *
  219. * @param masterPage
  220. */
  221. protected abstract void setActiveMasterPage( E masterPage );
  222. /**
  223. * Create an instance of master page.
  224. *
  225. * @param sectPr
  226. * @return
  227. */
  228. protected abstract IXWPFMasterPage createMasterPage( CTSectPr sectPr );
  229. }