<commit_before>var fs = require('fs'),
    xml2js = require('xml2js');

var cloc2sloc = function () {
    this.inputFile;
    this.outputFile;
};

cloc2sloc.prototype.setInputFile = function (inputFile) {
    if (!fs.existsSync(inputFile)) {
        throw new Error('File does not exist');
    }
    this.inputFile = inputFile;
};

cloc2sloc.prototype.setOutputFile = function (outputFile) {
    this.outputFile = outputFile;
};

cloc2sloc.prototype.getHeader = function () {
    return ['Creating filelist for source\n',
        'Categorizing files.\n',
        'Computing results.\n',
        '\n',
        '\n'
    ];
};

cloc2sloc.prototype.writeHeader = function (header) {
    fs.writeFileSync(this.outputFile, '');
    for (var i = 0; i < header.length; i++) {
        fs.appendFileSync(this.outputFile, header[i]);
    }
};

cloc2sloc.prototype.writeData = function () {
    fs.readFile(this.inputFile, this.onDataRead.bind(this));
};

cloc2sloc.prototype.onDataRead = function(err, data) {

    var dataString = data.toString();

    var parser = new xml2js.Parser();

    parser.parseString(dataString, this.onParserData.bind(this));
};

cloc2sloc.prototype.onParserData =  function (err, result) {
    var fileData = result.results.files[0].file;
    this.writeContent(fileData);
};

cloc2sloc.prototype.writeContent = function (data) {
    for (var j = 0; j < data.length; j++) {
        fs.appendFileSync(this.outputFile, this.constructLine(data[j]['$']));
    }
};

cloc2sloc.prototype.constructLine = function(data) {
    var line = '';
    line += data.code + '\t';
    line += ((data.language === 'Javascript') ? 'JS' : data.language) + '\t';
    line += 'source' + '\t';
    line += data.name + '\n';
    return line;
};

module.exports = cloc2sloc;<commit_msg>Add some XML file validation.<commit_after>var fs = require('fs'),
    xml2js = require('xml2js');

var cloc2sloc = function () {
    this.inputFile;
    this.outputFile;
};

cloc2sloc.prototype.setInputFile = function (inputFile) {
    if (!fs.existsSync(inputFile)) {
        throw new Error('File does not exist.');
    }
    this.inputFile = inputFile;
};

cloc2sloc.prototype.setOutputFile = function (outputFile) {
    this.outputFile = outputFile;
};

cloc2sloc.prototype.getHeader = function () {
    return [
        'Creating filelist for source\n',
        'Categorizing files.\n',
        'Computing results.\n',
        '\n',
        '\n'
    ];
};

cloc2sloc.prototype.writeHeader = function (header) {
    fs.writeFileSync(this.outputFile, '');
    for (var i = 0; i < header.length; i++) {
        fs.appendFileSync(this.outputFile, header[i]);
    }
};

cloc2sloc.prototype.writeData = function () {
    fs.readFile(this.inputFile, this.onDataRead.bind(this));
};

cloc2sloc.prototype.onDataRead = function(err, data) {
    var dataString = data.toString();
    var parser = new xml2js.Parser();

    parser.parseString(dataString, this.onParserData.bind(this));
};

cloc2sloc.prototype.onParserData =  function (err, result) {
    if (err) {
        throw new Error('Unable to parse input XML file.');
    }

    if (!result.results || !result.results.files) {
        throw new Error('Cannot find <files /> section in XML. Did you use `cloc --xml --by-file\'?');
    }

    var fileData = result.results.files[0].file;
    this.writeContent(fileData);
};

cloc2sloc.prototype.writeContent = function (data) {
    for (var j = 0; j < data.length; j++) {
        fs.appendFileSync(this.outputFile, this.constructLine(data[j]['$']));
    }
};

cloc2sloc.prototype.constructLine = function(data) {
    var line = '';
    line += data.code + '\t';
    line += ((data.language === 'Javascript') ? 'JS' : data.language) + '\t';
    line += 'source' + '\t';
    line += data.name + '\n';
    return line;
};

module.exports = cloc2sloc;
{'fraction_non_alphanumeric': 0.16283324621014114, 'fraction_numerical': 0.008886565603763722, 'mean_word_length': 3.1552660152008687, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, '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': '1407086', 'n_tokens_mistral': 1302, 'n_tokens_neox': 1290, 'n_words': 256}