Skip to content

Commit

Permalink
basic features
Browse files Browse the repository at this point in the history
  • Loading branch information
janzal committed Mar 30, 2014
1 parent b772ed1 commit 3d21345
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/node_modules
test-input2.xml
/node_modules
out.sql
10 changes: 7 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ var Parser = require('./parser');

commander
.version(package.version)
.usage('[options] <file ...>')
.usage('[options] <file ...>')
.option('-p, --pedantic', 'Pedantic mode is checking for inconsistency in map', true)
.option('-f, --foreign-keys', 'Disable foreign keys')
.option('-l, --leave-content', 'Does not truncate tables')
.parse(process.argv);

if(commander.args.length === 0) {
Expand All @@ -16,8 +19,9 @@ if(commander.args.length === 0) {
}

commander.args.forEach(function(file) {
var parser = new Parser(file);
parser.parse();
var parser = new Parser(file, commander);

process.stdout.write(parser.parse());
});


Expand Down
50 changes: 46 additions & 4 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ var Parser = function(file_name, options) {
}

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() {
Expand All @@ -21,9 +33,11 @@ Parser.prototype.getNodesQueries_ = function() {
var queries = [];
var self = this;

// this.dom_('nodes').each(function(index, node) {
// console.log(self.dom_('edge', this).toArray());
// });
this.dom_('nodes node').each(function(index, node) {
queries.push('INSERT INTO `node` (`id`) VALUES ('+this.attr('id')+');');
});

return queries;
}

Parser.prototype.parse = function() {
Expand All @@ -40,7 +54,35 @@ Parser.prototype.parse = function() {
var nodes_queries = this.getNodesQueries_() || [];
var edges_queries = this.getEdgesQueries_() || [];

var result
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;
11 changes: 8 additions & 3 deletions test-input2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
<Edge fromId="0" toId="8810" length="0.07158354732086207" />
<Edge fromId="0" toId="4739" length="0.11071461431446344" />
<Edge fromId="0" toId="6255" length="0.11232580038839102" />
</Node>
</Nodes>
</Node>

<A>Pokus</A>
<Node id="1" name="" x="0.13227525975661064" y="0.5234442241291377" radius="0">
<Edge fromId="0" toId="3224" length="0.06478646468754722" />
<Edge fromId="0" toId="8810" length="0.07158354732086207" />
<Edge fromId="0" toId="4739" length="0.11071461431446344" />
<Edge fromId="0" toId="6255" length="0.11232580038839102" />
</Node>
</Nodes>

0 comments on commit 3d21345

Please sign in to comment.