bpmn-moddle.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. isString,
  3. isFunction,
  4. assign
  5. } from 'min-dash';
  6. import Moddle from 'moddle';
  7. import {
  8. Reader,
  9. Writer
  10. } from 'moddle-xml';
  11. /**
  12. * A sub class of {@link Moddle} with support for import and export of BPMN 2.0 xml files.
  13. *
  14. * @class BpmnModdle
  15. * @extends Moddle
  16. *
  17. * @param {Object|Array} packages to use for instantiating the model
  18. * @param {Object} [options] additional options to pass over
  19. */
  20. export default function BpmnModdle(packages, options) {
  21. Moddle.call(this, packages, options);
  22. }
  23. BpmnModdle.prototype = Object.create(Moddle.prototype);
  24. /**
  25. * Instantiates a BPMN model tree from a given xml string.
  26. *
  27. * @param {String} xmlStr
  28. * @param {String} [typeName='bpmn:Definitions'] name of the root element
  29. * @param {Object} [options] options to pass to the underlying reader
  30. * @param {Function} done callback that is invoked with (err, result, parseContext)
  31. * once the import completes
  32. */
  33. BpmnModdle.prototype.fromXML = function(xmlStr, typeName, options, done) {
  34. if (!isString(typeName)) {
  35. done = options;
  36. options = typeName;
  37. typeName = 'bpmn:Definitions';
  38. }
  39. if (isFunction(options)) {
  40. done = options;
  41. options = {};
  42. }
  43. var reader = new Reader(assign({ model: this, lax: true }, options));
  44. var rootHandler = reader.handler(typeName);
  45. reader.fromXML(xmlStr, rootHandler, done);
  46. };
  47. /**
  48. * Serializes a BPMN 2.0 object tree to XML.
  49. *
  50. * @param {String} element the root element, typically an instance of `bpmn:Definitions`
  51. * @param {Object} [options] to pass to the underlying writer
  52. * @param {Function} done callback invoked with (err, xmlStr) once the import completes
  53. */
  54. BpmnModdle.prototype.toXML = function(element, options, done) {
  55. if (isFunction(options)) {
  56. done = options;
  57. options = {};
  58. }
  59. var writer = new Writer(options);
  60. var result;
  61. var err;
  62. try {
  63. result = writer.toXML(element);
  64. } catch (e) {
  65. err = e;
  66. }
  67. return done(err, result);
  68. };