Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
swiftMtMx
Overview
Overview
Details
Activity
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
isc-v3.1-tmp
swiftMtMx
Commits
43bda1f4
Commit
43bda1f4
authored
Jun 27, 2022
by
chengzhuoshen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加mx报文的schema校验,并提交appHdr.xsd
parent
99d4aada
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
25 deletions
+89
-25
SwiftTransfer.java
...ore/src/main/java/com/brilliance/swift/SwiftTransfer.java
+87
-0
Pacs00800108.xsd
swiftCore/src/main/resources/xsd/Pacs00800108.xsd
+0
-0
head00100102.xsd
swiftCore/src/main/resources/xsd/head00100102.xsd
+0
-0
Test.java
...ore/src/test/java/com/brilliance/mx2mtmap/mt103/Test.java
+2
-25
No files found.
swiftCore/src/main/java/com/brilliance/swift/SwiftTransfer.java
View file @
43bda1f4
...
@@ -8,15 +8,28 @@ import com.brilliance.swift.swiftdto2mx.SwiftDto2MxCreatorManager;
...
@@ -8,15 +8,28 @@ import com.brilliance.swift.swiftdto2mx.SwiftDto2MxCreatorManager;
import
com.brilliance.swift.util.StringUtil
;
import
com.brilliance.swift.util.StringUtil
;
import
com.brilliance.swift.util.XmlUtil
;
import
com.brilliance.swift.util.XmlUtil
;
import
com.brilliance.swift.vo.SwiftDto
;
import
com.brilliance.swift.vo.SwiftDto
;
import
com.prowidesoftware.swift.model.MxId
;
import
com.prowidesoftware.swift.model.mx.AbstractMX
;
import
com.prowidesoftware.swift.model.mx.AbstractMX
;
import
com.prowidesoftware.swift.model.mx.NamespaceReader
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.io.FileUtils
;
import
org.dom4j.Document
;
import
org.dom4j.Document
;
import
org.dom4j.DocumentException
;
import
org.dom4j.DocumentException
;
import
org.dom4j.DocumentHelper
;
import
org.dom4j.DocumentHelper
;
import
org.xml.sax.SAXException
;
import
javax.xml.transform.stream.StreamSource
;
import
javax.xml.validation.Schema
;
import
javax.xml.validation.SchemaFactory
;
import
javax.xml.validation.Validator
;
import
java.io.ByteArrayInputStream
;
import
java.io.File
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.net.URL
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
public
class
SwiftTransfer
{
public
class
SwiftTransfer
{
...
@@ -110,4 +123,78 @@ public class SwiftTransfer {
...
@@ -110,4 +123,78 @@ public class SwiftTransfer {
return
new
Mx2MapCreatorManager
().
mx2Map
(
xmlStr
);
return
new
Mx2MapCreatorManager
().
mx2Map
(
xmlStr
);
}
}
/**
* 根据xml的内容获取对应的xsd文件并校验
* 失败则抛出异常
* @param xml
* @return
*/
public
static
boolean
validateMx
(
String
xml
)
{
//1.获取AppHdr报文内容,根据@xmlns获取版本对应的xsd文件
String
regex
=
"(\\<AppHdr[\\w\\W]*\\<\\/AppHdr\\>)"
;
Pattern
p
=
Pattern
.
compile
(
regex
);
Matcher
m
=
p
.
matcher
(
xml
);
String
appHdrXml
=
""
;
if
(
m
.
find
())
{
appHdrXml
=
m
.
group
(
1
);
}
String
appHdrXsdName
=
null
;
Optional
<
String
>
namespace
=
NamespaceReader
.
findAppHdrNamespace
(
xml
);
if
(
namespace
.
isPresent
())
{
MxId
appHdrMxId
=
new
MxId
(
namespace
.
get
());
appHdrXsdName
=
appHdrMxId
.
getBusinessProcess
().
name
()
+
appHdrMxId
.
getFunctionality
()
+
appHdrMxId
.
getVariant
()
+
appHdrMxId
.
getVersion
();
}
if
(
StringUtil
.
isNotEmpty
(
appHdrXsdName
)
&&
StringUtil
.
isNotEmpty
(
appHdrXml
))
{
schemaValidate
(
appHdrXsdName
,
appHdrXml
);
}
//2.获取Document报文内容,根据@xmlns获取版本对应的xsd文件
regex
=
"(\\<Document[\\w\\W]*\\<\\/Document\\>)"
;
p
=
Pattern
.
compile
(
regex
);
m
=
p
.
matcher
(
xml
);
String
documentXml
=
""
;
if
(
m
.
find
())
{
documentXml
=
m
.
group
(
1
);
}
String
documentXsdName
=
""
;
namespace
=
NamespaceReader
.
findDocumentNamespace
(
xml
);
if
(
namespace
.
isPresent
())
{
MxId
documentMxId
=
new
MxId
(
namespace
.
get
());
documentXsdName
=
documentMxId
.
getBusinessProcess
().
name
()
+
documentMxId
.
getFunctionality
()
+
documentMxId
.
getVariant
()
+
documentMxId
.
getVersion
();
}
if
(
StringUtil
.
isNotEmpty
(
documentXsdName
)
&&
StringUtil
.
isNotEmpty
(
documentXml
))
{
schemaValidate
(
documentXsdName
,
documentXml
);
}
return
true
;
}
public
static
void
schemaValidate
(
String
xsdName
,
String
xml
)
{
try
{
//建立schema工厂
SchemaFactory
schemaFactory
=
SchemaFactory
.
newInstance
(
"http://www.w3.org/2001/XMLSchema"
);
URL
url
=
SwiftTransfer
.
class
.
getResource
(
"/xsd/"
+
xsdName
+
".xsd"
);
//利用schema工厂,接收验证文档文件对象生成Schema对象
Schema
schema
=
schemaFactory
.
newSchema
(
url
);
// 开始验证
Validator
validator
=
schema
.
newValidator
();
validator
.
validate
(
new
StreamSource
(
new
ByteArrayInputStream
(
xml
.
getBytes
(
StandardCharsets
.
UTF_8
))));
}
catch
(
SAXException
e
)
{
throw
new
SwiftException
(
"Error"
,
e
.
getMessage
());
}
catch
(
IOException
e
)
{
throw
new
SwiftException
(
"Error"
,
e
.
getMessage
());
}
}
public
static
void
main
(
String
[]
args
)
throws
IOException
{
File
file
=
new
File
(
System
.
getProperty
(
"user.dir"
)+
"\\swiftCore\\src\\main\\resources\\swiftXml\\MxPacs00800108.xml"
);
String
xmlStr
=
FileUtils
.
readFileToString
(
file
);
validateMx
(
xmlStr
);
}
}
}
swiftCore/src/main/resources/xsd/
p
acs00800108.xsd
→
swiftCore/src/main/resources/xsd/
P
acs00800108.xsd
View file @
43bda1f4
File moved
swiftCore/src/main/resources/xsd/head00100102.xsd
0 → 100644
View file @
43bda1f4
This diff is collapsed.
Click to expand it.
swiftCore/src/test/java/com/brilliance/mx2mtmap/mt103/Test.java
View file @
43bda1f4
package
com
.
brilliance
.
mx2mtmap
.
mt103
;
package
com
.
brilliance
.
mx2mtmap
.
mt103
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONArray
;
import
com.prowidesoftware.swift.model.mx.AbstractMX
;
import
com.prowidesoftware.swift.model.mx.AbstractMX
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.io.FileUtils
;
import
java.io.File
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.util.Map
;
public
class
Test
{
public
class
Test
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
File
file
=
new
File
(
System
.
getProperty
(
"user.dir"
)+
"\\swiftCore\\src\\main\\resources\\swiftXml\\MxPacs0080010
9
.xml"
);
File
file
=
new
File
(
System
.
getProperty
(
"user.dir"
)+
"\\swiftCore\\src\\main\\resources\\swiftXml\\MxPacs0080010
8
.xml"
);
String
xmlStr
=
FileUtils
.
readFileToString
(
file
);
String
xmlStr
=
FileUtils
.
readFileToString
(
file
);
//String xmlStr = FileUtils.readFileToString(new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftXml\\MxCamt05400108_CREDIT.xml"));
//String xmlStr = FileUtils.readFileToString(new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftXml\\MxCamt05400108_CREDIT.xml"));
//Map<String, Object> extraMap = new HashMap<>();
//Map<String, Object> extraMap = new HashMap<>();
...
@@ -33,27 +30,7 @@ public class Test {
...
@@ -33,27 +30,7 @@ public class Test {
AbstractMX
abstractMX
=
AbstractMX
.
parse
(
xmlStr
);
AbstractMX
abstractMX
=
AbstractMX
.
parse
(
xmlStr
);
String
gsonStr
=
abstractMX
.
toJson
();
String
gsonStr
=
abstractMX
.
toJson
();
System
.
out
.
println
(
gsonStr
);
System
.
out
.
println
(
gsonStr
);
AbstractMX
abstractMX1
=
AbstractMX
.
fromJson
(
gsonStr
);
System
.
out
.
println
(
abstractMX1
.
message
());
Map
<
String
,
Object
>
mxGsonMaps
=
JSON
.
parseObject
(
gsonStr
);
//String swiftGsonStr = new JSONObject(mxGsonMaps).toJSONString();
//AbstractMX abstractMX1 = AbstractMX.fromJson(swiftGsonStr);
// System.out.println(abstractMX1.toString());
Map
<
String
,
Object
>
maps
=
(
Map
<
String
,
Object
>)
mxGsonMaps
.
get
(
"fiToFICstmrCdtTrf"
);
JSONArray
jsonArrays
=
(
JSONArray
)
maps
.
get
(
"cdtTrfTxInf"
);
int
size
=
jsonArrays
.
size
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
Map
<
String
,
Object
>
tmpMaps
=
(
Map
<
String
,
Object
>)
jsonArrays
.
get
(
i
);
Map
<
String
,
Object
>
maps1
=
(
Map
<
String
,
Object
>)
tmpMaps
.
get
(
"cdtr"
);
maps1
=
(
Map
<
String
,
Object
>)
maps1
.
get
(
"pstlAdr"
);
JSONArray
objs
=
(
JSONArray
)
maps1
.
get
(
"adrLine"
);
int
objSize
=
objs
.
size
();
for
(
int
j
=
0
;
j
<
objSize
;
j
++)
{
System
.
out
.
println
(
objs
.
get
(
j
).
getClass
());
}
}
//System.out.println(mxGsonMaps);
//System.out.println(mxGsonMaps);
}
}
}
}
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