All files / universal/gpii/node_modules/settingsHandlers/src XMLSettingsHandler.js

100% Statements 41/41
83.33% Branches 15/18
100% Functions 4/4
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114                                    1x 1x 1x   1x                     1x 5x 5x 5x 5x 5x 124x 124x 41x 83x 34x 34x   49x 34x   15x     124x 124x 191x     124x 124x   5x     1x               1x   6x   6x                 1x 4x   4x     4x 3x     3x     1x     4x   4x 4x               1x 1x  
/*
 * 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.
 *
 * The research leading to these results has received funding from the European Union's
 * Seventh Framework Programme (FP7/2007-2013)
 * under grant agreement no. 289016.
 *
 * You may obtain a copy of the License at
 * https://github.com/GPII/universal/blob/master/LICENSE.txt
 */
 
"use strict";
 
var fluid = require("infusion"),
    xm = require("xml-mapping"),
    gpii = fluid.registerNamespace("gpii");
 
fluid.registerNamespace("gpii.settingsHandlers.XMLHandler");
 
/**
 * "Pretty-prints" an XML string.
 *
 * This function is derived from sample code posted by Dan Brooks on Stack Overflow:
 * http://stackoverflow.com/questions/376373/pretty-printing-xml-with-javascript
 *
 * @param {String} xml the XML string to reformat
 * @return {String} the reformatted XML document
 */
gpii.settingsHandlers.XMLHandler.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) {
        var indent = 0;
        if (node.match(/.+<\/\w[^>]*>$/)) {
            indent = 0;
        } else if (node.match(/^<\/\w/)) {
            Eif (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;
};
 
fluid.registerNamespace("gpii.settingsHandlers.XMLHandler.parser");
 
/**
 * Convert XML into JSON.
 * In case options.rules is present, trasform with those rules.
 * NOTE: array transformed to objects if given in knownArrays or detected
 * as using 'name' as pivot element.
 */
gpii.settingsHandlers.XMLHandler.parser.parse = function (content, options) {
    // Parse XML to JSON.
    var json = xm.tojson(content);
    //  Apply the settings
    return (options && options.rules) ?
        fluid.model.transformWithRules(json, options.rules) :
        json;
};
 
/**
 * Convert JSON to XML.
 * In case options.rules is present, first tranform JSON with invert rules.
 */
gpii.settingsHandlers.XMLHandler.parser.stringify = function (content, options) {
    options = options || {};
    // Create XML - first line is the xml-tag from options if set.
    var xml = options["xml-tag"] || "";
    // Get inverse transformation rules.
    var invertedJSON;
    if (options.rules) {
        var inverseRules = fluid.model.transform.invertConfiguration(
            options.rules);
        // Transform back.
        invertedJSON = fluid.model.transformWithRules(content,
            inverseRules);
    } else {
        invertedJSON = content;
    }
 
    xml += xm.toxml(invertedJSON);
    // Fix indentation/newlines so it's readable.
    xml = gpii.settingsHandlers.XMLHandler.formatXml(xml);
    return xml;
};
 
/* PUBLIC API FUNCTIONS */
 
// TODO: For testing purposes, accept additional arguments in
// "solution.options" to produce data via a callback rather than
// hardcoded to hit the filesystem (see GPII-99)
gpii.settingsHandlers.XMLHandler.get = gpii.settingsHandlers.makeFileGet(gpii.settingsHandlers.XMLHandler.parser);
gpii.settingsHandlers.XMLHandler.set = gpii.settingsHandlers.makeFileSet(gpii.settingsHandlers.XMLHandler.parser);