Permalink
Browse files

Facilitates tag and attribute name processing

resolves #115
resolves #114
  • Loading branch information...
1 parent 23b4776 commit a4b920512b3b4804601d2c548c7310081b14ca76 @creynders creynders committed Dec 13, 2013
Showing with 77 additions and 9 deletions.
  1. +30 −6 lib/xml2js.js
  2. +19 −3 src/xml2js.coffee
  3. +2 −0 test/fixtures/sample.xml
  4. +26 −0 test/parser.test.coffee
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -7,6 +7,13 @@ bom = require './bom'
isEmpty = (thing) ->
return typeof thing is "object" && thing? && Object.keys(thing).length is 0
+normalizeTag = (name) ->
+ return name.toLowerCase()
+
+processName = (processors, processedName) ->
+ processedName = process(processedName) for process in processors
+ return processedName
+
exports.defaults =
"0.1":
explicitCharkey: false
@@ -36,6 +43,8 @@ exports.defaults =
# callbacks are async? not in 0.1 mode
async: false
strict: true
+ attrNameProcessor: null
+ tagNameProcessor: null
"0.2":
explicitCharkey: false
@@ -56,6 +65,8 @@ exports.defaults =
# not async in 0.2 mode either
async: false
strict: true
+ attrNameProcessor: null
+ tagNameProcessor: null
# xml building options
rootName: 'root'
xmldec: {'version': '1.0', 'encoding': 'UTF-8', 'standalone': true}
@@ -139,6 +150,10 @@ class exports.Parser extends events.EventEmitter
# define the key used for namespaces
if @options.xmlns
@options.xmlnskey = @options.attrkey + "ns"
+ if @options.normalizeTags
+ if ! @options.tagNameProcessor
+ @options.tagNameProcessor = []
+ @options.tagNameProcessor.push normalizeTag
@reset()
@@ -190,13 +205,14 @@ class exports.Parser extends events.EventEmitter
if attrkey not of obj and not @options.mergeAttrs
obj[attrkey] = {}
newValue = node.attributes[key]
+ processedKey = if @options.attrNameProcessor then processName(@options.attrNameProcessor, key) else key
if @options.mergeAttrs
- @assignOrPush obj, key, newValue
+ @assignOrPush obj, processedKey, newValue
else
- obj[attrkey][key] = newValue
+ obj[attrkey][processedKey] = newValue
# need a place to store the node name
- obj["#name"] = if @options.normalizeTags then node.name.toLowerCase() else node.name
+ obj["#name"] = if @options.tagNameProcessor then processName(@options.tagNameProcessor, node.name) else node.name
if (@options.xmlns)
obj[@options.xmlnskey] = {uri: node.uri, local: node.local}
stack.push obj
View
@@ -50,4 +50,6 @@
<pfx:top xmlns:pfx="http://foo.com" pfx:attr="baz">
<middle xmlns="http://bar.com"/>
</pfx:top>
+ <attrNameProcessTest camelCaseAttr="camelCaseAttrValue" lowercaseattr="lowercaseattr" />
+ <tagNameProcessTest/>
</sample>
View
@@ -23,6 +23,12 @@ skeleton = (options, checks) ->
else
x2js.parseString xmlString
+nameToUpperCase = (name) ->
+ return name.toUpperCase()
+
+nameCutoff = (name) ->
+ return name.substr(0, 4)
+
###
The `validator` function validates the value at the XPath. It also transforms the value
if necessary to conform to the schema or other validation information being used. If there
@@ -340,3 +346,23 @@ module.exports =
equ err, null
assert.deepEqual resWithNew, resWithoutNew
test.done()
+
+ 'test single attrNameProcessor': skeleton(attrNameProcessor: [nameToUpperCase], (r)->
+ console.log 'Result object: ' + util.inspect r, false, 10
+ equ r.sample.attrNameProcessTest[0].$.hasOwnProperty('CAMELCASEATTR'), true
+ equ r.sample.attrNameProcessTest[0].$.hasOwnProperty('LOWERCASEATTR'), true)
+
+ 'test multiple attrNameProcessor': skeleton(attrNameProcessor: [nameToUpperCase, nameCutoff], (r)->
+ console.log 'Result object: ' + util.inspect r, false, 10
+ equ r.sample.attrNameProcessTest[0].$.hasOwnProperty('CAME'), true
+ equ r.sample.attrNameProcessTest[0].$.hasOwnProperty('LOWE'), true)
+
+ 'test single tagNameProcessor': skeleton(tagNameProcessor: [nameToUpperCase], (r)->
+ console.log 'Result object: ' + util.inspect r, false, 10
+ equ r.hasOwnProperty('SAMPLE'), true
+ equ r.SAMPLE.hasOwnProperty('TAGNAMEPROCESSTEST'), true)
+
+ 'test multiple tagNameProcessor': skeleton(tagNameProcessor: [nameToUpperCase, nameCutoff], (r)->
+ console.log 'Result object: ' + util.inspect r, false, 10
+ equ r.hasOwnProperty('SAMP'), true
+ equ r.SAMP.hasOwnProperty('TAGN'), true)

0 comments on commit a4b9205

Please sign in to comment.