1 |
| package com.sourceforge.jpatterns.utils; |
2 |
| |
3 |
| import com.sourceforge.jpatterns.core.JPConstants; |
4 |
| import com.sourceforge.jpatterns.core.JPException; |
5 |
| import com.sourceforge.jpatterns.core.configuration.exceptions.JPConfigException; |
6 |
| import com.sourceforge.jpatterns.core.configuration.exceptions.JPInitializationException; |
7 |
| import com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigBaseBean; |
8 |
| import com.sourceforge.jpatterns.schema.CastorGroupTypeItem; |
9 |
| import com.sourceforge.jpatterns.schema.CastorNameScopePriorityType; |
10 |
| import com.sourceforge.jpatterns.schema.CastorSectionType; |
11 |
| import com.sourceforge.jpatterns.schema.Config; |
12 |
| import com.sourceforge.jpatterns.schema.Factory; |
13 |
| import com.sourceforge.jpatterns.schema.JPatternsConfig; |
14 |
| import com.sourceforge.jpatterns.schema.JPatternsConfigItem; |
15 |
| import com.zmicer.utils.InputArgumentUtils; |
16 |
| import com.zmicer.utils.LoggingUtils; |
17 |
| import com.zmicer.utils.ReflexionUtils; |
18 |
| import com.zmicer.utils.StringUtils; |
19 |
| import org.apache.log4j.Logger; |
20 |
| import org.exolab.castor.xml.Unmarshaller; |
21 |
| |
22 |
| import java.io.File; |
23 |
| import java.io.FileReader; |
24 |
| import java.lang.reflect.Method; |
25 |
| import java.util.ArrayList; |
26 |
| import java.util.Arrays; |
27 |
| import java.util.List; |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public class CastorUtils |
36 |
| { |
37 |
| |
38 |
| |
39 |
| |
40 |
| final public static Logger LOG = Logger.getLogger(CastorUtils.class); |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| final protected static String GROUP_ITEM_CLASS_NAME = CastorGroupTypeItem.class.getName(); |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
140
| public static JPatternsConfig getJPatternsConfig(final File file)
|
58 |
| { |
59 |
140
| InputArgumentUtils.checkObjects(file);
|
60 |
139
| if (!file.isFile())
|
61 |
| { |
62 |
1
| throw new IllegalArgumentException("The provided object is not file.");
|
63 |
| } |
64 |
138
| try
|
65 |
| { |
66 |
138
| FileReader fileReader = new FileReader(file);
|
67 |
138
| Unmarshaller unmarshaller = new Unmarshaller(JPatternsConfig.class);
|
68 |
138
| Object object = unmarshaller.unmarshal(fileReader);
|
69 |
138
| if (null == object)
|
70 |
| { |
71 |
0
| final String message = "Can not instantiate the object using unmarshal operation.";
|
72 |
0
| LOG.debug(message);
|
73 |
0
| throw new IllegalStateException(message);
|
74 |
| } |
75 |
138
| if (!(object instanceof JPatternsConfig))
|
76 |
| { |
77 |
0
| final String message = "The object instantiated using operation unmarshal should be the type JPatternsConfig.";
|
78 |
0
| LOG.debug(message);
|
79 |
0
| throw new IllegalStateException(message);
|
80 |
| } |
81 |
138
| return (JPatternsConfig) object;
|
82 |
| } |
83 |
| catch (Exception e) |
84 |
| { |
85 |
0
| LoggingUtils.logException(LOG, e, null, null);
|
86 |
0
| throw new JPInitializationException(e);
|
87 |
| } |
88 |
| } |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
229
| public static List<CastorSectionType> extractCastorSectionTypeObjects(final JPatternsConfig config)
|
102 |
| { |
103 |
229
| InputArgumentUtils.checkObjects(config);
|
104 |
| |
105 |
228
| final List<CastorSectionType> result = new ArrayList<CastorSectionType>();
|
106 |
228
| final JPatternsConfigItem[] configItems = config.getJPatternsConfigItem();
|
107 |
| |
108 |
| |
109 |
228
| if (configItems.length == 0)
|
110 |
| { |
111 |
48
| return result;
|
112 |
| } |
113 |
180
| final JPatternsConfigItem configItem = new JPatternsConfigItem();
|
114 |
| |
115 |
180
| final String methodPrefix = "get";
|
116 |
180
| final Method[] methods = configItem.getClass().getMethods();
|
117 |
180
| final List<Method> methodsWeNeed = new ArrayList<Method>();
|
118 |
180
| for (final Method method : methods)
|
119 |
| { |
120 |
2880
| final Class returnedType = method.getReturnType();
|
121 |
| |
122 |
2880
| if (method.getName().contains(methodPrefix) && !method.getName().contains("getClass"))
|
123 |
| { |
124 |
| |
125 |
| |
126 |
720
| try
|
127 |
| { |
128 |
720
| final Object objectToCheck = returnedType.newInstance();
|
129 |
720
| if (objectToCheck instanceof CastorSectionType)
|
130 |
| { |
131 |
540
| methodsWeNeed.add(method);
|
132 |
| } |
133 |
| } |
134 |
| catch (Exception e) |
135 |
| { |
136 |
| |
137 |
0
| LoggingUtils.logException(LOG, e, null, null);
|
138 |
| } |
139 |
| } |
140 |
| } |
141 |
| |
142 |
| |
143 |
| |
144 |
180
| for (final JPatternsConfigItem item : configItems)
|
145 |
| { |
146 |
542
| for (final Method method : methodsWeNeed)
|
147 |
| { |
148 |
1626
| try
|
149 |
| { |
150 |
1626
| final Object object = method.invoke(item);
|
151 |
| |
152 |
1626
| if (object instanceof CastorSectionType)
|
153 |
| { |
154 |
542
| result.add((CastorSectionType) object);
|
155 |
| } |
156 |
| } |
157 |
| catch (Exception ex) |
158 |
| { |
159 |
0
| LoggingUtils.logException(LOG, ex, null, null);
|
160 |
| } |
161 |
| } |
162 |
| } |
163 |
180
| return result;
|
164 |
| } |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
769
| public static List<CastorNameScopePriorityType> extractCastorNameScopePriorityTypeObjects(final CastorSectionType sectionType)
|
179 |
| { |
180 |
769
| InputArgumentUtils.checkObjects(sectionType);
|
181 |
768
| final String methodPrefix = "get";
|
182 |
768
| final String choiceValueMethod = "getChoiceValue";
|
183 |
768
| final String groupItemName = "";
|
184 |
| |
185 |
768
| final Method[] methods = sectionType.getClass().getMethods();
|
186 |
768
| final List<Method> methodsWeNeed = new ArrayList<Method>();
|
187 |
768
| for (final Method method : methods)
|
188 |
| { |
189 |
24564
| final Class returnedType = method.getReturnType();
|
190 |
24564
| if (method.getName().contains(methodPrefix) && returnedType.isArray())
|
191 |
| { |
192 |
767
| final String arrayMembersClassName = ReflexionUtils.getClassNameOfArrayMembers(returnedType);
|
193 |
767
| try
|
194 |
| { |
195 |
| |
196 |
767
| final Object objectToCheck = sectionType.getClass().getClassLoader().loadClass(arrayMembersClassName).newInstance();
|
197 |
767
| if (objectToCheck instanceof CastorNameScopePriorityType || objectToCheck instanceof CastorGroupTypeItem)
|
198 |
| { |
199 |
767
| methodsWeNeed.add(method);
|
200 |
| } |
201 |
| } |
202 |
| catch (Exception e) |
203 |
| { |
204 |
| |
205 |
0
| LoggingUtils.logException(LOG, e, null, null);
|
206 |
| } |
207 |
| } |
208 |
| } |
209 |
| |
210 |
| |
211 |
| |
212 |
768
| final List<CastorNameScopePriorityType> result = new ArrayList<CastorNameScopePriorityType>();
|
213 |
768
| for (final Method method : methodsWeNeed)
|
214 |
| { |
215 |
767
| try
|
216 |
| { |
217 |
767
| final Object object = method.invoke(sectionType);
|
218 |
| |
219 |
767
| if (null != object)
|
220 |
| { |
221 |
767
| if (object instanceof CastorNameScopePriorityType[])
|
222 |
| { |
223 |
758
| result.addAll(Arrays.asList((CastorNameScopePriorityType[]) object));
|
224 |
| } |
225 |
9
| else if (object instanceof CastorGroupTypeItem[])
|
226 |
| { |
227 |
9
| CastorGroupTypeItem[] groupItems = (CastorGroupTypeItem[]) object;
|
228 |
9
| for (CastorGroupTypeItem groupItem : groupItems)
|
229 |
| { |
230 |
10
| if (groupItem.getChoiceValue() instanceof CastorNameScopePriorityType)
|
231 |
| { |
232 |
10
| result.add((CastorNameScopePriorityType) groupItem.getChoiceValue());
|
233 |
| } |
234 |
| } |
235 |
| } |
236 |
| } |
237 |
| } |
238 |
| catch (Exception ex) |
239 |
| { |
240 |
0
| LoggingUtils.logException(LOG, ex, null, null);
|
241 |
| } |
242 |
| } |
243 |
768
| return result;
|
244 |
| } |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
106
| public static void validateAndNormalizeScopesPriorities(final JPatternsConfig config)
|
267 |
| { |
268 |
106
| InputArgumentUtils.checkObjects(config);
|
269 |
| |
270 |
105
| final boolean configHasDefaultScope = (null != config.getDefaultScope() && !"".equals(config.getDefaultScope()));
|
271 |
| |
272 |
105
| final String defaultScope = (configHasDefaultScope) ? config.getDefaultScope() :
|
273 |
| com.sourceforge.jpatterns.core.JPConstants.DEFAULT_SCOPE_NAME; |
274 |
| |
275 |
105
| if (!configHasDefaultScope)
|
276 |
| { |
277 |
43
| config.setDefaultScope(defaultScope);
|
278 |
| } |
279 |
| |
280 |
| |
281 |
105
| final List<CastorSectionType> castorSectionTypes = CastorUtils.extractCastorSectionTypeObjects(config);
|
282 |
105
| for (final CastorSectionType castorSectionType : castorSectionTypes)
|
283 |
| { |
284 |
268
| if (null == castorSectionType.getScope() || "".equals(castorSectionType.getScope()))
|
285 |
| { |
286 |
187
| castorSectionType.setScope(defaultScope);
|
287 |
| } |
288 |
268
| setValidatePriority(castorSectionType, false);
|
289 |
| |
290 |
| |
291 |
268
| final List<CastorNameScopePriorityType> items = extractCastorNameScopePriorityTypeObjects(castorSectionType);
|
292 |
268
| for (final CastorNameScopePriorityType item : items)
|
293 |
| { |
294 |
| |
295 |
616
| if (null == item.getScope() || "".equals(item.getScope()))
|
296 |
| { |
297 |
393
| item.setScope(castorSectionType.getScope());
|
298 |
| } |
299 |
616
| setValidatePriority(item, false);
|
300 |
| } |
301 |
| } |
302 |
| } |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
| |
311 |
| |
312 |
| |
313 |
| |
314 |
| |
315 |
| |
316 |
19
| public static void makePrioritized(final JPatternsConfig castorConfig)
|
317 |
| { |
318 |
19
| InputArgumentUtils.checkObjects(castorConfig);
|
319 |
| |
320 |
18
| final List<CastorSectionType> castorSectionTypes = CastorUtils.extractCastorSectionTypeObjects(castorConfig);
|
321 |
18
| for (final CastorSectionType castorSectionType : castorSectionTypes)
|
322 |
| { |
323 |
4
| setValidatePriority(castorSectionType, true);
|
324 |
| |
325 |
| |
326 |
3
| final List<CastorNameScopePriorityType> items = extractCastorNameScopePriorityTypeObjects(castorSectionType);
|
327 |
3
| for (final CastorNameScopePriorityType item : items)
|
328 |
| { |
329 |
3
| setValidatePriority(item, true);
|
330 |
| } |
331 |
| } |
332 |
| } |
333 |
| |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
| |
340 |
35
| public static void makePrioritized(final JPatternsConfigBaseBean bean)
|
341 |
| { |
342 |
35
| InputArgumentUtils.checkObjects(bean);
|
343 |
| |
344 |
35
| final List<CastorSectionType> castorSectionTypes = bean.getListOfSectionItems();
|
345 |
35
| for (final CastorSectionType castorSectionType : castorSectionTypes)
|
346 |
| { |
347 |
0
| setValidatePriority(castorSectionType, true);
|
348 |
| |
349 |
| |
350 |
0
| final List<CastorNameScopePriorityType> items = extractCastorNameScopePriorityTypeObjects(castorSectionType);
|
351 |
0
| for (final CastorNameScopePriorityType item : items)
|
352 |
| { |
353 |
0
| setValidatePriority(item, true);
|
354 |
| } |
355 |
| } |
356 |
| } |
357 |
| |
358 |
| |
359 |
| |
360 |
| |
361 |
| |
362 |
| |
363 |
| |
364 |
| |
365 |
895
| protected static void setValidatePriority(final CastorNameScopePriorityType castor, final boolean makePrioritized)
|
366 |
| { |
367 |
895
| InputArgumentUtils.checkObjects(castor);
|
368 |
894
| if (null == castor.getPriority() || "".equals(castor.getPriority()))
|
369 |
| { |
370 |
843
| castor.setPriority(String.valueOf(JPConstants.DEFAULT_PRIORITY));
|
371 |
| } |
372 |
| |
373 |
894
| if (!castor.getPriority().contains(JPConstants.PRIORITIZED_PRIOTITY_PREFIX))
|
374 |
| { |
375 |
893
| if (null == StringUtils.getInteger(castor.getPriority()))
|
376 |
| { |
377 |
2
| throw new JPConfigException("The following priority " + castor.getPriority() + " is incorrect.");
|
378 |
| } |
379 |
891
| if (makePrioritized)
|
380 |
| { |
381 |
6
| castor.setPriority(JPConstants.PRIORITIZED_PRIOTITY_PREFIX + castor.getPriority());
|
382 |
| } |
383 |
| } |
384 |
| } |
385 |
| |
386 |
| |
387 |
| |
388 |
| |
389 |
| |
390 |
| |
391 |
| |
392 |
| |
393 |
| |
394 |
| |
395 |
78
| public static int getPriority(final CastorNameScopePriorityType castor)
|
396 |
| { |
397 |
78
| InputArgumentUtils.checkObjects(castor);
|
398 |
77
| InputArgumentUtils.checkStrings(true, castor.getPriority());
|
399 |
76
| String workingStr = null;
|
400 |
76
| if (castor.getPriority().startsWith(JPConstants.PRIORITIZED_PRIOTITY_PREFIX))
|
401 |
| { |
402 |
14
| workingStr = castor.getPriority().substring(JPConstants.PRIORITIZED_PRIOTITY_PREFIX.length());
|
403 |
| } |
404 |
| else |
405 |
| { |
406 |
62
| workingStr = castor.getPriority();
|
407 |
| } |
408 |
76
| final Integer integer = StringUtils.getInteger(workingStr);
|
409 |
76
| if (null == integer)
|
410 |
| { |
411 |
2
| throw new IllegalArgumentException("Invalid priority is set to the castor [" + castor.getPriority() + "]");
|
412 |
| } |
413 |
74
| return integer;
|
414 |
| } |
415 |
| |
416 |
| |
417 |
| |
418 |
| |
419 |
| |
420 |
| |
421 |
| |
422 |
| |
423 |
| |
424 |
| |
425 |
| |
426 |
2
| public static List<Config> getConfig(final JPatternsConfig config)
|
427 |
| { |
428 |
2
| InputArgumentUtils.checkObjects(config);
|
429 |
2
| final List<Config> result = new ArrayList<Config>();
|
430 |
2
| for (JPatternsConfigItem configItem : config.getJPatternsConfigItem())
|
431 |
| { |
432 |
2
| if (null != configItem.getConfig())
|
433 |
| { |
434 |
2
| result.add(configItem.getConfig());
|
435 |
| } |
436 |
| } |
437 |
2
| return result;
|
438 |
| } |
439 |
| |
440 |
| |
441 |
| |
442 |
| |
443 |
| |
444 |
| |
445 |
| |
446 |
| |
447 |
| |
448 |
| |
449 |
| |
450 |
0
| public static List<Factory> getFactory(final JPatternsConfig config)
|
451 |
| { |
452 |
0
| InputArgumentUtils.checkObjects(config);
|
453 |
0
| final List<Factory> result = new ArrayList<Factory>();
|
454 |
0
| for (JPatternsConfigItem configItem : config.getJPatternsConfigItem())
|
455 |
| { |
456 |
0
| if (null != configItem.getFactory())
|
457 |
| { |
458 |
0
| result.add(configItem.getFactory());
|
459 |
| } |
460 |
| } |
461 |
0
| return result;
|
462 |
| } |
463 |
| |
464 |
| |
465 |
| |
466 |
| |
467 |
| |
468 |
| |
469 |
| |
470 |
| |
471 |
13
| public static void addConfig(final JPatternsConfig config, final Config configItem)
|
472 |
| { |
473 |
13
| InputArgumentUtils.checkObjects(config, configItem);
|
474 |
13
| JPatternsConfigItem item = new JPatternsConfigItem();
|
475 |
13
| item.setConfig(configItem);
|
476 |
13
| config.addJPatternsConfigItem(item);
|
477 |
| } |
478 |
| |
479 |
| |
480 |
| |
481 |
| |
482 |
| |
483 |
| |
484 |
| |
485 |
| |
486 |
0
| public static void addFactory(final JPatternsConfig config, final Factory factoryItem)
|
487 |
| { |
488 |
0
| InputArgumentUtils.checkObjects(config, factoryItem);
|
489 |
0
| JPatternsConfigItem item = new JPatternsConfigItem();
|
490 |
0
| item.setFactory(factoryItem);
|
491 |
0
| config.addJPatternsConfigItem(item);
|
492 |
| } |
493 |
| |
494 |
| |
495 |
| |
496 |
| |
497 |
| |
498 |
| |
499 |
| |
500 |
| |
501 |
| |
502 |
| |
503 |
5
| public static CastorGroupTypeItem constructGroupItem(final CastorNameScopePriorityType object)
|
504 |
| { |
505 |
5
| InputArgumentUtils.checkObjects(object);
|
506 |
4
| final String typeToBeAdded = ReflexionUtils.getBaseName(object.getClass());
|
507 |
4
| final Method[] methods = CastorGroupTypeItem.class.getMethods();
|
508 |
4
| for (final Method method : methods)
|
509 |
| { |
510 |
38
| if (method.getName().equals("set" + typeToBeAdded))
|
511 |
| { |
512 |
1
| try
|
513 |
| { |
514 |
1
| CastorGroupTypeItem groupItem = new CastorGroupTypeItem();
|
515 |
1
| method.invoke(groupItem, object);
|
516 |
1
| return groupItem;
|
517 |
| } |
518 |
| catch (Exception ex) |
519 |
| { |
520 |
0
| LoggingUtils.logException(LOG, ex, "Can not instantiate CastorGroupTypeItem", null);
|
521 |
0
| throw new JPException("Can not instantiate CastorGroupTypeItem", ex);
|
522 |
| } |
523 |
| } |
524 |
| } |
525 |
3
| return null;
|
526 |
| } |
527 |
| |
528 |
| |
529 |
| |
530 |
| |
531 |
| |
532 |
| |
533 |
| |
534 |
| |
535 |
| |
536 |
| |
537 |
| |
538 |
| |
539 |
11
| public static String toString(final CastorSectionType section, final CastorNameScopePriorityType item)
|
540 |
| { |
541 |
11
| InputArgumentUtils.checkObjects(item);
|
542 |
9
| InputArgumentUtils.checkStrings(true, item.getScope(), item.getName());
|
543 |
8
| String result = "";
|
544 |
8
| if (null != section)
|
545 |
| { |
546 |
6
| InputArgumentUtils.checkStrings(true, section.getScope(), section.getName());
|
547 |
6
| result += "Section: scope [" + section.getScope() + "], name [" + section.getName() + "];";
|
548 |
| } |
549 |
8
| return result += "Item: scope [" + item.getScope() + "], name [" + item.getName() + "];";
|
550 |
| } |
551 |
| } |