-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.js
88 lines (64 loc) · 1.97 KB
/
parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var cheerio = require('cheerio');
var fs = require('fs');
var Parser = function(file_name, options) {
this.file_name = file_name;
this.options = options;
this.dom_ = null;
}
Parser.prototype.getEdgesQueries_ = function() {
if(!this.dom_) {
throw new Error('XML DOM is not loaded');
}
var queries = [];
var self = this;
var counter = 0;
this.dom_('nodes node edge').each(function(index, node) {
queries.push('INSERT INTO `edge` (`id`, `from_id`, `to_id`) VALUES ('+(++counter)+', '+this.attr('fromid')+', '+this.attr('toid')+');');
});
return queries;
}
Parser.prototype.getNodesQueries_ = function() {
if(!this.dom_) {
throw new Error('XML DOM is not loaded');
}
var queries = [];
var self = this;
this.dom_('nodes node').each(function(index, node) {
queries.push('INSERT INTO `node` (`id`) VALUES ('+this.attr('id')+');');
});
return queries;
}
Parser.prototype.parse = function() {
var file = fs.readFileSync(this.file_name, {
encoding: 'ascii'
});
this.dom_ = cheerio.load(file, {
xmlMode: true,
lowerCaseTags: true,
lowerCaseAttributeNames: true
});
var nodes_queries = this.getNodesQueries_() || [];
var edges_queries = this.getEdgesQueries_() || [];
var result = '';
result += '-- Nodes for ZUMScore --\n\n';
result += '-- Source: '+this.file_name+'\n';
result += '-- Generated at: '+(new Date().toUTCString())+'\n';
result += '-- Repository: https://github.com/fitak/zum-nodes-parser.git'+'\n';
result += '\n';
if(this.options['foreignKeys']) {
result += 'SET FOREIGN_KEY_CHECKS=0;\n';
}
if(!this.options['leaveContent']) {
result += 'TRUNCATE edge;\n'
result += 'TRUNCATE node;\n';
}
result += '\n';
result += '-- Generating nodes\n';
result += nodes_queries.join('\n');
result += '\n\n';
result += '-- Generating edges\n';
result += edges_queries.join('\n');
result += '\n\n-- End of '+this.file_name+'\n';
return result;
}
module.exports = Parser;