1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.xml;
17
18 import org.openehealth.ipf.commons.core.modules.api.ValidationException;
19 import org.openehealth.ipf.commons.core.modules.api.Validator;
20
21 import static java.util.Objects.requireNonNull;
22 import static org.openehealth.ipf.commons.xml.XmlUtils.rootElementName;
23 import static org.openehealth.ipf.commons.xml.XmlUtils.source;
24
25
26
27
28
29 public class CombinedXmlValidator implements Validator<String, CombinedXmlValidationProfile> {
30
31 private static final XsdValidator XSD_VALIDATOR = new XsdValidator(CombinedXmlValidator.class.getClassLoader());
32 private static final SchematronValidator SCHEMATRON_VALIDATOR = new SchematronValidator();
33
34
35 public void validate(String message, CombinedXmlValidationProfile profile) throws ValidationException {
36 requireNonNull(profile, "validation profile must be not null");
37
38 String rootElementName = rootElementName(requireNonNull(message, "message must be not null"));
39 if (! profile.isValidRootElement(rootElementName)) {
40 throw new ValidationException("Invalid root element '" + rootElementName + "'");
41 }
42
43
44 String xsdPath = profile.getXsdPath(rootElementName);
45 if (xsdPath != null) {
46 XSD_VALIDATOR.validate(source(message), xsdPath);
47 }
48
49
50 String schematronPath = profile.getSchematronPath(rootElementName);
51 if (schematronPath != null) {
52 SchematronProfile schematronProfile = new SchematronProfile(
53 schematronPath,
54 profile.getCustomSchematronParameters(rootElementName));
55 SCHEMATRON_VALIDATOR.validate(source(message), schematronProfile);
56 }
57 }
58 }