Skip to content

Commit

Permalink
Merge pull request vpulim#246 from devotis/feature-sax
Browse files Browse the repository at this point in the history
Replace Expat dependency with Sax
  • Loading branch information
Vinay Pulim committed Feb 15, 2014
2 parents 3835f9e + b46a4a0 commit d4a42f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
42 changes: 22 additions & 20 deletions lib/wsdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 = {
Expand All @@ -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,
attributeName,
top = stack[stack.length - 1],
Expand Down Expand Up @@ -868,9 +871,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],
Expand All @@ -896,9 +899,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;
Expand All @@ -921,11 +924,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) {
Expand Down Expand Up @@ -1012,11 +1013,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) {
Expand All @@ -1040,19 +1044,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;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"author": "Vinay Pulim <[email protected]>",
"dependencies": {
"node-expat": ">=1.6.1",
"sax": ">=0.6",
"request": ">=2.9.0"
},
"repository": {
Expand Down

0 comments on commit d4a42f8

Please sign in to comment.