package com.legend.word.docx;
import com.legend.word.entity.CustomElementMeta;
import com.legend.word.utils.MathMlUtils;
import com.microsoft.schemas.vml.CTGroup;
import com.microsoft.schemas.vml.CTShape;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.xwpf.converter.core.*;
import org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument;
import org.apache.poi.xwpf.converter.core.utils.DxaUtil;
import org.apache.poi.xwpf.converter.core.utils.StringUtils;
import org.apache.poi.xwpf.converter.core.utils.XWPFRunHelper;
import org.apache.poi.xwpf.converter.core.utils.XWPFTableUtil;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlTokenSource;
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.*;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMathPara;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.HdrDocument.Factory;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge.Enum;
import org.xml.sax.ContentHandler;
import java.io.IOException;
import java.math.BigInteger;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author legend
* @date 2021年5月25日
*/
public abstract class CustomXWPFDocumentVisitor<T, O extends Options, E extends IXWPFMasterPage> implements IMasterPageHandler<E> {
private static final Logger LOGGER = Logger.getLogger(XWPFDocumentVisitor.class.getName());
protected static final String WORD_MEDIA = "word/media/";
protected final XWPFDocument document;
public final MasterPageManager masterPageManager;
private XWPFHeader currentHeader;
private XWPFFooter currentFooter;
protected final XWPFStylesDocument stylesDocument;
protected final O options;
private boolean pageBreakOnNextParagraph;
protected boolean processingTotalPageCountField = false;
protected boolean totalPageFieldUsed = false;
private Map<Integer, ListContext> listContextMap;
//额外新增
private ContentHandler contentHandler;
//额外新增
private boolean isScanHeading;
public CustomXWPFDocumentVisitor(XWPFDocument document, O options, ContentHandler contentHandler) throws Exception {
this.document = document;
this.options = options;
this.stylesDocument = this.createStylesDocument(document);
this.masterPageManager = new MasterPageManager(document.getDocument(), this);
//额外新增
this.contentHandler = contentHandler;
}
protected XWPFStylesDocument createStylesDocument(XWPFDocument document) throws XmlException, IOException {
return new XWPFStylesDocument(document);
}
public XWPFStylesDocument getStylesDocument() {
return this.stylesDocument;
}
public O getOptions() {
return this.options;
}
public MasterPageManager getMasterPageManager() {
return this.masterPageManager;
}
public Map start() throws Exception {
//T container = this.startVisitDocument();
List<IBodyElement> bodyElements = this.document.getBodyElements();
// this.visitBodyElements(bodyElements, container);
return this.visitBodyElements(bodyElements, null);
//this.endVisitDocument();
}
protected abstract T startVisitDocument() throws Exception;
protected abstract void endVisitDocument() throws Exception;
protected Map visitBodyElements(List<IBodyElement> bodyElements, T container) throws Exception {
if (!this.masterPageManager.isInitialized()) {
this.masterPageManager.initialize();
}
CustomContentHandler customContentHandler = ((CustomContentHandler)contentHandler);
XWPFStyles styles = document.getStyles();
Map dataMap = new LinkedHashMap<String, String>();
for(int i = 0; i < bodyElements.size(); ++i) {
IBodyElement bodyElement = (IBodyElement)bodyElements.get(i);
CustomElementMeta customElementMeta;
switch(bodyElement.getElementType()) {
case PARAGRAPH:
//System.out.println("--开始---");
//String style = ((XWPFParagraph) bodyElement).getStyle();
String style = ((XWPFParagraph) bodyElement).getStyle();
if(StringUtils.isNotEmpty(style)){
style = styles.getStyle(((XWPFParagraph) bodyElement).getStyle()).getCTStyle().getName().getVal();
}
//System.out.println("标题的级别:"+style);
XWPFParagraph paragraph = (XWPFParagraph)bodyElement;
customElementMeta = this.visitParagraph(paragraph, i, container, style);
if(customContentHandler.isClearData()){
//组装数据
dataMap.put(customElementMeta,customContentHandler.getStringData());
//清理数据
customContentHandler.clearData();
}
break;
case TABLE:
style = ((XWPFTable)bodyElement).getStyleID();
//System.out.println("标题的级别:"+style);
customContentHandler.setClearData(false);
this.visitTable((XWPFTable)bodyElement, i, container);
customContentHandler.setClearData(true);
//设置类型为table
customElementMeta = new CustomElementMeta(CustomElementMeta.ELEMENT_TYPE_TABLE,i);
if(!isScanHeading){
customElementMeta.setMainTitle(true);
}
//组装数据
dataMap.put(customElementMeta,customContentHandler.getStringData());
//清理数据
((CustomContentHandler)contentHandler).clearData();
}
}
return dataMap;
}
protected CustomElementMeta visitParagraph(XWPFParagraph paragraph, int index, T container,String elementType) throws Exception {
if (this.isWordDocumentPartParsing()) {
this.masterPageManager.update(paragraph.getCTP());
}
if (this.pageBreakOnNextParagraph) {
this.pageBreak();
}
this.pageBreakOnNextParagraph = false;
ListItemContext itemContext = null;
CTNumPr originalNumPr = this.stylesDocument.getParagraphNumPr(paragraph);
CTNumPr numPr = this.getNumPr(originalNumPr);
if (numPr != null) {
XWPFNum num = this.getXWPFNum(numPr);
if (num != null) {
XWPFAbstractNum abstractNum = this.getXWPFAbstractNum(num);
CTDecimalNumber ilvl = numPr.getIlvl();
int level = ilvl != null ? ilvl.getVal().intValue() : 0;
CTLvl lvl = abstractNum.getAbstractNum().getLvlArray(level);
if (lvl != null) {
ListContext listContext = this.getListContext(originalNumPr.getNumId().getVal().intValue());
itemContext = listContext.addItem(lvl);
}
}
}
T paragraphContainer = this.startVisitParagraph(paragraph, itemContext, container);
CustomElementMeta customElementMeta = this.visitParagraphBody(paragraph, index, paragraphContainer, elementType);
this.endVisitParagraph(paragraph, container, paragraphContainer);
return customElementMeta;
}
private CTNumPr getNumPr(CTNumPr numPr) {
if (numPr != null) {
XWPFNum
@Legend
- 粉丝: 3
- 资源: 2
最新资源
- 阿里云api网关请求签名示例(java实现).zip
- 通过示例学习 Android 的 RxJava.zip
- 通过多线程编程在 Java 中发现并发模式和特性 线程、锁、原子等等 .zip
- 通过在终端中进行探索来学习 JavaScript .zip
- 通过不仅针对初学者而且针对 JavaScript 爱好者(无论他们的专业水平如何)设计的编码挑战,自然而自信地拥抱 JavaScript .zip
- 适用于 Kotlin 和 Java 的现代 JSON 库 .zip
- yolo5实战-yolo资源
- english-chinese-dictionary-数据结构课程设计
- mp-mysql-injector-spring-boot-starter-sql注入
- lunisolar-删除重复字符
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页