Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对word中的公式控件做了{{var}}支持 #548

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@
<artifactId>batik-codec</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/deepoove/poi/config/Configure.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Map;
import java.util.Set;

import com.deepoove.poi.policy.reference.*;
import com.deepoove.poi.template.MathTemplate;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;

Expand All @@ -32,10 +34,6 @@
import com.deepoove.poi.policy.RenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import com.deepoove.poi.policy.TextRenderPolicy;
import com.deepoove.poi.policy.reference.DefaultChartTemplateRenderPolicy;
import com.deepoove.poi.policy.reference.DefaultPictureTemplateRenderPolicy;
import com.deepoove.poi.policy.reference.MultiSeriesChartTemplateRenderPolicy;
import com.deepoove.poi.policy.reference.SingleSeriesChartTemplateRenderPolicy;
import com.deepoove.poi.render.RenderContext;
import com.deepoove.poi.render.compute.DefaultELRenderDataCompute;
import com.deepoove.poi.render.compute.RenderDataComputeFactory;
Expand Down Expand Up @@ -147,6 +145,7 @@ public class Configure implements Cloneable {

plugin(PictureTemplate.class, new DefaultPictureTemplateRenderPolicy());
plugin(ChartTemplate.class, new DefaultChartTemplateRenderPolicy());
plugin(MathTemplate.class, new DefaultMathTemplateRenderPolicy());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.render.compute.RenderDataCompute;
import com.deepoove.poi.resolver.Resolver;
import com.deepoove.poi.template.ChartTemplate;
import com.deepoove.poi.template.InlineIterableTemplate;
import com.deepoove.poi.template.IterableTemplate;
import com.deepoove.poi.template.MetaTemplate;
import com.deepoove.poi.template.PictureTemplate;
import com.deepoove.poi.template.*;
import com.deepoove.poi.template.run.RunTemplate;

public abstract class DefaultTemplateProcessor implements Visitor {
Expand Down Expand Up @@ -63,6 +59,11 @@ public void visit(IterableTemplate iterableTemplate) {
visitOther(iterableTemplate);
}

@Override
public void visit(MathTemplate mathTemplate) {
visitOther(mathTemplate);
}

protected void visitOther(MetaTemplate template) {
// no-op
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,13 @@
import java.util.List;
import java.util.Set;

import com.deepoove.poi.template.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.render.compute.RenderDataCompute;
import com.deepoove.poi.resolver.Resolver;
import com.deepoove.poi.template.BlockTemplate;
import com.deepoove.poi.template.ChartTemplate;
import com.deepoove.poi.template.InlineIterableTemplate;
import com.deepoove.poi.template.IterableTemplate;
import com.deepoove.poi.template.MetaTemplate;
import com.deepoove.poi.template.PictureTemplate;
import com.deepoove.poi.template.run.RunTemplate;
import com.deepoove.poi.xwpf.XWPFTextboxContent;

Expand Down Expand Up @@ -104,4 +99,9 @@ public void visit(ChartTemplate chartTemplate) {
chartTemplate.accept(elementProcessor);
}

@Override
public void visit(MathTemplate mathTemplate) {
mathTemplate.accept(elementProcessor);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Objects;

import com.deepoove.poi.template.MathTemplate;
import org.apache.commons.lang3.ClassUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -61,6 +62,11 @@ public void visit(RunTemplate runTemplate) {
visit((ElementTemplate) runTemplate);
}

@Override
public void visit(MathTemplate mathTemplate) {
visit((ElementTemplate) mathTemplate);
}

void visit(ElementTemplate eleTemplate) {
RenderPolicy policy = eleTemplate.findPolicy(template.getConfig());
Objects.requireNonNull(policy, "Cannot find render policy: [" + eleTemplate.getTagName() + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@

import java.util.List;

import com.deepoove.poi.template.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.deepoove.poi.template.ChartTemplate;
import com.deepoove.poi.template.InlineIterableTemplate;
import com.deepoove.poi.template.IterableTemplate;
import com.deepoove.poi.template.MetaTemplate;
import com.deepoove.poi.template.PictureTemplate;
import com.deepoove.poi.template.run.RunTemplate;

public class LogProcessor implements Visitor {
Expand Down Expand Up @@ -73,4 +69,9 @@ public void visit(ChartTemplate chartTemplate) {
log.info("{}{}", indentState, chartTemplate);
}

@Override
public void visit(MathTemplate mathTemplate) {
log.info("{}{}", indentState, mathTemplate);
}

}
12 changes: 8 additions & 4 deletions src/main/java/com/deepoove/poi/render/processor/Visitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.deepoove.poi.render.processor;

import com.deepoove.poi.template.ChartTemplate;
import com.deepoove.poi.template.InlineIterableTemplate;
import com.deepoove.poi.template.IterableTemplate;
import com.deepoove.poi.template.PictureTemplate;
import com.deepoove.poi.template.*;
import com.deepoove.poi.template.run.RunTemplate;

/**
Expand Down Expand Up @@ -62,4 +59,11 @@ public interface Visitor {
*/
void visit(ChartTemplate referenceTemplate);

/**
* visit chart template
*
* @param mathTemplate
*/
void visit(MathTemplate mathTemplate);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Set;

import com.deepoove.poi.template.MathTemplate;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFRun;
Expand All @@ -26,6 +27,7 @@
import com.deepoove.poi.template.ChartTemplate;
import com.deepoove.poi.template.PictureTemplate;
import com.deepoove.poi.template.run.RunTemplate;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTText;

/**
* @author Sayi
Expand Down Expand Up @@ -76,4 +78,12 @@ public ChartTemplate createChartTemplate(Configure config, String tag, XWPFChart
return template;
}

@Override
public MathTemplate createMathTemplate(Configure config, String tag, CTText text) {
MathTemplate template = new MathTemplate(tag, text);
template.setSource(config.getGramerPrefix() + tag + config.getGramerSuffix());
template.setSign(EMPTY_CHAR);
return template;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.deepoove.poi.resolver;

import com.deepoove.poi.template.MathTemplate;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFRun;
Expand All @@ -38,4 +39,6 @@ public interface ElementTemplateFactory {

ChartTemplate createChartTemplate(Configure config, String tag, XWPFChart chart, XWPFRun run);

MathTemplate createMathTemplate(Configure config, String tag, org.openxmlformats.schemas.officeDocument.x2006.math.CTText text);

}
116 changes: 82 additions & 34 deletions src/main/java/com/deepoove/poi/resolver/TemplateResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,28 @@
*/
package com.deepoove.poi.resolver;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

import com.deepoove.poi.config.Configure;
import com.deepoove.poi.exception.ResolverException;
import com.deepoove.poi.template.*;
import com.deepoove.poi.template.run.RunTemplate;
import com.deepoove.poi.util.ReflectionUtils;
import com.deepoove.poi.xwpf.CTDrawingWrapper;
import com.deepoove.poi.xwpf.XWPFRunWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.xwpf.usermodel.BodyElementType;
import org.apache.poi.xwpf.usermodel.IBody;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTD;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTF;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMathArg;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMathPara;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.deepoove.poi.config.Configure;
import com.deepoove.poi.exception.ResolverException;
import com.deepoove.poi.template.BlockTemplate;
import com.deepoove.poi.template.ChartTemplate;
import com.deepoove.poi.template.ElementTemplate;
import com.deepoove.poi.template.IterableTemplate;
import com.deepoove.poi.template.MetaTemplate;
import com.deepoove.poi.template.PictureTemplate;
import com.deepoove.poi.template.run.RunTemplate;
import com.deepoove.poi.util.ReflectionUtils;
import com.deepoove.poi.xwpf.CTDrawingWrapper;
import com.deepoove.poi.xwpf.XWPFRunWrapper;
import java.util.*;

/**
* Resolver
Expand Down Expand Up @@ -99,9 +84,15 @@ public List<MetaTemplate> resolveBodyElements(List<IBodyElement> bodyElements) {
if (element == null) continue;
if (element.getElementType() == BodyElementType.PARAGRAPH) {
XWPFParagraph paragraph = (XWPFParagraph) element;
new RunningRunParagraph(paragraph, templatePattern).refactorRun();
resolveXWPFRuns(paragraph.getRuns(), metaTemplates, stack);
} else if (element.getElementType() == BodyElementType.TABLE) {
new RunningRunParagraph(paragraph, templatePattern).refactorRun(); // 将runs合并
resolveXWPFRuns(paragraph.getRuns(), metaTemplates, stack);
if(!paragraph.getCTP().getOMathParaList().isEmpty()){ // 解析公式
List<MetaTemplate> mathTemplates = resolveCTOMathParas(paragraph.getCTP().getOMathParaList());
if(!mathTemplates.isEmpty()){
addNewMeta(metaTemplates, stack, mathTemplates);
}
}
} else if (element.getElementType() == BodyElementType.TABLE) {
XWPFTable table = (XWPFTable) element;
List<XWPFTableRow> rows = table.getRows();
if (null == rows) continue;
Expand Down Expand Up @@ -194,6 +185,62 @@ private ChartTemplate resolveXWPFChart(XWPFRun run) {
: (ChartTemplate) template;
}

private List<MetaTemplate> resolveCTOMathParas(List<CTOMathPara> mathParaList){
List<MetaTemplate> metaTemplates = new ArrayList<>();
mathParaList.forEach(mathPara -> {
mathPara.getOMathList().forEach(math -> {
resolveMathCTR(math.getRList(), metaTemplates);
resolveMathCTF(math.getFList(), metaTemplates);
resolveMathCTD(math.getDList(), metaTemplates);
});
});
return metaTemplates;
}

private void resolveMathArgs(List<CTOMathArg> maths, List<MetaTemplate> metaTemplates){
maths.forEach(math -> {
resolveMathCTR(math.getRList(), metaTemplates);
resolveMathCTF(math.getFList(), metaTemplates);
resolveMathCTD(math.getDList(), metaTemplates);
});
}

private void resolveMathCTF(List<CTF> ctfs, List<MetaTemplate> metaTemplates){
ctfs.forEach(ctf -> {
CTOMathArg math = ctf.getDen();
if(math != null){
resolveMathCTR(math.getRList(), metaTemplates);
resolveMathCTF(math.getFList(), metaTemplates);
resolveMathCTD(math.getDList(), metaTemplates);
}

math = ctf.getNum();
if(math != null){
resolveMathCTR(math.getRList(), metaTemplates);
resolveMathCTF(math.getFList(), metaTemplates);
resolveMathCTD(math.getDList(), metaTemplates);
}
});
}

private void resolveMathCTD(List<CTD> ctds, List<MetaTemplate> metaTemplates){
ctds.forEach(ctd -> {
resolveMathArgs(ctd.getEList(), metaTemplates);
});
}

private void resolveMathCTR(List<org.openxmlformats.schemas.officeDocument.x2006.math.CTR> ctrList, List<MetaTemplate> metaTemplates){
ctrList.forEach(ctr -> {
ctr.getTList();
ctr.getT2List().forEach(t2 -> {
ElementTemplate elementTemplate = parseTemplateFactory(t2.getStringValue(), t2, null);
if(elementTemplate != null){
metaTemplates.add(elementTemplate);
}
});
});
}

private List<PictureTemplate> resolveXWPFPictures(List<XWPFPicture> embeddedPictures) {
List<PictureTemplate> metaTemplates = new ArrayList<>();
if (embeddedPictures == null) return metaTemplates;
Expand Down Expand Up @@ -273,9 +320,10 @@ ElementTemplate parseTemplateFactory(String text, Object obj, XWPFRun run) {
return (PictureTemplate) elementTemplateFactory.createPicureTemplate(config, tag, (XWPFPicture) obj);
} else if (obj.getClass() == XWPFChart.class) {
return (ChartTemplate) elementTemplateFactory.createChartTemplate(config, tag, (XWPFChart) obj, run);
}
} else if(obj.getClass() == org.openxmlformats.schemas.officeDocument.x2006.math.impl.CTTextImpl.class){
return elementTemplateFactory.createMathTemplate(config, tag, (org.openxmlformats.schemas.officeDocument.x2006.math.CTText)obj);
}
}
return null;
}

}
Loading