-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use knowledge of the form structure to better handle selects
Introduces util/xform.js to centralize parsing of XForms. Fixes #96
- Loading branch information
Showing
5 changed files
with
194 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const async = require('async'); | ||
const { parseString } = require('xml2js'); | ||
const xpath = require('xml2js-xpath'); | ||
|
||
const settings = require('../settings'); | ||
|
||
const FORMS_DIR = path.join(settings.dataDir, 'forms/'); | ||
|
||
const loadXForm = (xformPath, callback) => { | ||
return async.waterfall([ | ||
async.apply(fs.readFile, xformPath), | ||
parseString | ||
], (err, form) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
try { | ||
const instanceName = path.basename(xformPath, path.extname(xformPath)); | ||
const fieldNames = Object | ||
.keys(xpath.evalFirst(form, `//model/instance/${instanceName}`)) | ||
.filter(x => x !== '$'); | ||
|
||
const fields = fieldNames.reduce((obj, k) => { | ||
const node = xpath.evalFirst(form, `//model/bind[@nodeset='/${instanceName}/${k}']`); | ||
|
||
if (node != null) { | ||
obj[k] = node.$.type; | ||
} | ||
|
||
return obj; | ||
}, {}); | ||
|
||
return callback(null, { | ||
path: xformPath, | ||
fields, | ||
filename: path.basename(xformPath), | ||
form, | ||
id: xpath.evalFirst(form, '//model/instance/@id').$.id, | ||
instanceName, | ||
title: xpath.evalFirst(form, '//h:title') | ||
}); | ||
} catch (err) { | ||
return callback(new Error(`Failed while parsing ${xformPath}: ${err.message}`)); | ||
} | ||
}); | ||
}; | ||
|
||
const getForms = (callback) => { | ||
return fs.readdir(FORMS_DIR, (err, forms) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
return async.waterfall([ | ||
async.apply(async.map, | ||
forms | ||
.filter(x => x.match(/\.xml$/i)) | ||
.map(x => path.join(FORMS_DIR, x)), | ||
/* eslint handle-callback-err: 0 */ | ||
(filename, callback) => | ||
loadXForm(filename, (err, form) => | ||
// ignore errors | ||
callback(null, form))), | ||
(forms, callback) => callback(null, forms.filter(x => x != null)) | ||
], callback); | ||
}); | ||
}; | ||
|
||
const getFormMetadata = (formName, callback) => { | ||
return getForms((err, forms) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
const meta = forms.filter(x => x.id === formName).pop(); | ||
|
||
return callback(null, meta); | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
getFormMetadata, | ||
getForms, | ||
loadXForm | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1350,6 +1350,10 @@ lodash.uniq@^4.3.0: | |
version "4.5.0" | ||
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773" | ||
|
||
[email protected]: | ||
version "4.17.2" | ||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.2.tgz#34a3055babe04ce42467b607d700072c7ff6bf42" | ||
|
||
lodash@^3.0.1, lodash@^3.5.0: | ||
version "3.10.1" | ||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6" | ||
|
@@ -2349,6 +2353,12 @@ xform-to-json@^1.2.1: | |
uuid "^2.0.1" | ||
xml2js "^0.4.4" | ||
|
||
xml2js-xpath@^0.8.0: | ||
version "0.8.0" | ||
resolved "https://registry.yarnpkg.com/xml2js-xpath/-/xml2js-xpath-0.8.0.tgz#4d3079a059de7b19fd1b4facdefab3757db45611" | ||
dependencies: | ||
lodash "4.17.2" | ||
|
||
xml2js@^0.4.16, xml2js@^0.4.4: | ||
version "0.4.17" | ||
resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.4.17.tgz#17be93eaae3f3b779359c795b419705a8817e868" | ||
|