浏览代码

Amélioration structure du Parser

Loquicom 5 年之前
父节点
当前提交
4668a1d707
共有 1 个文件被更改,包括 30 次插入4 次删除
  1. 30 4
      src/parser/parser.js

+ 30 - 4
src/parser/parser.js

@@ -1,10 +1,15 @@
 const file = require('../file');
 
 const ROOT_TAG = 'dblp';
+const TAGS = [];
 
 const parser = class Parser {
 
     constructor(path) {
+        this._group = [];
+        this._data = [];
+        this._tagOk = false;
+        this._isAuth = false;
         this.callback = null;
         this.result = {};
         this.from(path);
@@ -42,7 +47,9 @@ const parser = class Parser {
     }
 
     write() {
-
+        if (this.dest === null) {
+            throw 'No destination file';   
+        }
     }
 
     _error(err) {
@@ -50,17 +57,36 @@ const parser = class Parser {
     }
 
     _opentag(node) {
-        console.log(node);
+        if (!this._tagOk && TAGS.indexOf(node.name) !== -1) {
+            this._tagOk = true;
+            this._group = [];
+        } else if (!this._isAuth && node.name === 'author') {
+            this._isAuth = true;
+        } 
     }
 
     _closetag(tag) {
         if (tag === ROOT_TAG && this.callback !== null) {
-            this.callback();
+            this._finish();
+            if (this.callback !== null) {
+                this.callback();
+            }
+        } else if (this._tagOk && TAGS.indexOf(tag) !== -1) {
+            this._tagOk = false;
+            this._data.push(this._group);
+        } else if (this._isAuth && tag === 'author') {
+            this._isAuth = false;
         }
     }
 
     _text(text) {
-
+        if (this._isAuth) {
+            this._group.push(text);
+        }
+    }
+    
+    _finish() {
+        console.log(this._data);   
     }
 
 };