Skip to content

Commit

Permalink
augmented jsonml.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeard4 committed Apr 1, 2012
1 parent 37ed41f commit 66f8b9c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/jsonml.js 100644 → 100755
@@ -1,3 +1,4 @@
#!/usr/bin/env node
//TODO: add option to filter whitespace

var
Expand Down Expand Up @@ -28,7 +29,7 @@ function getParser(doneCb){
if(tupleStack && tupleStack.length){
var currentTuple = tupleStack.peek();
//we may read chars in chunks, so make sure we combine them when needed
if( typeof currentTuple[currentTuple.length - 1] === 'string'){
if( currentTuple.length > 1 && typeof currentTuple[currentTuple.length - 1] === 'string'){
currentTuple[currentTuple.length - 1] += chars;
}else{
currentTuple.push(chars);
Expand All @@ -42,7 +43,7 @@ function getParser(doneCb){
});

cb.onEndDocument(function() {
doneCb(root);
doneCb(null,root);
});

cb.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {
Expand All @@ -61,7 +62,6 @@ function getParser(doneCb){

newTuple.push(elem); //TODO: deal with prefix, uri, namespaces, etc.

console.log('attrs.length',attrs.length);
if(attrs.length){
newTuple.push(constructAttrObj(attrs));
}
Expand All @@ -71,7 +71,11 @@ function getParser(doneCb){
//console.log("end",elem, prefix, uri);

var prevTuple = tupleStack.pop();
assert.equal(prevTuple[0],elem);
try {
assert.equal(prevTuple[0],elem);
} catch(e){
doneCb(e,root);
}
});

cb.onCharacters(appendText);
Expand All @@ -83,11 +87,12 @@ function getParser(doneCb){
});

cb.onWarning(function(msg) {
util.log('<WARNING>'+msg+"</WARNING>");
doneCb(new Error(msg),root);
});

cb.onError(function(msg) {
util.log('<ERROR>'+JSON.stringify(msg)+"</ERROR>");
doneCb(new Error(msg),root);
});

});
Expand All @@ -102,8 +107,12 @@ exports.parseFile = function(fileName,done){
};

if( require.main === module){
exports.parseFile(process.argv[2],function(jsonml){
process.stdout.write(JSON.stringify(jsonml,4,4));
exports.parseFile(process.argv[2],function(err,jsonml){
if(err){
console.error(err);
}else{
process.stdout.write(JSON.stringify(jsonml,4,4));
}
});
}

40 changes: 40 additions & 0 deletions package.json
@@ -0,0 +1,40 @@
{
"name": "xml2jsonml",
"version": "0.0.1",
"description": "Converts XML to JsonML.",
"keywords": [
"xml",
"jsonml"
],
"maintainers": [{
"name": "Jacob Beard",
"email": "jbeard4@cs.mcgill.ca",
"url": "http://echo-flow.com"
}],
"bugs": {
"email": "jbeard4@cs.mcgill.ca",
"url": "https://github.com/jbeard4/node-xml2jsonml/issues"
},
"licenses": [
{
"type": "Apache-2.0"
}
],
"repositories": [
{
"type": "git",
"url": "https://github.com/jbeard4/node-xml2jsonml.git"
}
],
"implements": [
"http://jsonml.org/"
],
"dependencies": {
"node-xml" : "git://github.com/jbeard4/node-xml.git"
},
"engines" : { "node" : ">=0.4.7" },
"bin": {
"xml2jsonml" : "./lib/jsonml.js"
},
"main": "./lib/jsonml"
}

0 comments on commit 66f8b9c

Please sign in to comment.