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