
/**
 * Some functionality to facilitate the creation of XML documents
 *
 * @sdoc sdoc/xml.sdoc
 */

namespace ('XML');


/** @id XML.normalize */
XML.normalize = function (value) {
	if (typeof (value) != 'undefined') {
		return value.toString ().replace (/&/g, '&amp;').replace (/</g, '&lt;').replace (/>/g, '&gt;');
	}
	return '';
};


/** @id XML.normalizeAttribute */
XML.normalizeAttribute = function (value) {
	if (typeof (value) != 'undefined') {
		return value.toString ().replace (/&/g, '&amp;').replace (/</g, '&lt;').replace (/'/g, '&apos;').replace (/"/g, '&quot;');
	}
	return '';
};


/** @id XML.element */
XML.element = function (tag, attributes, content, normalize) {
	var attrs = '';
	for (var name in attributes) {
		attrs += ' ' + name + '="' + XML.normalizeAttribute (attributes[name]) + '"';
	}
	if (!content) {
		return '<' + tag + attrs + ' />';
	} else {
		return '<' + tag + attrs + '>' + ( normalize ? XML.normalize (content) : content ) + '</' + tag + '>';
	}
};
