From b46a4a0cebcf555cb3486cab80833cfb41560b8e Mon Sep 17 00:00:00 2001 From: Christiaan Westerbeek Date: Sat, 1 Feb 2014 13:51:14 +0100 Subject: [PATCH] remove expat as a dependency and add sax --- lib/wsdl.js | 42 ++++++++++++++++++++++-------------------- package.json | 2 +- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/wsdl.js b/lib/wsdl.js index ccbdc58f2..78df5267d 100644 --- a/lib/wsdl.js +++ b/lib/wsdl.js @@ -7,7 +7,7 @@ "use strict"; -var expat = require('node-expat'); +var sax = require('sax'); var inherits = require('util').inherits; var http = require('./http'); var fs = require('fs'); @@ -773,7 +773,7 @@ WSDL.prototype.toXML = function() { WSDL.prototype.xmlToObject = function(xml) { var self = this; - var p = new expat.Parser('UTF-8'); + var p = sax.parser(true); var objectName = null; var root = {}; var schema = { @@ -799,7 +799,10 @@ WSDL.prototype.xmlToObject = function(xml) { var refs = {}, id; // {id:{hrefs:[],obj:}, ...} - p.on('startElement', function(nsName, attrs) { + p.onopentag = function(node) { + var nsName = node.name; + var attrs = node.attributes; + var name = splitNSName(nsName).name, top = stack[stack.length - 1], topSchema = top.schema, @@ -856,9 +859,9 @@ WSDL.prototype.xmlToObject = function(xml) { if (topSchema && topSchema[name + '[]']) name = name + '[]'; stack.push({name: originalName, object: obj, schema: topSchema && topSchema[name], id: attrs.id}); - }); + }; - p.on('endElement', function(nsName) { + p.onclosetag = function(nsName) { var cur = stack.pop(), obj = cur.object, top = stack[stack.length - 1], @@ -884,9 +887,9 @@ WSDL.prototype.xmlToObject = function(xml) { if (cur.id) { refs[cur.id].obj = obj; } - }); + }; - p.on('text', function(text) { + p.ontext = function(text) { text = trim(text); if (!text.length) return; @@ -909,11 +912,9 @@ WSDL.prototype.xmlToObject = function(xml) { } } top.object = value; - }); + }; - if (!p.parse(xml, false)) { - throw new Error(p.getError()); - } + p.write(xml).close(); // merge obj with href var merge = function(href, obj) { @@ -1000,11 +1001,14 @@ WSDL.prototype.objectToXML = function(obj, name, namespace, xmlns, first) { WSDL.prototype._parse = function(xml) { var self = this, - p = new expat.Parser('UTF-8'), + p = sax.parser(true), stack = [], root = null; - p.on('startElement', function(nsName, attrs) { + p.onopentag = function(node) { + var nsName = node.name; + var attrs = node.attributes; + var top = stack[stack.length - 1]; var name; if (top) { @@ -1028,19 +1032,17 @@ WSDL.prototype._parse = function(xml) { } stack.push(root); } - }); + }; - p.on('endElement', function(name) { + p.onclosetag = function(name) { var top = stack[stack.length - 1]; assert(top, 'Unmatched close tag: ' + name); top.endElement(stack, name); - }); - - if (!p.parse(xml, false)) { - throw new Error(p.getError()); - } + }; + p.write(xml).close(); + return root; }; diff --git a/package.json b/package.json index 5343db707..0e072bc89 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ }, "author": "Vinay Pulim ", "dependencies": { - "node-expat": ">=1.6.1", + "sax": ">=0.6", "request": ">=2.9.0" }, "repository": {