1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.json; |
11 | |
|
12 | |
import java.io.IOException; |
13 | |
import java.io.InputStream; |
14 | |
import java.io.Reader; |
15 | |
import java.io.Serializable; |
16 | |
import java.io.StringReader; |
17 | |
import java.net.URL; |
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.codehaus.jackson.JsonNode; |
22 | |
import org.codehaus.jackson.map.ObjectMapper; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public class JsonData implements Serializable |
56 | |
{ |
57 | |
private JsonNode node; |
58 | |
|
59 | |
public JsonData(JsonNode node) |
60 | 0 | { |
61 | 0 | this.node = node; |
62 | 0 | } |
63 | |
|
64 | |
public JsonData(URL node) throws IOException |
65 | |
{ |
66 | 0 | this(node.openStream()); |
67 | 0 | } |
68 | |
|
69 | |
public JsonData(InputStream node) throws IOException |
70 | 0 | { |
71 | 0 | this.node = new ObjectMapper().readTree(node); |
72 | 0 | } |
73 | |
|
74 | |
public JsonData(Reader node) throws IOException |
75 | 0 | { |
76 | 0 | this.node = new ObjectMapper().readTree(node); |
77 | 0 | } |
78 | |
|
79 | |
public JsonData(String node) throws IOException |
80 | |
{ |
81 | 0 | this(new StringReader(node)); |
82 | 0 | } |
83 | |
|
84 | |
public JsonNode get(int index) |
85 | |
{ |
86 | 0 | return node.get(index); |
87 | |
} |
88 | |
|
89 | |
public boolean isArray() |
90 | |
{ |
91 | 0 | return node.isArray(); |
92 | |
} |
93 | |
|
94 | |
public Object get(String expression) |
95 | |
{ |
96 | 0 | List<String> tokens = parseTokens(expression); |
97 | |
|
98 | 0 | JsonNode o = node; |
99 | 0 | for (String token : tokens) |
100 | |
{ |
101 | 0 | if (token.startsWith("[")) |
102 | |
{ |
103 | 0 | if (o.isArray()) |
104 | |
{ |
105 | 0 | int index = Integer.valueOf(token.substring(1, token.length() - 1)); |
106 | 0 | o = o.path(index); |
107 | 0 | } |
108 | |
else |
109 | |
{ |
110 | 0 | throw new IllegalArgumentException("Current node is not an array, but expression is expecting one"); |
111 | |
} |
112 | |
} |
113 | |
else |
114 | |
{ |
115 | 0 | o = o.path(token); |
116 | |
} |
117 | |
|
118 | 0 | if (o.isMissingNode()) |
119 | |
{ |
120 | 0 | throw new IllegalArgumentException("Not a valid element: " + token); |
121 | |
} |
122 | |
} |
123 | |
|
124 | 0 | if (o.isValueNode()) |
125 | |
{ |
126 | 0 | return o.getValueAsText(); |
127 | |
} |
128 | |
else |
129 | |
{ |
130 | 0 | return o; |
131 | |
} |
132 | |
} |
133 | |
|
134 | |
public boolean hasNode(String key) |
135 | |
{ |
136 | 0 | JsonNode result = node.path(key); |
137 | 0 | return result.isMissingNode() == false; |
138 | |
} |
139 | |
|
140 | |
protected List<String> parseTokens(String expresion) |
141 | |
{ |
142 | 0 | List<String> tokens = new ArrayList<String>(); |
143 | 0 | while (expresion.length() > 0) |
144 | |
{ |
145 | 0 | int slash = expresion.indexOf("/"); |
146 | 0 | int brace = expresion.indexOf("["); |
147 | |
|
148 | 0 | if (expresion.charAt(0) == '\'') |
149 | |
{ |
150 | 0 | if (expresion.endsWith("'")) |
151 | |
{ |
152 | 0 | tokens.add(expresion.substring(1, expresion.length() - 1)); |
153 | 0 | expresion = ""; |
154 | |
} |
155 | |
else |
156 | |
{ |
157 | 0 | int x = expresion.indexOf("'/"); |
158 | 0 | tokens.add(expresion.substring(1, x)); |
159 | 0 | expresion = expresion.substring(x + 2); |
160 | 0 | } |
161 | |
} |
162 | 0 | else if (slash == -1 && brace == -1) |
163 | |
{ |
164 | 0 | tokens.add(expresion); |
165 | 0 | expresion = ""; |
166 | |
} |
167 | 0 | else if ((slash > -1 && slash < brace) || brace == -1) |
168 | |
{ |
169 | 0 | if (slash == 0) |
170 | |
{ |
171 | 0 | if (expresion.charAt(1) == '\'') |
172 | |
{ |
173 | 0 | int x = expresion.indexOf("'", 2); |
174 | 0 | tokens.add(expresion.substring(2, x)); |
175 | 0 | expresion = expresion.substring(x + 1); |
176 | 0 | } |
177 | 0 | else if (expresion.charAt(1) == '[') |
178 | |
{ |
179 | 0 | int i = expresion.indexOf("]", 1); |
180 | 0 | tokens.add(expresion.substring(0, i)); |
181 | 0 | expresion = expresion.substring(i + 1); |
182 | 0 | } |
183 | |
else |
184 | |
{ |
185 | 0 | expresion = expresion.substring(slash + 1); |
186 | |
} |
187 | |
} |
188 | |
else |
189 | |
{ |
190 | 0 | tokens.add(expresion.substring(0, slash)); |
191 | 0 | expresion = expresion.substring(slash + 1); |
192 | |
} |
193 | |
} |
194 | 0 | else if (brace > 0) |
195 | |
{ |
196 | 0 | tokens.add(expresion.substring(0, brace)); |
197 | 0 | expresion = expresion.substring(brace); |
198 | |
|
199 | |
} |
200 | |
else |
201 | |
{ |
202 | 0 | int i = expresion.indexOf("]", brace); |
203 | 0 | tokens.add(expresion.substring(0, i + 1)); |
204 | 0 | expresion = expresion.substring(i + 1); |
205 | |
} |
206 | 0 | } |
207 | 0 | return tokens; |
208 | |
} |
209 | |
|
210 | |
@Override |
211 | |
public boolean equals(Object obj) |
212 | |
{ |
213 | 0 | return node.equals(obj); |
214 | |
} |
215 | |
|
216 | |
@Override |
217 | |
public int hashCode() |
218 | |
{ |
219 | 0 | return node.hashCode(); |
220 | |
} |
221 | |
|
222 | |
@Override |
223 | |
public String toString() |
224 | |
{ |
225 | 0 | return node.toString(); |
226 | |
} |
227 | |
} |