mypackage_wrt/preview/script/xml2json.js
author nithyar
Wed, 12 May 2010 17:17:10 +0100
changeset 52 3f1ce447f91e
parent 42 20be4dd42b12
permissions -rwxr-xr-x
Updated S^4 Key Feature dates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     1
/*
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     2
xml2json v 1.1
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     3
copyright 2005-2007 Thomas Frank
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     4
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     5
This program is free software under the terms of the 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     6
GNU General Public License version 2 as published by the Free 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     7
Software Foundation. It is distributed without any warranty.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     8
*/
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     9
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    10
var xml2json={ 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    11
	parser:function(xmlcode,ignoretags,debug){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    12
		if(!ignoretags){ignoretags=""};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    13
		xmlcode=xmlcode.replace(/\s*\/>/g,'/>');
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    14
		xmlcode=xmlcode.replace(/<\?[^>]*>/g,"").replace(/<\![^>]*>/g,"");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    15
		if (!ignoretags.sort){ignoretags=ignoretags.split(",")};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    16
		var x=this.no_fast_endings(xmlcode);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    17
		x=this.attris_to_tags(x);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    18
		x=escape(x);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    19
		x=x.split("%3C").join("<").split("%3E").join(">").split("%3D").join("=").split("%22").join("\"");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    20
		for (var i=0;i<ignoretags.length;i++){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    21
			x=x.replace(new RegExp("<"+ignoretags[i]+">","g"),"*$**"+ignoretags[i]+"**$*");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    22
			x=x.replace(new RegExp("</"+ignoretags[i]+">","g"),"*$***"+ignoretags[i]+"**$*")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    23
		};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    24
		x='<JSONTAGWRAPPER>'+x+'</JSONTAGWRAPPER>';
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    25
		this.xmlobject={};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    26
		var y=this.xml_to_object(x).jsontagwrapper;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    27
		if(debug){y=this.show_json_structure(y,debug)};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    28
		return y
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    29
	},
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    30
	xml_to_object:function(xmlcode){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    31
		var x=xmlcode.replace(/<\//g,"§");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    32
		x=x.split("<");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    33
		var y=[];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    34
		var level=0;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    35
		var opentags=[];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    36
		for (var i=1;i<x.length;i++){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    37
			var tagname=x[i].split(">")[0];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    38
			opentags.push(tagname);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    39
			level++
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    40
			y.push(level+"<"+x[i].split("§")[0]);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    41
			while(x[i].indexOf("§"+opentags[opentags.length-1]+">")>=0){level--;opentags.pop()}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    42
		};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    43
		var oldniva=-1;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    44
		var objname="this.xmlobject";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    45
		for (var i=0;i<y.length;i++){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    46
			var preeval="";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    47
			var niva=y[i].split("<")[0];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    48
			var tagnamn=y[i].split("<")[1].split(">")[0];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    49
			tagnamn=tagnamn.toLowerCase();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    50
			var rest=y[i].split(">")[1];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    51
			if(niva<=oldniva){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    52
				var tabort=oldniva-niva+1;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    53
				for (var j=0;j<tabort;j++){objname=objname.substring(0,objname.lastIndexOf("."))}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    54
			};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    55
			objname+="."+tagnamn;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    56
			var pobject=objname.substring(0,objname.lastIndexOf("."));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    57
			if (eval("typeof "+pobject) != "object"){preeval+=pobject+"={value:"+pobject+"};\n"};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    58
			var objlast=objname.substring(objname.lastIndexOf(".")+1);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    59
			var already=false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    60
			for (k in eval(pobject)){if(k==objlast){already=true}};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    61
			var onlywhites=true;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    62
			for(var s=0;s<rest.length;s+=3){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    63
				if(rest.charAt(s)!="%"){onlywhites=false}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    64
			};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    65
			if (rest!="" && !onlywhites){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    66
				if(rest/1!=rest){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    67
					rest="'"+rest.replace(/\'/g,"\\'")+"'";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    68
					rest=rest.replace(/\*\$\*\*\*/g,"</");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    69
					rest=rest.replace(/\*\$\*\*/g,"<");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    70
					rest=rest.replace(/\*\*\$\*/g,">")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    71
				}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    72
			} 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    73
			else {rest="{}"};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    74
			if(rest.charAt(0)=="'"){rest='unescape('+rest+')'};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    75
			if (already && !eval(objname+".sort")){preeval+=objname+"=["+objname+"];\n"};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    76
			var before="=";after="";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    77
			if (already){before=".push(";after=")"};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    78
			var toeval=preeval+objname+before+rest+after;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    79
			eval(toeval);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    80
			if(eval(objname+".sort")){objname+="["+eval(objname+".length-1")+"]"};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    81
			oldniva=niva
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    82
		};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    83
		return this.xmlobject
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    84
	},
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    85
	show_json_structure:function(obj,debug,l){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    86
		var x='';
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    87
		if (obj.sort){x+="[\n"} else {x+="{\n"};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    88
		for (var i in obj){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    89
			if (!obj.sort){x+=i+":"};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    90
			if (typeof obj[i] == "object"){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    91
				x+=this.show_json_structure(obj[i],false,1)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    92
			}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    93
			else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    94
				if(typeof obj[i]=="function"){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    95
					var v=obj[i]+"";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    96
					//v=v.replace(/\t/g,"");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    97
					x+=v
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    98
				}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    99
				else if(typeof obj[i]!="string"){x+=obj[i]+",\n"}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   100
				else {x+="'"+obj[i].replace(/\'/g,"\\'").replace(/\n/g,"\\n").replace(/\t/g,"\\t").replace(/\r/g,"\\r")+"',\n"}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   101
			}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   102
		};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   103
		if (obj.sort){x+="],\n"} else {x+="},\n"};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   104
		if (!l){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   105
			x=x.substring(0,x.lastIndexOf(","));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   106
			x=x.replace(new RegExp(",\n}","g"),"\n}");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   107
			x=x.replace(new RegExp(",\n]","g"),"\n]");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   108
			var y=x.split("\n");x="";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   109
			var lvl=0;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   110
			for (var i=0;i<y.length;i++){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   111
				if(y[i].indexOf("}")>=0 || y[i].indexOf("]")>=0){lvl--};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   112
				tabs="";for(var j=0;j<lvl;j++){tabs+="\t"};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   113
				x+=tabs+y[i]+"\n";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   114
				if(y[i].indexOf("{")>=0 || y[i].indexOf("[")>=0){lvl++}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   115
			};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   116
			if(debug=="html"){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   117
				x=x.replace(/</g,"&lt;").replace(/>/g,"&gt;");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   118
				x=x.replace(/\n/g,"<BR>").replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   119
			};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   120
			if (debug=="compact"){x=x.replace(/\n/g,"").replace(/\t/g,"")}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   121
		};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   122
		return x
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   123
	},
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   124
	no_fast_endings:function(x){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   125
		x=x.split("/>");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   126
		for (var i=1;i<x.length;i++){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   127
			var t=x[i-1].substring(x[i-1].lastIndexOf("<")+1).split(" ")[0];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   128
			x[i]="></"+t+">"+x[i]
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   129
		}	;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   130
		x=x.join("");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   131
		return x
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   132
	},
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   133
	attris_to_tags: function(x){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   134
		var d=' ="\''.split("");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   135
		x=x.split(">");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   136
		for (var i=0;i<x.length;i++){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   137
			var temp=x[i].split("<");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   138
			for (var r=0;r<4;r++){temp[0]=temp[0].replace(new RegExp(d[r],"g"),"_jsonconvtemp"+r+"_")};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   139
			if(temp[1]){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   140
				temp[1]=temp[1].replace(/'/g,'"');
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   141
				temp[1]=temp[1].split('"');
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   142
				for (var j=1;j<temp[1].length;j+=2){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   143
					for (var r=0;r<4;r++){temp[1][j]=temp[1][j].replace(new RegExp(d[r],"g"),"_jsonconvtemp"+r+"_")}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   144
				};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   145
				temp[1]=temp[1].join('"')
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   146
			};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   147
			x[i]=temp.join("<")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   148
		};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   149
		x=x.join(">");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   150
		x=x.replace(/ ([^=]*)=([^ |>]*)/g,"><$1>$2</$1");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   151
		x=x.replace(/>"/g,">").replace(/"</g,"<");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   152
		for (var r=0;r<4;r++){x=x.replace(new RegExp("_jsonconvtemp"+r+"_","g"),d[r])}	;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   153
		return x
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   154
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   155
};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   156
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   157
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   158
if(!Array.prototype.push){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   159
	Array.prototype.push=function(x){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   160
		this[this.length]=x;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   161
		return true
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   162
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   163
};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   164
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   165
if (!Array.prototype.pop){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   166
	Array.prototype.pop=function(){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   167
  		var response = this[this.length-1];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   168
  		this.length--;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   169
  		return response
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   170
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   171
};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   172