Skip to content

Commit

Permalink
feat: write outputfile option and options object initialization (#1)
Browse files Browse the repository at this point in the history
add options object for Parser and Builder
  • Loading branch information
Francis Molina authored Feb 8, 2018
1 parent ade0a1b commit 9ce7a1c
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 71 deletions.
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
```

### Plan sample (with files):
### Plan sample using input files:
```json
{
"id":"parse-xml-json_default",
Expand All @@ -45,5 +45,46 @@
}
```

### Plan sample using output file:
```json
{
"id": "parse-xml-json_default",
"to": "json",
"xml_file": "./test/sample.xml",
"output_file": "./test/output.json"
}
```

### Plan sample using options object for json:
```json
{
"id": "parse-xml-json_default",
"to": "json",
"xml_file": "./test/sample.xml",
"output_file": "./test/output.json",
"json_options": {
"attrkey": "attribute",
"charkey": "value"
}
}
```
### Plan sample using options object for xml:
```json
{
"id": "parse-xml-json_default",
"to": "xml",
"json_file": "./test/sample.json",
"output_file": "./test/output.xml",
"xml_options": {
"headless": true,
"cdata": true
}
}
```
Options definitions for `json_options` and `xml_options` params can be found here:
- [json_options]
- [xml_options]

[Runnerty]: http://www.runnerty.io
[json_options]: https://github.com/Leonidas-from-XIV/node-xml2js#options
[xml_options]: https://github.com/Leonidas-from-XIV/node-xml2js#options-for-the-builder-class
[Runnerty]: http://www.runnerty.io
163 changes: 114 additions & 49 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const xml2js = require("xml2js");
const fs = require('fs');
const fs = require("fs");

const Execution = global.ExecutionClass;

Expand All @@ -12,70 +12,135 @@ class parseXmlJsonExecutor extends Execution {

exec(params) {
let _this = this;
let endOptions = {end: 'end'};
let endOptions = { end: "end" };

function parseToJson(xml) {
return new Promise(async function (resolve, reject) {
let parser = new xml2js.Parser();
parser.parseString(xml, function(err, result) {
resolve(result);
return new Promise(function (resolve, reject) {
let parser = new xml2js.Parser(params.json_options);
parser.parseString(xml, function (err, result) {
if (err) {
reject(err);
} else {
resolve(JSON.stringify(result));
}
});
});
}

function parseToXml(json) {
return new Promise(async function (resolve, reject) {
let builder = new xml2js.Builder();
let result = builder.buildObject(json);
return new Promise(function (resolve, reject) {
let builder = new xml2js.Builder(params.xml_options);
let result = builder.buildObject(JSON.parse(json));
resolve(result);
});
}

function evaluateResults(results) {
if (params.to === "json") {
endOptions.data_output = JSON.stringify(results);
return new Promise(function (resolve, reject) {
if (params.output_file) {
fs.writeFile(params.output_file, results, "utf8", function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
} else {
endOptions.data_output = results;
resolve();
}
_this.end(endOptions);
});
}

} else if (params.to === "xml") {
endOptions.data_output = results;
if (params.to === "json") {
new Promise(function (resolve, reject) {
let xml = "";
if (params.xml) {
xml = params.xml;
resolve(xml);
} else if (params.xml_file) {
fs.readFile(params.xml_file, function (err, result) {
if (err) {
reject(err);
} else {
xml = result;
resolve(xml);
}
});
} else {
reject(new Error(`Parsing to ${params.to}, params xml: ${params.xml} or xml_file: ${params.xml_file} not set correctly`));
}
}).then(xml => {
parseToJson(xml)
.then(results => {
evaluateResults(results)
.catch(err => {
endOptions.end = "error";
endOptions.messageLog = `writeToOutputFileJson: ${err}`;
endOptions.err_output = `writeToOutputFileJson: ${err}`;
_this.end(endOptions);
});
})
.catch(err => {
endOptions.end = "error";
endOptions.messageLog = `parseToJson: ${err}`;
endOptions.err_output = `parseToJson: ${err}`;
_this.end(endOptions);
});
}).catch(err => {
endOptions.end = "error";
endOptions.messageLog = `readFileToJson: ${err}`;
endOptions.err_output = `readFileToJson: ${err}`;
_this.end(endOptions);
}
}
});

if (params.to === 'json') {
let xml = '';
if (params.xml) {
xml = params.xml;
} else if (params.xml_file) {
xml = fs.readFileSync(params.xml_file);
}
parseToJson(xml)
.then((results) => {
evaluateResults(results);
})
.catch(function (err) {
endOptions.end = 'error';
endOptions.messageLog = `parseToJson: ${err}`;
endOptions.err_output = `parseToJson: ${err}`;
_this.end(endOptions);
});
} else if (params.to === 'xml') {
let json = '';
if (params.json) {
json = params.json;
} else if (params.json_file) {
json = JSON.parse(fs.readFileSync(params.json_file));
}
parseToXml(json)
.then((results) => {
evaluateResults(results);
})
.catch(function (err) {
endOptions.end = 'error';
endOptions.messageLog = `parseToXml: ${err}`;
endOptions.err_output = `parseToXml: ${err}`;
_this.end(endOptions);
});
} else if (params.to === "xml") {
new Promise(function (resolve, reject) {
let json = "";
if (params.json) {
json = params.json;
resolve(json);
} else if (params.json_file) {
fs.readFile(params.json_file, function (err, result) {
if (err) {
reject(err);
} else {
json = result;
resolve(json);
}
});
} else {
reject(new Error(`Parsing to ${params.to}, params json: ${params.json} or json_file: ${params.json_file} not set correctly`));
}
}).then(json => {
parseToXml(json)
.then(results => {
evaluateResults(results)
.catch(err => {
endOptions.end = "error";
endOptions.messageLog = `writeToOutputFileXml: ${err}`;
endOptions.err_output = `writeToOutputFileXml: ${err}`;
_this.end(endOptions);
});
})
.catch(err => {
endOptions.end = "error";
endOptions.messageLog = `parseToXml: ${err}`;
endOptions.err_output = `parseToXml: ${err}`;
_this.end(endOptions);
});
}).catch(err => {
endOptions.end = "error";
endOptions.messageLog = `readFileToXml: ${err}`;
endOptions.err_output = `readFileToXml: ${err}`;
_this.end(endOptions);
});
} else {
endOptions.end = "error";
endOptions.messageLog = `param 'to': ${params.to} not set correctly use to json/xml`;
endOptions.err_output = `param 'to': ${params.to} not set correctly use to json/xml`;
_this.end(endOptions);
}
}
}
Expand Down
80 changes: 60 additions & 20 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,74 @@
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "runnerty-executor-parse-xml-json",
"definitions": {
"config":
{ "type": "object",
"properties":{
"id": {"type": "string"},
"type": {"type": "string", "pattern": "runnerty-executor-parse-xml-json"}
"config": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string",
"pattern": "runnerty-executor-parse-xml-json"
}
}
},
"params":
{
"oneOf":[
"params": {
"oneOf": [
{
"type": "object",
"required": ["id", "to"],
"properties":{
"id": {"type": "string"},
"to": {"type": "string", "pattern": "xml"},
"json": {"type": "object"},
"json_file": {"type": "string"}
"required": [
"id",
"to"
],
"properties": {
"id": {
"type": "string"
},
"to": {
"type": "string",
"pattern": "xml"
},
"json": {
"type": "object"
},
"json_file": {
"type": "string"
},
"output_file": {
"type": "string"
},
"xml_options": {
"type": "object"
}
}
},
{
"type": "object",
"required": ["id", "to"],
"properties":{
"id": {"type": "string"},
"to": {"type": "string", "pattern": "json"},
"xml": {"type": "string"},
"xml_file": {"type": "string"}
"required": [
"id",
"to"
],
"properties": {
"id": {
"type": "string"
},
"to": {
"type": "string",
"pattern": "json"
},
"xml": {
"type": "string"
},
"xml_file": {
"type": "string"
},
"output_file": {
"type": "string"
},
"json_options": {
"type": "object"
}
}
}
]
Expand Down

0 comments on commit 9ce7a1c

Please sign in to comment.