forked from lxm_tools/screw
♻️ 优化代码,针对word和markdown做特殊处理,word:对数据进行xml转义、markdown:对数据换行进行处理
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
package cn.smallbun.screw.core.process;
|
||||
|
||||
import cn.smallbun.screw.core.Configuration;
|
||||
import cn.smallbun.screw.core.engine.EngineFileType;
|
||||
import cn.smallbun.screw.core.metadata.Column;
|
||||
import cn.smallbun.screw.core.metadata.Database;
|
||||
import cn.smallbun.screw.core.metadata.PrimaryKey;
|
||||
@@ -37,6 +38,7 @@ import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.smallbun.screw.core.constant.DefaultConstants.*;
|
||||
import static cn.smallbun.screw.core.util.BeanUtils.*;
|
||||
|
||||
/**
|
||||
* 数据模型处理
|
||||
@@ -129,18 +131,13 @@ public class DataModelProcess extends AbstractProcess {
|
||||
for (Column column : columnsCaching.get(table.getTableName())) {
|
||||
packageColumn(columnModels, keyList, column);
|
||||
}
|
||||
//去除空格
|
||||
columnModels.forEach(BeanUtils::beanAttributeValueTrim);
|
||||
//放入列
|
||||
tableModel.setColumns(columnModels);
|
||||
}
|
||||
//处理忽略表
|
||||
List<TableModel> ignore = handleIgnore(tableModels);
|
||||
//处理字段非空
|
||||
ignore.forEach(BeanUtils::beanAttributeValueTrim);
|
||||
model.setTables(ignore);
|
||||
//去除model空格
|
||||
BeanUtils.beanAttributeValueTrim(model);
|
||||
model.setTables(handleIgnore(tableModels));
|
||||
//优化数据
|
||||
optimizeData(model);
|
||||
/*封装数据结束*/
|
||||
logger.debug("encapsulation processing data time consuming:{}ms",
|
||||
(System.currentTimeMillis() - start));
|
||||
@@ -215,4 +212,49 @@ public class DataModelProcess extends AbstractProcess {
|
||||
}
|
||||
return tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* 优化数据
|
||||
* @param dataModel {@link DataModel}
|
||||
*/
|
||||
public void optimizeData(DataModel dataModel) {
|
||||
//trim
|
||||
beanAttributeValueTrim(dataModel);
|
||||
//tables
|
||||
List<TableModel> tables = dataModel.getTables();
|
||||
//columns
|
||||
tables.forEach(i -> {
|
||||
//table escape xml
|
||||
beanAttributeValueTrim(i);
|
||||
List<ColumnModel> columns = i.getColumns();
|
||||
//columns escape xml
|
||||
columns.forEach(BeanUtils::beanAttributeValueTrim);
|
||||
});
|
||||
//if file type is word
|
||||
if (config.getEngineConfig().getFileType().equals(EngineFileType.WORD)) {
|
||||
//escape xml
|
||||
beanAttributeValueEscapeXml(dataModel);
|
||||
//tables
|
||||
tables.forEach(i -> {
|
||||
//table escape xml
|
||||
beanAttributeValueEscapeXml(i);
|
||||
List<ColumnModel> columns = i.getColumns();
|
||||
//columns escape xml
|
||||
columns.forEach(BeanUtils::beanAttributeValueEscapeXml);
|
||||
});
|
||||
}
|
||||
//if file type is markdown
|
||||
if (config.getEngineConfig().getFileType().equals(EngineFileType.MD)) {
|
||||
//escape xml
|
||||
beanAttributeValueReplaceBlank(dataModel);
|
||||
//columns
|
||||
tables.forEach(i -> {
|
||||
//table escape xml
|
||||
beanAttributeValueReplaceBlank(i);
|
||||
List<ColumnModel> columns = i.getColumns();
|
||||
//columns escape xml
|
||||
columns.forEach(BeanUtils::beanAttributeValueReplaceBlank);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,12 +22,67 @@ import org.apache.commons.lang.StringEscapeUtils;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static cn.smallbun.screw.core.util.StringUtils.replaceBlank;
|
||||
|
||||
/**
|
||||
* BeanUtils
|
||||
* @author SanLi
|
||||
* Created by qinggang.zuo@gmail.com / 2689170096@qq.com on 2020/7/5 19:58
|
||||
*/
|
||||
public class BeanUtils {
|
||||
/**
|
||||
* 转义bean中所有属性为字符串的
|
||||
* @param bean {@link Object}
|
||||
*/
|
||||
public static void beanAttributeValueEscapeXml(Object bean) {
|
||||
try {
|
||||
if (bean != null) {
|
||||
//获取所有的字段包括public,private,protected,private
|
||||
Field[] fields = bean.getClass().getDeclaredFields();
|
||||
for (Field f : fields) {
|
||||
if ("java.lang.String".equals(f.getType().getName())) {
|
||||
//获取字段名
|
||||
String key = f.getName();
|
||||
Object value = getFieldValue(bean, key);
|
||||
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
setFieldValue(bean, key, StringEscapeUtils.escapeXml(value.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw ExceptionUtils.mpe(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* bean 中所有属性为字符串的进行\n\t\s处理
|
||||
* @param bean {@link Object}
|
||||
*/
|
||||
public static void beanAttributeValueReplaceBlank(Object bean) {
|
||||
try {
|
||||
if (bean != null) {
|
||||
//获取所有的字段包括public,private,protected,private
|
||||
Field[] fields = bean.getClass().getDeclaredFields();
|
||||
for (Field f : fields) {
|
||||
if ("java.lang.String".equals(f.getType().getName())) {
|
||||
//获取字段名
|
||||
String key = f.getName();
|
||||
Object value = getFieldValue(bean, key);
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
setFieldValue(bean, key, replaceBlank(value.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw ExceptionUtils.mpe(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉bean中所有属性为字符串的前后空格
|
||||
* @param bean {@link Object}
|
||||
@@ -46,9 +101,7 @@ public class BeanUtils {
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
setFieldValue(bean, key,
|
||||
StringEscapeUtils.escapeXml(value.toString().trim()));
|
||||
setFieldValue(bean, key, value.toString().trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
*/
|
||||
package cn.smallbun.screw.core.util;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author SanLi
|
||||
* Created by qinggang.zuo@gmail.com / 2689170096@qq.com on 2020/3/30 22:21
|
||||
@@ -55,6 +58,25 @@ public class StringUtils {
|
||||
return !isBlank(cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除换行
|
||||
*/
|
||||
static final Pattern REPLACE_BLANK_PATTERN = Pattern.compile("\\s*|\t|\r|\n");
|
||||
|
||||
/**
|
||||
* replaceBlank
|
||||
* @param str {@link String}
|
||||
* @return {@link String}
|
||||
*/
|
||||
public static String replaceBlank(String str) {
|
||||
String dest = "";
|
||||
if (str != null) {
|
||||
Matcher m = REPLACE_BLANK_PATTERN.matcher(str);
|
||||
dest = m.replaceAll("");
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空默认
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user