KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > scripts > OptionsManager_impl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2005 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Mathieu Vadet.
23 Contributor(s): .
24
25 ====================================================================
26 $Id: OptionsManager_impl.java,v 1.1 2005/06/17 15:58:27 merle Exp $
27 ====================================================================*/

28
29 package org.objectweb.ccm.scripts;
30
31 /**
32  * A options manager implementation.
33  *
34  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
35  * <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
36  *
37  * @version 0.3
38  */

39
40 public abstract class OptionsManager_impl
41                 implements OptionsManager
42 {
43
44     // ==================================================================
45
//
46
// Constructor.
47
//
48
// ==================================================================
49

50     /**
51      ** Constructor.
52      **/

53     public
54     OptionsManager_impl(String JavaDoc indentation)
55     {
56         indentation_ = indentation;
57         options_ = new java.util.Hashtable JavaDoc();
58         others_ = new StringBuffer JavaDoc("");
59     }
60
61     // ==================================================================
62
//
63
// Internal state.
64
//
65
// ==================================================================
66

67     protected String JavaDoc indentation_;
68
69     protected StringBuffer JavaDoc others_;
70
71     protected java.util.Hashtable JavaDoc options_;
72
73     // ==================================================================
74
//
75
// Internal methods.
76
//
77
// ==================================================================
78

79     /**
80      ** Display the options usage for options beginning with prefix.
81      **/

82     protected void
83     optionsUsage(String JavaDoc prefix)
84     {
85         java.util.Enumeration JavaDoc elements = options_.elements();
86         Option opt = null;
87         while (elements.hasMoreElements())
88         {
89             opt = (Option)elements.nextElement();
90             if (opt.cmd_.startsWith(prefix))
91                 System.err.println(opt.usage_);
92         }
93     }
94
95     /**
96      ** Analyse options on a command line.
97      ** Return -1 if an error occurs.
98      **/

99     protected int
100     analyseOptions(String JavaDoc[] commandline,int n)
101     {
102         Option opt = null;
103         for (int i=0;i<commandline.length-n;i++)
104     {
105             if (options_.containsKey(commandline[i]))
106             {
107                 try
108                 {
109                     opt = (Option)options_.get(commandline[i]);
110                     for(int j=0;j<opt.paramNumber_;j++)
111                         opt.value_.add(commandline[i+j+1]);
112                     opt.isSet_ = true;
113                     i += opt.paramNumber_;
114                 }
115                 catch (java.lang.ArrayIndexOutOfBoundsException JavaDoc e)
116                 {
117                     System.err.println("Missing parameter for option " + opt.cmd_);
118                     return -1;
119                 }
120             }
121             else
122             {
123                 others_.append(" "+commandline[i]+" ");
124             }
125         }
126         return 0;
127     }
128
129     // ==================================================================
130
//
131
// Public methods.
132
//
133
// ==================================================================
134

135     /**
136      ** Add the option opt in the possible options list.
137      **/

138     public void
139     addOption(Option opt)
140     {
141         options_.put(opt.cmd_,opt);
142     }
143
144     /**
145      ** Check if the option opt is set to true.
146      **/

147     public boolean
148     isSet(String JavaDoc opt)
149     {
150         if (!options_.containsKey(opt))
151             return false;
152         return ((Option)options_.get(opt)).isSet_;
153     }
154
155     /**
156      ** Return the values associated with the option opt.
157      **/

158     public String JavaDoc[]
159     get(String JavaDoc opt)
160     {
161         if (!options_.containsKey(opt))
162             return null;
163         return (String JavaDoc[])((Option)options_.get(opt)).value_.toArray(new String JavaDoc[0]);
164     }
165
166     /**
167      ** Return all the options that are not recognized and their possible values.
168      ** We intend it to be used for returning the options that will be passed to another compiler.
169      **/

170     public String JavaDoc
171     getOthers()
172     {
173         return others_.toString();
174     }
175
176     /**
177      ** Display the usage of the OMG IDL compiler.
178      **/

179     public abstract void
180     usage();
181
182     /**
183      ** Analyse a command line.
184      ** Return -1 if an error occurs.
185      **/

186     public abstract int
187     analyse(String JavaDoc[] commandline);
188  
189 }
190
Popular Tags