<commit_before>var _ = require('lodash');

var fs = require('fs');
var spawn = require('child_process').spawn;

var JAVA_HOME = process.env.JAVA_HOME;

function withValidator(callback) {

  if (!JAVA_HOME) {
    callback(new Error('JAVA_HOME is not defined'));
  } else {
    fs.exists('support/XMLValidator.class', function(exists) {
      if (!exists) {
        spawn(JAVA_HOME + '/bin/javac', [ 'support/XMLValidator.java' ], { cwd: __dirname + '/../' }).on('exit', callback);
      } else {
        callback();
      }
    });
  }
}

function stripLineEnding(str) {
  return str.replace(/[\r\n]+/g, '');
}

/**
 * Validate a xml file agains the given schema
 * 
 * @param  {String}   xml
 * @param  {String}   schemaLocation the location of the schema
 * @param  {Function} callback to be invoked with (err, result)
 */
function validateXML(xml, schema, callback) {

  withValidator(function(err) {

    if (err) {
      callback(err);
      return;
    }

    var validator = spawn(JAVA_HOME + '/bin/java', [ '-cp', __dirname + '/../', 'support.XMLValidator', '-stdin', '-schema=' + schema ]);

    var result, code;

    var messages = [];

    function finish(result, code) {
      var success = !code,
          err = success ? null : buildError(result);

      callback(err, {
        valid: success,
        result: result,
        messages: messages
      });
    }

    function handleText(data) {
      var msg = data.toString('utf-8');

      if (msg.indexOf('[') === 0) {
        messages.push(stripLineEnding(msg));
      } else
      if (msg.indexOf('result=') === 0) {
        result = stripLineEnding(msg.slice('result='.length));

        if (code !== undefined) {
          finish(result, code);
        }
      }
    }

    function buildError(result) {
      var msg = 'invalid xml (status=' + result + ')';
      _.forEach(messages, function(m) {
        msg += '\n\t' + m;
      });

      return new Error(msg);
    }

    validator.on('exit', function(exitCode) {
      code = exitCode;

      finish(result ? result : code ? 'WITH_ERRORS' : 'OK', code);
    });

    validator.stderr.on('data', handleText);
    validator.stdout.on('data', handleText);

    var stdin = validator.stdin;
    stdin.write(xml);
    stdin.write('\n---end---\n');
  });
}

module.exports.validateXML = validateXML;<commit_msg>chore(validator): throw error on missing jdk<commit_after>var _ = require('lodash');

var fs = require('fs');
var spawn = require('child_process').spawn;

var JAVA_HOME = process.env.JAVA_HOME;

var VALIDATOR = __dirname + '/../support/XMLValidator';

function withValidator(callback) {

  if (!JAVA_HOME) {
    callback(new Error('JAVA_HOME is not defined'));
  } else
  if (!fs.existsSync(JAVA_HOME + '/bin/javac')) {
    callback(new Error('JDK required at JAVA_HOME to compile helper'));
  } else {
    if (fs.existsSync(VALIDATOR + '.class')) {
      callback();
    } else {
      spawn(JAVA_HOME + '/bin/javac', [ 'support/XMLValidator.java' ], { cwd: __dirname + '/../' }).on('exit', callback);
    }
  }
}

function stripLineEnding(str) {
  return str.replace(/[\r\n]+/g, '');
}

/**
 * Validate a xml file agains the given schema
 *
 * @param  {String}   xml
 * @param  {String}   schemaLocation the location of the schema
 * @param  {Function} callback to be invoked with (err, result)
 */
function validateXML(xml, schema, callback) {

  withValidator(function(err) {

    if (err) {
      callback(err);
      return;
    }

    var validator = spawn(JAVA_HOME + '/bin/java', [ '-cp', __dirname + '/../', 'support.XMLValidator', '-stdin', '-schema=' + schema ]);

    var result, code;

    var messages = [];

    function finish(result, code) {
      var success = !code,
          err = success ? null : buildError(result);

      callback(err, {
        valid: success,
        result: result,
        messages: messages
      });
    }

    function handleText(data) {
      var msg = data.toString('utf-8');

      if (msg.indexOf('[') === 0) {
        messages.push(stripLineEnding(msg));
      } else
      if (msg.indexOf('result=') === 0) {
        result = stripLineEnding(msg.slice('result='.length));

        if (code !== undefined) {
          finish(result, code);
        }
      }
    }

    function buildError(result) {
      var msg = 'invalid xml (status=' + result + ')';
      _.forEach(messages, function(m) {
        msg += '\n\t' + m;
      });

      return new Error(msg);
    }

    validator.on('exit', function(exitCode) {
      code = exitCode;

      finish(result ? result : code ? 'WITH_ERRORS' : 'OK', code);
    });

    validator.stderr.on('data', handleText);
    validator.stdout.on('data', handleText);

    var stdin = validator.stdin;
    stdin.write(xml);
    stdin.write('\n---end---\n');
  });
}

module.exports.validateXML = validateXML;
{'fraction_non_alphanumeric': 0.16890912854481474, 'fraction_numerical': 0.001241978886358932, 'mean_word_length': 2.61136023916293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '1019090', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1537, 'n_words': 347}