/thirdparties-extension/org.apache.poi.xwpf.converter/src/main/java/org/apache/poi/xwpf/converter/internal/XWPFDocumentVisitor.java
Java | 258 lines | 196 code | 29 blank | 33 comment | 27 complexity | d6d3c3a30f634d51c3155de9cc6c7667 MD5 | raw file
- package org.apache.poi.xwpf.converter.internal;
-
- import java.io.IOException;
- import java.util.List;
- import java.util.logging.Logger;
-
- import org.apache.poi.openxml4j.opc.PackagePart;
- import org.apache.poi.xwpf.converter.styles.XWPFStylesDocument;
- import org.apache.poi.xwpf.usermodel.IBodyElement;
- import org.apache.poi.xwpf.usermodel.XWPFDocument;
- import org.apache.poi.xwpf.usermodel.XWPFFooter;
- import org.apache.poi.xwpf.usermodel.XWPFHeader;
- import org.apache.poi.xwpf.usermodel.XWPFParagraph;
- import org.apache.poi.xwpf.usermodel.XWPFPictureData;
- import org.apache.xmlbeans.XmlCursor;
- import org.apache.xmlbeans.XmlException;
- import org.apache.xmlbeans.XmlObject;
- import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
- import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
- import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
- import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;
- import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
- import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
- import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
- import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef;
- import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
- import org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument;
- import org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument;
-
- /**
- * Visitor to visit elements from entry word/document.xml, word/header*.xml, word/footer*.xml
- *
- * @param <T>
- */
- public abstract class XWPFDocumentVisitor<T, E extends IXWPFMasterPage>
- extends XWPFElementVisitor<T>
- {
-
- /**
- * Logger for this class
- */
- private static final Logger LOGGER = Logger.getLogger( XWPFDocumentVisitor.class.getName() );
-
- private final MasterPageManager masterPageManager;
-
- private XWPFHeader currentHeader;
-
- private XWPFFooter currentFooter;
-
- protected final XWPFStylesDocument stylesDocument;
-
- public XWPFDocumentVisitor( XWPFDocument document )
- throws Exception
- {
- super( document );
- this.masterPageManager = new MasterPageManager( document, this );
-
- this.stylesDocument = new XWPFStylesDocument( document );
- }
-
- public MasterPageManager getMasterPageManager()
- {
- return masterPageManager;
- }
-
- protected void visitBodyElements( List<IBodyElement> bodyElements, T container )
- throws Exception
- {
- if ( !masterPageManager.isInitialized() )
- {
- // master page manager which hosts each <:w;sectPr declared in the word/document.xml
- // must be initialized. The initialisation loop for each
- // <w:p paragraph to compute a list of <w:sectPr which contains information
- // about header/footer declared in the <w:headerReference/<w:footerReference
- masterPageManager.initialize();
- }
- super.visitBodyElements( bodyElements, container );
- }
-
- protected void visitParagraph( XWPFParagraph paragraph, T container )
- throws Exception
- {
- if ( isWordDocumentPartParsing() )
- {
- // header/footer is not parsing.
- // It's the word/document.xml which is parsing
- // test if the current paragraph define a <w:sectPr
- // to update the header/footer declared in the <w:headerReference/<w:footerReference
- masterPageManager.update( paragraph );
- }
- super.visitParagraph( paragraph, container );
- }
-
- /**
- * Return true if word/document.xml is parsing and false otherwise.
- *
- * @return
- */
- protected boolean isWordDocumentPartParsing()
- {
- return currentHeader == null && currentFooter == null;
- }
-
- // ------------------------------ Header/Footer visitor -----------
-
- protected void visitHeaderRef( CTHdrFtrRef headerRef, CTSectPr sectPr, E masterPage )
- throws Exception
- {
- this.currentHeader = getXWPFHeader( headerRef );
- visitHeader( currentHeader, headerRef, sectPr, masterPage );
- this.currentHeader = null;
- }
-
- protected abstract void visitHeader( XWPFHeader header, CTHdrFtrRef headerRef, CTSectPr sectPr, E masterPage )
- throws Exception;
-
- protected void visitFooterRef( CTHdrFtrRef footerRef, CTSectPr sectPr, E masterPage )
- throws Exception
- {
- this.currentFooter = getXWPFFooter( footerRef );
- visitFooter( currentFooter, footerRef, sectPr, masterPage );
- this.currentFooter = null;
- }
-
- protected abstract void visitFooter( XWPFFooter footer, CTHdrFtrRef footerRef, CTSectPr sectPr, E masterPage )
- throws Exception;
-
- protected XWPFHeader getXWPFHeader( CTHdrFtrRef headerRef )
- throws XmlException, IOException
- {
- PackagePart hdrPart = document.getPartById( headerRef.getId() );
- List<XWPFHeader> headers = document.getHeaderList();
- for ( XWPFHeader header : headers )
- {
- if ( header.getPackagePart().equals( hdrPart ) )
- {
- return header;
- }
- }
- HdrDocument hdrDoc = HdrDocument.Factory.parse( hdrPart.getInputStream() );
- CTHdrFtr hdrFtr = hdrDoc.getHdr();
- XWPFHeader hdr = new XWPFHeader( document, hdrFtr );
- return hdr;
- }
-
- protected XWPFFooter getXWPFFooter( CTHdrFtrRef footerRef )
- throws XmlException, IOException
- {
- PackagePart hdrPart = document.getPartById( footerRef.getId() );
- List<XWPFFooter> footers = document.getFooterList();
- for ( XWPFFooter footer : footers )
- {
- if ( footer.getPackagePart().equals( hdrPart ) )
- {
- return footer;
- }
- }
-
- FtrDocument hdrDoc = FtrDocument.Factory.parse( hdrPart.getInputStream() );
- CTHdrFtr hdrFtr = hdrDoc.getFtr();
- XWPFFooter ftr = new XWPFFooter( document, hdrFtr );
- return ftr;
- }
-
- protected void visitDrawing( CTDrawing drawing, T parentContainer )
- throws Exception
- {
- List<CTInline> inlines = drawing.getInlineList();
- for ( CTInline inline : inlines )
- {
- visitInline( inline, parentContainer );
- }
- List<CTAnchor> anchors = drawing.getAnchorList();
- for ( CTAnchor anchor : anchors )
- {
- visitAnchor( anchor, parentContainer );
- }
- }
-
- protected void visitAnchor( CTAnchor anchor, T parentContainer )
- throws Exception
- {
- CTGraphicalObject graphic = anchor.getGraphic();
- if ( graphic != null )
- {
- CTGraphicalObjectData graphicData = graphic.getGraphicData();
- if ( graphicData != null )
- {
- XmlCursor c = graphicData.newCursor();
- c.selectPath( "./*" );
- while ( c.toNextSelection() )
- {
- XmlObject o = c.getObject();
- if ( o instanceof CTPicture )
- {
- visitPicture( (CTPicture) o, parentContainer );
- }
- }
- c.dispose();
- }
- }
- }
-
- protected void visitInline( CTInline inline, T parentContainer )
- throws Exception
- {
- CTGraphicalObject graphic = inline.getGraphic();
- if ( graphic != null )
- {
- CTGraphicalObjectData graphicData = graphic.getGraphicData();
- if ( graphicData != null )
- {
- XmlCursor c = graphicData.newCursor();
- c.selectPath( "./*" );
- while ( c.toNextSelection() )
- {
- XmlObject o = c.getObject();
- if ( o instanceof CTPicture )
- {
- visitPicture( (CTPicture) o, parentContainer );
- }
- }
- c.dispose();
- }
- }
- }
-
- protected abstract void visitPicture( CTPicture picture, T parentContainer )
- throws Exception;
-
- protected XWPFPictureData getPictureDataByID( String blipId )
- {
- if ( currentHeader != null )
- {
- return currentHeader.getPictureDataByID( blipId );
- }
- if ( currentFooter != null )
- {
- return currentFooter.getPictureDataByID( blipId );
- }
- return document.getPictureDataByID( blipId );
- }
-
- /**
- * Set active master page.
- *
- * @param masterPage
- */
- protected abstract void setActiveMasterPage( E masterPage );
-
- /**
- * Create an instance of master page.
- *
- * @param sectPr
- * @return
- */
- protected abstract IXWPFMasterPage createMasterPage( CTSectPr sectPr );
- }