diff --git a/lib/Deprecated.js b/lib/Deprecated.js index 84b13e30..26b573d3 100644 --- a/lib/Deprecated.js +++ b/lib/Deprecated.js @@ -38,6 +38,7 @@ const iWorkDeprecate = deprecate('iWork'); const iCmdDeprecate = deprecate('iCmd'); const iQshDeprecate = deprecate('iQsh'); const iShDeprecate = deprecate('iSh'); +const xmlToJsonDeprecate = deprecate('xmlToJson'); const { ProgramCall } = require('./ProgramCall'); const { CommandCall } = require('./CommandCall'); @@ -625,6 +626,210 @@ const iSh = (sh, options) => { return command.toXML(); }; +const xmlToJson = (xml) => { + xmlToJsonDeprecate('As of v1.0, \'xmlToJson\' is deprecated. Use xml2js npm package instead.'); + + const cmdRegG = /[\s\S]+?<\/cmd>/g; + const shRegG = /[\s\S]+?<\/sh>/g; + const qshRegG = /[\s\S]+?<\/qsh>/g; + const pgmRegG = /[\s\S]+?<\/pgm>/g; + const sqlRegG = /[\s\S]+?<\/sql>/g; + + const shReg = /([\s\S]+?)<\/sh>/; + const qshReg = /([\s\S]+?)<\/qsh>/; + const pgmReg = //; + + const successReg = /.*?\+\+\+ success (.*?)<\/success>/; + const errorReg = /.*?\*\*\* error (.*?)<\/error>.*?(.*?)<\/error>/; + const rtDataRegG = /[\s\S]*?<\/data>/g; + const rtDataReg = /([\s\S]*?)<\/data>/; + + // const dsRegG = /[\s\S]+?<\/ds>/g; // TODO: Not used + // const dsReg = /([\s\S]+?)<\/ds>/; // TODO: Not used + const dataRegG = //g; + const dataReg = /([\s\S]*?)<\/data>/; + const sqlResultG = /[\s\S]+?<\/row>/g; + const sqlRowG = /[\s\S]*?<\/data>/g; + const sqlRow = /([\s\S]*?)<\/data>/; + + const cmdData = xml.match(cmdRegG); + const shData = xml.match(shRegG); + const qshData = xml.match(qshRegG); + const pgmData = xml.match(pgmRegG); + const sqlData = xml.match(sqlRegG); + + const ResultArr = []; + + // parse cmd matches + if (cmdData && cmdData.length > 0) { + cmdData.forEach((cmd) => { + const rs = { type: 'cmd' }; + const sucFlag = cmd.match(successReg); + + if (sucFlag && sucFlag.length > 0) { + rs.success = true; + if (sucFlag.length > 1) { + [, rs.cmd] = sucFlag; + } + } else { + rs.success = false; + const errFlag = cmd.match(errorReg); + if (errFlag && errFlag.length > 1) { + [, rs.cmd] = errFlag; + } + if (errFlag && errFlag.length > 2) { + [, , rs.error] = errFlag; + } + } + const rowArray = cmd.match(rtDataRegG); + + if (rowArray && rowArray.length > 0) { + const arr = []; + + rowArray.forEach((row) => { + const eachRow = row.match(rtDataReg); + if (eachRow && eachRow.length > 1) { + arr.push({ name: eachRow[1], value: eachRow[2] ? eachRow[2] : '' }); + } + }); + + rs.data = arr; + } + ResultArr.push(rs); + }); + } + + // parse sh matches + if (shData && shData.length > 0) { + shData.forEach((sh) => { + const rs = { type: 'sh' }; + const shOutput = sh.match(shReg); + + if (shOutput && shOutput.length > 0) { + [, rs.data] = shOutput; + } + ResultArr.push(rs); + }); + } + + // parse qsh matches + if (qshData && qshData.length > 0) { + qshData.forEach((qsh) => { + const rs = { type: 'qsh' }; + const qshOutput = qsh.match(qshReg); + if (qshOutput && qshOutput.length > 0) { + [, rs.data] = qshOutput; + } + ResultArr.push(rs); + }); + } + + // parse pgm matches + if (pgmData && pgmData.length > 0) { + pgmData.forEach((pgm) => { + const rs = { type: 'pgm' }; + const sucFlag = pgm.match(successReg); + + if (sucFlag && sucFlag.length > 0) { + rs.success = true; + } else { + rs.success = false; + } + + const pgmLib = pgm.match(pgmReg); + + if (pgmLib && pgmLib.length > 2) { + [, rs.pgm, rs.lib] = pgmLib; + } + + const paramData = pgm.match(dataRegG); + + if (paramData && paramData.length > 0) { + const arr = []; + paramData.forEach((param) => { + const obj = {}; + const rx = / \b(.*?)\s*=\s*'([^']*)'/g; + const eachRow = param.match(dataReg); + let attr; + + if (eachRow && eachRow.length > 1) { + obj.value = (eachRow[2] ? eachRow[2] : ''); + } + + // eslint-disable-next-line no-cond-assign + while (attr = rx.exec(param)) { + // eslint-disable-next-line prefer-destructuring + obj[attr[1]] = attr[2]; + } + + arr.push(obj); + }); + + rs.data = arr; + } + ResultArr.push(rs); + }); + } + + // parse sql matches + if (sqlData && sqlData.length > 0) { + sqlData.forEach((sql) => { + const rs = { type: 'sql' }; + const sucFlag = sql.match(successReg); + + if (sucFlag && sucFlag.length > 0) { + rs.success = true; + if (sucFlag.length > 1) { + if (sucFlag[0].includes('![CDATA')) { + const fixed = sucFlag[1].replace(/]]>/, ''); + rs.stmt = fixed; + } else { + [, rs.stmt] = sucFlag; + } + } + } else { + rs.success = false; + const errFlag = sql.match(errorReg); + + if (errFlag && errFlag.length > 1) { + [, rs.stmt] = errFlag; + } + if (errFlag && errFlag.length > 2) { + [, , rs.error] = errFlag; + } + } + + const sqlResult = sql.match(sqlResultG); + + if (sqlResult && sqlResult.length > 0) { + const arr = []; + + sqlResult.forEach((result) => { + const eachRow = result.match(sqlRowG); + + if (eachRow) { + const theRow = []; + + eachRow.forEach((row) => { + const perField = row.match(sqlRow); + + if (perField && perField.length > 2) { + theRow.push({ desc: perField[1], value: perField[2] }); + } + }); + + arr.push(theRow); + } + }); + + rs.result = arr; + } + ResultArr.push(rs); + }); + } + return ResultArr; +}; + module.exports.iPgm = iPgm; module.exports.iSql = iSql; module.exports.iConn = iConn; @@ -637,3 +842,4 @@ module.exports.iWork = iWork; module.exports.iCmd = iCmd; module.exports.iQsh = iQsh; module.exports.iSh = iSh; +module.exports.xmlToJson = xmlToJson; diff --git a/lib/Toolkit.js b/lib/Toolkit.js index 86d67539..42d28e0c 100644 --- a/lib/Toolkit.js +++ b/lib/Toolkit.js @@ -17,7 +17,6 @@ const { parseString } = require('xml2js'); const { ProgramCall } = require('./ProgramCall'); -const { xmlToJson } = require('./utils'); const { Connection } = require('./Connection'); const sysvalArray = [ diff --git a/lib/itoolkit.js b/lib/itoolkit.js index 551c2243..c4360e7c 100644 --- a/lib/itoolkit.js +++ b/lib/itoolkit.js @@ -23,7 +23,6 @@ const { ProgramCall } = require('./ProgramCall'); const { CommandCall } = require('./CommandCall'); const { Connection } = require('./Connection'); const { Toolkit } = require('./Toolkit'); -const { xmlToJson } = require('./utils'); const { iPgm, @@ -38,6 +37,7 @@ const { iCmd, iQsh, iSh, + xmlToJson, } = require('./Deprecated'); module.exports = { diff --git a/lib/utils.js b/lib/utils.js index 463c9c38..c40e5b6c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -15,208 +15,6 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -const xmlToJson = (xml) => { - const cmdRegG = /[\s\S]+?<\/cmd>/g; - const shRegG = /[\s\S]+?<\/sh>/g; - const qshRegG = /[\s\S]+?<\/qsh>/g; - const pgmRegG = /[\s\S]+?<\/pgm>/g; - const sqlRegG = /[\s\S]+?<\/sql>/g; - - const shReg = /([\s\S]+?)<\/sh>/; - const qshReg = /([\s\S]+?)<\/qsh>/; - const pgmReg = //; - - const successReg = /.*?\+\+\+ success (.*?)<\/success>/; - const errorReg = /.*?\*\*\* error (.*?)<\/error>.*?(.*?)<\/error>/; - const rtDataRegG = /[\s\S]*?<\/data>/g; - const rtDataReg = /([\s\S]*?)<\/data>/; - - // const dsRegG = /[\s\S]+?<\/ds>/g; // TODO: Not used - // const dsReg = /([\s\S]+?)<\/ds>/; // TODO: Not used - const dataRegG = //g; - const dataReg = /([\s\S]*?)<\/data>/; - const sqlResultG = /[\s\S]+?<\/row>/g; - const sqlRowG = /[\s\S]*?<\/data>/g; - const sqlRow = /([\s\S]*?)<\/data>/; - - const cmdData = xml.match(cmdRegG); - const shData = xml.match(shRegG); - const qshData = xml.match(qshRegG); - const pgmData = xml.match(pgmRegG); - const sqlData = xml.match(sqlRegG); - - const ResultArr = []; - - // parse cmd matches - if (cmdData && cmdData.length > 0) { - cmdData.forEach((cmd) => { - const rs = { type: 'cmd' }; - const sucFlag = cmd.match(successReg); - - if (sucFlag && sucFlag.length > 0) { - rs.success = true; - if (sucFlag.length > 1) { - [, rs.cmd] = sucFlag; - } - } else { - rs.success = false; - const errFlag = cmd.match(errorReg); - if (errFlag && errFlag.length > 1) { - [, rs.cmd] = errFlag; - } - if (errFlag && errFlag.length > 2) { - [, , rs.error] = errFlag; - } - } - const rowArray = cmd.match(rtDataRegG); - - if (rowArray && rowArray.length > 0) { - const arr = []; - - rowArray.forEach((row) => { - const eachRow = row.match(rtDataReg); - if (eachRow && eachRow.length > 1) { - arr.push({ name: eachRow[1], value: eachRow[2] ? eachRow[2] : '' }); - } - }); - - rs.data = arr; - } - ResultArr.push(rs); - }); - } - - // parse sh matches - if (shData && shData.length > 0) { - shData.forEach((sh) => { - const rs = { type: 'sh' }; - const shOutput = sh.match(shReg); - - if (shOutput && shOutput.length > 0) { - [, rs.data] = shOutput; - } - ResultArr.push(rs); - }); - } - - // parse qsh matches - if (qshData && qshData.length > 0) { - qshData.forEach((qsh) => { - const rs = { type: 'qsh' }; - const qshOutput = qsh.match(qshReg); - if (qshOutput && qshOutput.length > 0) { - [, rs.data] = qshOutput; - } - ResultArr.push(rs); - }); - } - - // parse pgm matches - if (pgmData && pgmData.length > 0) { - pgmData.forEach((pgm) => { - const rs = { type: 'pgm' }; - const sucFlag = pgm.match(successReg); - - if (sucFlag && sucFlag.length > 0) { - rs.success = true; - } else { - rs.success = false; - } - - const pgmLib = pgm.match(pgmReg); - - if (pgmLib && pgmLib.length > 2) { - [, rs.pgm, rs.lib] = pgmLib; - } - - const paramData = pgm.match(dataRegG); - - if (paramData && paramData.length > 0) { - const arr = []; - paramData.forEach((param) => { - const obj = {}; - const rx = / \b(.*?)\s*=\s*'([^']*)'/g; - const eachRow = param.match(dataReg); - let attr; - - if (eachRow && eachRow.length > 1) { - obj.value = (eachRow[2] ? eachRow[2] : ''); - } - - // eslint-disable-next-line no-cond-assign - while (attr = rx.exec(param)) { - // eslint-disable-next-line prefer-destructuring - obj[attr[1]] = attr[2]; - } - - arr.push(obj); - }); - - rs.data = arr; - } - ResultArr.push(rs); - }); - } - - // parse sql matches - if (sqlData && sqlData.length > 0) { - sqlData.forEach((sql) => { - const rs = { type: 'sql' }; - const sucFlag = sql.match(successReg); - - if (sucFlag && sucFlag.length > 0) { - rs.success = true; - if (sucFlag.length > 1) { - if (sucFlag[0].includes('![CDATA')) { - const fixed = sucFlag[1].replace(/]]>/, ''); - rs.stmt = fixed; - } else { - [, rs.stmt] = sucFlag; - } - } - } else { - rs.success = false; - const errFlag = sql.match(errorReg); - - if (errFlag && errFlag.length > 1) { - [, rs.stmt] = errFlag; - } - if (errFlag && errFlag.length > 2) { - [, , rs.error] = errFlag; - } - } - - const sqlResult = sql.match(sqlResultG); - - if (sqlResult && sqlResult.length > 0) { - const arr = []; - - sqlResult.forEach((result) => { - const eachRow = result.match(sqlRowG); - - if (eachRow) { - const theRow = []; - - eachRow.forEach((row) => { - const perField = row.match(sqlRow); - - if (perField && perField.length > 2) { - theRow.push({ desc: perField[1], value: perField[2] }); - } - }); - - arr.push(theRow); - } - }); - - rs.result = arr; - } - ResultArr.push(rs); - }); - } - return ResultArr; -}; - function returnTransports(transportOptions) { // eslint-disable-next-line global-require const { Connection } = require('./Connection'); @@ -259,4 +57,4 @@ function returnTransportsDeprecated(options) { return transports; } -module.exports = { xmlToJson, returnTransports, returnTransportsDeprecated }; +module.exports = { returnTransports, returnTransportsDeprecated };