Loquicom 5 лет назад
Родитель
Сommit
fcb0e7c612
1 измененных файлов с 68 добавлено и 0 удалено
  1. 68 0
      src/parser/parser.js

+ 68 - 0
src/parser/parser.js

@@ -0,0 +1,68 @@
+const file = require('../file');
+
+const ROOT_TAG = 'dblp';
+
+const parser = class Parser {
+
+    constructor(path) {
+        this.callback = null;
+        this.result = {};
+        this.from(path);
+        this.dest = null;
+        this.sax = require('sax').createStream(true);
+        this.sax.on('error', this._error);
+        this.sax.on('opentag', this._opentag);
+        this.sax.on('closetag', this._closetag);
+        this.sax.on('text', this._text);
+    }
+
+    parse(callback = null) {
+        this.callback = callback;
+        file.fs.createReadStream(this.source).pipe(this.sax);
+    }
+
+    from(source) {
+        if (!file.exist(source)) {
+            throw 'File not found';
+        }
+        let split = source.split('/');
+        split = split[split.length - 1].split('.');
+        if (split[split.length - 1] !== 'xml') {
+            throw 'File is not an XML';
+        }
+        this.source = source;
+    }
+
+    to(dest) {
+        if (!file.makedir(dest, true)) {
+            throw 'Unable to create destination folder';
+        }
+        this.dest = dest;
+        return this;
+    }
+
+    write() {
+
+    }
+
+    _error(err) {
+        throw err;
+    }
+
+    _opentag(node) {
+        console.log(node);
+    }
+
+    _closetag(tag) {
+        if (tag === ROOT_TAG && this.callback !== null) {
+            this.callback();
+        }
+    }
+
+    _text(text) {
+
+    }
+
+};
+
+module.exports = parser;