|
| 1 | +package uk.nhs.adaptors.gp2gp.transformjsontoxmltool; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.apache.commons.io.FilenameUtils; |
| 6 | +import org.springframework.stereotype.Component; |
| 7 | +import org.xml.sax.SAXException; |
| 8 | +import org.xml.sax.SAXParseException; |
| 9 | +import uk.nhs.adaptors.gp2gp.common.configuration.RedactionsContext; |
| 10 | + |
| 11 | +import javax.xml.XMLConstants; |
| 12 | +import javax.xml.transform.stream.StreamSource; |
| 13 | +import javax.xml.validation.SchemaFactory; |
| 14 | +import javax.xml.validation.Validator; |
| 15 | +import java.io.BufferedWriter; |
| 16 | +import java.io.File; |
| 17 | +import java.io.FileWriter; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.StringReader; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.Paths; |
| 22 | + |
| 23 | +@Component |
| 24 | +@Slf4j |
| 25 | +@RequiredArgsConstructor() |
| 26 | +public class XmlSchemaValidator { |
| 27 | + private final RedactionsContext redactionsContext; |
| 28 | + |
| 29 | + private static final String SCHEMA_PATH = "../service/src/test/resources/mim/Schemas/"; |
| 30 | + private static final String RCMR_IN030000UK06_SCHEMA_PATH = SCHEMA_PATH + RedactionsContext.NON_REDACTION_INTERACTION_ID + ".xsd"; |
| 31 | + private static final String RCMR_IN030000UK07_SCHEMA_PATH = SCHEMA_PATH + RedactionsContext.REDACTION_INTERACTION_ID + ".xsd"; |
| 32 | + private static final String OUTPUT_PATH = |
| 33 | + Paths.get("src/").toFile().getAbsoluteFile().getAbsolutePath() + "/../../transformJsonToXml/output/"; |
| 34 | + |
| 35 | + public void validateOutputToXmlSchema(String filename, String xmlResult) { |
| 36 | + LOGGER.info("Validating {} against {} schema", filename, RedactionsContext.REDACTION_INTERACTION_ID); |
| 37 | + |
| 38 | + var xsdErrorHandler = new uk.nhs.adaptors.gp2gp.transformjsontoxmltool.XsdErrorHandler(); |
| 39 | + Validator xmlValidator; |
| 40 | + |
| 41 | + try { |
| 42 | + xmlValidator = getXmlValidator(xsdErrorHandler); |
| 43 | + } catch (SAXException e) { |
| 44 | + LOGGER.info("Could not load schema file for {} context.", RedactionsContext.REDACTION_INTERACTION_ID); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + try { |
| 49 | + var xmlResultSource = new StreamSource(new StringReader(xmlResult)); |
| 50 | + xmlValidator.validate(xmlResultSource); |
| 51 | + } catch (SAXParseException parseException) { |
| 52 | + LOGGER.info("Failed to validate {} against {} schema", filename, RedactionsContext.REDACTION_INTERACTION_ID); |
| 53 | + writeValidationExceptionsToFile(xsdErrorHandler, filename); |
| 54 | + } catch (IOException e) { |
| 55 | + LOGGER.info("Could not read from stream source for produced XML for {}", filename); |
| 56 | + return; |
| 57 | + } catch (Exception e) { |
| 58 | + throw new RuntimeException(e); |
| 59 | + } |
| 60 | + |
| 61 | + if (!xsdErrorHandler.isValid()) { |
| 62 | + LOGGER.info("Failed to validate {} against {} schema", filename, RedactionsContext.REDACTION_INTERACTION_ID); |
| 63 | + writeValidationExceptionsToFile(xsdErrorHandler, filename); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + LOGGER.info("Successfully validated {} against {} schema", filename, RedactionsContext.REDACTION_INTERACTION_ID); |
| 68 | + } |
| 69 | + |
| 70 | + private void writeValidationExceptionsToFile( |
| 71 | + uk.nhs.adaptors.gp2gp.transformjsontoxmltool.XsdErrorHandler xsdErrorHandler, |
| 72 | + String fileName |
| 73 | + ) { |
| 74 | + String outputFileName = FilenameUtils.removeExtension(fileName) + ".validation-errors.log"; |
| 75 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(OUTPUT_PATH + outputFileName, StandardCharsets.UTF_8))) { |
| 76 | + for (SAXParseException e : xsdErrorHandler.getExceptions()) { |
| 77 | + var message = String.format("[%d:%d] %s", e.getLineNumber(), e.getColumnNumber(), e.getMessage()); |
| 78 | + writer.write(message); |
| 79 | + writer.newLine(); |
| 80 | + } |
| 81 | + LOGGER.info("Validation errors written to {}", outputFileName); |
| 82 | + } catch (IOException e) { |
| 83 | + LOGGER.error("Could not write validation errors to {}", outputFileName, e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private Validator getXmlValidator(uk.nhs.adaptors.gp2gp.transformjsontoxmltool.XsdErrorHandler xsdErrorHandler) throws SAXException { |
| 88 | + var schemaPath = RedactionsContext.REDACTION_INTERACTION_ID.equals(redactionsContext.ehrExtractInteractionId()) |
| 89 | + ? RCMR_IN030000UK07_SCHEMA_PATH |
| 90 | + : RCMR_IN030000UK06_SCHEMA_PATH; |
| 91 | + |
| 92 | + var schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
| 93 | + var schemaFileStream = new StreamSource(new File(schemaPath)); |
| 94 | + var schema = schemaFactory.newSchema(schemaFileStream); |
| 95 | + var validator = schema.newValidator(); |
| 96 | + |
| 97 | + validator.setErrorHandler(xsdErrorHandler); |
| 98 | + |
| 99 | + return validator; |
| 100 | + } |
| 101 | +} |
0 commit comments