1 /*jslint 
  2 browser: true,
  3 nomen: false,
  4 debug: true,
  5 forin: true,
  6 undef: true,
  7 white: false,
  8 onevar: false 
  9  */
 10 var sc;
 11  
 12 /* A wrapper for JSON.parse() that correct Twitter issues and perform logging if JSON data could not be parsed
 13  * which will help to find out what is wrong
 14  * @param {String} text 
 15  * @member sc.helpers
 16  */
 17 sc.helpers.deJSON = function(json)
 18  {
 19 
 20 	// Fix twitter data bug
 21 	// var re = new RegExp("Couldn\\'t\\ find\\ Status\\ with\\ ID\\=[0-9]+\\,", "g");
 22 	// json = json.replace(re, "");
 23 
 24 	var done = false;
 25 	try {
 26 		var obj = JSON.parse(json);
 27 		done = true;
 28 	} finally {
 29 		if (!done) {
 30 			sc.helpers.dump("Could not parse JSON text " + json);
 31 		}
 32 	}
 33 
 34 	return obj;
 35 };
 36 
 37 /**
 38  * really just a simple wrapper for JSON.stringify	
 39  * @param  any js construct
 40  * @member sc.helpers
 41 
*/
 42 sc.helpers.enJSON = function(jsobj) {
 43 	return JSON.stringify(jsobj);
 44 };
 45 
 46 
 47 /*
 48  * Based on jQuery XML to JSON Plugin
 49  * 
 50  *	### jQuery XML to JSON Plugin v1.0 - 2008-07-01 ###
 51  * http://www.fyneworks.com/ - diego@fyneworks.com
 52  * Dual licensed under the MIT and GPL licenses:
 53  *	 http://www.opensource.org/licenses/mit-license.php
 54  *	 http://www.gnu.org/licenses/gpl.html
 55  ###
 56  Website: http://www.fyneworks.com/jquery/xml-to-json/
 57 */
 58 /*
 59  # INSPIRED BY: http://www.terracoder.com/
 60 		   AND: http://www.thomasfrank.se/xml_to_json.html
 61 											AND: http://www.kawa.net/works/js/xml/objtree-e.html
 62 */
 63 /*
 64  This simple script converts XML (document of code) into a JSON object. It is the combination of 2
 65  'xml to json' great parsers (see below) which allows for both 'simple' and 'extended' parsing modes.
 66  * @member sc.helpers
 67 */
 68 sc.helpers.xml2json = function(xml, extended) {
 69 	if (!xml) return {};
 70 	// quick fail
 71 	//### PARSER LIBRARY
 72 	// Core function
 73 	function parseXML(node, simple) {
 74 		if (!node) return null;
 75 		var txt = '',
 76 		obj = null,
 77 		att = null;
 78 		var nt = node.nodeType,
 79 		nn = jsVar(node.localName || node.nodeName);
 80 		var nv = node.text || node.nodeValue || '';
 81 		/*DBG*/
 82 		//if(window.console) console.log(['x2j',nn,nt,nv.length+' bytes']);
 83 		if (node.childNodes) {
 84 			if (node.childNodes.length > 0) {
 85 				/*DBG*/
 86 				//if(window.console) console.log(['x2j',nn,'CHILDREN',node.childNodes]);
 87 				jQuery.each(node.childNodes,
 88 				function(n, cn) {
 89 					var cnt = cn.nodeType,
 90 					cnn = jsVar(cn.localName || cn.nodeName);
 91 					var cnv = cn.text || cn.nodeValue || '';
 92 					/*DBG*/
 93 					//if(window.console) console.log(['x2j',nn,'node>a',cnn,cnt,cnv]);
 94 					if (cnt == 8) {
 95 						/*DBG*/
 96 						//if(window.console) console.log(['x2j',nn,'node>b',cnn,'COMMENT (ignore)']);
 97 						return;
 98 						// ignore comment node
 99 					}
100 					else if (cnt == 3 || cnt == 4 || !cnn) {
101 						// ignore white-space in between tags
102 						if (cnv.match(/^\s+$/)) {
103 							/*DBG*/
104 							//if(window.console) console.log(['x2j',nn,'node>c',cnn,'WHITE-SPACE (ignore)']);
105 							return;
106 						};
107 						/*DBG*/
108 						//if(window.console) console.log(['x2j',nn,'node>d',cnn,'TEXT']);
109 						txt += cnv.replace(/^\s+/, '').replace(/\s+$/, '');
110 						// make sure we ditch trailing spaces from markup
111 					}
112 					else {
113 						/*DBG*/
114 						//if(window.console) console.log(['x2j',nn,'node>e',cnn,'OBJECT']);
115 						obj = obj || {};
116 						if (obj[cnn]) {
117 							/*DBG*/
118 							//if(window.console) console.log(['x2j',nn,'node>f',cnn,'ARRAY']);
119 							if (!obj[cnn].length) obj[cnn] = myArr(obj[cnn]);
120 							obj[cnn][obj[cnn].length] = parseXML(cn, true
121 							/* simple */
122 							);
123 							obj[cnn].length = obj[cnn].length;
124 						}
125 						else {
126 							/*DBG*/
127 							//if(window.console) console.log(['x2j',nn,'node>g',cnn,'dig deeper...']);
128 							obj[cnn] = parseXML(cn);
129 						};
130 					};
131 				});
132 			};
133 			//node.childNodes.length>0
134 		};
135 		//node.childNodes
136 		if (node.attributes) {
137 			if (node.attributes.length > 0) {
138 				/*DBG*/
139 				//if(window.console) console.log(['x2j',nn,'ATTRIBUTES',node.attributes])
140 				att = {};
141 				obj = obj || {};
142 				jQuery.each(node.attributes, function(a, at) {
143 					var atn = jsVar(at.name),
144 					atv = at.value;
145 					att[atn] = atv;
146 					if (obj[atn]) {
147 						/*DBG*/
148 						//if(window.console) console.log(['x2j',nn,'attr>',atn,'ARRAY']);
149 						if (!obj[atn].length) obj[atn] = myArr(obj[atn]);
150 						//[ obj[ atn ] ];
151 						obj[atn][obj[atn].length] = atv;
152 						obj[atn].length = obj[atn].length;
153 					}
154 					else {
155 						/*DBG*/
156 						//if(window.console) console.log(['x2j',nn,'attr>',atn,'TEXT']);
157 						obj[atn] = atv;
158 					};
159 				});
160 				//obj['attributes'] = att;
161 			};
162 			//node.attributes.length>0
163 		};
164 		//node.attributes
165 		if (obj) {
166 			obj = jQuery.extend((txt != '' ? new String(txt) : {}),
167 			/* {text:txt},*/
168 			obj || {}
169 			/*, att || {}*/
170 			);
171 			txt = (obj.text) ? (typeof(obj.text) == 'object' ? obj.text: [obj.text || '']).concat([txt]) : txt;
172 			if (txt) obj.text = txt;
173 			txt = '';
174 		};
175 		var out = obj || txt;
176 		//console.log([extended, simple, out]);
177 		if (extended) {
178 			if (txt) out = {};
179 			//new String(out);
180 			txt = out.text || txt || '';
181 			if (txt) out.text = txt;
182 			if (!simple) out = myArr(out);
183 		};
184 		return out;
185 	};
186 	// parseXML
187 	// Core Function End
188 	// Utility functions
189 	var jsVar = function(s) {
190 		return String(s || '').replace(/-/g, "_");
191 	};
192 	var isNum = function(s) {
193 		return (typeof s == "number") || String((s && typeof s == "string") ? s: '').test(/^((-)?([0-9]*)((\.{0,1})([0-9]+))?$)/);
194 	};
195 	var myArr = function(o) {
196 		if (!o.length) o = [o];
197 		o.length = o.length;
198 		// here is where you can attach additional functionality, such as searching and sorting...
199 		return o;
200 	};
201 	// Utility functions End
202 	//### PARSER LIBRARY END
203 	// Convert plain text to xml
204 	if (typeof xml == 'string') {xml = sc.helpers.createXMLFromString(xml);}
205 
206 	// Quick fail if not xml (or if this is a node)
207 	if (!xml.nodeType) {return;}
208 	if (xml.nodeType == 3 || xml.nodeType == 4) {return xml.nodeValue;}
209 
210 	// Find xml root node
211 	var root = (xml.nodeType == 9) ? xml.documentElement: xml;
212 
213 	// Convert xml to json
214 	var out = parseXML(root, true
215 	/* simple */
216 	);
217 
218 	// Clean-up memory
219 	xml = null;
220 	root = null;
221 
222 	// Send output
223 	return out;
224 };
225 
226 
227