ludc
2023-08-24 56c45e1f4be85d6bbfb3a03437021c6742b32ad9
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/////////////////////////////////////////////////////////////////////////////////////////////////////
var XmlNode = (function () {
  function XmlNode(tagName, attributes, children) {
 
    if (!(this instanceof XmlNode)) {
      return new XmlNode(tagName, attributes, children);
    }
    this.tagName = tagName;
    this._attributes = attributes || {};
    this._children = children || [];
    this._prefix = '';
    return this;
  }
 
  XmlNode.prototype.createElement = function () {
    return new XmlNode(arguments)
  }
 
  XmlNode.prototype.children = function() {
    return this._children;
  }
 
  XmlNode.prototype.append = function (node) {
    this._children.push(node);
    return this;
  }
 
  XmlNode.prototype.prefix = function (prefix) {
    if (arguments.length==0) { return this._prefix;}
    this._prefix = prefix;
    return this;
  }
 
  XmlNode.prototype.attr = function (attr, value) {
    if (value == undefined) {
      delete this._attributes[attr];
      return this;
    }
    if (arguments.length == 0) {
      return this._attributes;
    }
    else if (typeof attr == 'string' && arguments.length == 1) {
      return this._attributes.attr[attr];
    }
    if (typeof attr == 'object' && arguments.length == 1) {
      for (var key in attr) {
        this._attributes[key] = attr[key];
      }
    }
    else if (arguments.length == 2 && typeof attr == 'string') {
      this._attributes[attr] = value;
    }
    return this;
  }
 
  var APOS = "'"; QUOTE = '"'
  var ESCAPED_QUOTE = {  }
  ESCAPED_QUOTE[QUOTE] = '"'
  ESCAPED_QUOTE[APOS] = '''
 
  XmlNode.prototype.escapeAttributeValue = function(att_value) {
    return '"' + att_value.replace(/\"/g,'"') + '"';// TODO Extend with four other codes
 
  }
 
  XmlNode.prototype.toXml = function (node) {
    if (!node) node = this;
    var xml = node._prefix;
    xml += '<' + node.tagName;
    if (node._attributes) {
      for (var key in node._attributes) {
        xml += ' ' + key + '=' + this.escapeAttributeValue(''+node._attributes[key]) + ''
      }
    }
    if (node._children && node._children.length > 0) {
      xml += ">";
      for (var i = 0; i < node._children.length; i++) {
        xml += this.toXml(node._children[i]);
      }
      xml += '</' + node.tagName + '>';
    }
    else {
      xml += '/>';
    }
    return xml;
  }
  return XmlNode;
})();