1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.apache.commons.contract.util; |
21 | |
|
22 | |
import java.util.HashMap; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
import java.util.Locale; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import org.apache.commons.contract.Context; |
29 | |
import org.apache.commons.contract.ContractViolationException; |
30 | |
import org.apache.commons.contract.Executor; |
31 | |
import org.apache.commons.contract.Information; |
32 | |
import org.apache.commons.contract.Processor; |
33 | |
import org.apache.commons.contract.Result; |
34 | |
import org.apache.commons.contract.context.VMContext; |
35 | |
import org.apache.commons.contract.descriptor.ParameterDescriptor; |
36 | |
import org.apache.commons.i18n.LocalizedException; |
37 | |
import org.apache.commons.i18n.XMLMessageProvider; |
38 | |
import org.apache.commons.i18n.bundles.MessageBundle; |
39 | |
|
40 | 0 | public class MainWrapper { |
41 | |
static { |
42 | |
|
43 | |
|
44 | |
} |
45 | |
|
46 | 0 | private static MessageBundle usage = new MessageBundle("usage"); |
47 | |
|
48 | |
public static Result main(String []args, Processor processor) { |
49 | 0 | Context context = new VMContext(); |
50 | 0 | ParameterDescriptor[] parameterDescriptors = processor.getParameterDescriptors(); |
51 | 0 | Map parameters = new HashMap(); |
52 | 0 | for ( int i = 0; i < parameterDescriptors.length; i++ ) { |
53 | 0 | if ( i < args.length ) { |
54 | 0 | parameters.put(parameterDescriptors[i].getName(), args[i]); |
55 | |
} |
56 | |
} |
57 | |
try { |
58 | 0 | return Executor.process(processor, parameters, context); |
59 | 0 | } catch ( ContractViolationException exception ) { |
60 | 0 | System.out.println(exception.getErrorMessage().getTitle(Locale.getDefault())); |
61 | 0 | System.out.print(exception.getErrorMessage().getDetails(Locale.getDefault())); |
62 | 0 | Throwable throwable = (Throwable)exception; |
63 | 0 | while ( throwable.getCause() != null ) { |
64 | 0 | throwable = throwable.getCause(); |
65 | 0 | if ( throwable instanceof LocalizedException ) { |
66 | 0 | System.out.print(" "+((LocalizedException)throwable).getErrorMessage().getDetails(Locale.getDefault())); |
67 | |
} else { |
68 | 0 | System.out.print(" "+throwable.getMessage()); |
69 | |
} |
70 | |
} |
71 | 0 | System.out.println(); |
72 | 0 | System.out.println(); |
73 | 0 | printUsage(processor); |
74 | 0 | } catch ( Exception exception ) { |
75 | 0 | exception.printStackTrace(); |
76 | 0 | } |
77 | 0 | return null; |
78 | |
} |
79 | |
|
80 | |
public static void printUsage(Processor processor) { |
81 | 0 | System.out.println(usage.getTitle(Locale.getDefault())); |
82 | 0 | System.out.println(usage.getText(Locale.getDefault())); |
83 | 0 | ParameterDescriptor[] parameterDescriptors = processor.getParameterDescriptors(); |
84 | 0 | for ( int i = 0; i < parameterDescriptors.length; i++ ) { |
85 | 0 | System.out.print((i+1)+". "); |
86 | 0 | printParameterInfo(parameterDescriptors[i]); |
87 | |
} |
88 | 0 | } |
89 | |
|
90 | |
public static void printParameterInfo(ParameterDescriptor parameterDescriptor) { |
91 | 0 | System.out.println(parameterDescriptor.getDescription().getTitle(Locale.getDefault())); |
92 | 0 | System.out.println(parameterDescriptor.getDescription().getText(Locale.getDefault())); |
93 | 0 | System.out.println(parameterDescriptor.getConstraints().verboseConstraints().getText(Locale.getDefault())); |
94 | 0 | } |
95 | |
|
96 | |
public static void printException(ContractViolationException exception) { |
97 | 0 | System.out.println(exception.getErrorMessage().getTitle(Locale.getDefault())); |
98 | 0 | System.out.print(exception.getErrorMessage().getDetails(Locale.getDefault())); |
99 | 0 | Throwable throwable = (Throwable)exception; |
100 | 0 | while ( throwable.getCause() != null ) { |
101 | 0 | throwable = throwable.getCause(); |
102 | 0 | if ( throwable instanceof LocalizedException ) { |
103 | 0 | System.out.print(" "+((LocalizedException)throwable).getErrorMessage().getDetails(Locale.getDefault())); |
104 | |
} else { |
105 | 0 | System.out.print(" "+throwable.getMessage()); |
106 | |
} |
107 | |
} |
108 | 0 | System.out.println(); |
109 | 0 | } |
110 | |
|
111 | |
public static void printInformations(List informations) { |
112 | 0 | for ( Iterator i = informations.iterator(); i.hasNext(); ) { |
113 | 0 | Information information = (Information)i.next(); |
114 | 0 | System.out.println("Folgendes: "+information.getErrorBundle().getText(Locale.getDefault())); |
115 | 0 | } |
116 | 0 | } |
117 | |
} |