KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > common > Main


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.common;
25
26 // ToolBox imports
27
import org.enhydra.tool.ToolBoxInfo;
28
29 // Standard imports
30
import java.io.File JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34 public class Main {
35
36     //
37
static ResourceBundle JavaDoc res =
38         ResourceBundle.getBundle("org.enhydra.tool.common.Res"); // nores
39

40     //
41
private final String JavaDoc PARAM_ARCHIVE = "-archive"; // nores
42
private final String JavaDoc PARAM_CONFIG = "-config"; // nores
43
private final String JavaDoc PARAM_CODEGEN = "-codegen"; // nores
44
private final String JavaDoc PARAM_INFO = "-info"; // nores
45

46     //
47
private boolean archive = false;
48     private boolean config = false;
49     private boolean codeGen = false;
50     private boolean info = false;
51
52     public Main() {
53     }
54
55     public static void main(String JavaDoc[] args) {
56         Main main = new Main();
57
58         main.init(args);
59     }
60
61     private String JavaDoc[] getToolArgs(String JavaDoc[] args) {
62         ArrayList JavaDoc list = null;
63         String JavaDoc[] toolArgs = new String JavaDoc[0];
64
65         if (args.length >= 1) {
66             list = new ArrayList JavaDoc(Arrays.asList(args));
67             list.remove(0);
68             list.trimToSize();
69             toolArgs = new String JavaDoc[list.size()];
70             toolArgs = (String JavaDoc[]) list.toArray(toolArgs);
71         }
72         return toolArgs;
73     }
74
75     private void init(String JavaDoc[] args) {
76         try {
77             parseArgs(args);
78             if (isArchive()) {
79                 org.enhydra.tool.archive.Main.main(getToolArgs(args));
80             } else if (isCodeGen()) {
81                 org.enhydra.tool.codegen.Main.main(getToolArgs(args));
82             } else if (isConfig()) {
83                 org.enhydra.tool.configure.Main.main(getToolArgs(args));
84             } else if (isInfo()) {
85                 org.enhydra.tool.ToolBoxInfo.main(getToolArgs(args));
86             }
87         } catch (ArgumentException e) {
88             System.out.println(ResUtil.format(res.getString("Error_0_"),
89                                               e.getMessage()));
90             showUsage();
91         }
92     }
93
94     private void parseArgs(String JavaDoc[] args) throws ArgumentException {
95         if (args.length < 1) {
96             throw new ArgumentException(res.getString("Too_few_arguments"));
97         } else if (!isValidTool(args[0])) {
98             throw new ArgumentException(ResUtil.format(res.getString("Tool_not_found_0_"),
99                                                        args[0]));
100         }
101     }
102
103     private boolean isArchive() {
104         return archive;
105     }
106
107     private void setArchive(boolean b) {
108         archive = b;
109     }
110
111     private boolean isConfig() {
112         return config;
113     }
114
115     private void setConfig(boolean b) {
116         config = b;
117     }
118
119     private boolean isCodeGen() {
120         return codeGen;
121     }
122
123     private void setCodeGen(boolean b) {
124         codeGen = b;
125     }
126
127     private boolean isInfo() {
128         return info;
129     }
130
131     private void setInfo(boolean b) {
132         info = b;
133     }
134
135     private boolean isValidTool(String JavaDoc tool) {
136         boolean valid = false;
137
138         if (tool == null) {
139
140             // done
141
} else if (tool.equalsIgnoreCase(PARAM_ARCHIVE)) {
142             setArchive(true);
143             valid = true;
144         } else if (tool.equalsIgnoreCase(PARAM_CONFIG)) {
145             setConfig(true);
146             valid = true;
147         } else if (tool.equalsIgnoreCase(PARAM_CODEGEN)) {
148             setCodeGen(true);
149             valid = true;
150         } else if (tool.equalsIgnoreCase(PARAM_INFO)) {
151             setInfo(true);
152             valid = true;
153         }
154         return valid;
155     }
156
157     private void showUsage() {
158         System.out.println(res.getString("Kelp_ToolBox") + ' '
159                            + ToolBoxInfo.getToolBoxVersion());
160         System.out.println(ToolBoxInfo.getCopyright());
161         System.out.println(res.getString("Usage1"));
162         System.out.println(res.getString("Usage2"));
163         System.out.println(res.getString("Usage3"));
164     }
165
166     class ArgumentException extends Exception JavaDoc {
167         public ArgumentException(String JavaDoc msg) {
168             super(msg);
169         }
170
171     }
172 }
173
Popular Tags