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