Commit 131fa479 authored by Administrator's avatar Administrator
Browse files

xml reponse

parent ed292354
../xml-js/bin/cli.js
\ No newline at end of file
The MIT License (MIT)
Copyright (c) 2016-2017 Yousuf Almarzooqi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
This diff is collapsed.
module.exports = {
getCommandLineHelp: function (command, requiredArgs, optionalArgs) {
var reqArgs = requiredArgs.reduce(function (res, arg) {return res + ' <' + arg.arg + '>';}, '');
var output = 'Usage: ' + command + reqArgs + ' [options]\n';
requiredArgs.forEach(function (argument) {
output += ' <' + argument.arg + '>' + Array(20 - argument.arg.length).join(' ') + argument.desc + '\n';
});
output += '\nOptions:\n';
optionalArgs.forEach(function (argument) {
output += ' --' + argument.arg + Array(20 - argument.arg.length).join(' ') + argument.desc + '\n';
});
return output;
},
mapCommandLineArgs: function (requiredArgs, optionalArgs) {
var options = {}, r, o, a = 2;
for (r = 0; r < requiredArgs.length; r += 1) {
if (a < process.argv.length && process.argv[a].substr(0, 1) !== '-' && process.argv[a] !== 'JASMINE_CONFIG_PATH=./jasmine.json') {
options[requiredArgs[r].option] = process.argv[a++];
} else {
break;
}
}
for (; a < process.argv.length; a += 1) {
for (o = 0; o < optionalArgs.length; o += 1) {
if (optionalArgs[o].alias === process.argv[a].slice(1) || optionalArgs[o].arg === process.argv[a].slice(2)) {
break;
}
}
if (o < optionalArgs.length) {
switch (optionalArgs[o].type) {
case 'file': case 'string': case 'number':
if (a + 1 < process.argv.length) {
a += 1;
options[optionalArgs[o].option] = (optionalArgs[o].type === 'number' ? Number(process.argv[a]) : process.argv[a]);
}
break;
case 'flag':
options[optionalArgs[o].option] = true; break;
}
}
}
return options;
}
};
#!/usr/bin/env node
var fs = require('fs');
var helper = require('./cli-helper');
var project = require('../package.json');
var xml2json = require('../lib/xml2json');
var json2xml = require('../lib/json2xml');
var output = '';
var stream = '';
var options = {};
var requiredArgs = [
{ arg: 'src', type: 'file', option: 'src', desc: 'Input file that need to be converted.'}
];
var optionalArgs = [
{ arg: 'help', alias: 'h', type: 'flag', option: 'help', desc: 'Display this help content.' },
{ arg: 'version', alias: 'v', type: 'flag', option: 'version', desc: 'Display version number of this module.' },
{ arg: 'out', type: 'file', option: 'out', desc: 'Output file where the converted result should be written.' },
{ arg: 'to-json', type: 'flag', option:'toJason', desc: 'Convert.' },
{ arg: 'compact', type: 'flag', option:'compact', desc: 'Compact JSON form (see explanation in www.npmjs.com/package/xml-js).' },
{ arg: 'spaces', type: 'number', option:'spaces', desc: 'Specifies amount of space indentation in the output.' },
{ arg: 'trim', type: 'flag', option:'trim', desc: 'Any whitespaces surrounding texts will be trimmed.' },
// { arg: 'sanitize', type: 'flag', option:'sanitize', desc: 'Special xml characters will be replaced with entity codes.' },
{ arg: 'native-type', type: 'flag', option:'nativeType', desc: 'Numbers and boolean will be converted (coerced) to native type instead of text.' },
{ arg: 'always-array', type: 'flag', option:'alwaysArray', desc: 'Every element will always be an array type (applicable if --compact is set). If the passed value is an array, only elements with names in the passed array are always made arrays.' },
{ arg: 'always-children', type: 'flag', option:'alwaysChildren', desc: 'Every element will always contain sub-elements (applicable if --compact is not set).' },
{ arg: 'instruction-attr', type: 'flag', option:'instructionHasAttributes', desc: 'Whether to parse contents of processing instruction as attributes.' },
{ arg: 'full-tag', type: 'flag', option:'fullTagEmptyElement', desc: 'XML elements will always be in <a></a> form.' },
{ arg: 'no-decl', type: 'flag', option:'ignoreDeclaration', desc: 'Declaration instruction <?xml?> will be ignored.' },
{ arg: 'no-decl', type: 'flag', option:'ignoreInstruction', desc: 'Processing instruction <?...?> will be ignored.' },
{ arg: 'no-attr', type: 'flag', option:'ignoreAttributes', desc: 'Attributes of elements will be ignored.' },
{ arg: 'no-text', type: 'flag', option:'ignoreText', desc: 'Texts of elements will be ignored.' },
{ arg: 'no-cdata', type: 'flag', option:'ignoreCdata', desc: 'CData of elements will be ignored.' },
{ arg: 'no-doctype', type: 'flag', option:'ignoreDoctype', desc: 'DOCTYPE of elements will be ignored.' },
{ arg: 'no-comment', type: 'flag', option:'ignoreComment', desc: 'Comments of elements will be ignored.' },
{ arg: 'text-key', type: 'string', option:'textKey', desc: 'To change the default \'text\' key.' },
{ arg: 'cdata-key', type: 'string', option:'cdataKey', desc: 'To change the default \'cdata\' key.' },
{ arg: 'doctype-key', type: 'string', option:'doctypeKey', desc: 'To change the default \'doctype\' key.' },
{ arg: 'comment-key', type: 'string', option:'commentKey', desc: 'To change the default \'comment\' key.' },
{ arg: 'attributes-key', type: 'string', option:'attributesKey', desc: 'To change the default \'attributes\' key.' },
{ arg: 'declaration-key', type: 'string', option:'declarationKey', desc: 'To change the default \'declaration\' key <?xml?>.' },
{ arg: 'instruction-key', type: 'string', option:'instructionKey', desc: 'To change the default \'processing instruction\' key <?...?>.' },
{ arg: 'type-key', type: 'string', option:'typeKey', desc: 'To change the default \'type\' key (applicable if --compact is not set).' },
{ arg: 'name-key', type: 'string', option:'nameKey', desc: 'To change the default \'name\' key (applicable if --compact is not set).' },
{ arg: 'elements-key', type: 'string', option:'elementsKey', desc: 'To change the default \'elements\' key (applicable if --compact is not set).' }
];
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function () {
var chunk = process.stdin.read();
if (chunk !== null) {
stream += chunk;
}
});
process.stdin.on('end', function () {
process.stdout.write(xml2json(stream, {}) + '\n');
});
options = helper.mapCommandLineArgs(requiredArgs, optionalArgs);
if (options.version) {
console.log(project.version);
process.exit(0);
} else if (options.help || process.argv.length <= 2 + requiredArgs.length - 1) {
console.log(helper.getCommandLineHelp('xml-js', requiredArgs, optionalArgs));
process.exit(process.argv.length <= 2 ? 1 : 0);
} else if ('src' in options) {
if (fs.statSync(options.src).isFile()) {
if (options.src.split('.').pop() === 'xml') {
output = xml2json(fs.readFileSync(options.src, 'utf8'), options);
} else if (options.src.split('.').pop() === 'json') {
output = json2xml(fs.readFileSync(options.src, 'utf8'), options);
}
if (options.out) {
fs.writeFileSync(options.out, output, 'utf8');
} else {
console.log(output);
}
process.exit(0);
}
} else {
process.exit(1);
}
{
"elements": [
{
"type": "element",
"name": "a",
"attributes": {
"x": "1"
},
"elements": [
{
"type": "element",
"name": "b",
"elements": [
{
"type": "text",
"text": "bye!"
}
]
}
]
}
]
}
\ No newline at end of file
<a x="1">
<b>bye!</b>
</a>
This diff is collapsed.
This diff is collapsed.
module.exports = require('./lib');
module.exports = {
isArray: function(value) {
if (Array.isArray) {
return Array.isArray(value);
}
// fallback for older browsers like IE 8
return Object.prototype.toString.call( value ) === '[object Array]';
}
};
/*jslint node:true */
var xml2js = require('./xml2js');
var xml2json = require('./xml2json');
var js2xml = require('./js2xml');
var json2xml = require('./json2xml');
module.exports = {
xml2js: xml2js,
xml2json: xml2json,
js2xml: js2xml,
json2xml: json2xml
};
var helper = require('./options-helper');
var isArray = require('./array-helper').isArray;
var currentElement, currentElementName;
function validateOptions(userOptions) {
var options = helper.copyOptions(userOptions);
helper.ensureFlagExists('ignoreDeclaration', options);
helper.ensureFlagExists('ignoreInstruction', options);
helper.ensureFlagExists('ignoreAttributes', options);
helper.ensureFlagExists('ignoreText', options);
helper.ensureFlagExists('ignoreComment', options);
helper.ensureFlagExists('ignoreCdata', options);
helper.ensureFlagExists('ignoreDoctype', options);
helper.ensureFlagExists('compact', options);
helper.ensureFlagExists('indentText', options);
helper.ensureFlagExists('indentCdata', options);
helper.ensureFlagExists('indentAttributes', options);
helper.ensureFlagExists('indentInstruction', options);
helper.ensureFlagExists('fullTagEmptyElement', options);
helper.ensureFlagExists('noQuotesForNativeAttributes', options);
helper.ensureSpacesExists(options);
if (typeof options.spaces === 'number') {
options.spaces = Array(options.spaces + 1).join(' ');
}
helper.ensureKeyExists('declaration', options);
helper.ensureKeyExists('instruction', options);
helper.ensureKeyExists('attributes', options);
helper.ensureKeyExists('text', options);
helper.ensureKeyExists('comment', options);
helper.ensureKeyExists('cdata', options);
helper.ensureKeyExists('doctype', options);
helper.ensureKeyExists('type', options);
helper.ensureKeyExists('name', options);
helper.ensureKeyExists('elements', options);
helper.checkFnExists('doctype', options);
helper.checkFnExists('instruction', options);
helper.checkFnExists('cdata', options);
helper.checkFnExists('comment', options);
helper.checkFnExists('text', options);
helper.checkFnExists('instructionName', options);
helper.checkFnExists('elementName', options);
helper.checkFnExists('attributeName', options);
helper.checkFnExists('attributeValue', options);
helper.checkFnExists('attributes', options);
helper.checkFnExists('fullTagEmptyElement', options);
return options;
}
function writeIndentation(options, depth, firstLine) {
return (!firstLine && options.spaces ? '\n' : '') + Array(depth + 1).join(options.spaces);
}
function writeAttributes(attributes, options, depth) {
if (options.ignoreAttributes) {
return '';
}
if ('attributesFn' in options) {
attributes = options.attributesFn(attributes, currentElementName, currentElement);
}
var key, attr, attrName, quote, result = [];
for (key in attributes) {
if (attributes.hasOwnProperty(key) && attributes[key] !== null && attributes[key] !== undefined) {
quote = options.noQuotesForNativeAttributes && typeof attributes[key] !== 'string' ? '' : '"';
attr = '' + attributes[key]; // ensure number and boolean are converted to String
attr = attr.replace(/"/g, '&quot;');
attrName = 'attributeNameFn' in options ? options.attributeNameFn(key, attr, currentElementName, currentElement) : key;
result.push((options.spaces && options.indentAttributes? writeIndentation(options, depth+1, false) : ' '));
result.push(attrName + '=' + quote + ('attributeValueFn' in options ? options.attributeValueFn(attr, key, currentElementName, currentElement) : attr) + quote);
}
}
if (attributes && Object.keys(attributes).length && options.spaces && options.indentAttributes) {
result.push(writeIndentation(options, depth, false));
}
return result.join('');
}
function writeDeclaration(declaration, options, depth) {
currentElement = declaration;
currentElementName = 'xml';
return options.ignoreDeclaration ? '' : '<?' + 'xml' + writeAttributes(declaration[options.attributesKey], options, depth) + '?>';
}
function writeInstruction(instruction, options, depth) {
if (options.ignoreInstruction) {
return '';
}
var key;
for (key in instruction) {
if (instruction.hasOwnProperty(key)) {
break;
}
}
var instructionName = 'instructionNameFn' in options ? options.instructionNameFn(key, instruction[key], currentElementName, currentElement) : key;
if (typeof instruction[key] === 'object') {
currentElement = instruction;
currentElementName = instructionName;
return '<?' + instructionName + writeAttributes(instruction[key][options.attributesKey], options, depth) + '?>';
} else {
var instructionValue = instruction[key] ? instruction[key] : '';
if ('instructionFn' in options) instructionValue = options.instructionFn(instructionValue, key, currentElementName, currentElement);
return '<?' + instructionName + (instructionValue ? ' ' + instructionValue : '') + '?>';
}
}
function writeComment(comment, options) {
return options.ignoreComment ? '' : '<!--' + ('commentFn' in options ? options.commentFn(comment, currentElementName, currentElement) : comment) + '-->';
}
function writeCdata(cdata, options) {
return options.ignoreCdata ? '' : '<![CDATA[' + ('cdataFn' in options ? options.cdataFn(cdata, currentElementName, currentElement) : cdata.replace(']]>', ']]]]><![CDATA[>')) + ']]>';
}
function writeDoctype(doctype, options) {
return options.ignoreDoctype ? '' : '<!DOCTYPE ' + ('doctypeFn' in options ? options.doctypeFn(doctype, currentElementName, currentElement) : doctype) + '>';
}
function writeText(text, options) {
if (options.ignoreText) return '';
text = '' + text; // ensure Number and Boolean are converted to String
text = text.replace(/&amp;/g, '&'); // desanitize to avoid double sanitization
text = text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return 'textFn' in options ? options.textFn(text, currentElementName, currentElement) : text;
}
function hasContent(element, options) {
var i;
if (element.elements && element.elements.length) {
for (i = 0; i < element.elements.length; ++i) {
switch (element.elements[i][options.typeKey]) {
case 'text':
if (options.indentText) {
return true;
}
break; // skip to next key
case 'cdata':
if (options.indentCdata) {
return true;
}
break; // skip to next key
case 'instruction':
if (options.indentInstruction) {
return true;
}
break; // skip to next key
case 'doctype':
case 'comment':
case 'element':
return true;
default:
return true;
}
}
}
return false;
}
function writeElement(element, options, depth) {
currentElement = element;
currentElementName = element.name;
var xml = [], elementName = 'elementNameFn' in options ? options.elementNameFn(element.name, element) : element.name;
xml.push('<' + elementName);
if (element[options.attributesKey]) {
xml.push(writeAttributes(element[options.attributesKey], options, depth));
}
var withClosingTag = element[options.elementsKey] && element[options.elementsKey].length || element[options.attributesKey] && element[options.attributesKey]['xml:space'] === 'preserve';
if (!withClosingTag) {
if ('fullTagEmptyElementFn' in options) {
withClosingTag = options.fullTagEmptyElementFn(element.name, element);
} else {
withClosingTag = options.fullTagEmptyElement;
}
}
if (withClosingTag) {
xml.push('>');
if (element[options.elementsKey] && element[options.elementsKey].length) {
xml.push(writeElements(element[options.elementsKey], options, depth + 1));
currentElement = element;
currentElementName = element.name;
}
xml.push(options.spaces && hasContent(element, options) ? '\n' + Array(depth + 1).join(options.spaces) : '');
xml.push('</' + elementName + '>');
} else {
xml.push('/>');
}
return xml.join('');
}
function writeElements(elements, options, depth, firstLine) {
return elements.reduce(function (xml, element) {
var indent = writeIndentation(options, depth, firstLine && !xml);
switch (element.type) {
case 'element': return xml + indent + writeElement(element, options, depth);
case 'comment': return xml + indent + writeComment(element[options.commentKey], options);
case 'doctype': return xml + indent + writeDoctype(element[options.doctypeKey], options);
case 'cdata': return xml + (options.indentCdata ? indent : '') + writeCdata(element[options.cdataKey], options);
case 'text': return xml + (options.indentText ? indent : '') + writeText(element[options.textKey], options);
case 'instruction':
var instruction = {};
instruction[element[options.nameKey]] = element[options.attributesKey] ? element : element[options.instructionKey];
return xml + (options.indentInstruction ? indent : '') + writeInstruction(instruction, options, depth);
}
}, '');
}
function hasContentCompact(element, options, anyContent) {
var key;
for (key in element) {
if (element.hasOwnProperty(key)) {
switch (key) {
case options.parentKey:
case options.attributesKey:
break; // skip to next key
case options.textKey:
if (options.indentText || anyContent) {
return true;
}
break; // skip to next key
case options.cdataKey:
if (options.indentCdata || anyContent) {
return true;
}
break; // skip to next key
case options.instructionKey:
if (options.indentInstruction || anyContent) {
return true;
}
break; // skip to next key
case options.doctypeKey:
case options.commentKey:
return true;
default:
return true;
}
}
}
return false;
}
function writeElementCompact(element, name, options, depth, indent) {
currentElement = element;
currentElementName = name;
var elementName = 'elementNameFn' in options ? options.elementNameFn(name, element) : name;
if (typeof element === 'undefined' || element === null || element === '') {
return 'fullTagEmptyElementFn' in options && options.fullTagEmptyElementFn(name, element) || options.fullTagEmptyElement ? '<' + elementName + '></' + elementName + '>' : '<' + elementName + '/>';
}
var xml = [];
if (name) {
xml.push('<' + elementName);
if (typeof element !== 'object') {
xml.push('>' + writeText(element,options) + '</' + elementName + '>');
return xml.join('');
}
if (element[options.attributesKey]) {
xml.push(writeAttributes(element[options.attributesKey], options, depth));
}
var withClosingTag = hasContentCompact(element, options, true) || element[options.attributesKey] && element[options.attributesKey]['xml:space'] === 'preserve';
if (!withClosingTag) {
if ('fullTagEmptyElementFn' in options) {
withClosingTag = options.fullTagEmptyElementFn(name, element);
} else {
withClosingTag = options.fullTagEmptyElement;
}
}
if (withClosingTag) {
xml.push('>');
} else {
xml.push('/>');
return xml.join('');
}
}
xml.push(writeElementsCompact(element, options, depth + 1, false));
currentElement = element;
currentElementName = name;
if (name) {
xml.push((indent ? writeIndentation(options, depth, false) : '') + '</' + elementName + '>');
}
return xml.join('');
}
function writeElementsCompact(element, options, depth, firstLine) {
var i, key, nodes, xml = [];
for (key in element) {
if (element.hasOwnProperty(key)) {
nodes = isArray(element[key]) ? element[key] : [element[key]];
for (i = 0; i < nodes.length; ++i) {
switch (key) {
case options.declarationKey: xml.push(writeDeclaration(nodes[i], options, depth)); break;
case options.instructionKey: xml.push((options.indentInstruction ? writeIndentation(options, depth, firstLine) : '') + writeInstruction(nodes[i], options, depth)); break;
case options.attributesKey: case options.parentKey: break; // skip
case options.textKey: xml.push((options.indentText ? writeIndentation(options, depth, firstLine) : '') + writeText(nodes[i], options)); break;
case options.cdataKey: xml.push((options.indentCdata ? writeIndentation(options, depth, firstLine) : '') + writeCdata(nodes[i], options)); break;
case options.doctypeKey: xml.push(writeIndentation(options, depth, firstLine) + writeDoctype(nodes[i], options)); break;
case options.commentKey: xml.push(writeIndentation(options, depth, firstLine) + writeComment(nodes[i], options)); break;
default: xml.push(writeIndentation(options, depth, firstLine) + writeElementCompact(nodes[i], key, options, depth, hasContentCompact(nodes[i], options)));
}
firstLine = firstLine && !xml.length;
}
}
}
return xml.join('');
}
module.exports = function (js, options) {
options = validateOptions(options);
var xml = [];
currentElement = js;
currentElementName = '_root_';
if (options.compact) {
xml.push(writeElementsCompact(js, options, 0, true));
} else {
if (js[options.declarationKey]) {
xml.push(writeDeclaration(js[options.declarationKey], options, 0));
}
if (js[options.elementsKey] && js[options.elementsKey].length) {
xml.push(writeElements(js[options.elementsKey], options, 0, !xml.length));
}
}
return xml.join('');
};
var js2xml = require('./js2xml.js');
module.exports = function (json, options) {
if (json instanceof Buffer) {
json = json.toString();
}
var js = null;
if (typeof (json) === 'string') {
try {
js = JSON.parse(json);
} catch (e) {
throw new Error('The JSON structure is invalid');
}
} else {
js = json;
}
return js2xml(js, options);
};