diff --git a/client/lib/app/util/parse-file-type.js b/client/lib/app/util/parse-file-type.js index 2337d0fe91..9955f0bd76 100644 --- a/client/lib/app/util/parse-file-type.js +++ b/client/lib/app/util/parse-file-type.js @@ -10,13 +10,47 @@ var TYPES = { cmmn: 'http://www.omg.org/spec/CMMN' }; +var defintionsNoPrefix = /[<\s]+(?:Definitions|definitions)\s+/, + namespaceNoPrefix = /xmlns="([^"]*)"/, + defintionsPrefix = /[<\s]+(.*):(?:Definitions|definitions)\s+/; + module.exports = function parseType(file) { - var type = null; + var contents = file.contents, + type = null, + matches, + matchedNamespace, + namespacePrefix; + + // no prefix e.g. + if (defintionsNoPrefix.test(contents)) { + matches = contents.match(namespaceNoPrefix); + + if (matches && matches[1]) { + matchedNamespace = matches[1]; + } + } + + // prefix e.g. + if (defintionsPrefix.test(contents)) { + matches = contents.match(defintionsPrefix); + + if (matches && matches[1]) { + + // dynamically create regex based on matched prefix + namespacePrefix = new RegExp(`xmlns:${ matches[1] }="([^"]*)"`); + + matches = contents.match(namespacePrefix); + + if (matches && matches[1]) { + matchedNamespace = matches[1]; + } + } + } - forEach(TYPES, function(ns, t) { - if (file.contents.indexOf(ns) !== -1) { + forEach(TYPES, function(uri, t) { + if (matchedNamespace && matchedNamespace.indexOf(uri) !== -1) { type = t; } }); diff --git a/client/test/fixtures/basic.cmmn11.xml b/client/test/fixtures/basic.cmmn11.xml new file mode 100644 index 0000000000..b739fad2aa --- /dev/null +++ b/client/test/fixtures/basic.cmmn11.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/client/test/fixtures/bpmnWithDmnUri.xml b/client/test/fixtures/bpmnWithDmnUri.xml new file mode 100644 index 0000000000..7dcb626a29 --- /dev/null +++ b/client/test/fixtures/bpmnWithDmnUri.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/client/test/spec/util/parse-file-type-spec.js b/client/test/spec/util/parse-file-type-spec.js index 461c2e6c9d..cff567a929 100644 --- a/client/test/spec/util/parse-file-type-spec.js +++ b/client/test/spec/util/parse-file-type-spec.js @@ -5,6 +5,8 @@ var parseFileType = require('app/util/parse-file-type'); var files = { bpmn: require('test/fixtures/basic.bpmn20.xml'), + bpmnWithDmnUri: require('test/fixtures/bpmnWithDmnUri.xml'), + cmmn: require('test/fixtures/basic.cmmn11.xml'), dmn: require('test/fixtures/basic.dmn11.xml'), nyan: require('test/fixtures/nyan_cat.png'), random: require('test/fixtures/random.xml') @@ -15,7 +17,7 @@ function getFile(type) { } -describe('util - parse file type', function() { +describe.only('util - parse file type', function() { it('should identify bpmn file', function() { // given @@ -29,6 +31,30 @@ describe('util - parse file type', function() { }); + it('should identify bpmn file regardless of dmn uri', function() { + // given + var bpmnFile = getFile('bpmnWithDmnUri'); + + // when + var notation = parseFileType({ contents: bpmnFile }); + + // then + expect(notation).to.equal('bpmn'); + }); + + + it('should identify cmmn file', function() { + // given + var dmnFile = getFile('cmmn'); + + // when + var notation = parseFileType({ contents: dmnFile }); + + // then + expect(notation).to.equal('cmmn'); + }); + + it('should identify dmn file', function() { // given var dmnFile = getFile('dmn');