Commit c0ec8f1e authored by yanzg's avatar yanzg

打印模板处理

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