-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathHelpers.java
More file actions
239 lines (216 loc) · 7.65 KB
/
Copy pathHelpers.java
File metadata and controls
239 lines (216 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.bitso.helpers;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
public class Helpers {
private static final String PATH = "src/test/java/JSONFiles/";
public static final String dateTimeFormatterZOffset = ("yyyy-MM-dd'T'HH:mm:ssZZZ");
public static final String dateTimeFormatterXOffset = ("yyyy-MM-dd'T'HH:mm:ssXXX");
private static DatatypeFactory dtf;
static {
try {
dtf = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException ex) {
System.out.println("FATAL: Cannot instantiate DatatypeFactory");
}
}
private static final List<Field> getAllFields(List<Field> fields, Class<?> type) {
fields.addAll(Arrays.asList(type.getDeclaredFields()));
if (type.getSuperclass() != null) {
fields = getAllFields(fields, type.getSuperclass());
}
return fields;
}
public static final String fieldPrinter(Object obj) {
StringBuilder sb = new StringBuilder();
sb.append("==============");
List<Field> fields = getAllFields(new ArrayList<Field>(), obj.getClass());
for (Field f : fields) {
try {
Object o = f.get(obj);
sb.append('\n');
sb.append(f.getName());
sb.append(": ");
sb.append(o);
} catch (Exception e) {
e.printStackTrace();
}
}
sb.append("\n==============\n");
return sb.toString();
}
public static final String fieldPrinter(Object object, Class<?> genericType) {
StringBuilder sb = new StringBuilder();
sb.append("==============");
Method[] methods = genericType.getDeclaredMethods();
for (Method method : methods) {
String methodName = method.getName();
if (methodName.startsWith("get")) {
try {
Object methodExecutionResult = method.invoke(object);
sb.append('\n');
sb.append(methodName);
sb.append(": ");
sb.append(methodExecutionResult);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
sb.append("\n==============\n");
return sb.toString();
}
public static final void printStackTrace(PrintStream out) {
StringBuilder sb = new StringBuilder();
sb.append("Printing Stack Trace\n");
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
sb.append('\t');
sb.append(ste);
sb.append('\n');
}
out.print(sb);
}
public static final void printStackTrace() {
printStackTrace(System.err);
}
public static JSONObject parseJson(String json) throws JSONException {
return new JSONObject(json);
}
public static int getInt(JSONObject o, String key) {
if (o.has(key)) {
return o.getInt(key);
} else {
System.err.println("No " + key + ": " + o);
Helpers.printStackTrace();
}
return -1;
}
public static String getString(JSONObject o, String key) {
if (o.has(key)) {
return o.getString(key);
} else {
System.err.println("No " + key + ": " + o);
Helpers.printStackTrace();
}
return null;
}
public static BigDecimal getBD(JSONObject o, String key) {
if (o.has(key)) {
String value = o.isNull(key) ? "null" : o.getString(key);
value = (value.equals("null") || value.length() == 0) ? "0" : value.trim();
return new BigDecimal(value);
} else {
System.err.println("No " + key + ": " + o);
Helpers.printStackTrace();
}
return null;
}
public static Integer getInteger(JSONObject o, String key) {
if (o.has(key)) {
return o.getInt(key);
} else {
System.err.println("No " + key + ": " + o);
Helpers.printStackTrace();
}
return null;
}
public static Date getZonedDatetime(JSONObject o, String key) {
if (o.has(key)) {
final String date = o.getString(key);
try {
return new SimpleDateFormat(dateTimeFormatterZOffset).parse(date);
} catch (ParseException e) {
try {
return new SimpleDateFormat(dateTimeFormatterXOffset).parse(date);
} catch (ParseException e2) {
try {
return dtf.newXMLGregorianCalendar(date).toGregorianCalendar().getTime();
} catch (IllegalArgumentException e3) {
Helpers.printStackTrace();
}
}
}
} else {
System.err.println("No " + key + ": " + o);
Helpers.printStackTrace();
}
return null;
}
public static String[] getJSONArrayElements(JSONArray arrray) {
int totalElements = arrray.length();
String[] elements = new String[totalElements];
for (int i = 0; i < totalElements; i++) {
elements[i] = arrray.getString(i);
}
return elements;
}
public static JSONObject getJSONFromFile(String fileName) throws JSONException {
String jsonString = getJSONString(fileName);
return Helpers.parseJson(jsonString);
}
private static String getJSONString(String fileName) {
BufferedReader br = null;
String line = "";
StringBuffer sb = new StringBuffer();
try {
FileReader fr = new FileReader(PATH + fileName);
br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
sb.append(line);
}
line = sb.toString();
} catch (IOException e) {
e.printStackTrace();
line = null;
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return line;
}
public static String convertInputStreamToString(InputStream inputStream) {
if (inputStream == null) {
return null;
}
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
return stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}