<commit_before>package com.cabolabs.ehrserver.parsers

import javax.xml.transform.Source
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.Schema
import javax.xml.validation.SchemaFactory
import javax.xml.validation.Validator
import javax.xml.XMLConstants

import org.xml.sax.ErrorHandler
import org.xml.sax.SAXException
import org.xml.sax.SAXParseException

import grails.util.Holders
import groovy.util.slurpersupport.GPathResult
import grails.util.Holders

class XmlValidationService {

   def errors = []
   
   
   public boolean validateOPT(String xml)
   {
      return this.validate(xml, Holders.config.app.opt_xsd)
   }
   
   public boolean validateVersion(GPathResult xml, Map namespaces)
   {
      //xml.'@xmlns' = 'http://schemas.openehr.org/v1'
      //xml.'@xmlns:xsi' = 'http://www.w3.org/2001/XMLSchema-instance'
      namespaces.each { ns, val ->
         xml."@$ns" = val
      }
      def xmlStr = groovy.xml.XmlUtil.serialize( xml )
      
      return this.validate(xmlStr, Holders.config.app.version_xsd)
   }
   
   public boolean validateVersion(String xml)
   {
      return this.validate(xml, Holders.config.app.version_xsd)
   }
   
   
   public List<String> getErrors()
   {
      return this.errors
   }
   
   
   private boolean validate(String xml, String xsdPath)
   {
      this.errors = [] // Reset the errors for reuse
      
      SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
      Schema schema
      
      def xsd = new File(xsdPath)
      if (!xsd.exists()) // try to load from resources
      {
         //Holders.applicationContext
         //Holders.grailsApplication.parentContext.getResource("classpath:$filePath")
         
         // getResource returns a ServletContextResource
         def xsdInputStream = Holders.grailsApplication.parentContext.getResource(xsdPath).inputStream
         //def xsdInputStream = this.getClass().getResourceAsStream(System.getProperty("file.separator") + xsdPath)
         
         // reserouce on xsd\Version.xsd = null
         println "reserouce on "+ xsdPath +" = "+ xsdInputStream
         
         schema = schemaFactory.newSchema( [ new StreamSource( xsdInputStream ) ] as Source[] )
      }
      else
         schema = schemaFactory.newSchema( [ new StreamSource( xsdPath ) ] as Source[] )
      
      // Validate with validator
      Validator validator = schema.newValidator()
      ErrorHandler errorHandler = new SimpleErrorHandler(xml)
      validator.setErrorHandler(errorHandler)
      
      try
      {
         validator.validate(new StreamSource(new StringReader(xml)))
      }
      catch (org.xml.sax.SAXParseException e) // XML not valid
      {
         errorHandler.exceptions << e
      }
      <commit_msg>removed comments<commit_after>package com.cabolabs.ehrserver.parsers

import javax.xml.transform.Source
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.Schema
import javax.xml.validation.SchemaFactory
import javax.xml.validation.Validator
import javax.xml.XMLConstants

import org.xml.sax.ErrorHandler
import org.xml.sax.SAXException
import org.xml.sax.SAXParseException

import grails.util.Holders
import groovy.util.slurpersupport.GPathResult
import grails.util.Holders

class XmlValidationService {

   def errors = []
   
   
   public boolean validateOPT(String xml)
   {
      return this.validate(xml, Holders.config.app.opt_xsd)
   }
   
   public boolean validateVersion(GPathResult xml, Map namespaces)
   {
      //xml.'@xmlns' = 'http://schemas.openehr.org/v1'
      //xml.'@xmlns:xsi' = 'http://www.w3.org/2001/XMLSchema-instance'
      namespaces.each { ns, val ->
         xml."@$ns" = val
      }
      def xmlStr = groovy.xml.XmlUtil.serialize( xml )
      
      return this.validate(xmlStr, Holders.config.app.version_xsd)
   }
   
   public boolean validateVersion(String xml)
   {
      return this.validate(xml, Holders.config.app.version_xsd)
   }
   
   
   public List<String> getErrors()
   {
      return this.errors
   }
   
   
   private boolean validate(String xml, String xsdPath)
   {
      this.errors = [] // Reset the errors for reuse
      
      SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
      Schema schema
      
      def xsd = new File(xsdPath)
      if (!xsd.exists()) // try to load from resources
      {
         // getResource returns a ServletContextResource
         def xsdInputStream = Holders.grailsApplication.parentContext.getResource(xsdPath).inputStream

         // reserouce on xsd\Version.xsd = null
         println "resource on "+ xsdPath +" = "+ xsdInputStream
         
         schema = schemaFactory.newSchema( [ new StreamSource( xsdInputStream ) ] as Source[] )
      }
      else
         schema = schemaFactory.newSchema( [ new StreamSource( xsdPath ) ] as Source[] )
      
      // Validate with validator
      Validator validator = schema.newValidator()
      ErrorHandler errorHandler = new SimpleErrorHandler(xml)
      validator.setErrorHandler(errorHandler)
      
      try
      {
         validator.validate(new StreamSource(new StringReader(xml)))
      }
      catch (org.xml.sax.SAXParseException e) // XML not valid
      {
         errorHandler.exceptions << e
      }
      
{'fraction_non_alphanumeric': 0.09529169011442506, 'fraction_numerical': 0.0026261489401613208, 'mean_word_length': 2.677241379310345, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 54}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2772633', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1571, 'n_words': 313}