Sun Adapter for TCP/IP HL7 Tutorial

Schematron Validation Inside the JCD

The schematron validation API is invoked from the JCD. The JCD follows the standard HL7 V3 validation; the schematron validation API is only invoked if the Enable Schematron Validation property is true. Following is an excerpt from the JCD code that scans the schematron files list and calls the method validateWithSchematron() if schematron validation is enabled.

Excerpt for Beginning Schematron Validation


boolean validated = validateHL7Message( HL7message );
java.util.ArrayList outputList = new java.util.ArrayList();
if (validated) {
   boolean schematronValidationEnabled =
     input.getHL7v3MessageInfo().getSchematronValidationInfo().
     isSchematronValidationEnabled();
   log( LOG_LEVEL_INFO, "SchematronEnabled = " + schematronValidationEnabled );
   if (schematronValidationEnabled) {
      String[] schFiles =
        input.getHL7v3MessageInfo().getSchematronValidationInfo().
        getSchematronFilesList();
      log( LOG_LEVEL_INFO, "schFilesList  = " + schFiles );
      for (int i = 0; i < schFiles.length; i++) {
         log( LOG_LEVEL_INFO, "Adding schematron file for validation  = " 
           + schFiles[i] );
         com.stc.connector.hl7.schematron.ValidationOutput output =
           validateWithSchematron( "/" + schFiles[i], HL7message );
         outputList.add( output );
   }
   for (int i = 0; i < outputList.size(); i++) {
      com.stc.connector.hl7.schematron.ValidationOutput output =
        (com.stc.connector.hl7.schematron.ValidationOutput) outputList.get( i );
      if (!output.isValid()) {
         validated = false;
         schematronValidationError = true;
         log( LOG_LEVEL_INFO, "Schematron Validation failed." );
         break;
   } else {
      validated = true;
   }
}

The above text has been wrapped for display purposes. The images below show the code as it appears in the Collaboration Editor.

Inside the JCDInside the JCD

validateWithSchematron() method

The schematron method invokes the schematron API. It reads the schematron files from classpath and constructs a DOM source. The DOM source passes it to obtain the SchematronValidator object. The object invokes the validate() method to pass the hl7payload. The following excerpt from the JCD defines the validation method.


private com.stc.connector.hl7.schematron.ValidationOutput 
    validateWithSchematron( String schematronFile, byte[] hl7payload )
   throws Exception
   {
     com.stc.connector.hl7.schematron.SchematronValidatorFactory 
       factory =com.stc.connector.hl7.schematron.SchematronValidatorFactory.
       getSchematronValidatorFactory();
     log( LOG_LEVEL_INFO, "Schematron URI :" + 
       this.getClass().getResource( schematronFile ).toString() );
     java.io.InputStream in = this.getClass().getResourceAsStream( schematronFile );
     java.io.BufferedReader bufReader = new java.io.
       BufferedReader( new java.io.InputStreamReader( in ) );
     StringBuffer schematronXml = new StringBuffer();
     String line = bufReader.readLine();
     while (line != null) {
        schematronXml.append( line );
        line = bufReader.readLine();
   }

   bufReader.close();

   javax.xml.parsers.DocumentBuilderFactory domBuilderFactory =
     javax.xml.parsers.DocumentBuilderFactory.newInstance();
   domBuilderFactory.setNamespaceAware( true );
   org.w3c.dom.Document doc = domBuilderFactory.newDocumentBuilder().
     parse( new org.xml.sax.InputSource( new java.io.ByteArrayInputStream
     (schematronXml.toString().getBytes() ) ) );
   javax.xml.transform.dom.DOMSource domSource = new
     javax.xml.transform.dom.DOMSource( doc.getDocumentElement() );
   com.stc.connector.hl7.schematron.SchematronValidator 
     validator = factory.getDefaultValidator( domSource );
   javax.xml.transform.stream.StreamSource dataSrc = new javax.xml.
     transform.stream.StreamSource(new java.io.ByteArrayInputStream( hl7payload ) );
   com.stc.connector.hl7.schematron.ValidationOutput output = 
    validator.validate( dataSrc );
    return output;
    }

The above text has been wrapped for display purposes. The images below show the code as it appears in the Collaboration Editor.

Validate With SchematronValidate With Schematron

Retrieving the Validation Results

The makeNAK() method retrieves the XML document generated from the schematron validation. The XML document is embedded in the AcknowledgementDetail section of HL7V3 Acknowledgement XML. The following excerpt defines the makeNAK() method.

makeNAK() method


if (schematronValidationError) {
   log( LOG_LEVEL_INFO, "Schematron validationOutputList.size=" 
     + validationOutputList.size());
   for (int i = 0; i < validationOutputList.size(); i++) {
      com.stc.connector.hl7.schematron.ValidationOutput output =
        (com.stc.connector.hl7.schematron.ValidationOutput) 
      validationOutputList.get( i );
      String outputStr = output.getOutputAsString();

      otd_MCCI_IN000004UV01_1.getAcknowledgement( 0 ).
        getAcknowledgementDetail( i ).getTypeCode().setCode( "E" );
      otd_MCCI_IN000004UV01_1.getAcknowledgement( 0 ).
        getAcknowledgementDetail( i ).getCode().setCode
        ( "Validation Failure: Schematron Validation Failed" );
      otd_MCCI_IN000004UV01_1.getAcknowledgement( 0 ).
        getAcknowledgementDetail( i ).getText().setMediaType( "text/xml" );
      otd_MCCI_IN000004UV01_1.getAcknowledgement( 0 ).
        getAcknowledgementDetail( i ).getText().setX__PCDATA__MX( outputStr );
      log( LOG_LEVEL_INFO, "Schematron Validation Output = " + outputStr );
   }
}

The above text has been wrapped for display purposes. The image below shows the code as it appears in the Collaboration Editor.

Properties of makeNAK