Permalink
Browse files

Updated XmlRecorder to support multi-track-recording

  • Loading branch information...
1 parent 0b8d017 commit 9a0186d9e1bd0933183e458d8d87c1969701ca1c @StevenLooman committed Aug 19, 2012
Showing with 21 additions and 55 deletions.
  1. +16 −53 lib/xml_recorder.js
  2. +5 −2 run.js
View
@@ -1,76 +1,39 @@
-var debug = false;
-function trace(message) {
- if (debug) {
- console.log(message);
- }
-}
-
-
-/**
- * XML DOM recorder
- */
function XmlRecorder() {
- this.recording = false;
+ this.streams = {};
}
-/**
- * Start recording
- */
XmlRecorder.prototype.start = function(state) {
- trace('start recording');
-
- this.recording = true;
- this.str = '';
+ this.streams[state.id] = '';
};
-/**
- * Stop recording
- */
XmlRecorder.prototype.stop = function(state) {
- trace('stop recording');
+ var stream = this.streams[state.id];
+
+ delete this.streams[state.id];
- this.recording = false;
- return this.str;
+ return stream;
};
-/**
- * Event handlers for SAX-stream
- */
XmlRecorder.prototype.onOpenTag = function(node) {
- if (!this.recording) {
- return;
+ var id;
+ for (id in this.streams) {
+ this.streams[id] += '<' + node.name + '>';
}
-
- trace('record open tag');
-
- this.str += '<' + node.name + '>';
};
-/**
- * Event handlers for SAX-stream
- */
XmlRecorder.prototype.onCloseTag = function(tag) {
- if (!this.recording) {
- return;
+ var id;
+ for (id in this.streams) {
+ this.streams[id] += '</' + tag + '>';
}
-
- trace('record close tag');
-
- this.str += '</' + tag + '>';
};
-/**
- * Event handlers for SAX-stream
- */
-XmlRecorder.prototype.onText = function(text) {
- if (!this.recording) {
- return;
+XmlRecorder.prototype.onText = function (text) {
+ var id;
+ for (id in this.streams) {
+ this.streams[id] += text;
}
-
- trace('record text');
-
- this.str += text;
};
View
@@ -4,17 +4,20 @@ var fs = require('fs');
var sax = require('sax');
var xps = require('./lib');
-var filename = 'test/bookstore.xml';
+//var filename = 'test/bookstore.xml';
+var filename = 'test/inception.xml';
function onMatch(xml) {
+ console.log('=== on match');
console.log(xml);
}
function main() {
var saxParser = sax.createStream(true);
// var streamer = new xps.SaXPath(saxParser, '/bookstore/book');
// var streamer = new xps.SaXPath(saxParser, '/bookstore/book[@category="COOKING"]');
- var streamer = new xps.SaXPath(saxParser, '//book');
+// var streamer = new xps.SaXPath(saxParser, '//book');
+ var streamer = new xps.SaXPath(saxParser, '//node');
streamer.on('match', onMatch);

0 comments on commit 9a0186d

Please sign in to comment.