|
| 1 | +/* |
| 2 | + * JXON.js |
| 3 | + * Ratatosk |
| 4 | + * |
| 5 | + * Created by Alexander Ljungberg on March 15th, 2012. |
| 6 | + * |
| 7 | + * Public domain JXON implementation from algorithm #3 of: |
| 8 | + * https://developer.mozilla.org/en/Parsing_and_serializing_XML |
| 9 | + * |
| 10 | + * Any copyright is dedicated to the Public Domain. |
| 11 | + * |
| 12 | + * Modified by Andrew (Pat) Patterson, 2015 from original at |
| 13 | + * https://github.com/wireload/Ratatosk/blob/master/jxon.js |
| 14 | + * to preserve case of element and attribute names |
| 15 | + */ |
| 16 | + |
| 17 | +JXON = function() |
| 18 | +{ |
| 19 | + |
| 20 | +}; |
| 21 | + |
| 22 | +JXON.buildValue = function(sValue) |
| 23 | +{ |
| 24 | + if (/^\s*$/.test(sValue)) |
| 25 | + return null; |
| 26 | + if (/^(true|false)$/i.test(sValue)) |
| 27 | + return sValue.toLowerCase() === "true"; |
| 28 | + if (isFinite(sValue)) |
| 29 | + return parseFloat(sValue); |
| 30 | + // Don't do this - it'll parse anything that looks like a date and get the timezone wrong. |
| 31 | + // https://bugzilla.mozilla.org/show_bug.cgi?id=693077 |
| 32 | + //if (isFinite(Date.parse(sValue))) |
| 33 | + // return new Date(sValue); |
| 34 | + return sValue; |
| 35 | +}; |
| 36 | + |
| 37 | +JXON.fromXML = function(xmlString) |
| 38 | +{ |
| 39 | + return JXON.getJXONData((new DOMParser()).parseFromString(xmlString, "text/xml")); |
| 40 | +}; |
| 41 | + |
| 42 | +JXON.getJXONData = function(oXMLParent) |
| 43 | +{ |
| 44 | + var vResult = /* put here the default value for empty nodes! */ null, |
| 45 | + nLength = 0, |
| 46 | + sCollectedTxt = ""; |
| 47 | + if (oXMLParent.hasAttributes && oXMLParent.hasAttributes()) |
| 48 | + { |
| 49 | + vResult = {}; |
| 50 | + for (nLength; nLength < oXMLParent.attributes.length; nLength++) |
| 51 | + { |
| 52 | + oItAttr = oXMLParent.attributes.item(nLength); |
| 53 | + vResult["@" + oItAttr.nodeName] = JXON.buildValue(oItAttr.value.replace(/^\s+|\s+$/g, "")); |
| 54 | + } |
| 55 | + } |
| 56 | + if (oXMLParent.hasChildNodes()) |
| 57 | + { |
| 58 | + for (var oItChild, sItKey, sItVal, nChildId = 0; nChildId < oXMLParent.childNodes.length; nChildId++) |
| 59 | + { |
| 60 | + oItChild = oXMLParent.childNodes.item(nChildId); |
| 61 | + if (oItChild.nodeType === 4) |
| 62 | + { |
| 63 | + sCollectedTxt += oItChild.nodeValue; |
| 64 | + } /* nodeType is "CDATASection" (4) */ |
| 65 | + else if (oItChild.nodeType === 3) |
| 66 | + { |
| 67 | + sCollectedTxt += oItChild.nodeValue.replace(/^\s+|\s+$/g, ""); |
| 68 | + } /* nodeType is "Text" (3) */ |
| 69 | + else if (oItChild.nodeType === 1 && !oItChild.prefix) |
| 70 | + { /* nodeType is "Element" (1) */ |
| 71 | + if (nLength === 0) |
| 72 | + vResult = {}; |
| 73 | + sItKey = oItChild.nodeName; |
| 74 | + sItVal = JXON.getJXONData(oItChild); |
| 75 | + if (vResult.hasOwnProperty(sItKey)) |
| 76 | + { |
| 77 | + if (vResult[sItKey].constructor !== Array) |
| 78 | + vResult[sItKey] = [vResult[sItKey]]; |
| 79 | + vResult[sItKey].push(sItVal); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + vResult[sItKey] = sItVal; |
| 84 | + nLength++; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + if (sCollectedTxt) |
| 90 | + nLength > 0 ? vResult.keyValue = JXON.buildValue(sCollectedTxt) : vResult = JXON.buildValue(sCollectedTxt); |
| 91 | + /* if (nLength > 0) { Object.freeze(vResult); } */ |
| 92 | + return vResult; |
| 93 | +}; |
| 94 | + |
| 95 | +JXON.loadObj = function(oParentObj, oParentEl, oNewDoc) |
| 96 | +{ |
| 97 | + var nSameIdx, |
| 98 | + vValue, |
| 99 | + oChild; |
| 100 | + for (var sName in oParentObj) |
| 101 | + { |
| 102 | + vValue = oParentObj[sName]; |
| 103 | + if (sName === "keyValue") |
| 104 | + { |
| 105 | + if (vValue !== null && vValue !== true) |
| 106 | + oParentEl.appendChild(oNewDoc.createTextNode(String(vValue))); |
| 107 | + } |
| 108 | + else if (sName.charAt(0) === "@") |
| 109 | + oParentEl.setAttribute(sName.slice(1), vValue); |
| 110 | + else |
| 111 | + { |
| 112 | + oChild = oNewDoc.createElement(sName); |
| 113 | + if (vValue && vValue.constructor === Date) |
| 114 | + oChild.appendChild(oNewDoc.createTextNode(vValue.toGMTString())); |
| 115 | + else if (vValue && vValue.constructor === Array) |
| 116 | + { |
| 117 | + for (nSameIdx = 0; nSameIdx < vValue.length; nSameIdx++) |
| 118 | + JXON.loadObj(vValue[nSameIdx], oChild); |
| 119 | + } |
| 120 | + else if (vValue && vValue instanceof Object) |
| 121 | + { |
| 122 | + JXON.loadObj(vValue, oChild, oNewDoc); |
| 123 | + } |
| 124 | + else if (vValue !== null && vValue !== true) |
| 125 | + oChild.appendChild(oNewDoc.createTextNode(vValue.toString())); |
| 126 | + |
| 127 | + oParentEl.appendChild(oChild); |
| 128 | + // CPLog.error("Document: " + (new XMLSerializer()).serializeToString(oNewDoc)); |
| 129 | + } |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +JXON.toXML = function(oJXONObj, rootName) |
| 134 | +{ |
| 135 | + var oNewDoc = document.implementation.createDocument("", "", null), |
| 136 | + rootNode = oNewDoc.createElement(rootName || 'xml'); |
| 137 | + oNewDoc.appendChild(rootNode); |
| 138 | + JXON.loadObj(oJXONObj, rootNode, oNewDoc); |
| 139 | + return (new XMLSerializer()).serializeToString(oNewDoc); |
| 140 | +}; |
0 commit comments