diff --git a/index.html b/index.html index a611cd631..f62cfc515 100755 --- a/index.html +++ b/index.html @@ -11,6 +11,7 @@ + @@ -34,6 +35,7 @@ + diff --git a/js/iD/XML.js b/js/iD/XML.js new file mode 100644 index 000000000..c68649a88 --- /dev/null +++ b/js/iD/XML.js @@ -0,0 +1,38 @@ +iD.XML = { + mapping: function(entity) { + if (this.mappings[entity.type]) { + return this.mappings[entity.type](entity); + } + }, + mappings: { + node: function(entity) { + /* + return { + type: 'Feature', + properties: entity.tags, + geometry: { + type: 'Point', + coordinates: [entity.lon, entity.lat] + } + }; + */ + }, + way: function(entity) { + return (new XMLSerializer()).serializeToString( + JXON.unbuild({ + way: { + '@id': entity.id, + 'nd': entity.children.map(function(e) { + return { + keyAttributes: { + ref: e.id + } + }; + }) + } + })).replace(/>/g,'>'). + replace(/ 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; + }; +})(); + + +// var myObject = JXON.build(doc); +// we got our javascript object! try: alert(JSON.stringify(myObject)); + +// var newDoc = JXON.unbuild(myObject); +// we got our Document instance! try: alert((new XMLSerializer()).serializeToString(newDoc));