Commit cc9c950e authored by yanzg's avatar yanzg

打印模板处理

parent b0109f2d
......@@ -15,6 +15,7 @@
<module>yzg-util-cloud</module>
<module>yzg-util-mq</module>
<module>yzg-util-image</module>
<module>yzg-util-print</module>
</modules>
<properties>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yzg-util</artifactId>
<groupId>com.yanzuoguang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yzg-util-print</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.yanzuoguang</groupId>
<artifactId>yzg-util-base</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.yanzuoguang.util;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PrinterHelper {
/**
* 将数据格式化为新得数据
*
* @param resultTo 结果数据
* @param resultFrom 结果来源
* @param format 格式化
* @param defaultField 默认字段
* @return
*/
public static String getFormatTo(Map<String, Object> resultTo, Object resultFrom,
String format, String defaultField) {
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()));
}
sb.append("{");
String fieldFrom = matches.group(1);
if (fieldFrom.startsWith("0")) {
fieldFrom = defaultField + fieldFrom.substring(1);
}
String[] fields = fieldFrom.split(":");
String field = fields[0];
Object fieldValue = ObjectHelper.get(resultFrom, field);
if (fields.length > 1) {
field = StringHelper.md5(fieldFrom);
}
sb.append(field);
resultTo.put(field, fieldValue);
sb.append("}");
start = matches.end();
}
sb.append(format.substring(start));
return sb.toString();
}
/**
* 获取结果字符串,调用本方法前请先调用getFormatTo函数
*
* @param resultFrom 来源字符
* @param format 格式化字符串
* @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()));
}
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 helper;
import com.yanzuoguang.util.PrinterHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class TestMediaHelper {
@Test
public void testConvert() {
Map<String, Object> from = new HashMap<>();
from.put("name", "颜佐光");
from.put("age", "19");
Map<String, Object> to = new HashMap<>();
String format = "xxxx{0:4*4}{0:4*3}{0:4*3}xxxx{age}";
String formatTo = PrinterHelper.getFormatTo(to, from, format, "name");
String result = PrinterHelper.getResult(to, formatTo);
System.out.println("format:" + formatTo);
System.out.println("json:" + JsonHelper.serialize(to, true));
System.out.println("result:" + result);
}
}
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