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..11981363 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; + return err = _err; + }; + this.parseString(str, cb); + if (err) { + throw err; + } + return retval; + }; + return Parser; })(events.EventEmitter); @@ -513,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/src/xml2js.coffee b/src/xml2js.coffee index bc0d6372..f0aab9de 100644 --- a/src/xml2js.coffee +++ b/src/xml2js.coffee @@ -402,6 +402,20 @@ 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 + + #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? @@ -419,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 diff --git a/test/parser.test.coffee b/test/parser.test.coffee index 8010f9c9..cafcd5ac 100644 --- a/test/parser.test.coffee +++ b/test/parser.test.coffee @@ -161,6 +161,23 @@ module.exports = equ r.sample.$$[10].$$[5]['#name'], 'three' equ r.sample.$$[10].$$[5]._, '6') + 'test non-async parsing': (test) -> + data = fs.readFileSync fileName, 'utf8' + 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) -> + data = fs.readFileSync fileName, 'utf8' + err = null + try + xmldoc = xml2js.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'