1 /** 2 * @class ProviderGET 3 * @augments wso2vis.p.Provider 4 * @constructor 5 * @param {String} url The data source URL 6 **/ 7 wso2vis.p.ProviderGET = function(url) { 8 this.url = url; 9 this.xmlHttpReq = null; 10 11 wso2vis.p.Provider.call(this); 12 }; 13 14 wso2vis.extend(wso2vis.p.ProviderGET, wso2vis.p.Provider); 15 16 wso2vis.p.ProviderGET.prototype.initialize = function() { 17 this.pullDataSync(); // initial pullData call should fill the wire with data to populate filter forms properly, hense the sync call. 18 }; 19 20 wso2vis.p.ProviderGET.prototype.pullData = function() { 21 // Make sure the XMLHttpRequest object was instantiated 22 var that = this; 23 if (!that.xmlHttpReq) { 24 that.xmlHttpReq = this.createXmlHttpRequest(); 25 } 26 if (that.xmlHttpReq) 27 { 28 that.xmlHttpReq.open("GET", that.getURLwithRandomParam()); // to prevent IE caching 29 that.xmlHttpReq.onreadystatechange = function() { 30 if (that.xmlHttpReq.readyState == 4) { 31 that.pushData(that.parseResponse(that.xmlHttpReq.responseText, that)); 32 } 33 }; 34 that.xmlHttpReq.send(null); 35 } 36 } 37 38 wso2vis.p.ProviderGET.prototype.pullDataSync = function() { 39 var that = this; 40 if (!that.xmlHttpReq) { 41 that.xmlHttpReq = this.createXmlHttpRequest(); 42 } 43 44 if (that.xmlHttpReq) 45 { 46 that.xmlHttpReq.open("GET", that.getURLwithRandomParam(), false); // to prevent IE caching 47 that.xmlHttpReq.send(null); 48 if (that.xmlHttpReq.status == 200) { 49 that.pushData(that.parseResponse(that.xmlHttpReq.responseText, that)); 50 } 51 } 52 53 return false; 54 } 55 56 wso2vis.p.ProviderGET.prototype.parseResponse = function(response, that) { 57 var resp = that.parseXml(response); 58 return that.xmlToJson(resp, " "); 59 } 60 61 wso2vis.p.ProviderGET.prototype.getURLwithRandomParam = function() { 62 if (this.url.indexOf('?') == -1) { 63 return this.url + '?random=' + new Date().getTime(); 64 } 65 return this.url + '&random=' + new Date().getTime(); 66 } 67 68 wso2vis.p.ProviderGET.prototype.createXmlHttpRequest = function() { 69 var request; 70 71 // Lets try using ActiveX to instantiate the XMLHttpRequest 72 // object 73 try { 74 request = new ActiveXObject("Microsoft.XMLHTTP"); 75 } catch(ex1) { 76 try { 77 request = new ActiveXObject("Msxml2.XMLHTTP"); 78 } catch(ex2) { 79 request = null; 80 } 81 } 82 83 // If the previous didn't work, lets check if the browser natively support XMLHttpRequest 84 if (!request && typeof XMLHttpRequest != "undefined") { 85 //The browser does, so lets instantiate the object 86 request = new XMLHttpRequest(); 87 } 88 89 return request; 90 } 91 92 /** 93 * converts xml string to a dom object 94 * 95 * @param {string} [xml] a xml string 96 * @returns {dom} a xml dom object 97 */ 98 wso2vis.p.ProviderGET.prototype.parseXml = function(xml) { 99 var dom = null; 100 if (window.DOMParser) { 101 try { 102 dom = (new DOMParser()).parseFromString(xml, "text/xml"); 103 } 104 catch (e) { dom = null; } 105 } 106 else if (window.ActiveXObject) { 107 try { 108 dom = new ActiveXObject('Microsoft.XMLDOM'); 109 dom.async = false; 110 if (!dom.loadXML(xml)) // parse error .. 111 window.alert(dom.parseError.reason + dom.parseError.srcText); 112 } 113 catch (e) { dom = null; } 114 } 115 else 116 window.alert("oops"); 117 return dom; 118 } 119 120 /** 121 * Once passed an xml dom object xmlToJson will create a corresponding JSON object. 122 * 123 * @param {DOM} [xml] a xml dom object 124 * @param {string} [tab] an optional whitespace character to beutify the created JSON string. 125 * @returns {object} a JSON object 126 */ 127 wso2vis.p.ProviderGET.prototype.xmlToJson = function(xml, tab) { 128 var X = { 129 toObj: function(xml) { 130 var o = {}; 131 if (xml.nodeType == 1) { 132 if (xml.attributes.length) 133 for (var i=0; i<xml.attributes.length; i++) 134 o["@"+xml.attributes[i].nodeName] = (xml.attributes[i].nodeValue||"").toString(); 135 if (xml.firstChild) { 136 var textChild=0, cdataChild=0, hasElementChild=false; 137 for (var n=xml.firstChild; n; n=n.nextSibling) { 138 if (n.nodeType==1) hasElementChild = true; 139 else if (n.nodeType==3 && n.nodeValue.match(/[^ \f\n\r\t\v]/)) textChild++; 140 else if (n.nodeType==4) cdataChild++; 141 } 142 if (hasElementChild) { 143 if (textChild < 2 && cdataChild < 2) { 144 X.removeWhite(xml); 145 for (var n=xml.firstChild; n; n=n.nextSibling) { 146 if (n.nodeType == 3) 147 o["#text"] = X.escape(n.nodeValue); 148 else if (n.nodeType == 4) 149 o["#cdata"] = X.escape(n.nodeValue); 150 else if (o[n.nodeName]) { 151 if (o[n.nodeName] instanceof Array) 152 o[n.nodeName][o[n.nodeName].length] = X.toObj(n); 153 else 154 o[n.nodeName] = [o[n.nodeName], X.toObj(n)]; 155 } 156 else 157 o[n.nodeName] = X.toObj(n); 158 } 159 } 160 else { 161 if (!xml.attributes.length) 162 o = X.escape(X.innerXml(xml)); 163 else 164 o["#text"] = X.escape(X.innerXml(xml)); 165 } 166 } //(hasElementChild) 167 else if (textChild) { 168 if (!xml.attributes.length) 169 o = X.escape(X.innerXml(xml)); 170 else 171 o["#text"] = X.escape(X.innerXml(xml)); 172 } 173 else if (cdataChild) { 174 if (cdataChild > 1) 175 o = X.escape(X.innerXml(xml)); 176 else 177 for (var n=xml.firstChild; n; n=n.nextSibling) 178 o["#cdata"] = X.escape(n.nodeValue); 179 } 180 } 181 if (!xml.attributes.length && !xml.firstChild) o = null; 182 } 183 else if (xml.nodeType==9) { 184 o = X.toObj(xml.documentElement); 185 } 186 else 187 alert("unhandled node type: " + xml.nodeType); 188 return o; 189 }, 190 toJson: function(o, name, ind) { 191 var p = name.lastIndexOf(':'); 192 if (p != -1) { 193 if (p + 1 >= name.length) 194 name = ""; 195 else 196 name = name.substr(p + 1); 197 } 198 var json = name ? ("\""+name+"\"") : ""; 199 if (o instanceof Array) { 200 for (var i=0,n=o.length; i<n; i++) 201 o[i] = X.toJson(o[i], "", ind+"\t"); 202 json += (name?":[":"[") + (o.length > 1 ? ("\n"+ind+"\t"+o.join(",\n"+ind+"\t")+"\n"+ind) : o.join("")) + "]"; 203 } 204 else if (o == null) 205 json += (name&&":") + "null"; 206 else if (typeof(o) == "object") { 207 var arr = []; 208 for (var m in o) 209 arr[arr.length] = X.toJson(o[m], m, ind+"\t"); 210 json += (name?":{":"{") + (arr.length > 1 ? ("\n"+ind+"\t"+arr.join(",\n"+ind+"\t")+"\n"+ind) : arr.join("")) + "}"; 211 } 212 else if (typeof(o) == "string") 213 json += (name&&":") + "\"" + o.toString() + "\""; 214 else 215 json += (name&&":") + o.toString(); 216 return json; 217 }, 218 innerXml: function(node) { 219 var s = "" 220 if ("innerHTML" in node) 221 s = node.innerHTML; 222 else { 223 var asXml = function(n) { 224 var s = ""; 225 if (n.nodeType == 1) { 226 s += "<" + n.nodeName; 227 for (var i=0; i<n.attributes.length;i++) 228 s += " " + n.attributes[i].nodeName + "=\"" + (n.attributes[i].nodeValue||"").toString() + "\""; 229 if (n.firstChild) { 230 s += ">"; 231 for (var c=n.firstChild; c; c=c.nextSibling) 232 s += asXml(c); 233 s += "</"+n.nodeName+">"; 234 } 235 else 236 s += "/>"; 237 } 238 else if (n.nodeType == 3) 239 s += n.nodeValue; 240 else if (n.nodeType == 4) 241 s += "<![CDATA[" + n.nodeValue + "]]>"; 242 return s; 243 }; 244 for (var c=node.firstChild; c; c=c.nextSibling) 245 s += asXml(c); 246 } 247 return s; 248 }, 249 escape: function(txt) { 250 return txt.replace(/[\\]/g, "\\\\") 251 .replace(/[\"]/g, '\\"') 252 .replace(/[\n]/g, '\\n') 253 .replace(/[\r]/g, '\\r'); 254 }, 255 removeWhite: function(e) { 256 e.normalize(); 257 for (var n = e.firstChild; n; ) { 258 if (n.nodeType == 3) { 259 if (!n.nodeValue.match(/[^ \f\n\r\t\v]/)) { 260 var nxt = n.nextSibling; 261 e.removeChild(n); 262 n = nxt; 263 } 264 else 265 n = n.nextSibling; 266 } 267 else if (n.nodeType == 1) { 268 X.removeWhite(n); 269 n = n.nextSibling; 270 } 271 else 272 n = n.nextSibling; 273 } 274 return e; 275 } 276 }; 277 if (xml.nodeType == 9) 278 xml = xml.documentElement; 279 var json = X.toJson(X.toObj(X.removeWhite(xml)), xml.nodeName, "\t"); 280 return JSON.parse("{\n" + tab + (tab ? json.replace(/\t/g, tab) : json.replace(/\t|\n/g, "")) + "\n}"); 281 } 282 283