
cn.afterturn.easypoi.word.parse.ParseWord07 Maven / Gradle / Ivy
/**
* Copyright 2013-2015 JueYue ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.afterturn.easypoi.word.parse;
import cn.afterturn.easypoi.cache.WordCache;
import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import cn.afterturn.easypoi.word.entity.MyXWPFDocument;
import cn.afterturn.easypoi.word.entity.params.ExcelListEntity;
import cn.afterturn.easypoi.word.parse.excel.ExcelEntityParse;
import cn.afterturn.easypoi.word.parse.excel.ExcelMapParse;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import static cn.afterturn.easypoi.util.PoiElUtil.*;
/**
* 解析07版的Word,替换文字,生成表格,生成图片
*
* @author JueYue
* 2013-11-16
* @version 1.0
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public class ParseWord07 {
private static final Logger LOGGER = LoggerFactory.getLogger(ParseWord07.class);
/**
* 根据条件改变值
*
* @param map
* @author JueYue
* 2013-11-16
*/
private void changeValues(XWPFParagraph paragraph, XWPFRun currentRun, String currentText,
List runIndex, Map map) throws Exception {
// 判断是不是迭代输出
if (currentText.contains(FOREACH) && currentText.startsWith(START_STR)) {
currentText = currentText.replace(FOREACH, EMPTY).replace(START_STR, EMPTY).replace(END_STR, EMPTY);
String[] keys = currentText.replaceAll("\\s{1,}", " ").trim().split(" ");
List list = (List)PoiPublicUtil.getParamsValue(keys[0], map);
list.forEach(obj -> {
if (obj instanceof ImageEntity) {
currentRun.setText("", 0);
ExcelMapParse.addAnImage((ImageEntity) obj, currentRun);
} else {
PoiPublicUtil.setWordText(currentRun, obj.toString());
}
});
} else {
Object obj = PoiPublicUtil.getRealValue(currentText, map);
// 如果是图片就设置为图片
if (obj instanceof ImageEntity) {
currentRun.setText("", 0);
ExcelMapParse.addAnImage((ImageEntity) obj, currentRun);
} else {
currentText = obj.toString();
PoiPublicUtil.setWordText(currentRun, currentText);
}
}
for (int k = 0; k < runIndex.size(); k++) {
paragraph.getRuns().get(runIndex.get(k)).setText("", 0);
}
runIndex.clear();
}
/**
* 判断是不是迭代输出
*
* @return
* @throws Exception
* @author JueYue
* 2013-11-18
*/
private Object checkThisTableIsNeedIterator(XWPFTableCell cell,
Map map) throws Exception {
String text = cell.getText().trim();
// 判断是不是迭代输出
if (text != null && text.contains(FOREACH) && text.startsWith(START_STR)) {
text = text.replace(FOREACH_NOT_CREATE, EMPTY).replace(FOREACH_AND_SHIFT, EMPTY)
.replace(FOREACH, EMPTY).replace(START_STR, EMPTY);
String[] keys = text.replaceAll("\\s{1,}", " ").trim().split(" ");
Object result = PoiPublicUtil.getParamsValue(keys[0], map);
//添加list默认值,避免将{{$fe: list t.sn t.hoby t.remark}} 这类标签直接显示出来
return Objects.nonNull(result) ? result : new ArrayList