/*
* 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);
|