Skip to content
Browse files

Cleaned up and fixed a few bugs

  • Loading branch information...
1 parent 690b1c8 commit 26626d0e75b7218aa535b987321ace9e616b8ac9 @jacksenechal jacksenechal committed Oct 25, 2013
Showing with 104 additions and 91 deletions.
  1. +55 −48 lib/xml2js.js
  2. +49 −43 src/xml2js.coffee
View
103 lib/xml2js.js
@@ -1,9 +1,9 @@
-// Generated by CoffeeScript 1.4.0
+// Generated by CoffeeScript 1.6.1
(function() {
var builder, events, isEmpty, sax,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
- __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ _this = this;
sax = require('sax');
@@ -42,7 +42,15 @@
mergeAttrs: false,
explicitRoot: true,
validator: null,
- xmlns: false
+ xmlns: false,
+ rootName: 'root',
+ xmldec: {
+ 'version': '1.0',
+ 'encoding': 'UTF-8',
+ 'standalone': true
+ },
+ doctype: null,
+ pretty: true
}
};
@@ -63,7 +71,6 @@
function Builder(opts) {
var key, value, _ref;
this.options = {};
- this.options = {};
_ref = exports.defaults["0.2"];
for (key in _ref) {
if (!__hasProp.call(_ref, key)) continue;
@@ -78,57 +85,54 @@
}
Builder.prototype.buildObject = function(rootObj) {
- var attrkey, charkey, pretty, render, root, rootElement, rootName;
+ var attrkey, charkey, render, rootElement, rootName;
attrkey = this.options.attrkey;
charkey = this.options.charkey;
- rootName = this.options.rootName;
- if (this.options.explicitRoot) {
- root = Object.keys(rootObj)[0];
- rootObj = rootObj[root];
- if (!rootName) {
- rootName = root;
- }
+ if ((Object.keys(rootObj).length === 1) && (this.options.rootName === exports.defaults['0.2'].rootName)) {
+ rootName = Object.keys(rootObj)[0];
+ rootObj = rootObj[rootName];
+ } else {
+ rootName = this.options.rootName;
}
- rootElement = builder.create(rootName, {
- version: "1.0",
- encoding: "UTF-8",
- standalone: true
- });
render = function(element, obj) {
- var attr, child, entry, index, key, value;
- for (key in obj) {
- if (!__hasProp.call(obj, key)) continue;
- child = obj[key];
- if (key === attrkey) {
- if (typeof child === "object") {
- for (attr in child) {
- value = child[attr];
- element = element.att(attr, value);
+ var attr, child, entry, index, key, value, _ref, _ref1;
+ if (typeof obj !== 'object') {
+ element.txt(obj);
+ } else {
+ for (key in obj) {
+ if (!__hasProp.call(obj, key)) continue;
+ child = obj[key];
+ if (key === attrkey) {
+ if (typeof child === "object") {
+ for (attr in child) {
+ value = child[attr];
+ element = element.att(attr, value);
+ }
}
- }
- } else if (key === charkey) {
- element = element.txt(child);
- } else if (typeof child === 'object' && child.constructor && child.constructor.name && child.constructor.name === 'Array') {
- for (index in child) {
- if (!__hasProp.call(child, index)) continue;
- entry = child[index];
- if (typeof entry === 'string') {
- element = element.ele(key, entry).up();
- } else {
- element = arguments.callee(element.ele(key), entry).up();
+ } else if (key === charkey) {
+ element = element.txt(child);
+ } else if (typeof child === 'object' && ((child != null ? child.constructor : void 0) != null) && ((child != null ? (_ref = child.constructor) != null ? _ref.name : void 0 : void 0) != null) && (child != null ? (_ref1 = child.constructor) != null ? _ref1.name : void 0 : void 0) === 'Array') {
+ for (index in child) {
+ if (!__hasProp.call(child, index)) continue;
+ entry = child[index];
+ if (typeof entry === 'string') {
+ element = element.ele(key, entry).up();
+ } else {
+ element = arguments.callee(element.ele(key), entry).up();
+ }
}
+ } else if (typeof child === "object") {
+ element = arguments.callee(element.ele(key), child).up();
+ } else {
+ element = element.ele(key, child.toString()).up();
}
- } else if (typeof child === "object") {
- element = arguments.callee(element.ele(key), child).up();
- } else {
- element = element.ele(key, child.toString()).up();
}
}
return element;
};
- pretty = (this.options.pretty ? this.options.pretty : true);
+ rootElement = builder.create(rootName, this.options.xmldec, this.options.doctype);
return render(rootElement, rootObj).end({
- pretty: pretty
+ pretty: this.options.pretty
});
};
@@ -141,11 +145,14 @@
__extends(Parser, _super);
function Parser(opts) {
- this.parseString = __bind(this.parseString, this);
-
- this.reset = __bind(this.reset, this);
-
- var key, value, _ref;
+ var key, value, _ref,
+ _this = this;
+ this.parseString = function(str, cb) {
+ return Parser.prototype.parseString.apply(_this, arguments);
+ };
+ this.reset = function() {
+ return Parser.prototype.reset.apply(_this, arguments);
+ };
this.options = {};
_ref = exports.defaults["0.2"];
for (key in _ref) {
View
92 src/xml2js.coffee
@@ -41,7 +41,10 @@ exports.defaults =
explicitRoot: true
validator: null,
xmlns : false,
+ # xml building options
rootName: 'root',
+ xmldec: {'version': '1.0', 'encoding': 'UTF-8', 'standalone': true},
+ doctype: null,
pretty: true
class exports.ValidationError extends Error
@@ -50,7 +53,6 @@ class exports.ValidationError extends Error
class exports.Builder
constructor: (opts) ->
- @options = {}
# copy this versions default options
@options = {}
@options[key] = value for own key, value of exports.defaults["0.2"]
@@ -60,51 +62,55 @@ class exports.Builder
buildObject: (rootObj) ->
attrkey = @options.attrkey
charkey = @options.charkey
- rootName = @options.rootName;
-
- # If explicitRoot is defined and rootName is defined, this latter
- # override the root element, otherwiswe, the first object key
- # is used as root element.
- if this.options.explicitRoot
- root = Object.keys(rootObj)[0];
- rootObj = rootObj[root];
- if !rootName
- rootName = root;
-
- rootElement = builder.create(rootName,version: "1.0",encoding: "UTF-8",standalone: true)
-
+
+ # If there is a sane-looking first element to use as the root,
+ # and the user hasn't specified a non-default rootName,
+ if ( Object.keys(rootObj).length is 1 ) and ( @options.rootName == exports.defaults['0.2'].rootName )
+ # we'll take the first element as the root element
+ rootName = Object.keys(rootObj)[0]
+ rootObj = rootObj[rootName]
+ else
+ # otherwise we'll use whatever they've set, or the default
+ rootName = @options.rootName
+
render = (element, obj) ->
- for own key, child of obj
- # Case #1 Attribute
- if key is attrkey
- if typeof child is "object"
- # Inserts tag attributes
- for attr, value of child
- element = element.att(attr, value)
-
- # Case #2 Char data (CDATA, etc.)
- else if key is charkey
- element = element.txt(child);
-
- # Case #3 Array data
- else if typeof child is 'object' and child.constructor and child.constructor.name and child.constructor.name is 'Array'
- for own index, entry of child
- if typeof entry is 'string'
- element = element.ele(key, entry).up();
- else
- element = arguments.callee(element.ele(key), entry).up();
-
- # Case #4 Objects
- else if typeof child is "object"
- element = arguments.callee(element.ele(key), child).up()
-
- # Case #5 String and remaining types
- else element = element.ele(key, child.toString()).up()
+ if typeof obj isnt 'object'
+ # single element, just append it as text
+ element.txt obj
+ else
+ for own key, child of obj
+ # Case #1 Attribute
+ if key is attrkey
+ if typeof child is "object"
+ # Inserts tag attributes
+ for attr, value of child
+ element = element.att(attr, value)
+
+ # Case #2 Char data (CDATA, etc.)
+ else if key is charkey
+ element = element.txt(child)
+
+ # Case #3 Array data
+ else if typeof child is 'object' and child?.constructor? and child?.constructor?.name? and child?.constructor?.name is 'Array'
+ for own index, entry of child
+ if typeof entry is 'string'
+ element = element.ele(key, entry).up()
+ else
+ element = arguments.callee(element.ele(key), entry).up()
+
+ # Case #4 Objects
+ else if typeof child is "object"
+ element = arguments.callee(element.ele(key), child).up()
+
+ # Case #5 String and remaining types
+ else
+ element = element.ele(key, child.toString()).up()
element
- pretty = (if (@options.pretty) then @options.pretty else true)
- render(rootElement, rootObj).end pretty: pretty
+ rootElement = builder.create(rootName, @options.xmldec, @options.doctype)
+
+ render(rootElement, rootObj).end(pretty: @options.pretty)
class exports.Parser extends events.EventEmitter
constructor: (opts) ->
@@ -171,7 +177,7 @@ class exports.Parser extends events.EventEmitter
obj = stack.pop()
nodeName = obj["#name"]
delete obj["#name"]
-
+
s = stack[stack.length - 1]
# remove the '#' key altogether if it's blank
if obj[charkey].match(/^\s*$/)

0 comments on commit 26626d0

Please sign in to comment.
Something went wrong with that request. Please try again.