<commit_before>package com.rackspace.com.papi.components.checker.step

import javax.xml.validation.Schema
import javax.xml.validation.Validator

import javax.xml.transform.dom.DOMSource
import javax.xml.transform.dom.DOMResult

import javax.servlet.FilterChain

import org.w3c.dom.Document

import com.rackspace.com.papi.components.checker.servlet._
import com.rackspace.com.papi.components.checker.util.ValidatorPool._

class XSD(id : String, label : String, schema : Schema, transform : Boolean, next : Array[Step]) extends ConnectedStep(id, label, next) {
  override val mismatchMessage : String = "The XML does not validate against the schema."

  override def checkStep(req : CheckerServletRequest, resp : CheckerServletResponse, chain : FilterChain, uriLevel : Int) : Int = {
    var ret = -1
    var validator : Validator = null
    val capture = new ErrorCapture //Used to capture parse errors
    var error : Exception = null   //Other errors may be caught here

    try {
      validator = borrowValidator (schema)
      validator.setErrorHandler(capture)
      if (transform) {
        val result = new DOMResult

        validator.validate (new DOMSource (req.parsedXML), result)
        req.parsedXML = result.getNode().asInstanceOf[Document]
      } else {
        validator.validate (new DOMSource (req.parsedXML))
      }
      ret = uriLevel
    } catch {
      case e : Exception => error = e
    } finally {
      if (validator != null) returnValidator (schema, validator)
    }

    //<commit_msg>Use our own prebuilt parser rather than letting saxon build one.<commit_after>package com.rackspace.com.papi.components.checker.step

import javax.xml.validation.Schema
import javax.xml.validation.Validator

import javax.xml.transform.dom.DOMSource
import javax.xml.transform.dom.DOMResult

import javax.xml.parsers.DocumentBuilder

import javax.servlet.FilterChain

import org.w3c.dom.Document

import com.rackspace.com.papi.components.checker.servlet._

import com.rackspace.com.papi.components.checker.util.ValidatorPool.borrowValidator
import com.rackspace.com.papi.components.checker.util.ValidatorPool.returnValidator
import com.rackspace.com.papi.components.checker.util.XMLParserPool.borrowParser
import com.rackspace.com.papi.components.checker.util.XMLParserPool.returnParser

class XSD(id : String, label : String, schema : Schema, transform : Boolean, next : Array[Step]) extends ConnectedStep(id, label, next) {
  override val mismatchMessage : String = "The XML does not validate against the schema."

  override def checkStep(req : CheckerServletRequest, resp : CheckerServletResponse, chain : FilterChain, uriLevel : Int) : Int = {
    var ret = -1
    var validator : Validator = null
    var parser : DocumentBuilder = null
    val capture = new ErrorCapture //Used to capture parse errors
    var error : Exception = null   //Other errors may be caught here

    try {
      validator = borrowValidator(schema)
      validator.setErrorHandler(capture)

      if (transform) {
        //
        //  We create a new document because Saxon doesn't pool
        //  DocumentBuilders and letting saxon create a new builder
        //  slows things down.
        //
        parser = borrowParser
        val result = parser.newDocument()
        returnParser(parser); parser = null

        validator.validate (new DOMSource (req.parsedXML), new DOMResult(result))
        req.parsedXML = result
      } else {
        validator.validate (new DOMSource (req.parsedXML))
      }
      ret = uriLevel
    } catch {
      case e : Exception => error = e
    } finally {
      if (validator != null) returnValidator (schema, validator)
      if (parser != null) returnParser(parser)
    }

    //
{'fraction_non_alphanumeric': 0.0907869997313994, 'fraction_numerical': 0.0010744023636852001, 'mean_word_length': 3.305202312138728, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 9}, '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': '6181790', 'n_tokens_mistral': 1053, 'n_tokens_neox': 1032, 'n_words': 290}