com.github.microwww.ttp.opt.ReplaceOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of template-ppt Show documentation
Show all versions of template-ppt Show documentation
This is a template language for PPTX
package com.github.microwww.ttp.opt;
import com.github.microwww.ttp.Assert;
import com.github.microwww.ttp.Tools;
import com.github.microwww.ttp.replace.ReplaceExpress;
import com.github.microwww.ttp.replace.SearchContent;
import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFPieChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFRadarChartData;
import org.apache.poi.xslf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.List;
public class ReplaceOperation extends Operation {
private static final Logger logger = LoggerFactory.getLogger(ReplaceOperation.class);
public ReplaceOperation() {
}
@Override
public void parse(ParseContext context) {
List> search = super.search(context);
for (Object o : search) {
thisInvoke("replace", context, o);
}
}
// DEFAULT
public void replace(ParseContext context, Object item) {
logger.warn("Not support type : {}", item.getClass());
}
public void replace(ParseContext context, XSLFGraphicChart item) {
XSLFChart chart = item.getChart();
List data = chart.getChartSeries();
if (data.isEmpty()) {
throw new IllegalArgumentException("NO know chart type");
}
XDDFChartData type = data.get(0);
String[] params = this.getParams();
Assert.isTrue(params.length >= 3, "Chart data [title, category[], data[]], Must 3 params value");
ParamMessage msg = this.getParamsWithPattern()[0];
Object value = super.getValue(msg.getParam(), context.getDataStack());
String title = msg.format(value);
List categories = super.getCollectionValue(params[1], context.getDataStack());
String[] cts = parse2string(categories);
if (type instanceof XDDFPieChartData) {
List values = super.getCollectionValue(params[2], context.getDataStack());
Assert.isTrue(values.size() == categories.size(), "Error CATEGORY.length != VALUE.length");
Double[] dbs = parse2double(values);
Tools.setPieDate(chart, title, cts, dbs);
} else {
if (!(type instanceof XDDFRadarChartData) && !(type instanceof XDDFBarChartData)){
logger.warn("UNKNOWN Chart type : {}, ", type);
}
List series = super.getCollectionValue(params[2], context.getDataStack());
String[] ss = parse2string(series);
Double[][] dbs = new Double[params.length - 3][];
for (int i = 0; i < dbs.length; i++) {
List values = super.getCollectionValue(params[i + 3], context.getDataStack());
dbs[i] = parse2double(values);
}
Tools.setRadarData(chart, title, cts, ss, dbs);
}
}
public static Double[] parse2double(Collection