summaryrefslogtreecommitdiffstats
path: root/mailnews/base/util/JXON.js
blob: 509c9b5d893204d638a9b401b3ddb90d234c9229 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// This is a modification of the JXON parsers found on the page
// <https://developer.mozilla.org/en-US/docs/JXON>

var EXPORTED_SYMBOLS = ["JXON"];

var JXON = new (function() {
  const sValueProp = "value"; /* you can customize these values */
  const sAttributesProp = "attr";
  const sAttrPref = "@";
  const sElementListPrefix = "$";
  const sConflictSuffix = "_"; // used when there's a name conflict with special JXON properties
  const aCache = [];
  const rIsNull = /^\s*$/;
  const rIsBool = /^(?:true|false)$/i;

  function parseText(sValue) {
    //if (rIsNull.test(sValue))
    //  return null;
    if (rIsBool.test(sValue)) 
      return sValue.toLowerCase() === "true";
    if (isFinite(sValue))
      return parseFloat(sValue);
    if (isFinite(Date.parse(sValue)))
      return new Date(sValue);
    return sValue;
  };

  function EmptyTree() {
  }
  EmptyTree.prototype = {
    toString : function () {
      return "null";
    },
    valueOf : function () {
      return null;
    },
  };

  function objectify(vValue) {
    if (vValue === null)
      return new EmptyTree();
    else if (vValue instanceof Object)
      return vValue;
    else
      return new vValue.constructor(vValue); // What does this? copy?
  };

  function createObjTree(oParentNode, nVerb, bFreeze, bNesteAttr) {
    const nLevelStart = aCache.length;
    const bChildren = oParentNode.hasChildNodes();
    const bAttributes = oParentNode.attributes &&
                        oParentNode.attributes.length;
    const bHighVerb = Boolean(nVerb & 2);

    var sProp = 0;
    var vContent = 0;
    var nLength = 0;
    var sCollectedTxt = "";
    var vResult = bHighVerb ? {} : /* put here the default value for empty nodes: */ true;

    if (bChildren) {
      for (var oNode, nItem = 0; nItem < oParentNode.childNodes.length; nItem++) {
        oNode = oParentNode.childNodes.item(nItem);
        if (oNode.nodeType === 4) // CDATASection
          sCollectedTxt += oNode.nodeValue; 
        else if (oNode.nodeType === 3) // Text
          sCollectedTxt += oNode.nodeValue;
        else if (oNode.nodeType === 1) // Element
          aCache.push(oNode);
      }
    }

    const nLevelEnd = aCache.length;
    const vBuiltVal = parseText(sCollectedTxt);

    if (!bHighVerb && (bChildren || bAttributes))
      vResult = nVerb === 0 ? objectify(vBuiltVal) : {};

    for (var nElId = nLevelStart; nElId < nLevelEnd; nElId++) {
      sProp = aCache[nElId].nodeName;
      if (sProp == sValueProp || sProp == sAttributesProp)
        sProp = sProp + sConflictSuffix;
      vContent = createObjTree(aCache[nElId], nVerb, bFreeze, bNesteAttr);
      if (!vResult.hasOwnProperty(sProp)) {
        vResult[sProp] = vContent;
        vResult[sElementListPrefix + sProp] = [];
      }
      vResult[sElementListPrefix + sProp].push(vContent);
      nLength++;
    }

    if (bAttributes) {
      const nAttrLen = oParentNode.attributes.length;
      const sAPrefix = bNesteAttr ? "" : sAttrPref;
      const oAttrParent = bNesteAttr ? {} : vResult;

      for (var oAttrib, nAttrib = 0; nAttrib < nAttrLen; nLength++, nAttrib++) {
        oAttrib = oParentNode.attributes.item(nAttrib);
        oAttrParent[sAPrefix + oAttrib.name] = parseText(oAttrib.value);
      }

      if (bNesteAttr) {
        if (bFreeze)
          Object.freeze(oAttrParent);
        vResult[sAttributesProp] = oAttrParent;
        nLength -= nAttrLen - 1;
      }
    }

    if (nVerb === 3 || (nVerb === 2 || nVerb === 1 && nLength > 0) && sCollectedTxt)
      vResult[sValueProp] = vBuiltVal;
    else if (!bHighVerb && nLength === 0 && sCollectedTxt)
      vResult = vBuiltVal;

    if (bFreeze && (bHighVerb || nLength > 0))
      Object.freeze(vResult);

    aCache.length = nLevelStart;

    return vResult;
  };

  function loadObjTree(oXMLDoc, oParentEl, oParentObj) {
    var vValue, oChild;

    if (oParentObj instanceof String || oParentObj instanceof Number ||
        oParentObj instanceof Boolean)
      oParentEl.appendChild(oXMLDoc.createTextNode(oParentObj.toString())); /* verbosity level is 0 */
    else if (oParentObj.constructor === Date)
      oParentEl.appendChild(oXMLDoc.createTextNode(oParentObj.toGMTString()));

    for (var sName in oParentObj) {
      vValue = oParentObj[sName];
      if (isFinite(sName) || vValue instanceof Function)
        continue; /* verbosity level is 0 */
      if (sName === sValueProp) {
        if (vValue !== null && vValue !== true) {
          oParentEl.appendChild(oXMLDoc.createTextNode(
              vValue.constructor === Date ? vValue.toGMTString() : String(vValue)));
        }
      } else if (sName === sAttributesProp) { /* verbosity level is 3 */
        for (var sAttrib in vValue)
          oParentEl.setAttribute(sAttrib, vValue[sAttrib]);
      } else if (sName.charAt(0) === sAttrPref) {
        oParentEl.setAttribute(sName.slice(1), vValue);
      } else if (vValue.constructor === Array) {
        for (var nItem = 0; nItem < vValue.length; nItem++) {
          oChild = oXMLDoc.createElement(sName);
          loadObjTree(oXMLDoc, oChild, vValue[nItem]);
          oParentEl.appendChild(oChild);
        }
      } else {
        oChild = oXMLDoc.createElement(sName);
        if (vValue instanceof Object)
          loadObjTree(oXMLDoc, oChild, vValue);
        else if (vValue !== null && vValue !== true)
          oChild.appendChild(oXMLDoc.createTextNode(vValue.toString()));
        oParentEl.appendChild(oChild);
     }
   }
  };

  this.build = function(oXMLParent, nVerbosity /* optional */, bFreeze /* optional */, bNesteAttributes /* optional */) {
    const _nVerb = arguments.length > 1 &&
        typeof nVerbosity === "number" ? nVerbosity & 3 :
        /* put here the default verbosity level: */ 1;
    return createObjTree(oXMLParent, _nVerb, bFreeze || false,
        arguments.length > 3 ? bNesteAttributes : _nVerb === 3);
  };

  this.unbuild = function(oObjTree) {
    const oNewDoc = document.implementation.createDocument("", "", null);
    loadObjTree(oNewDoc, oNewDoc, oObjTree);
    return oNewDoc;
  };
})();