-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLValidator.java
More file actions
119 lines (95 loc) · 3.3 KB
/
XMLValidator.java
File metadata and controls
119 lines (95 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package controller;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class XMLValidator {
private List<String> schemaFileList;
private String xmlFilePath, schemaFolderPath;
public XMLValidator(String xmlFilePath, String schemaFolderPath) {
setXmlFilePath(xmlFilePath);
setSchemaFolderPath(schemaFolderPath);
schemaFileList = new ArrayList<String>();
}
public List<SAXParseException> validate() throws Exception {
listSchemaFile(new File(this.schemaFolderPath));
List<SAXParseException> exceptions = new LinkedList<SAXParseException>();
StreamSource[] schemaSourceList = Arrays.stream(schemaFileList.toArray(new String[0])).map(StreamSource::new).collect(Collectors.toList()).toArray(new StreamSource[schemaFileList.size()]);
Source xmlFile = new StreamSource(new File(this.xmlFilePath));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaSourceList);
Validator validator = schema.newValidator();
validator.setErrorHandler(new ErrorHandler()
{
@Override
public void warning(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
@Override
public void fatalError(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
@Override
public void error(SAXParseException exception) throws SAXException
{
exceptions.add(exception);
}
});
validator.validate(xmlFile);
//System.out.println("\t" + xmlFile.getSystemId() + " is valid");
return exceptions;
}
public void validateWithFHIRCli(String filePath) throws Exception {
File file = new File(filePath);
try {
HL7FHIRController.convertAndValidate(file.getAbsolutePath());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
/* Future support with custom profile
public void validateWithFHIRCli(String filePath, String fhirProfile) throws Exception {
File file = new File(filePath);
try {
HL7FHIRController.convertAndValidate(file.getAbsolutePath(), fhirProfile);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
*/
public void listSchemaFile(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listSchemaFile(fileEntry);
} else {
if (fileEntry.getName().endsWith(".xsd"))
schemaFileList.add(fileEntry.getAbsolutePath());
}
}
}
public String getXmlFilePath() {
return xmlFilePath;
}
public void setXmlFilePath(String xmlFilePath) {
this.xmlFilePath = xmlFilePath;
}
public String getSchemaFolderPath() {
return schemaFolderPath;
}
public void setSchemaFolderPath(String schemaFolderPath) {
this.schemaFolderPath = schemaFolderPath;
}
}