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 package org.riverock.dbrevision.utils;
27
28 import java.io.*;
29 import java.sql.Timestamp;
30 import java.text.SimpleDateFormat;
31 import java.util.*;
32
33 import javax.xml.bind.*;
34 import javax.xml.namespace.QName;
35 import javax.xml.transform.Source;
36 import javax.xml.transform.stream.StreamSource;
37
38 import org.apache.commons.lang.StringUtils;
39 import org.apache.commons.lang.time.DateFormatUtils;
40 import org.apache.log4j.Logger;
41
42 import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
43
44
45
46
47
48
49
50
51
52
53 public class Utils {
54
55
56
57 private final static Logger log = Logger.getLogger(Utils.class);
58
59
60
61
62
63
64
65
66
67 public static void putKey(final Map<String, Object> map, final String key, final Object value) {
68 Object obj = map.get(key);
69 if (obj == null) {
70 map.put(key, value);
71 return;
72 }
73
74 if (obj instanceof List) {
75 if (value instanceof List) {
76 ((List) obj).addAll((List) value);
77 }
78 else {
79 ((List) obj).add(value);
80 }
81 }
82 else {
83 List<Object> v = new ArrayList<Object>();
84 v.add(obj);
85
86 if (value instanceof List) {
87 v.addAll((List) value);
88 }
89 else {
90 v.add(value);
91 }
92
93 map.remove(key);
94 map.put(key, v);
95 }
96 }
97
98
99
100
101
102
103 public static java.sql.Timestamp getCurrentTime() {
104 return new Timestamp(System.currentTimeMillis());
105 }
106
107
108
109
110
111
112
113
114
115 public static String getStringDate( final Calendar c, final String mask, final Locale loc ) {
116 if (c == null) return null;
117 return DateFormatUtils.format(c.getTimeInMillis(), mask, c.getTimeZone(), loc);
118 }
119
120
121
122
123
124
125
126
127 public static String getStringDate( final Calendar c, final String mask ) {
128 return DateFormatUtils.format(c.getTimeInMillis(), mask, c.getTimeZone(), Locale.ENGLISH);
129 }
130
131
132
133
134
135
136
137
138 public static java.util.Date getDateWithMask( final String date, final String mask )
139 throws java.text.ParseException {
140 if (date == null || mask == null)
141 return null;
142
143 SimpleDateFormat dFormat = new SimpleDateFormat(mask);
144
145 return dFormat.parse(date);
146 }
147
148
149
150
151
152
153
154
155
156 public static String getStringDate( final java.util.Date date, final String mask, final Locale loc, final TimeZone tz) {
157 if (date == null) return null;
158
159 SimpleDateFormat df = new SimpleDateFormat(mask, loc);
160 df.setTimeZone( tz );
161
162 return df.format( date );
163 }
164
165
166
167
168
169
170
171
172 public static byte[] getXml(final Object obj, final String rootElement) throws Exception {
173 return getXml(obj, rootElement, "utf-8");
174 }
175
176
177
178
179
180
181
182
183
184 public static byte[] getXml(final Object obj, final String rootElement, final String encoding) throws JAXBException {
185 return getXml(obj, rootElement, encoding, false, null);
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199 public static byte[] getXml(final Object obj, final String rootElement, final String encoding, boolean isIndent, NamespacePrefixMapper[] namespacePrefixMappers) throws JAXBException {
200
201
202
203
204
205
206
207 ByteArrayOutputStream fos = new ByteArrayOutputStream(1000);
208
209
210
211
212
213
214
215 writeMarshalToOutputStream(obj, encoding, rootElement, fos, isIndent, namespacePrefixMappers);
216 return fos.toByteArray();
217 }
218
219
220
221
222
223
224
225
226
227
228 public static void writeMarshalToOutputStream(
229 Object obj, String encoding, String rootElement, OutputStream fos,
230 boolean isIndent, NamespacePrefixMapper[] namespacePrefixMappers) throws JAXBException {
231
232 JAXBContext jaxbContext = JAXBContext.newInstance ( obj.getClass().getPackage().getName() );
233 Marshaller marshaller = jaxbContext.createMarshaller();
234 marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
235 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isIndent);
236
237 if (namespacePrefixMappers!=null) {
238 for (NamespacePrefixMapper namespacePrefixMapper : namespacePrefixMappers) {
239 marshaller.setProperty( "com.sun.xml.bind.namespacePrefixMapper", namespacePrefixMapper );
240 }
241 }
242
243 if (rootElement != null && rootElement.trim().length() > 0) {
244
245 marshaller.marshal( new JAXBElement(new QName("",rootElement), obj.getClass(), obj ), fos);
246 }
247 else {
248 marshaller.marshal(obj, fos);
249 }
250 }
251
252
253
254
255
256
257
258
259 public static void writeToFile(final Object obj, final String fileName) throws JAXBException, FileNotFoundException {
260 writeToFile(obj, fileName, "utf-8");
261 }
262
263
264
265
266
267
268
269
270
271 public static void writeToFile(final Object obj, final String fileName, final String encoding) throws FileNotFoundException, JAXBException {
272 writeMarshalToOutputStream(obj, encoding, null, new FileOutputStream(fileName), false, null );
273 }
274
275
276
277
278
279
280
281
282 public static void writeObjectAsXml(final Object obj, OutputStream outputStream, final String encoding) throws JAXBException {
283 writeMarshalToOutputStream(obj, encoding, null, outputStream, false, null );
284 }
285
286
287
288
289
290
291
292
293
294 public static void writeObjectAsXml(final Object obj, OutputStream outputStream, String rootElement, final String encoding) throws JAXBException {
295 writeMarshalToOutputStream(obj, encoding, rootElement, outputStream, false, null );
296 }
297
298 public static <T> T getObjectFromXml(final Class<T> classType, final String str) throws JAXBException {
299 return getObjectFromXml(classType, new StreamSource(new StringReader(str)), null);
300 }
301
302
303
304
305
306
307
308
309 public static <T> T getObjectFromXml(final Class<T> classType, InputStream is) throws JAXBException {
310 return getObjectFromXml(classType, new StreamSource(is), null);
311 }
312
313 public static <T> T getObjectFromXml(final Class<T> classType, InputStream is, ValidationEventHandler handler) throws JAXBException {
314 return getObjectFromXml(classType, new StreamSource(is), handler);
315 }
316
317 public static <T> T getObjectFromXml(final Class<T> classType, Source inSrc, ValidationEventHandler handler) throws JAXBException {
318 JAXBContext jaxbContext = JAXBContext.newInstance ( classType.getPackage().getName() );
319 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
320
321 if (handler!=null) {
322 unmarshaller.setEventHandler(handler);
323 }
324
325 return unmarshaller.unmarshal(inSrc, classType).getValue();
326 }
327
328
329
330
331
332
333
334
335
336 public static Object createCustomObject(final String s)
337 throws ClassNotFoundException, InstantiationException, IllegalAccessException {
338 Object obj;
339 try {
340 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
341 if (log.isDebugEnabled()) {
342 log.debug("Starting create class object for name '" + s + "'");
343 log.debug(" class loader:\n" + classLoader +"\nhash: "+ classLoader.hashCode() );
344 }
345
346 if (s == null)
347 return null;
348
349 Class className;
350
351 if (log.isDebugEnabled())
352 log.debug("Create class for name '" + s + "'");
353
354 className = Class.forName(s, true, classLoader);
355
356 if (log.isDebugEnabled())
357 log.debug("Class for name '" + s + "' is " + className);
358
359 if (className == null)
360 throw new ClassNotFoundException("Error create class for name " + s);
361
362 if (log.isDebugEnabled())
363 log.debug("Create object for name '" + s + "'");
364
365 obj = className.newInstance();
366
367 if (log.isDebugEnabled())
368 log.debug("Object for name '" + s + "' is " + obj);
369 }
370 catch (ClassNotFoundException e) {
371 log.error("Error create reflection object for class name '" + s + "'", e);
372 throw e;
373 }
374 catch (InstantiationException e) {
375 log.error("Error create reflection object for class name '" + s + "'", e);
376 throw e;
377 }
378 catch (IllegalAccessException e) {
379 log.error("Error create reflection object for class name '" + s + "'", e);
380 throw e;
381 }
382 return obj;
383 }
384
385
386
387
388
389
390 public static String replaceStringArray( final String str_, final String repl[][]) {
391 String qqq = str_;
392 for (final String[] newVar : repl) {
393 qqq = StringUtils.replace(qqq, newVar[0], newVar[1]);
394 }
395 return qqq;
396
397 }
398
399
400
401
402
403
404 public static byte[] getBytesUTF( final String s) {
405 if (s==null)
406 return new byte[0];
407
408 try {
409 return s.getBytes("utf-8");
410 }
411 catch (java.io.UnsupportedEncodingException e) {
412 log.warn("String.getBytes(\"utf-8\") not supported");
413 return new byte[0];
414 }
415 }
416
417
418
419
420
421
422
423 public static int getStartUTF( final String s, final int maxByte) {
424 return getStartUTF(getBytesUTF(s), maxByte);
425 }
426
427
428
429
430
431
432
433 public static int getStartUTF( final byte[] b, final int maxByte) {
434 return getStartUTF(b, maxByte, 0);
435 }
436
437
438
439
440
441
442
443
444 public static int getStartUTF( final byte[] b, final int maxByte, final int offset) {
445 if (b.length <= offset)
446 return -1;
447
448 if (b.length < maxByte)
449 return b.length;
450
451 int idx = Math.min(b.length, maxByte + offset);
452
453 for (int i = idx - 1; i > offset; i--)
454 {
455 int j = (b[i] < 0?0x100 + b[i]:b[i]);
456 if (j < 0x80)
457 {
458 return i + 1;
459 }
460 }
461 return -1;
462 }
463
464
465
466
467
468 public static String getTempDir() {
469 return System.getProperty("java.io.tmpdir");
470 }
471 }