KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > parameters > Handling


1 /**
2  * $Id: Handling.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.parameters;
30
31 import com.idaremedia.antx.helpers.Strings;
32 import com.idaremedia.antx.helpers.Tk;
33
34 /**
35  * Enumeration that represents how a component (task|macro) should handle a particular
36  * kind of problem. The following list describes what each setting means:<ul>
37  * <li><span class="src">accept</span>: The value(s) should be accepted
38  * unconditionally.</li>
39  * <li><span class="src">reject</span>: The value should be rejected. Whether or
40  * not this leads to a script error is task dependent.</li>
41  * <li><span class="src">balk</span>: The value should trigger a script error.</li>
42  * <li><span class="src">ignore</span>: The value(s) should be ignored (as if
43  * it did not exist or was never presented).</li>
44  * <li><span class="src">inherit</span>: The handling behavior should be
45  * determined by the surrounding or "parent" context. What context is
46  * used is actually task dependent.</li>
47  * </ul>
48  *
49  * @since JWare/AntX 0.4
50  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
51  * @version 0.5
52  * @.safety multiple
53  * @.group api,helper
54  **/

55
56 public final class Handling extends EnumSkeleton
57 {
58     /** Index of {@linkplain #ACCEPT ACCEPT}. **/
59     public static final int ACCEPT_INDEX = 0;
60     /** Index of {@linkplain #IGNORE IGNORE}. **/
61     public static final int IGNORE_INDEX = ACCEPT_INDEX+1;
62     /** Index of {@linkplain #REJECT REJECT}. **/
63     public static final int REJECT_INDEX = IGNORE_INDEX+1;
64     /** Index of {@linkplain #BALK BALK}. **/
65     public static final int BALK_INDEX = REJECT_INDEX+1;
66     /** Index of {@linkplain #INHERIT INHERIT}. **/
67     public static final int INHERIT_INDEX = BALK_INDEX+1;
68
69
70     /** Singleton "<span class="src">accept</span>" choice. **/
71     public static final Handling ACCEPT=
72         new Handling("accept",ACCEPT_INDEX);
73
74     /** Singleton "<span class="src">reject</span>" choice. **/
75     public static final Handling REJECT=
76         new Handling("reject",REJECT_INDEX);
77
78     /** Singleton "<span class="src">ignore</span>" choice. **/
79     public static final Handling IGNORE=
80         new Handling("ignore",IGNORE_INDEX);
81
82     /** Singleton "<span class="src">balk</span>" choice. **/
83     public static final Handling BALK=
84         new Handling("balk",BALK_INDEX);
85
86     /** Singleton "<span class="src">inherit</span>" choice. **/
87     public static final Handling INHERIT=
88         new Handling("inherit",INHERIT_INDEX);
89
90
91     /**
92      * Required bean void constructor for Ant's introspector.
93      **/

94     public Handling()
95     {
96         super();
97     }
98
99
100     /**
101      * Use to create public singletons. Ensures this enum is
102      * initialized as if with the default Ant Introspector
103      * helper thingy.
104      **/

105     private Handling(String JavaDoc v, int i)
106     {
107         super(v);
108     }
109
110
111     /**
112      * Returns copy of all possible source values as an ordered
113      * string array. Note: ordering should be same as our
114      * singleton indices.
115      **/

116     public String JavaDoc[] getValues()
117     {
118         return new String JavaDoc[] {"accept", "ignore",
119                              "reject", "balk", "inherit"};
120     };
121
122
123
124     /**
125      * Helper that converts a scalar to a known Handling.
126      * Returns <i>null</i> if value does not match any of expected
127      * source.
128      * @param i the index to be matched
129      **/

130     public static Handling from(int i)
131     {
132         if (i==IGNORE.index) { return IGNORE; }
133         if (i==BALK.index) { return BALK; }
134         if (i==ACCEPT.index) { return ACCEPT; }
135         if (i==INHERIT.index) { return INHERIT; }
136         if (i==REJECT.index) { return REJECT; }
137         return null;
138     }
139
140
141     /**
142      * Same as {@linkplain #from(int) from(int)} but with a
143      * default value if value does not match any known
144      * Handling's index.
145      * @param i the index to be matched
146      * @param dflt the default Handling if necessary
147      **/

148     public static Handling from(int i, Handling dflt)
149     {
150         Handling choice= from(i);
151         return (choice==null) ? dflt : choice;
152     }
153
154
155     /**
156      * Helper that converts a string to a known Handling
157      * singleton. Returns <i>null</i> if string unrecognized. String
158      * can be either Handling's symbolic name or its index.
159      **/

160     public static Handling from(String JavaDoc s)
161     {
162         if (s!=null && s.length()>1) {
163             s = Tk.lowercaseFrom(s);
164             if (Character.isDigit(s.charAt(0))) {
165                 try { return from(Integer.parseInt(s)); }
166                 catch(Exception JavaDoc nfx) {/*burp*/}
167             } else {
168                 if (IGNORE.value.equals(s)) { return IGNORE; }
169                 if (INHERIT.value.equals(s)) { return INHERIT; }
170                 if (BALK.value.equals(s)) { return BALK; }
171                 if (ACCEPT.value.equals(s)) { return ACCEPT; }
172                 if (REJECT.value.equals(s)) { return REJECT; }
173                 if (Strings.DEFAULT.equals(s)){ return BALK; }/*safest*/
174                 if (Strings.ENCLOSING.equals(s)) { return INHERIT; }
175             }
176         }
177         return null;
178     }
179
180
181     /**
182      * Same as {@linkplain #from(String) from(String)} but with a
183      * default value if supplied value does not match any known
184      * Handling's name.
185      * @param s the symbolic name to be matched
186      * @param dflt the default Handling if necessary
187      **/

188     public static Handling from(String JavaDoc s, Handling dflt)
189     {
190         Handling choice= from(s);
191         return (choice==null) ? dflt : choice;
192     }
193
194
195 //----------------------------------------------------------------------
196
// Common combinations for simple interpretations of choices:
197
//----------------------------------------------------------------------
198

199     /**
200      * Collapses a fine-grained handling into a simple "accept" or
201      * "reject" choice. If the choice is either "ignore" or "accept"
202      * it is collapsed to "accept". If the choice is either "balk" or
203      * "reject" it is collapsed to "reject". Caller decides how
204      * "inherit" and <i>null</i> are interpreted.
205      * @param choice script value
206      * @param inheritProxy returned if choice is "inherit"
207      * @param nullProxy returned if choice is <i>null</i>
208      **/

209     public static Handling simplify(Handling choice,
210                                     Handling inheritProxy,
211                                     Handling nullProxy)
212     {
213         if (choice==null) {
214             return nullProxy;
215         }
216         switch (choice.getIndex()) {
217             case ACCEPT_INDEX:
218             case IGNORE_INDEX: {
219                 return ACCEPT;
220             }
221             case REJECT_INDEX:
222             case BALK_INDEX: {
223                 return REJECT;
224             }
225         }
226         return inheritProxy;
227     }
228
229
230     /**
231      * Collapses a fine-grained handling into a simple "accept" or
232      * "reject" choice. Returns <i>null</i> if incoming choice is
233      * <i>null</i>. See {@linkplain #simplify(Handling,Handling,Handling)
234      * simplify(&#8230;)}.
235      * @param choice script value (non-null)
236      * @param inheritProxy returned if choice is "inherit"
237      **/

238     public static Handling simplify(Handling choice,
239                                     Handling inheritProxy)
240     {
241         if (choice==null) {
242             throw new IllegalArgumentException JavaDoc();
243         }
244         return simplify(choice,inheritProxy,null);
245     }
246
247
248     /**
249      * Collapses a fine-grained handling choice into a simple "yes"
250      * or "no" boolean choice. Returns <i>true</i> if choice is
251      * determined to be "accept;" otherwise returns <i>false</i>
252      * (including if incoming choice is <i>null</i>). Based on
253      * {@linkplain #simplify(Handling,Handling) simplify(&#8230;)}.
254      * @param choice script value (non-null)
255      * @param inheritProxy used when choice is "inherit"
256      **/

257     public static boolean isYes(Handling choice,
258                                 Handling inheritProxy)
259     {
260         if (choice==null) {
261             throw new IllegalArgumentException JavaDoc();
262         }
263         choice = simplify(choice,inheritProxy,null);
264         return (choice==Handling.ACCEPT);
265     }
266
267
268     /**
269      * Collapses a fine-grained handling into a simple "ignore" or
270      * "no ignore" choice. This utility provides backward-compatibility
271      * with the original AntX simple "ignore" or nothing handler
272      * choices.
273      * @param choice script value (non-null)
274      * @param inheritProxy returned if choice is "inherit"
275      **/

276     public static Handling simplifyIgnoreOrNot(Handling choice,
277                                                Handling inheritProxy)
278     {
279         if (choice==null) {
280             throw new IllegalArgumentException JavaDoc();
281         }
282         switch (choice.getIndex()) {
283             case REJECT_INDEX:
284             case BALK_INDEX:
285             case IGNORE_INDEX: {
286                 return IGNORE;
287             }
288             case ACCEPT_INDEX: {
289                 return ACCEPT;
290             }
291         }
292         return inheritProxy;
293     }
294 }
295
296 /* end-of-Handling.java */
297
Popular Tags