root / trunk / web / dojo / dojox / jsonPath / query.js
History | View | Annotate | Download (3.71 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.jsonPath.query"]){ |
||
9 | dojo._hasResource["dojox.jsonPath.query"]=true; |
||
10 | dojo.provide("dojox.jsonPath.query");
|
||
11 | dojox.jsonPath.query=function(_1,_2,_3){ |
||
12 | var re=dojox.jsonPath._regularExpressions;
|
||
13 | if(!_3){
|
||
14 | _3={}; |
||
15 | } |
||
16 | var _4=[];
|
||
17 | function _5(i){ |
||
18 | return _4[i];
|
||
19 | }; |
||
20 | var _6;
|
||
21 | if(_3.resultType=="PATH"&&_3.evalType=="RESULT"){ |
||
22 | throw Error("RESULT based evaluation not supported with PATH based results"); |
||
23 | } |
||
24 | var P={resultType:_3.resultType||"VALUE",normalize:function(_7){ |
||
25 | var _8=[];
|
||
26 | _7=_7.replace(/'([^']|'')*'/g,function(t){ |
||
27 | return "_str("+(_4.push(eval(t))-1)+")"; |
||
28 | }); |
||
29 | var ll=-1; |
||
30 | while(ll!=_8.length){
|
||
31 | ll=_8.length; |
||
32 | _7=_7.replace(/(\??\([^\(\)]*\))/g,function($0){ |
||
33 | return "#"+(_8.push($0)-1); |
||
34 | }); |
||
35 | } |
||
36 | _7=_7.replace(/[\['](#[0-9]+)[\]']/g,"[$1]").replace(/'?\.'?|\['?/g,";").replace(/;;;|;;/g,";..;").replace(/;$|'?\]|'$/g,""); |
||
37 | ll=-1;
|
||
38 | while(ll!=_7){
|
||
39 | ll=_7; |
||
40 | _7=_7.replace(/#([0-9]+)/g,function($0,$1){ |
||
41 | return _8[$1]; |
||
42 | }); |
||
43 | } |
||
44 | return _7.split(";"); |
||
45 | },asPaths:function(_9){ |
||
46 | for(var j=0;j<_9.length;j++){ |
||
47 | var p="$"; |
||
48 | var x=_9[j];
|
||
49 | for(var i=1,n=x.length;i<n;i++){ |
||
50 | p+=/^[0-9*]+$/.test(x[i])?("["+x[i]+"]"):("['"+x[i]+"']"); |
||
51 | } |
||
52 | _9[j]=p; |
||
53 | } |
||
54 | return _9;
|
||
55 | },exec:function(_a,_b,rb){ |
||
56 | var _c=["$"]; |
||
57 | var _d=rb?_b:[_b];
|
||
58 | var _e=[_c];
|
||
59 | function _f(v,p,def){ |
||
60 | if(v&&v.hasOwnProperty(p)&&P.resultType!="VALUE"){ |
||
61 | _e.push(_c.concat([p])); |
||
62 | } |
||
63 | if(def){
|
||
64 | _d=v[p]; |
||
65 | }else{
|
||
66 | if(v&&v.hasOwnProperty(p)){
|
||
67 | _d.push(v[p]); |
||
68 | } |
||
69 | } |
||
70 | }; |
||
71 | function _10(v){ |
||
72 | _d.push(v); |
||
73 | _e.push(_c); |
||
74 | P.walk(v,function(i){
|
||
75 | if(typeof v[i]==="object"){ |
||
76 | var _11=_c;
|
||
77 | _c=_c.concat(i); |
||
78 | _10(v[i]); |
||
79 | _c=_11; |
||
80 | } |
||
81 | }); |
||
82 | }; |
||
83 | function _12(loc,val){ |
||
84 | if(val instanceof Array){ |
||
85 | var len=val.length,_13=0,end=len,_14=1; |
||
86 | loc.replace(/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/g,function($0,$1,$2,$3){ |
||
87 | _13=parseInt($1||_13);
|
||
88 | end=parseInt($2||end);
|
||
89 | _14=parseInt($3||_14);
|
||
90 | }); |
||
91 | _13=(_13<0)?Math.max(0,_13+len):Math.min(len,_13); |
||
92 | end=(end<0)?Math.max(0,end+len):Math.min(len,end); |
||
93 | for(var i=_13;i<end;i+=_14){ |
||
94 | _f(val,i); |
||
95 | } |
||
96 | } |
||
97 | }; |
||
98 | function _15(str){ |
||
99 | var i=loc.match(/^_str\(([0-9]+)\)$/); |
||
100 | return i?_4[i[1]]:str; |
||
101 | }; |
||
102 | function _16(val){ |
||
103 | if(/^\(.*?\)$/.test(loc)){ |
||
104 | _f(val,P.eval(loc,val),rb); |
||
105 | }else{
|
||
106 | if(loc==="*"){ |
||
107 | P.walk(val,rb&&val instanceof Array?function(i){ |
||
108 | P.walk(val[i],function(j){
|
||
109 | _f(val[i],j); |
||
110 | }); |
||
111 | }:function(i){
|
||
112 | _f(val,i); |
||
113 | }); |
||
114 | }else{
|
||
115 | if(loc===".."){ |
||
116 | _10(val); |
||
117 | }else{
|
||
118 | if(/,/.test(loc)){ |
||
119 | for(var s=loc.split(/'?,'?/),i=0,n=s.length;i<n;i++){ |
||
120 | _f(val,_15(s[i])); |
||
121 | } |
||
122 | }else{
|
||
123 | if(/^\?\(.*?\)$/.test(loc)){ |
||
124 | P.walk(val,function(i){
|
||
125 | if(P.eval(loc.replace(/^\?\((.*?)\)$/,"$1"),val[i])){ |
||
126 | _f(val,i); |
||
127 | } |
||
128 | }); |
||
129 | }else{
|
||
130 | if(/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)){ |
||
131 | _12(loc,val); |
||
132 | }else{
|
||
133 | loc=_15(loc); |
||
134 | if(rb&&val instanceof Array&&!/^[0-9*]+$/.test(loc)){ |
||
135 | P.walk(val,function(i){
|
||
136 | _f(val[i],loc); |
||
137 | }); |
||
138 | }else{
|
||
139 | _f(val,loc,rb); |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | }; |
||
148 | while(_a.length){
|
||
149 | var loc=_a.shift();
|
||
150 | if((_b=_d)===null||_b===undefined){ |
||
151 | return _b;
|
||
152 | } |
||
153 | _d=[]; |
||
154 | var _17=_e;
|
||
155 | _e=[]; |
||
156 | if(rb){
|
||
157 | _16(_b); |
||
158 | }else{
|
||
159 | P.walk(_b,function(i){
|
||
160 | _c=_17[i]||_c; |
||
161 | _16(_b[i]); |
||
162 | }); |
||
163 | } |
||
164 | } |
||
165 | if(P.resultType=="BOTH"){ |
||
166 | _e=P.asPaths(_e); |
||
167 | var _18=[];
|
||
168 | for(var i=0;i<_e.length;i++){ |
||
169 | _18.push({path:_e[i],value:_d[i]}); |
||
170 | } |
||
171 | return _18;
|
||
172 | } |
||
173 | return P.resultType=="PATH"?P.asPaths(_e):_d; |
||
174 | },walk:function(val,f){ |
||
175 | if(val instanceof Array){ |
||
176 | for(var i=0,n=val.length;i<n;i++){ |
||
177 | if(i in val){ |
||
178 | f(i); |
||
179 | } |
||
180 | } |
||
181 | }else{
|
||
182 | if(typeof val==="object"){ |
||
183 | for(var m in val){ |
||
184 | if(val.hasOwnProperty(m)){
|
||
185 | f(m); |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | },eval:function(x,_19){ |
||
191 | try{
|
||
192 | return $&&_19&&eval(x.replace(/@/g,"_v")); |
||
193 | } |
||
194 | catch(e){
|
||
195 | throw new SyntaxError("jsonPath: "+e.message+": "+x.replace(/@/g,"_v").replace(/\^/g,"_a")); |
||
196 | } |
||
197 | }}; |
||
198 | var $=_1; |
||
199 | if(_2&&_1){
|
||
200 | return P.exec(P.normalize(_2).slice(1),_1,_3.evalType=="RESULT"); |
||
201 | } |
||
202 | return false; |
||
203 | }; |
||
204 | } |