composite-xform.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const BaseXform = require('./base-xform');
  2. /* 'virtual' methods used as a form of documentation */
  3. /* eslint-disable class-methods-use-this */
  4. // base class for xforms that are composed of other xforms
  5. // offers some default implementations
  6. class CompositeXform extends BaseXform {
  7. createNewModel(node) {
  8. return {};
  9. }
  10. parseOpen(node) {
  11. // Typical pattern for composite xform
  12. this.parser = this.parser || this.map[node.name];
  13. if (this.parser) {
  14. this.parser.parseOpen(node);
  15. return true;
  16. }
  17. if (node.name === this.tag) {
  18. this.model = this.createNewModel(node);
  19. return true;
  20. }
  21. return false;
  22. }
  23. parseText(text) {
  24. // Default implementation. Send text to child parser
  25. if (this.parser) {
  26. this.parser.parseText(text);
  27. }
  28. }
  29. onParserClose(name, parser) {
  30. // parseClose has seen a child parser close
  31. // now need to incorporate into this.model somehow
  32. this.model[name] = parser.model;
  33. }
  34. parseClose(name) {
  35. // Default implementation
  36. if (this.parser) {
  37. if (!this.parser.parseClose(name)) {
  38. this.onParserClose(name, this.parser);
  39. this.parser = undefined;
  40. }
  41. return true;
  42. }
  43. return name !== this.tag;
  44. }
  45. }
  46. module.exports = CompositeXform;