UNPKG

2.97 kBJavaScriptView Raw
1/** @module Node Pom Parser */
2
3"use strict";
4
5var Promise = require("bluebird");
6var fs = Promise.promisifyAll(require("fs"));
7var xml2js = require("xml2js");
8var traverse = require('traverse');
9
10// xmljs options https://github.com/Leonidas-from-XIV/node-xml2js#options
11var XML2JS_OPTS = {
12 trim: true,
13 normalizeTags: true,
14 normalize: true,
15 mergeAttrs: true
16};
17
18/**
19 * Parses xml into javascript object by using a file path or an xml content.
20 * @param {object} opt Is the option with the filePath or xmlContent and the optional format.
21 * @return {object} The pom object along with the timers.
22 */
23module.exports.parse = function(opt, callback) {
24 if (!opt) {
25 throw new Error("You must provide options: opt.filePath and any other option of " +
26 "https://github.com/Leonidas-from-XIV/node-xml2js#options");
27 }
28 if (!opt.xmlContent && !opt.filePath) {
29 throw new Error("You must provide the opt.filePath or the opt.xmlContent");
30 }
31
32
33 // If the xml content is was not provided by the api client.
34 // https://github.com/petkaantonov/bluebird/blob/master/API.md#error-rejectedhandler----promise
35 if (!opt.xmlContent) {
36 fs.readFileAsync(opt.filePath, "utf8").then(function(xmlContent) {
37 return xmlContent;
38
39 }).then(_parseWithXml2js).then(function(result) {
40 callback(null, result);
41
42 }).catch(function(e) {
43 callback(e, null);
44
45 }).error(function (e) {
46 callback(e, null);
47 });
48
49 } else {
50 // parse the xml provided by the api client.
51 _parseWithXml2js(opt.xmlContent).then(function(result) {
52 delete result.xmlContent;
53 callback(null, result);
54
55 }).error(function (e) {
56 callback(e);
57 });
58 }
59
60};
61
62/**
63 * Parses the given xml content.
64 * @param xmlContent {string} Is the xml content in string using utf-8 format.
65 * @param loadedXml {boolean} Whether the xml was loaded from the file-system.
66 * @param callback {function} The callback function using Javascript PCS.
67 */
68function _parseWithXml2js(xmlContent) {
69 return new Promise(function(resolve, reject) {
70 // parse the pom, erasing all
71 xml2js.parseString(xmlContent, XML2JS_OPTS, function(err, pomObject) {
72 if (err) {
73 // Reject with the error
74 reject(err);
75 }
76
77 // Replace the arrays with single elements with strings
78 removeSingleArrays(pomObject);
79
80 // Response to the call
81 resolve({
82 pomXml: xmlContent, // Only add the pomXml when loaded from the file-system.
83 pomObject: pomObject // Always add the object
84 });
85 });
86 });
87}
88
89/**
90 * Removes all the arrays with single elements with a string value.
91 * @param {object} o is the object to be traversed.
92 */
93function removeSingleArrays(obj) {
94 // Traverse all the elements of the object
95 traverse(obj).forEach(function traversing(value) {
96 // As the XML parser returns single fields as arrays.
97 if (value instanceof Array && value.length === 1) {
98 this.update(value[0]);
99 }
100 });
101}