all files / model/ XMLExporter.js

25.93% Statements 7/27
5.88% Branches 1/17
25% Functions 2/8
26.92% Lines 7/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70                      12×                         12×     12×     12×                                                                        
import { forEach, isBoolean, isNumber, isString } from '../util'
import { DefaultDOMElement } from '../dom'
import DOMExporter from './DOMExporter'
 
/*
  Base class for custom XML exporters. If you want to use HTML as your
  exchange format see {@link model/HTMLExporter}.
*/
class XMLExporter extends DOMExporter {
 
  constructor(config, context) {
    super(_defaultConfig(config), context)
  }
 
  getDefaultBlockConverter() {
    return defaultBlockConverter // eslint-disable-line no-use-before-define
  }
 
  getDefaultPropertyAnnotationConverter() {
    return defaultAnnotationConverter // eslint-disable-line no-use-before-define
  }
 
}
 
function _defaultConfig(config) {
  config = Object.assign({
    idAttribute: 'id'
  }, config)
  Iif (!config.elementFactory) {
    config.elementFactory = DefaultDOMElement.createDocument('xml')
  }
  return config
}
 
const defaultAnnotationConverter = {
  tagName: 'annotation',
  export: function(node, el) {
    el.attr('type', node.type)
    const properties = node.toJSON()
    forEach(properties, function(value, name) {
      if (name === 'id' || name === 'type') return
      if (isString(value) || isNumber(value) || isBoolean(value)) {
        el.attr(name, value)
      }
    })
  }
}
 
const defaultBlockConverter = {
  tagName: 'block',
  export: function(node, el, converter) {
    el.attr('type', node.type)
    const properties = node.toJSON()
    forEach(properties, function(value, name) {
      if (name === 'id' || name === 'type') {
        return
      }
      const prop = converter.$$(name)
      if (node.getPropertyType(name) === 'string') {
        prop.append(converter.annotatedText([node.id, name]))
      } else {
        prop.text(value)
      }
      el.append(prop)
    })
  }
}
 
export default XMLExporter