Skip to content

Commit 20ed83e

Browse files
committed
[#759] Validator Signature: Change signature for XSD validator
1 parent dee705f commit 20ed83e

File tree

3 files changed

+60
-29
lines changed

3 files changed

+60
-29
lines changed

dmn-core/src/main/java/com/gs/dmn/serialization/XSDSchemaValidator.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
*/
1313
package com.gs.dmn.serialization;
1414

15-
import com.gs.dmn.runtime.DMNRuntimeException;
15+
import com.gs.dmn.ast.TDefinitions;
16+
import com.gs.dmn.error.ErrorFactory;
17+
import com.gs.dmn.error.SemanticErrorException;
18+
import com.gs.dmn.error.ValidationError;
19+
import com.gs.dmn.feel.ModelLocation;
1620
import org.slf4j.Logger;
1721
import org.slf4j.LoggerFactory;
1822
import org.xml.sax.SAXException;
@@ -29,18 +33,17 @@
2933
import java.util.ArrayList;
3034
import java.util.List;
3135
import java.util.Objects;
36+
import java.util.stream.Collectors;
3237

3338
public class XSDSchemaValidator {
3439
private static final Logger LOGGER = LoggerFactory.getLogger(XSDSchemaValidator.class);
40+
static final String RULE_NAME = "xsd-validation";
3541

36-
private XSDSchemaValidator() {
37-
}
38-
39-
public static List<String> validateXSDSchema(Source source, DMNVersion dmnVersion) {
42+
public List<ValidationError> validateXSDSchema(Source source, DMNVersion dmnVersion) {
4043
return validateXSDSchema(source, dmnVersion.getSchemaLocation());
4144
}
4245

43-
private static List<String> validateXSDSchema(Source source, String schemaPath) {
46+
private List<ValidationError> validateXSDSchema(Source source, String schemaPath) {
4447
try {
4548
Validator validator = makeValidator(schemaPath);
4649
XsdErrorHandler errorHandler = new XsdErrorHandler();
@@ -54,15 +57,39 @@ private static List<String> validateXSDSchema(Source source, String schemaPath)
5457
errors.add(errorMessage);
5558
}
5659
}
57-
return errors;
60+
ModelLocation modelLocation = makeModelLocation(source);
61+
return errors.stream().map(e -> new ValidationError(ErrorFactory.makeDMNError(modelLocation, e), RULE_NAME)).collect(Collectors.toList());
5862
} catch (Exception e) {
5963
String errorMessage = "Validation failed due to a critical error: " + e.getMessage();
6064
LOGGER.error(errorMessage);
61-
throw new DMNRuntimeException(e);
65+
throw new SemanticErrorException(errorMessage, e);
66+
}
67+
}
68+
69+
private ModelLocation makeModelLocation(Source source) {
70+
String systemId = source.getSystemId();
71+
ModelLocation modelLocation = null;
72+
if (systemId != null && systemId.endsWith(DMNConstants.DMN_FILE_EXTENSION)) {
73+
// Determine file name
74+
String fileName;
75+
int index = systemId.lastIndexOf("/");
76+
if (index != -1) {
77+
fileName = systemId.substring(index + 1);
78+
} else {
79+
fileName = systemId;
80+
}
81+
// Determine model name
82+
String modelName = fileName.substring(0, fileName.indexOf(DMNConstants.DMN_FILE_EXTENSION));
83+
// Make dummy model
84+
TDefinitions model = new TDefinitions();
85+
model.setName(modelName);
86+
87+
modelLocation = new ModelLocation(model, null);
6288
}
89+
return modelLocation;
6390
}
6491

65-
private static Validator makeValidator(String schemaPath) throws MalformedURLException, URISyntaxException, SAXException {
92+
private Validator makeValidator(String schemaPath) throws MalformedURLException, URISyntaxException, SAXException {
6693
URL schemaURL = Objects.requireNonNull(XSDSchemaValidator.class.getClassLoader().getResource(schemaPath)).toURI().toURL();
6794
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
6895
// Prohibit the use of all protocols by external entities:

dmn-core/src/main/java/com/gs/dmn/serialization/xstream/XStreamMarshaller.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import com.gs.dmn.ast.DMNBaseElement;
1616
import com.gs.dmn.ast.TDefinitions;
17+
import com.gs.dmn.error.ValidationError;
1718
import com.gs.dmn.runtime.DMNRuntimeException;
1819
import com.gs.dmn.serialization.DMNMarshaller;
1920
import com.gs.dmn.serialization.DMNVersion;
@@ -125,7 +126,7 @@ public TDefinitions unmarshal(String input, boolean validateSchema) {
125126
DMNVersion dmnVersion = inferDMNVersion(firstStringReader);
126127
if (validateSchema && dmnVersion != null) {
127128
try (StringReader reader = new StringReader(input)) {
128-
List<String> errors = XSDSchemaValidator.validateXSDSchema(new StreamSource(reader), dmnVersion);
129+
List<ValidationError> errors = new XSDSchemaValidator().validateXSDSchema(new StreamSource(reader), dmnVersion);
129130
if (!errors.isEmpty()) {
130131
throw new DMNRuntimeException(String.format("%s", errors));
131132
}

dmn-core/src/test/java/com/gs/dmn/serialization/XSDSchemaValidatorTest.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.gs.dmn.serialization;
1414

15+
import com.gs.dmn.error.ValidationError;
1516
import com.gs.dmn.validation.AbstractValidatorTest;
1617
import org.junit.jupiter.api.Test;
1718

@@ -23,35 +24,37 @@
2324
import static org.junit.jupiter.api.Assertions.assertTrue;
2425

2526
public class XSDSchemaValidatorTest extends AbstractValidatorTest {
27+
private final XSDSchemaValidator validator = new XSDSchemaValidator();
28+
2629
@Test
2730
public void testSchemaValidationFor11() {
2831
File dmnFile = new File(resource("dmn/input/1.1/test-dmn.dmn").getPath());
29-
List<String> actualErrors = XSDSchemaValidator.validateXSDSchema(new StreamSource(dmnFile), DMNVersion.DMN_11);
32+
List<ValidationError> actualErrors = validator.validateXSDSchema(new StreamSource(dmnFile), DMNVersion.DMN_11);
3033
assertTrue(actualErrors.isEmpty());
3134
}
3235

3336
@Test
3437
public void testSchemaValidationFor15() {
3538
File dmnFile = new File(resource("dmn/input/1.5/test-dmn.dmn").getPath());
36-
List<String> actualErrors = XSDSchemaValidator.validateXSDSchema(new StreamSource(dmnFile), DMNVersion.DMN_15);
39+
List<ValidationError> actualErrors = validator.validateXSDSchema(new StreamSource(dmnFile), DMNVersion.DMN_15);
3740
List<String> expectedErrors = Arrays.asList(
38-
"Line 80, Column 15: cvc-complex-type.4: Attribute 'name' must appear on element 'decision'.",
39-
"Line 83, Column 24: cvc-complex-type.4: Attribute 'name' must appear on element 'decision'.",
40-
"Line 87, Column 20: cvc-complex-type.4: Attribute 'name' must appear on element 'variable'.",
41-
"Line 98, Column 29: cvc-complex-type.4: Attribute 'name' must appear on element 'businessKnowledgeModel'.",
42-
"Line 101, Column 38: cvc-complex-type.4: Attribute 'name' must appear on element 'businessKnowledgeModel'.",
43-
"Line 104, Column 39: cvc-complex-type.4: Attribute 'name' must appear on element 'businessKnowledgeModel'.",
44-
"Line 105, Column 20: cvc-complex-type.4: Attribute 'name' must appear on element 'variable'.",
45-
"Line 112, Column 29: cvc-complex-type.2.4.a: Invalid content was found starting with element '{\"https://www.omg.org/spec/DMN/20230324/MODEL/\":literalExpression}'. One of '{\"https://www.omg.org/spec/DMN/20230324/MODEL/\":knowledgeRequirement, \"https://www.omg.org/spec/DMN/20230324/MODEL/\":authorityRequirement}' is expected.",
46-
"Line 115, Column 22: cvc-complex-type.4: Attribute 'name' must appear on element 'decisionService'.",
47-
"Line 118, Column 30: cvc-complex-type.4: Attribute 'name' must appear on element 'decisionService'.",
48-
"Line 121, Column 31: cvc-complex-type.4: Attribute 'name' must appear on element 'decisionService'.",
49-
"Line 122, Column 20: cvc-complex-type.4: Attribute 'name' must appear on element 'variable'.",
50-
"Line 123, Column 21: cvc-complex-type.4: Attribute 'href' must appear on element 'inputData'.",
51-
"Line 124, Column 25: cvc-complex-type.2.4.a: Invalid content was found starting with element '{\"https://www.omg.org/spec/DMN/20230324/MODEL/\":inputDecision}'. One of '{\"https://www.omg.org/spec/DMN/20230324/MODEL/\":inputData}' is expected.",
52-
"Line 124, Column 25: cvc-complex-type.4: Attribute 'href' must appear on element 'inputDecision'.",
53-
"Line 125, Column 26: cvc-complex-type.4: Attribute 'href' must appear on element 'outputDecision'."
41+
"[ERROR] (modelName = 'test-dmn'): Line 80, Column 15: cvc-complex-type.4: Attribute 'name' must appear on element 'decision'.",
42+
"[ERROR] (modelName = 'test-dmn'): Line 83, Column 24: cvc-complex-type.4: Attribute 'name' must appear on element 'decision'.",
43+
"[ERROR] (modelName = 'test-dmn'): Line 87, Column 20: cvc-complex-type.4: Attribute 'name' must appear on element 'variable'.",
44+
"[ERROR] (modelName = 'test-dmn'): Line 98, Column 29: cvc-complex-type.4: Attribute 'name' must appear on element 'businessKnowledgeModel'.",
45+
"[ERROR] (modelName = 'test-dmn'): Line 101, Column 38: cvc-complex-type.4: Attribute 'name' must appear on element 'businessKnowledgeModel'.",
46+
"[ERROR] (modelName = 'test-dmn'): Line 104, Column 39: cvc-complex-type.4: Attribute 'name' must appear on element 'businessKnowledgeModel'.",
47+
"[ERROR] (modelName = 'test-dmn'): Line 105, Column 20: cvc-complex-type.4: Attribute 'name' must appear on element 'variable'.",
48+
"[ERROR] (modelName = 'test-dmn'): Line 112, Column 29: cvc-complex-type.2.4.a: Invalid content was found starting with element '{\"https://www.omg.org/spec/DMN/20230324/MODEL/\":literalExpression}'. One of '{\"https://www.omg.org/spec/DMN/20230324/MODEL/\":knowledgeRequirement, \"https://www.omg.org/spec/DMN/20230324/MODEL/\":authorityRequirement}' is expected.",
49+
"[ERROR] (modelName = 'test-dmn'): Line 115, Column 22: cvc-complex-type.4: Attribute 'name' must appear on element 'decisionService'.",
50+
"[ERROR] (modelName = 'test-dmn'): Line 118, Column 30: cvc-complex-type.4: Attribute 'name' must appear on element 'decisionService'.",
51+
"[ERROR] (modelName = 'test-dmn'): Line 121, Column 31: cvc-complex-type.4: Attribute 'name' must appear on element 'decisionService'.",
52+
"[ERROR] (modelName = 'test-dmn'): Line 122, Column 20: cvc-complex-type.4: Attribute 'name' must appear on element 'variable'.",
53+
"[ERROR] (modelName = 'test-dmn'): Line 123, Column 21: cvc-complex-type.4: Attribute 'href' must appear on element 'inputData'.",
54+
"[ERROR] (modelName = 'test-dmn'): Line 124, Column 25: cvc-complex-type.2.4.a: Invalid content was found starting with element '{\"https://www.omg.org/spec/DMN/20230324/MODEL/\":inputDecision}'. One of '{\"https://www.omg.org/spec/DMN/20230324/MODEL/\":inputData}' is expected.",
55+
"[ERROR] (modelName = 'test-dmn'): Line 124, Column 25: cvc-complex-type.4: Attribute 'href' must appear on element 'inputDecision'.",
56+
"[ERROR] (modelName = 'test-dmn'): Line 125, Column 26: cvc-complex-type.4: Attribute 'href' must appear on element 'outputDecision'."
5457
);
55-
checkXSDErrors(expectedErrors, actualErrors);
58+
checkErrors(XSDSchemaValidator.RULE_NAME, expectedErrors, actualErrors);
5659
}
5760
}

0 commit comments

Comments
 (0)