From ea2f9cdc2174165f48bd187e33161b163725a759 Mon Sep 17 00:00:00 2001 From: Jim deVos Date: Thu, 1 Oct 2015 21:40:18 -0700 Subject: [PATCH 1/5] adding parseStringSync + simple tests --- lib/bom.js | 2 +- lib/processors.js | 2 +- lib/xml2js.js | 30 +++++++++++++++++++++++------- src/xml2js.coffee | 17 +++++++++++++++++ test/parser.test.coffee | 21 ++++++++++++++++++++- 5 files changed, 62 insertions(+), 10 deletions(-) diff --git a/lib/bom.js b/lib/bom.js index e9e19fff..0f6be316 100644 --- a/lib/bom.js +++ b/lib/bom.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.9.3 +// Generated by CoffeeScript 1.10.0 (function() { "use strict"; var xml2js; diff --git a/lib/processors.js b/lib/processors.js index aabb842d..31ccde28 100644 --- a/lib/processors.js +++ b/lib/processors.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.9.3 +// Generated by CoffeeScript 1.10.0 (function() { "use strict"; var prefixMatch; diff --git a/lib/xml2js.js b/lib/xml2js.js index 452c0ebe..d7fdfcb0 100644 --- a/lib/xml2js.js +++ b/lib/xml2js.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.9.3 +// Generated by CoffeeScript 1.10.0 (function() { "use strict"; var bom, builder, escapeCDATA, events, isEmpty, processName, processors, requiresCDATA, sax, setImmediate, wrapCDATA, @@ -216,6 +216,7 @@ extend(Parser, superClass); function Parser(opts) { + this.parseStringSync = bind(this.parseStringSync, this); this.parseString = bind(this.parseString, this); this.reset = bind(this.reset, this); this.assignOrPush = bind(this.assignOrPush, this); @@ -335,7 +336,7 @@ })(this); this.saxParser.onclosetag = (function(_this) { return function() { - var cdata, emptyStr, err, key, node, nodeName, obj, objClone, old, s, xpath; + var cdata, emptyStr, err, error1, key, node, nodeName, obj, objClone, old, s, xpath; obj = stack.pop(); nodeName = obj["#name"]; if (!_this.options.explicitChildren || !_this.options.preserveChildrenOrder) { @@ -376,8 +377,8 @@ })()).concat(nodeName).join("/"); try { obj = _this.options.validator(xpath, s && s[nodeName], obj); - } catch (_error) { - err = _error; + } catch (error1) { + err = error1; _this.emit("error", err); } } @@ -455,7 +456,7 @@ }; Parser.prototype.parseString = function(str, cb) { - var err; + var err, error1; if ((cb != null) && typeof cb === "function") { this.on("end", function(result) { this.reset(); @@ -479,8 +480,8 @@ return this.saxParser; } return this.saxParser.write(str).close(); - } catch (_error) { - err = _error; + } catch (error1) { + err = error1; if (!(this.saxParser.errThrown || this.saxParser.ended)) { this.emit('error', err); return this.saxParser.errThrown = true; @@ -490,6 +491,21 @@ } }; + Parser.prototype.parseStringSync = function(str) { + var cb, err, retval; + retval = void 0; + err = void 0; + cb = function(_err, _retval) { + retval = _retval; + err = _err; + }; + this.parseString(str, cb); + if (err) { + throw err; + } + return retval; + }; + return Parser; })(events.EventEmitter); diff --git a/src/xml2js.coffee b/src/xml2js.coffee index bc0d6372..040f31b0 100644 --- a/src/xml2js.coffee +++ b/src/xml2js.coffee @@ -402,6 +402,23 @@ class exports.Parser extends events.EventEmitter else if @saxParser.ended throw err + parseStringSync: (str) => + #assumption 1: sax events block when given a string instead of a stream + retval = undefined + err = undefined + cb = (_err, _retval) -> + retval = _retval + err = _err + return + + #assumption 2: parseString will implicitly call cb (thus setting closure retval) before returing + @parseString str, cb + if err + throw err + retval + + + exports.parseString = (str, a, b) -> # let's determine what we got as arguments if b? diff --git a/test/parser.test.coffee b/test/parser.test.coffee index 8010f9c9..7c4a98fb 100644 --- a/test/parser.test.coffee +++ b/test/parser.test.coffee @@ -156,11 +156,30 @@ module.exports = equ r.sample.$$[10].$$[2]._, '3' equ r.sample.$$[10].$$[3]['#name'], 'one' equ r.sample.$$[10].$$[3]._, '4' - equ r.sample.$$[10].$$[4]['#name'], 'two' + equ r.sample.$$[10].$$[4]['#name'], 'two' equ r.sample.$$[10].$$[4]._, '5' equ r.sample.$$[10].$$[5]['#name'], 'three' equ r.sample.$$[10].$$[5]._, '6') + 'test non-async parsing': (test) -> + x2js = new xml2js.Parser() + data = fs.readFileSync fileName, 'utf8' + xmldoc = x2js.parseStringSync data + assert.notEqual xmldoc, null + equ xmldoc.sample.listtest[0].item[0].subitem[0], 'Foo(1)' + test.finish() + + 'test non-async with bad input': (test) -> + x2js = new xml2js.Parser() + data = fs.readFileSync fileName, 'utf8' + err = null + try + xmldoc = x2js.parseStringSync "< a moose bit my sister>"; + catch _err + err = _err + assert.notEqual err, null + test.finish() + 'test parse with explicitChildren and charsAsChildren and preserveChildrenOrder': skeleton(explicitChildren: true, preserveChildrenOrder: true, charsAsChildren: true, (r) -> console.log 'Result object: ' + util.inspect r, false, 10 equ r.sample.$$[10]['#name'], 'ordertest' From 690da16e95e09947280cf5ba6d4b41a053ec616e Mon Sep 17 00:00:00 2001 From: Mrparkers Date: Fri, 12 Aug 2016 11:19:40 -0500 Subject: [PATCH 2/5] removed bonus space --- test/parser.test.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parser.test.coffee b/test/parser.test.coffee index 7c4a98fb..3b657fc2 100644 --- a/test/parser.test.coffee +++ b/test/parser.test.coffee @@ -156,7 +156,7 @@ module.exports = equ r.sample.$$[10].$$[2]._, '3' equ r.sample.$$[10].$$[3]['#name'], 'one' equ r.sample.$$[10].$$[3]._, '4' - equ r.sample.$$[10].$$[4]['#name'], 'two' + equ r.sample.$$[10].$$[4]['#name'], 'two' equ r.sample.$$[10].$$[4]._, '5' equ r.sample.$$[10].$$[5]['#name'], 'three' equ r.sample.$$[10].$$[5]._, '6') From f7ccd7c559577e4707bd46b6adc5ce5ea7f485f0 Mon Sep 17 00:00:00 2001 From: Mrparkers Date: Fri, 12 Aug 2016 11:21:59 -0500 Subject: [PATCH 3/5] removed explicit return statement --- src/xml2js.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/src/xml2js.coffee b/src/xml2js.coffee index 040f31b0..c9b42845 100644 --- a/src/xml2js.coffee +++ b/src/xml2js.coffee @@ -409,7 +409,6 @@ class exports.Parser extends events.EventEmitter cb = (_err, _retval) -> retval = _retval err = _err - return #assumption 2: parseString will implicitly call cb (thus setting closure retval) before returing @parseString str, cb From 435fcb5f1a165993ca42e97c3ac60639b7a9fb9d Mon Sep 17 00:00:00 2001 From: Mrparkers Date: Fri, 12 Aug 2016 11:27:26 -0500 Subject: [PATCH 4/5] added exports shorthand --- src/xml2js.coffee | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/xml2js.coffee b/src/xml2js.coffee index c9b42845..f0aab9de 100644 --- a/src/xml2js.coffee +++ b/src/xml2js.coffee @@ -410,14 +410,12 @@ class exports.Parser extends events.EventEmitter retval = _retval err = _err - #assumption 2: parseString will implicitly call cb (thus setting closure retval) before returing + #assumption 2: parseString will implicitly call cb (thus setting closure retval) before returning @parseString str, cb if err throw err retval - - exports.parseString = (str, a, b) -> # let's determine what we got as arguments if b? @@ -435,3 +433,7 @@ exports.parseString = (str, a, b) -> # the rest is super-easy parser = new exports.Parser options parser.parseString str, cb + +exports.parseStringSync = (str) -> + parser = new exports.Parser {} + parser.parseStringSync str From 45724fc688251b48fbeb47eaca2543f2c1ff4f69 Mon Sep 17 00:00:00 2001 From: Mrparkers Date: Fri, 12 Aug 2016 11:34:17 -0500 Subject: [PATCH 5/5] create dist and update test for parseStringSync --- lib/xml2js.js | 8 +++++++- test/parser.test.coffee | 6 ++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/xml2js.js b/lib/xml2js.js index d7fdfcb0..11981363 100644 --- a/lib/xml2js.js +++ b/lib/xml2js.js @@ -497,7 +497,7 @@ err = void 0; cb = function(_err, _retval) { retval = _retval; - err = _err; + return err = _err; }; this.parseString(str, cb); if (err) { @@ -529,4 +529,10 @@ return parser.parseString(str, cb); }; + exports.parseStringSync = function(str) { + var parser; + parser = new exports.Parser({}); + return parser.parseStringSync(str); + }; + }).call(this); diff --git a/test/parser.test.coffee b/test/parser.test.coffee index 3b657fc2..cafcd5ac 100644 --- a/test/parser.test.coffee +++ b/test/parser.test.coffee @@ -162,19 +162,17 @@ module.exports = equ r.sample.$$[10].$$[5]._, '6') 'test non-async parsing': (test) -> - x2js = new xml2js.Parser() data = fs.readFileSync fileName, 'utf8' - xmldoc = x2js.parseStringSync data + xmldoc = xml2js.parseStringSync data assert.notEqual xmldoc, null equ xmldoc.sample.listtest[0].item[0].subitem[0], 'Foo(1)' test.finish() 'test non-async with bad input': (test) -> - x2js = new xml2js.Parser() data = fs.readFileSync fileName, 'utf8' err = null try - xmldoc = x2js.parseStringSync "< a moose bit my sister>"; + xmldoc = xml2js.parseStringSync "< a moose bit my sister>"; catch _err err = _err assert.notEqual err, null