KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > frontends > CLISetup


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.frontends;
88
89 import java.util.Properties JavaDoc;
90 import org.apache.commons.cli.CommandLine;
91 import org.apache.commons.cli.CommandLineParser;
92 import org.apache.commons.cli.HelpFormatter;
93 import org.apache.commons.cli.Options;
94 import org.apache.commons.cli.ParseException;
95 import org.apache.commons.cli.PosixParser;
96 import org.codehaus.spice.salt.i18n.ResourceManager;
97 import org.codehaus.spice.salt.i18n.Resources;
98
99 /**
100  * The class prepare parameters based on input options.
101  */

102 class CLISetup
103 {
104     private static final Resources REZ = ResourceManager.getPackageResources(
105         CLISetup.class );
106
107     private static final String JavaDoc DEBUG_LOG_OPT = "d";
108     private static final String JavaDoc HELP_OPT = "h";
109     private static final String JavaDoc LOG_FILE_OPT = "l";
110     private static final String JavaDoc PERSISTENT_OPT = "p";
111     private static final String JavaDoc CONFIGFILE_OPT = "f";
112     private static final String JavaDoc STDOUT_OPT = "s";
113     private static final String JavaDoc TEMPORARY_OPT = "t";
114
115     ///Parameters created by parsing CLI options
116
private final Properties JavaDoc m_parameters = new Properties JavaDoc();
117
118     ///Command used to execute program
119
private final String JavaDoc m_command;
120
121     public CLISetup( final String JavaDoc command )
122     {
123         m_command = command;
124     }
125
126     /**
127      * Initialise the options for command line parser.
128      */

129     private Options createCLOptions()
130     {
131         final Options options = new Options();
132         options.addOption( STDOUT_OPT,
133                            "std-out",
134                            false,
135                            REZ.getString( "cli.opt.configfile.stdout" ) );
136         options.addOption( HELP_OPT,
137                            "help",
138                            false,
139                            REZ.getString( "cli.opt.help.desc" ) );
140         options.addOption( CONFIGFILE_OPT,
141                            "configfile",
142                            true,
143                            REZ.getString( "cli.opt.configfile.desc" ) );
144         options.addOption( LOG_FILE_OPT,
145                            "log-file",
146                            true,
147                            REZ.getString( "cli.opt.log-file.desc" ) );
148         options.addOption( DEBUG_LOG_OPT,
149                            "debug-init",
150                            false,
151                            REZ.getString( "cli.opt.debug-init.desc" ) );
152         options.addOption( PERSISTENT_OPT,
153                            "persistent",
154                            false,
155                            REZ.getString( "cli.opt.persistent.desc" ) );
156         options.addOption( TEMPORARY_OPT,
157                            "temporary",
158                            false,
159                            REZ.getString( "cli.opt.temporary.desc" ) );
160         return options;
161     }
162
163     public Properties JavaDoc getParameters()
164     {
165         return m_parameters;
166     }
167
168     public boolean parseCommandLineOptions( final String JavaDoc[] args )
169     {
170         // create the command line parser
171
final CommandLineParser parser = new PosixParser();
172
173         // create the Options
174
final Options options = createCLOptions();
175         try
176         {
177             // parse the command line arguments
178
final CommandLine line = parser.parse( options, args );
179
180             if( line.hasOption( HELP_OPT ) )
181             {
182                 final HelpFormatter formatter = new HelpFormatter();
183                 formatter.printHelp( m_command, options );
184                 return false;
185             }
186             if( line.getArgList().size() > 0 )
187             {
188                 final String JavaDoc message = REZ.getString( "cli.error.unknown.arg" );
189                 System.err.println( message );
190                 return false;
191             }
192             if( line.hasOption( DEBUG_LOG_OPT ) )
193             {
194                 m_parameters.setProperty( "log-priority", "DEBUG" );
195             }
196             if( line.hasOption( TEMPORARY_OPT ) )
197             {
198                 m_parameters.setProperty( CLIMain.TEMPORARY, "true" );
199             }
200             if( line.hasOption( PERSISTENT_OPT ) )
201             {
202                 m_parameters.setProperty( CLIMain.TEMPORARY, "false" );
203             }
204             if( line.hasOption( CONFIGFILE_OPT ) )
205             {
206                 final String JavaDoc file = line.getOptionValue( CONFIGFILE_OPT );
207                 m_parameters.setProperty( CLIMain.CONFIGFILE, file );
208             }
209             if( line.hasOption( LOG_FILE_OPT ) )
210             {
211                 final String JavaDoc file = line.getOptionValue( LOG_FILE_OPT );
212                 m_parameters.setProperty( "log-destination", file );
213             }
214             if( line.hasOption( STDOUT_OPT ) )
215             {
216                 m_parameters.setProperty( "log-stdout", "true" );
217             }
218             return true;
219         }
220         catch( final ParseException pe )
221         {
222             final String JavaDoc message = REZ.format( "cli.error.parser",
223                                                pe.getMessage() );
224             System.err.println( message );
225             return false;
226         }
227     }
228 }
229
Popular Tags