View Javadoc

1   /*
2   *
3   * ====================================================================
4   *
5   * Copyright 2004 The Apache Software Foundation 
6   *
7   * Licensed under the Apache License, Version 2.0 (the "License");
8   * you may not use this file except in compliance with the License.
9   * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
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  public class MainWrapper {
41      static {
42          // FIXME - install() method has been removed
43          //XMLMessageProvider.install("contract/util", Thread.currentThread().getContextClassLoader().getResourceAsStream("util.xml"));
44      }
45      
46      private static MessageBundle usage = new MessageBundle("usage");
47  
48      public static Result main(String []args, Processor processor) {
49          Context context = new VMContext();
50          ParameterDescriptor[] parameterDescriptors = processor.getParameterDescriptors();
51          Map parameters = new HashMap();
52          for ( int i = 0; i < parameterDescriptors.length; i++ ) {
53              if ( i < args.length ) {
54                  parameters.put(parameterDescriptors[i].getName(), args[i]);
55              }
56          }
57          try {
58              return Executor.process(processor, parameters, context);
59          } catch ( ContractViolationException exception ) {
60              System.out.println(exception.getErrorMessage().getTitle(Locale.getDefault()));
61              System.out.print(exception.getErrorMessage().getDetails(Locale.getDefault()));
62              Throwable throwable = (Throwable)exception;
63              while ( throwable.getCause() != null ) {
64                  throwable = throwable.getCause();
65                  if ( throwable instanceof LocalizedException ) { 
66                     System.out.print(" "+((LocalizedException)throwable).getErrorMessage().getDetails(Locale.getDefault()));
67                  } else {
68                      System.out.print(" "+throwable.getMessage());
69                  }
70              }
71              System.out.println();
72              System.out.println();
73              printUsage(processor);
74          } catch ( Exception exception ) {
75              exception.printStackTrace();
76          }
77          return null;
78      }
79  
80      public static void printUsage(Processor processor) {
81          System.out.println(usage.getTitle(Locale.getDefault()));
82          System.out.println(usage.getText(Locale.getDefault()));
83          ParameterDescriptor[] parameterDescriptors = processor.getParameterDescriptors();
84          for ( int i = 0; i < parameterDescriptors.length; i++ ) {
85              System.out.print((i+1)+". ");
86              printParameterInfo(parameterDescriptors[i]);
87          }
88      }
89  
90      public static void printParameterInfo(ParameterDescriptor parameterDescriptor) {
91          System.out.println(parameterDescriptor.getDescription().getTitle(Locale.getDefault()));
92          System.out.println(parameterDescriptor.getDescription().getText(Locale.getDefault()));
93          System.out.println(parameterDescriptor.getConstraints().verboseConstraints().getText(Locale.getDefault()));
94      }
95  
96      public static void printException(ContractViolationException exception) {
97          System.out.println(exception.getErrorMessage().getTitle(Locale.getDefault()));
98          System.out.print(exception.getErrorMessage().getDetails(Locale.getDefault()));
99          Throwable throwable = (Throwable)exception;
100         while ( throwable.getCause() != null ) {
101             throwable = throwable.getCause();
102             if ( throwable instanceof LocalizedException ) { 
103                 System.out.print(" "+((LocalizedException)throwable).getErrorMessage().getDetails(Locale.getDefault()));
104             } else {
105                 System.out.print(" "+throwable.getMessage());
106             }
107         }
108         System.out.println();
109     }
110 
111     public static void printInformations(List informations) {
112         for ( Iterator i = informations.iterator(); i.hasNext(); ) {
113             Information information = (Information)i.next();
114             System.out.println("Folgendes: "+information.getErrorBundle().getText(Locale.getDefault()));
115         }
116     }
117 }