Commit c0ec8f1e authored by yanzg's avatar yanzg

打印模板处理

parent cc9c950e
......@@ -3,24 +3,28 @@ package com.yanzuoguang.util;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.printer.PrinterFormatHandle;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 构造函数
*
* @author 颜佐光
*/
public class PrinterHelper {
/**
* 将数据格式化为新得数据
* 进行字段格式化处理
*
* @param resultTo 结果数据
* @param resultFrom 结果来源
* @param format 格式化
* @param defaultField 默认字段
* @return
* @param format 需要处理得格式字符串
* @param defaultField 默认字段
* @param fieldFormatHandle 对字段进行格式化处理
* @return 处理之后得值
*/
public static String getFormatTo(Map<String, Object> resultTo, Object resultFrom,
String format, String defaultField) {
public static String getFormat(String format, String defaultField, PrinterFormatHandle fieldFormatHandle) {
StringBuilder sb = new StringBuilder();
Pattern reg = Pattern.compile("\\{(\\S+?)\\}");
Matcher matches = reg.matcher(format);
......@@ -30,26 +34,49 @@ public class PrinterHelper {
if (len > 0) {
sb.append(format.substring(start, matches.start()));
}
sb.append("{");
String fieldFrom = matches.group(1);
if (fieldFrom.startsWith("0")) {
fieldFrom = defaultField + fieldFrom.substring(1);
String fieldFull = matches.group(1);
if (fieldFull.startsWith("0")) {
fieldFull = defaultField + fieldFull.substring(1);
}
String[] fields = fieldFrom.split(":");
String[] fields = fieldFull.split(":");
String field = fields[0];
Object fieldValue = ObjectHelper.get(resultFrom, field);
String fieldFormat = StringHelper.EMPTY;
if (fields.length > 1) {
field = StringHelper.md5(fieldFrom);
fieldFormat = fieldFull.substring(field.length() + 1);
}
sb.append(field);
resultTo.put(field, fieldValue);
sb.append("}");
fieldFormatHandle.addPos(sb, fieldFull, field, fieldFormat);
start = matches.end();
}
sb.append(format.substring(start));
return sb.toString();
}
/**
* 将数据格式化为新得数据
*
* @param resultTo 结果数据
* @param resultFrom 结果来源
* @param format 格式化
* @param defaultField 默认字段
* @return
*/
public static String getFormatTo(Map<String, Object> resultTo, Object resultFrom,
String format, String defaultField) {
return getFormat(format, defaultField, new PrinterFormatHandle() {
@Override
public void addPos(StringBuilder sb, String fieldFull, String field, String fieldFormat) {
sb.append("{");
Object fieldValue = ObjectHelper.get(resultFrom, field);
if (!StringHelper.isEmpty(fieldFormat)) {
field = StringHelper.md5(fieldFull);
}
sb.append(field);
resultTo.put(field, fieldValue);
sb.append("}");
}
});
}
/**
* 获取结果字符串,调用本方法前请先调用getFormatTo函数
*
......@@ -58,21 +85,12 @@ public class PrinterHelper {
* @return
*/
public static String getResult(Object resultFrom, String format) {
StringBuilder sb = new StringBuilder();
Pattern reg = Pattern.compile("\\{(\\S+?)\\}");
Matcher matches = reg.matcher(format);
int start = 0;
while (matches.find()) {
int len = matches.start() - start;
if (len > 0) {
sb.append(format.substring(start, matches.start()));
return getFormat(format, StringHelper.EMPTY, new PrinterFormatHandle() {
@Override
public void addPos(StringBuilder sb, String fieldFull, String field, String fieldFormat) {
Object fieldValue = ObjectHelper.get(resultFrom, field);
sb.append(fieldValue);
}
String field = matches.group(1);
Object fieldValue = ObjectHelper.get(resultFrom, field);
sb.append(fieldValue);
start = matches.end();
}
sb.append(format.substring(start));
return sb.toString();
});
}
}
package com.yanzuoguang.util.printer;
public interface PrinterFormatHandle {
/**
* 对模板进行格式化处理
*
* @param sb 返回得结果模板
* @param fieldFull 全格式
* @param field 字段
* @param fieldFormat 字段格式化
*/
void addPos(StringBuilder sb, String fieldFull, String field, String fieldFormat);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment