Skip to content

Commit

Permalink
Merge pull request #270 from entur/add_jaxb_validator_demo
Browse files Browse the repository at this point in the history
Add JAXB validator demo
  • Loading branch information
vpaturet authored Jan 2, 2025
2 parents 085f590 + 966f92b commit e581598
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,23 @@ A **ValidationReportEntry** represents a unique validation finding identified by
- the name of the file being analyzed.

# XML Schema validation
The entry point **NetexValidatorsRunner** runs by default an XML Schema validation as the first step in the validation process.

The entry point **NetexValidatorsRunner** runs by default an XML Schema validation as the first step in the validation process.
This validation step is blocking: in case of an XML Schema validation error, further validations are skipped.

# XPath validation
The entry point **NetexValidatorsRunner** can be configured with a list of XPath validators.
XPath validators are run after XML schema validation.
XPath validators assert validation rules by executing XPath queries on the NeTEx document.
This validation step is blocking: in case of a validation error, further validations are skipped.

# JAXB validation
The entry point **NetexValidatorsRunner** can be configured with a list of JAXB validators.
JAXB validators are run after XML schema validation and XPath validation.
JAXB validators assert validation rules by navigating a (JAXB) object model of the NeTEx document.
The object model makes it easier to validate more complex rules than XPath validators. On the other hand, these
validators expect that the NeTEx document is well-formed and that the NeTEx entities required by the NeTEx profile are present.
It is therefore recommended that any assumption made by JAXB validators are asserted during the XPath validation step.

# Configurable validators
The entry point **NetexValidatorsRunner** can be configured with a list of **NetexValidator** instances that are executed sequentially during a validation run, after a successful XML Schema validation.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.entur.netex.validation.xml.NetexXMLParser;

/**
* Minimal executable class that demonstrates how to run a validation.
* Minimal executable class that demonstrates how to run a validation with XPath validators.
*/
public class Demo {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.entur.netex.validation.validator.demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.entur.netex.validation.validator.NetexValidatorsRunner;
import org.entur.netex.validation.validator.Severity;
import org.entur.netex.validation.validator.ValidationIssue;
import org.entur.netex.validation.validator.ValidationReport;
import org.entur.netex.validation.validator.ValidationReportEntry;
import org.entur.netex.validation.validator.ValidationRule;
import org.entur.netex.validation.validator.jaxb.JAXBValidationContext;
import org.entur.netex.validation.validator.jaxb.JAXBValidator;
import org.entur.netex.validation.validator.jaxb.SiteFrameStopPlaceRepository;
import org.entur.netex.validation.validator.schema.NetexSchemaValidator;
import org.entur.netex.validation.xml.NetexXMLParser;
import org.rutebanken.netex.model.ServiceJourney;

/**
* Minimal executable class that demonstrates how to run a validation with JAXB validators.
*/
public class DemoJAXBValidator {

private static final ValidationRule RULE = new ValidationRule(
"DEMO_2",
"A service journey should have at least 2 passing times",
Severity.ERROR
);

private static final String DEMO_FILE_NAME = "/demo/oym_line_105626_4176.xml";

public static void main(String[] args) throws IOException {
// create a NeTEx XML Parser
NetexXMLParser netexXMLParser = new NetexXMLParser();
// create a NeTEx schema validator, limit the number of findings to 100
NetexSchemaValidator netexSchemaValidator = new NetexSchemaValidator(100);
// create a NeTEx validators runner that registers the NeTEx schema validator and the custom NeTEx validator
JAXBValidator validator = new CustomJaxbValidator();
NetexValidatorsRunner netexValidatorsRunner = NetexValidatorsRunner
.of()
.withNetexXMLParser(netexXMLParser)
.withNetexSchemaValidator(netexSchemaValidator)
.withStopPlaceRepository(SiteFrameStopPlaceRepository::new)
.withJaxbValidators(List.of(validator))
.build();
// run the validation for a given codespace, report id, NeTEx filename and file binary content
String codespace = "ENT";
String reportId = "XXX";
byte[] content =
DemoJAXBValidator.class.getResourceAsStream(DEMO_FILE_NAME)
.readAllBytes();
ValidationReport validationReport = netexValidatorsRunner.validate(
codespace,
reportId,
DEMO_FILE_NAME,
content
);
validationReport
.getValidationReportEntries()
.stream()
.map(ValidationReportEntry::getMessage)
.forEach(System.out::println);
}

private static class CustomJaxbValidator implements JAXBValidator {

@Override
public List<ValidationIssue> validate(
JAXBValidationContext validationContext
) {
List<ValidationIssue> issues = new ArrayList<>();
for (ServiceJourney serviceJourney : validationContext.serviceJourneys()) {
if (
serviceJourney.getPassingTimes().getTimetabledPassingTime().size() < 2
) {
issues.add(
new ValidationIssue(
RULE,
validationContext.dataLocation(serviceJourney.getId())
)
);
}
}

return issues;
}

@Override
public Set<ValidationRule> getRules() {
return Set.of(RULE);
}
}
}

0 comments on commit e581598

Please sign in to comment.