<commit_before>/*!
GPII XML Settings Handler

Copyright 2012 Raising the Floor - International

Licensed under the New BSD license. You may not use this file except in
compliance with this License.

You may obtain a copy of the License at
https://github.com/gpii/universal/LICENSE.txt
*/
(function () {
    var fluid = require("infusion"),
        fs = require("fs"),
        xm = require('xml-mapping'),
        gpii = fluid.registerNamespace("gpii");

    fluid.registerNamespace("gpii.settingsHandlers.XMLHandler");

    //http://stackoverflow.com/questions/376373/pretty-printing-xml-with-javascript
    var formatXml = function(xml) {
        var formatted = '';
        var reg = /(>)(<)(\/*)/g;
        xml = xml.replace(reg, '$1\r\n$2$3');
        var pad = 0;
        fluid.each(xml.split('\r\n'), function(node, index) {
            var indent = 0;
            if (node.match( /.+<\/\w[^>]*>$/ )) {
                indent = 0;
            } else if (node.match( /^<\/\w/ )) {
                if (pad != 0) {
                    pad -= 1;
                }
            } else if (node.match( /^<\w[^>]*[^\/]>.*$/ )) {
                indent = 1;
            } else {
                indent = 0;
            }

            var padding = '';
            for (var i = 0; i < pad; i++) {
                padding += '  ';
            }

            formatted += padding + node + '\r\n';
            pad += indent;
        });
        return formatted;
    };

    gpii.settingsHandlers.XMLHandler.applySettings = function(settings, obj) {
        var returnPayload = {};
        //edit each of the settings
        fluid.each(settings, function(value, el) {
            //get the previous value
            var prev = fluid.get(obj, el);
            //set the new one
            fluid.set(obj, el, value);
            console.log("Value: "+prev+" --> "+fluid.get(obj, el));
            //create entry in the return payload
            returnPayload[el] = { oldValue: prev, newValue: value };
        });
        return returnPayload;                
    };

    gpii.settingsHandlers.XMLHandler.set = function(data) {
        var returnPayload = {};
        fluid.each(data, function(application, applicationId) {
            var options = application.options;
            var settings = application.settings;
            //only valid if a filename is set in options, else ignore this block
            if (options && options.filename) {
                //read XML file
                //TODO CHECK FOR ERRORS
                var xmlString = fs.readFileSync(options.filename, options.encoding || "utf-8");
                //console.log(tmp);
                //parse XML to JSON
                var json = xm.tojson(xmlString);
                returnPayload[applicationId] = gpii.settingsHandlers.XMLHandler.applySettings(settings, json);
                console.log(json);
                //Create XML - first the xml-tag from options if set, then our modified json converted to XML
                var xml = options["xml-tag"] || "";
                xml += xm.toxml(json);
                //ensure text formatting is correct
                var formatting = { "&": "&amp;" };
                fluid.each(formatting, function(to, from) {
                    xml = xml.replace(new RegExp(from, "g"), to);
                });
                //and fix indentation/newlines so it's readable
                var formattedXML = formatXml(xml);
                fs.writeFileSync(options.filename, formattedXML, options.encoding || "utf-8");
            };
        });
        //console.log("OLD-SETTINGS "+ JSON.stringify(returnPayload));
        return returnPayload;
    };
}());<commit_msg>A few more comments and a bit of cleanup<commit_after>/*!
GPII XML Settings Handler

Copyright 2012 Raising the Floor - International

Licensed under the New BSD license. You may not use this file except in
compliance with this License.

You may obtain a copy of the License at
https://github.com/gpii/universal/LICENSE.txt
*/
(function () {
    var fluid = require("infusion"),
        fs = require("fs"),
        xm = require('xml-mapping'),
        gpii = fluid.registerNamespace("gpii");

    fluid.registerNamespace("gpii.settingsHandlers.XMLHandler");

    //http://stackoverflow.com/questions/376373/pretty-printing-xml-with-javascript
    var formatXml = function(xml) {
        var formatted = '';
        var reg = /(>)(<)(\/*)/g;
        xml = xml.replace(reg, '$1\r\n$2$3');
        var pad = 0;
        fluid.each(xml.split('\r\n'), function(node, index) {
            var indent = 0;
            if (node.match( /.+<\/\w[^>]*>$/ )) {
                indent = 0;
            } else if (node.match( /^<\/\w/ )) {
                if (pad != 0) {
                    pad -= 1;
                }
            } else if (node.match( /^<\w[^>]*[^\/]>.*$/ )) {
                indent = 1;
            } else {
                indent = 0;
            }

            var padding = '';
            for (var i = 0; i < pad; i++) {
                padding += '  ';
            }

            formatted += padding + node + '\r\n';
            pad += indent;
        });
        return formatted;
    };

    //Gets a settings array and a json-ified version of the XML,
    //sets each the relevant settings and returns the settings object with 
    //{oldValue: XXXX, newValue: YYYY} as values
    gpii.settingsHandlers.XMLHandler.applySettings = function(settings, obj) {
        var returnPayload = {};
        //edit each of the settings
        fluid.each(settings, function(value, el) {
            //store the previous value
            var prev = fluid.get(obj, el);
            //set the new value
            fluid.set(obj, el, value);
            //create entry in the return payload
            returnPayload[el] = { oldValue: prev, newValue: value };
        });
        return returnPayload;                
    };

    gpii.settingsHandlers.XMLHandler.set = function(data) {
        var returnPayload = {};
        fluid.each(data, function(application, applicationId) {
            var options = application.options;
            var settings = application.settings;
            //only valid if a filename is set in options, else ignore this block
            if (options && options.filename) {
                //read XML file
                //TODO check for read errors
                var xmlString = fs.readFileSync(options.filename, options.encoding || "utf-8");
                //parse XML to JSON and apply the settings
                var json = xm.tojson(xmlString);
                returnPayload[applicationId] = gpii.settingsHandlers.XMLHandler.applySettings(settings, json);
                //Create XML - first line is the xml-tag from options if set
                //the remaining file is our modified json converted to XML
                var xml = options["xml-tag"] || "";
                xml += xm.toxml(json);
                //ensure text formatting is correct
                var formatting = { "&": "&amp;" };
                fluid.each(formatting, function(to, from) {
                    xml = xml.replace(new RegExp(from, "g"), to);
                });
                //and fix indentation/newlines so it's readable
                var formattedXML = formatXml(xml);
                fs.writeFileSync(options.filename, formattedXML, options.encoding || "utf-8");
            };
        });
        return returnPayload;
    };
}());
{'fraction_non_alphanumeric': 0.12866907921190188, 'fraction_numerical': 0.006165393378903632, 'mean_word_length': 1.6386138613861385, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 32}, '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': '3108358', 'n_tokens_mistral': 2099, 'n_tokens_neox': 1956, 'n_words': 566}