Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
cc9c950e
Commit
cc9c950e
authored
Jun 17, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
打印模板处理
parent
b0109f2d
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
135 additions
and
0 deletions
+135
-0
pom.xml
pom.xml
+1
-0
pom.xml
yzg-util-print/pom.xml
+27
-0
readme.md
yzg-util-print/src/main/java/com/yanzuoguang/readme.md
+0
-0
readme.txt
yzg-util-print/src/main/java/com/yanzuoguang/readme.txt
+0
-0
PrinterHelper.java
...int/src/main/java/com/yanzuoguang/util/PrinterHelper.java
+78
-0
TestMediaHelper.java
yzg-util-print/src/test/java/helper/TestMediaHelper.java
+29
-0
No files found.
pom.xml
View file @
cc9c950e
...
...
@@ -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>
...
...
yzg-util-print/pom.xml
0 → 100644
View file @
cc9c950e
<?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
yzg-util-print/src/main/java/com/yanzuoguang/readme.md
0 → 100644
View file @
cc9c950e
yzg-util-print/src/main/java/com/yanzuoguang/readme.txt
0 → 100644
View file @
cc9c950e
yzg-util-print/src/main/java/com/yanzuoguang/util/PrinterHelper.java
0 → 100644
View file @
cc9c950e
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
();
}
}
yzg-util-print/src/test/java/helper/TestMediaHelper.java
0 → 100644
View file @
cc9c950e
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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment