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
c0ec8f1e
Commit
c0ec8f1e
authored
Jun 17, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
打印模板处理
parent
cc9c950e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
65 additions
and
33 deletions
+65
-33
PrinterHelper.java
...int/src/main/java/com/yanzuoguang/util/PrinterHelper.java
+51
-33
PrinterFormatHandle.java
...ava/com/yanzuoguang/util/printer/PrinterFormatHandle.java
+14
-0
No files found.
yzg-util-print/src/main/java/com/yanzuoguang/util/PrinterHelper.java
View file @
c0ec8f1e
...
...
@@ -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
=
fieldF
rom
.
split
(
":"
);
String
[]
fields
=
fieldF
ull
.
split
(
":"
);
String
field
=
fields
[
0
];
Object
fieldValue
=
ObjectHelper
.
get
(
resultFrom
,
field
)
;
String
fieldFormat
=
StringHelper
.
EMPTY
;
if
(
fields
.
length
>
1
)
{
field
=
StringHelper
.
md5
(
fieldFrom
);
field
Format
=
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
();
});
}
}
yzg-util-print/src/main/java/com/yanzuoguang/util/printer/PrinterFormatHandle.java
0 → 100644
View file @
c0ec8f1e
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
);
}
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