bpmn-moddle 7.0.3 → 7.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,86 +0,0 @@
1
- const fs = require('fs');
2
-
3
- const parseFile = require('cmof-parser');
4
-
5
- const {
6
- transformDC,
7
- transformDI,
8
- transformBPMN,
9
- transformBPMNDI
10
- } = require('./transforms');
11
-
12
- async function generateSchema(files) {
13
- files.forEach(async file => {
14
- const {
15
- options,
16
- source,
17
- target,
18
- transform
19
- } = file;
20
-
21
- const parsed = await parseFile(fs.readFileSync(source, 'utf8'), options);
22
-
23
- const transformed = await transform(parsed);
24
-
25
- fs.writeFileSync(target, JSON.stringify(transformed, null, 2));
26
- });
27
- }
28
-
29
- generateSchema([
30
- {
31
- source: 'resources/bpmn/cmof/BPMN20.cmof',
32
- target: 'resources/bpmn/json/bpmn.json',
33
- transform: transformBPMN,
34
- options: {
35
- clean: true,
36
- prefixNamespaces: {
37
- 'BPMN20.cmof': 'bpmn',
38
- 'BPMNDI.cmof': 'bpmndi',
39
- 'DC.cmof': 'dc',
40
- 'DI.cmof': 'di'
41
- }
42
- }
43
- },
44
- {
45
- source: 'resources/bpmn/cmof/BPMNDI.cmof',
46
- target: 'resources/bpmn/json/bpmndi.json',
47
- transform: transformBPMNDI,
48
- options: {
49
- clean: true,
50
- prefixNamespaces: {
51
- 'BPMN20.cmof': 'bpmn',
52
- 'BPMNDI.cmof': 'bpmndi',
53
- 'DC.cmof': 'dc',
54
- 'DI.cmof': 'di'
55
- }
56
- }
57
- },
58
- {
59
- source: 'resources/bpmn/cmof/DC.cmof',
60
- target: 'resources/bpmn/json/dc.json',
61
- transform: transformDC,
62
- options: {
63
- clean: true,
64
- prefixNamespaces: {
65
- 'BPMN20.cmof': 'bpmn',
66
- 'BPMNDI.cmof': 'bpmndi',
67
- 'DC.cmof': 'dc',
68
- 'DI.cmof': 'di'
69
- }
70
- }
71
- },
72
- {
73
- source: 'resources/bpmn/cmof/DI.cmof',
74
- target: 'resources/bpmn/json/di.json',
75
- transform: transformDI,
76
- options: {
77
- clean: true,
78
- prefixNamespaces: {
79
- 'BPMN20.cmof': 'bpmn',
80
- 'BPMNDI.cmof': 'bpmndi',
81
- 'DC.cmof': 'dc',
82
- 'DI.cmof': 'di'
83
- }
84
- }
85
- }
86
- ]);
@@ -1,513 +0,0 @@
1
- 'use strict';
2
-
3
- const {
4
- isArray,
5
- isObject,
6
- matchPattern
7
- } = require('min-dash');
8
-
9
- const sax = require('sax');
10
-
11
- const Stack = require('tiny-stack');
12
-
13
- /**
14
- * Recursively find children in tree that match pattern.
15
- *
16
- * @param {Object} parent
17
- * @param {Function} pattern
18
- *
19
- * @returns {Array|null}
20
- */
21
- function findChildren(parent, pattern) {
22
- const { children } = parent;
23
-
24
- if (!children) {
25
- return null;
26
- }
27
-
28
- const result = children.reduce((result, child) => {
29
- if (pattern(child)) {
30
- result = [
31
- ...result,
32
- child
33
- ];
34
- }
35
-
36
- if (child.children) {
37
- result = [
38
- ...result,
39
- ...(findChildren(child, pattern) || [])
40
- ];
41
- }
42
-
43
- return result;
44
- }, []);
45
-
46
- if (!result.length) {
47
- return null;
48
- }
49
-
50
- return result;
51
- }
52
-
53
- module.exports.findChildren = findChildren;
54
-
55
- /**
56
- * Find type with specified name in schema.
57
- *
58
- * @param {String} type
59
- * @param {Object} schema
60
- *
61
- * @returns {Object}
62
- */
63
- function findType(type, schema) {
64
- return schema.types.find(({ name }) => name === type);
65
- }
66
-
67
- module.exports.findType = findType;
68
-
69
- /**
70
- * Find property with specified name in schema.
71
- *
72
- * @param {String} typeProperty
73
- * @param {Object} schema
74
- *
75
- * @returns {Object}
76
- */
77
- function findProperty(typeProperty, schema) {
78
- const [ type, property ] = typeProperty.split('#');
79
-
80
- return findType(type, schema).properties.find(({ name }) => name === property);
81
- }
82
-
83
- module.exports.findProperty = findProperty;
84
-
85
-
86
- /**
87
- * Fix order of properties according to <sequence> indicators specified in XSD.
88
- *
89
- * Example:
90
- *
91
- * <xsd:complexType name="tDecisionRule">
92
- * <xsd:complexContent>
93
- * <xsd:extension base="tDMNElement">
94
- * <xsd:sequence>
95
- * <xsd:element name="inputEntry" />
96
- * <xsd:element name="outputEntry" />
97
- * ...
98
- * </xsd:sequence>
99
- * </xsd:extension>
100
- * </xsd:complexContent>
101
- * </xsd:complexType>
102
- *
103
- * specifies that inputEntry elements must appear before outputEntry elements.
104
- *
105
- * @param {Object} model
106
- * @param {Object} parsedXSD
107
- *
108
- * @returns {Object}
109
- */
110
- function fixSequence(model, parsedXSD) {
111
- const { elementsByTagName } = parsedXSD,
112
- xsdSchema = elementsByTagName[ 'xsd:schema' ][ 0 ];
113
-
114
- const xsdComplexTypes = findChildren(xsdSchema, matchPattern({ tagName: 'xsd:complexType' }));
115
-
116
- if (!xsdComplexTypes) {
117
- return;
118
- }
119
-
120
- xsdComplexTypes.forEach(xsdComplexType => {
121
-
122
- // (1) find sequences
123
- const xsdSequences = findChildren(xsdComplexType, matchPattern({ tagName: 'xsd:sequence' }));
124
-
125
- if (!xsdSequences) {
126
- return;
127
- }
128
-
129
- const xsdSequence = xsdSequences[ 0 ];
130
-
131
- // (2) find elements in sequence
132
- const xsdElements = findChildren(xsdSequence, matchPattern({ tagName: 'xsd:element' }));
133
-
134
- if (!xsdElements) {
135
- return;
136
- }
137
-
138
- // (3) find corresponding type
139
- const type = model.types.find(matchPattern({ name: xsdComplexType.name.slice(1) }));
140
-
141
- if (!type) {
142
- return;
143
- }
144
-
145
- // (4) fix sequence of type properties
146
- type.properties = type.properties
147
- .map(property => {
148
-
149
- // (4.1) find corresponding element
150
- const xsdElement = xsdElements.find(({ name, ref }) => name === property.name || ref === property.name);
151
-
152
- if (xsdElement) {
153
-
154
- // (4.1.1) return index of found corresponding element
155
- return {
156
- index: xsdElements.indexOf(xsdElement),
157
- name: property.name
158
- };
159
- } else {
160
-
161
- // (4.1.2) find substitute
162
- const matcher = matchPattern({
163
- tagName: 'xsd:element',
164
- type: `t${ property.type }`
165
- });
166
-
167
- const substitutes = findChildren(xsdSchema, child => {
168
- return matcher(child) && child.substitutionGroup;
169
- });
170
-
171
- if (substitutes) {
172
- const substitute = substitutes[ 0 ];
173
-
174
- // (4.1.3) find substitutable
175
- const substitutable = xsdElements.find(matchPattern({ ref: substitute.substitutionGroup }));
176
-
177
- if (substitutable) {
178
- const index = xsdElements.indexOf(substitutable);
179
-
180
- // (4.1.4) return index of found substitutable
181
- return {
182
- substituteFor: substitutable.ref,
183
- index,
184
- name: property.name
185
- };
186
- }
187
- }
188
- }
189
-
190
- // (4.2) return maximum index if index not specified in sequence
191
- return {
192
- index: type.properties.length - 1,
193
- name: property.name
194
- };
195
- })
196
- .sort((a, b) => {
197
- return a.index - b.index;
198
- })
199
- .map(({ name }) => {
200
- return type.properties.find(matchPattern({ name }));
201
- });
202
- });
203
-
204
- return model;
205
- }
206
-
207
- module.exports.fixSequence = fixSequence;
208
-
209
- /**
210
- * Order properties of type.
211
- *
212
- * @param {string} typeName
213
- * @param {Array<string>} propertiesOrder
214
- * @param {Object} schema
215
- *
216
- * @returns {Object}
217
- */
218
- function orderProperties(typeName, propertiesOrder, schema) {
219
- const type = findType(typeName, schema);
220
-
221
- const { properties } = type;
222
-
223
- propertiesOrder.reverse().forEach(propertyName => {
224
- const index = properties.indexOf(
225
- properties.find(matchPattern({ name: propertyName }))
226
- );
227
-
228
- properties.unshift(properties.splice(index, 1).pop());
229
- });
230
-
231
- return schema;
232
- }
233
-
234
- module.exports.orderProperties = orderProperties;
235
-
236
- /**
237
- * Parse XML tag.
238
- *
239
- * @param {Object} tag
240
- * @param {Object} parent
241
- * @param {Object} context
242
- * @param {Object} context.elementsByName
243
- * @param {Object} context.elementsByTagName
244
- *
245
- * @returns {Object}
246
- */
247
- function parseTag(tag, parent, context) {
248
- const {
249
- attributes,
250
- name: tagName
251
- } = tag;
252
-
253
- const { name } = attributes;
254
-
255
- const {
256
- elementsByName,
257
- elementsByTagName
258
- } = context;
259
-
260
- const element = {
261
- tagName
262
- };
263
-
264
- Object.entries(attributes).forEach(([ key, value ]) => {
265
- if (value === 'true') {
266
- value = true;
267
- }
268
-
269
- if (value === 'false') {
270
- value = false;
271
- }
272
-
273
- element[ key ] = value;
274
- });
275
-
276
- if (name) {
277
- elementsByName[ name ] = element;
278
- }
279
-
280
- if (!elementsByTagName[ tagName ]) {
281
- elementsByTagName[ tagName ] = [];
282
- }
283
-
284
- elementsByTagName[ tagName ].push(element);
285
-
286
- if (parent) {
287
- if (!parent.children) {
288
- parent.children = [];
289
- }
290
-
291
- parent.children.push(element);
292
- }
293
-
294
- return element;
295
- }
296
-
297
- async function parseXML(file) {
298
- return new Promise((resolve, reject) => {
299
- const elementsByName = {},
300
- elementsByTagName = {};
301
-
302
- const context = {
303
- elementsByName,
304
- elementsByTagName
305
- };
306
-
307
- const stack = new Stack();
308
-
309
- const saxParser = sax.parser(true);
310
-
311
- saxParser.onerror = function(err) {
312
- console.error('error', err);
313
-
314
- this._parser.error = null;
315
- this._parser.resume();
316
-
317
- reject(err);
318
- };
319
-
320
- saxParser.onopentag = tag => {
321
- const parent = stack.peek();
322
-
323
- stack.push(parseTag(tag, parent, context));
324
- };
325
-
326
- saxParser.onclosetag = name => {
327
- stack.pop();
328
- };
329
-
330
- saxParser.onend = () => {
331
- resolve(context);
332
- };
333
-
334
- saxParser.write(file).close();
335
- });
336
- }
337
-
338
- module.exports.parseXML = parseXML;
339
-
340
- /**
341
- * Remove prefixes from all type names.
342
- *
343
- * @param {Object} model
344
- * @param {Array<string>|string} prefixes
345
- *
346
- * @returns {Object}
347
- */
348
- function removePrefixes(model, prefixes) {
349
- if (prefixes && !isArray(prefixes)) {
350
- prefixes = [ prefixes ];
351
- }
352
-
353
- model.types.forEach(type => {
354
- type.name = withoutPrefixes(type.name, prefixes);
355
-
356
- if (type.superClass) {
357
- type.superClass = type.superClass.map(superClass => {
358
- return withoutPrefixes(superClass, prefixes);
359
- });
360
- }
361
-
362
- if (type.properties) {
363
- type.properties.forEach(property => {
364
- property.type = withoutPrefixes(property.type, prefixes);
365
- });
366
- }
367
- });
368
-
369
- model.enumerations.forEach(enumeration => {
370
- enumeration.name = withoutPrefixes(enumeration.name, prefixes);
371
- });
372
-
373
- return model;
374
- }
375
-
376
- module.exports.removePrefixes = removePrefixes;
377
-
378
- /**
379
- * Remove whitespace from all property names.
380
- *
381
- * @param {Object} model
382
- *
383
- * @returns {Object}
384
- */
385
- function removeWhitespace(model) {
386
- model.types.forEach(({ properties }) => {
387
- if (!properties) {
388
- return;
389
- }
390
-
391
- properties.forEach(property => {
392
- property.name = property.name.replace(/\s/, '');
393
- });
394
- });
395
-
396
- return model;
397
- }
398
-
399
- module.exports.removeWhitespace = removeWhitespace;
400
-
401
- /**
402
- * Replace all occurrences of key in object.
403
- *
404
- * @param {*} key
405
- * @param {*} newKey
406
- * @param {Object} object
407
- */
408
- function replaceKey(key, newKey, object) {
409
-
410
- // (1) iterate object keys
411
- Object.entries(object).forEach(entry => {
412
-
413
- if (isArray[ entry[ 1 ]]) {
414
-
415
- // (2.1) recurse
416
- entry[ 1 ].forEach(item => replaceKey(key, newKey, item));
417
- }
418
-
419
- if (isObject(entry[ 1 ])) {
420
-
421
- // (2.2) recurse
422
- replaceKey(key, newKey, entry[ 1 ]);
423
- }
424
-
425
- if (entry[ 0 ] === key) {
426
-
427
- // (3) replace key
428
- object[ newKey ] = entry[ 1 ];
429
-
430
- delete object[ key ];
431
- }
432
- });
433
- }
434
-
435
- module.exports.replaceKey = replaceKey;
436
-
437
- /**
438
- * Replace all occurrences of value in object.
439
- *
440
- * @param {*} value
441
- * @param {*} newValue
442
- * @param {Object} object
443
- */
444
- function replaceValue(value, newValue, object) {
445
-
446
- // (1) iterate object values
447
- Object.entries(object).forEach(entry => {
448
- if (entry[ 1 ] === value) {
449
-
450
- // (2) replace value
451
- object[ entry[ 0 ] ] = newValue;
452
- } else if (isArray(entry[1])) {
453
-
454
- // (3.1) recurse
455
- entry[ 1 ].forEach(item => replaceValue(value, newValue, item));
456
- } else if (isObject(entry[ 1 ])) {
457
-
458
- // (3.2) recurse
459
- replaceValue(value, newValue, entry[ 1 ]);
460
- }
461
- });
462
- }
463
-
464
- module.exports.replaceValue = replaceValue;
465
-
466
- /**
467
- * Swap properties.
468
- *
469
- * @param {string} typePropertyA
470
- * @param {string} typePropertyB
471
- * @param {Object} schema
472
- *
473
- * @returns {Object}
474
- */
475
- function swapProperties(typePropertyA, typePropertyB, schema) {
476
- const typeA = findType(typePropertyA.split('#').shift(), schema),
477
- typeB = findType(typePropertyB.split('#').shift(), schema);
478
-
479
- typePropertyA = findProperty(typePropertyA, schema);
480
- typePropertyB = findProperty(typePropertyB, schema);
481
-
482
- const indexOfA = typeA.properties.indexOf(typePropertyA),
483
- indexOfB = typeB.properties.indexOf(typePropertyB);
484
-
485
- typeA.properties[ indexOfA ] = typePropertyB;
486
- typeB.properties[ indexOfB ] = typePropertyA;
487
-
488
- return schema;
489
- }
490
-
491
- module.exports.swapProperties = swapProperties;
492
-
493
- /**
494
- * Remove specified prefixes from type.
495
- *
496
- * @param {string} prefixType
497
- * @param {Array<string>} prefixes
498
- *
499
- * @returns {string}
500
- */
501
- function withoutPrefixes(prefixType, prefixes) {
502
- let [ prefix, type ] = prefixType.split(':');
503
-
504
- if (!type) {
505
- return prefix;
506
- }
507
-
508
- if (prefixes && prefixes.includes(prefix)) {
509
- return type;
510
- }
511
-
512
- return prefixType;
513
- }
@@ -1,11 +0,0 @@
1
- const transformDC = require('./transformDC'),
2
- transformDI = require('./transformDI'),
3
- transformBPMN = require('./transformBPMN'),
4
- transformBPMNDI = require('./transformBPMNDI');
5
-
6
- module.exports = {
7
- transformDC,
8
- transformDI,
9
- transformBPMN,
10
- transformBPMNDI
11
- };