KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: CaseTransform.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2003-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.Tk;
32
33 /**
34  * Enumeration that represents the type of case transformations a task might implement.
35  * The following list explains what the various settings mean:<ul>
36  * <li><span class="src">none</span>: The text should be left as-is.</li>
37  * <li><span class="src">lowercase</span>: The text should be lower-cased
38  * using the current locale.</li>
39  * <li><span class="src">uppercase</span>: The text should be UPPER-cased
40  * using the current locale.</li>
41  * <li><span class="src">invertcase</span>: The text's case should be
42  * inverted (UPPER to lower, lower to UPPER).</li>
43  * <li><span class="src">capitalize</span>: The text should be capitalized
44  * according to the task's definition.</li>
45  * </ul>
46  *
47  * @since JWare/AntX 0.3
48  * @author ssmc, &copy;2003-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
49  * @version 0.5
50  * @.safety multiple
51  * @.group api,helper
52  * @see TransformHelper
53  * @see ValueTransform
54  **/

55
56 public class CaseTransform extends EnumSkeleton
57 {
58     //@.impl ORDERING of static declarations is important! ------
59

60     //1) Indices (in order)
61

62     /** Index of {@linkplain #NONE NONE}. **/
63     public static final int NONE_INDEX = 0;
64     /** Index of {@linkplain #LOWERCASE LOWERCASE}. **/
65     public static final int LOWERCASE_INDEX = NONE_INDEX+1;
66     /** Index of {@linkplain #UPPERCASE UPPERCASE}. **/
67     public static final int UPPERCASE_INDEX = LOWERCASE_INDEX+1;
68     /** Index of {@linkplain #INVERTCASE INVERTCASE}. **/
69     public static final int INVERTCASE_INDEX = UPPERCASE_INDEX+1;
70     /** Index of {@linkplain #CAPITALIZE CAPITALIZE}. **/
71     public static final int CAPITALIZE_INDEX = INVERTCASE_INDEX+1;
72
73     /** The number of base case transform values.
74      * @since JWare/AntX 0.4
75      **/

76     protected static final int BASE_COUNT = CAPITALIZE_INDEX+1;
77
78
79     //2) Values (in order)
80

81     /** Values in same order as public indices. **/
82     private static final String JavaDoc[] VALUES_= new String JavaDoc[]
83     {"none", "lowercase", "uppercase","invertcase","capitalize"};
84
85
86     //3) Singletons (depend on Indices and Values already existing!)
87

88     /** Singleton "<span class="src">none</span>" transform. **/
89     public static final CaseTransform NONE=
90         new CaseTransform(VALUES_[NONE_INDEX],NONE_INDEX);
91     /** Singleton "<span class="src">lowercase</span>" transform. **/
92     public static final CaseTransform LOWERCASE=
93         new CaseTransform(VALUES_[LOWERCASE_INDEX],LOWERCASE_INDEX);
94     /** Singleton "<span class="src">uppercase</span>" transform. **/
95     public static final CaseTransform UPPERCASE=
96         new CaseTransform(VALUES_[UPPERCASE_INDEX],UPPERCASE_INDEX);
97     /** Singleton "<span class="src">invertcase</span>" transform. **/
98     public static final CaseTransform INVERTCASE=
99         new CaseTransform(VALUES_[INVERTCASE_INDEX],INVERTCASE_INDEX);
100     /** Singleton "<span class="src">capitalize</span>" transform. **/
101     public static final CaseTransform CAPITALIZE=
102         new CaseTransform(VALUES_[CAPITALIZE_INDEX],CAPITALIZE_INDEX);
103
104
105     /**
106      * Required bean void constructor for Ant's introspector.
107      **/

108     public CaseTransform()
109     {
110         super();
111     }
112
113
114     /**
115      * Use to create public singletons. Ensure it's initialized
116      * as with default Ant Introspector helper thingy.
117      **/

118     private CaseTransform(String JavaDoc v, int i)
119     {
120         super(v);
121     }
122
123
124
125     /**
126      * Returns a <em>copy</em> of the standard case transform values
127      * in order. Subclasses can use this method to pre-fill their
128      * value arrays with the inherited list.
129      * @param fillin [optional] array of strings to update with values.
130      * @since JWare/AntX 0.4
131      **/

132     public static String JavaDoc[] copyOfDefaultValues(String JavaDoc[] fillin)
133     {
134         if (fillin==null) {
135             fillin = new String JavaDoc[VALUES_.length];
136         }
137         System.arraycopy(VALUES_,0,fillin,0,VALUES_.length);
138         return fillin;
139     }
140
141
142
143     /**
144      * Returns copy of all possible modification values as an ordered
145      * string array. Note: ordering should be same as singletons indice.
146      **/

147     public String JavaDoc[] getValues()
148     {
149         return CaseTransform.copyOfDefaultValues(null);
150     };
151
152
153
154     /**
155      * Helper that converts a scalar to a known CaseTransform. Returns <i>null</i>
156      * if value does not match any of expected modifications.
157      * @param i the index to be matched
158      **/

159     public static CaseTransform from(int i)
160     {
161         if (i==LOWERCASE.index) { return LOWERCASE; }
162         if (i==UPPERCASE.index) { return UPPERCASE; }
163         if (i==INVERTCASE.index) { return INVERTCASE; }
164         if (i==CAPITALIZE.index) { return CAPITALIZE; }
165         if (i==NONE.index) { return NONE; }
166         return null;
167     }
168
169
170     /**
171      * Same as {@linkplain #from(int) from(int)} but with a
172      * default value if value does not match any known CaseTransform's
173      * index.
174      * @param i the index to be matched
175      * @param dflt the default CaseTransform if necessary
176      **/

177     public static CaseTransform from(int i, CaseTransform dflt)
178     {
179         CaseTransform choice= from(i);
180         return (choice==null) ? dflt : choice;
181     }
182
183
184     /**
185      * Helper that converts a string to a known CaseTransform singleton.
186      * Returns <i>null</i> if string unrecognized. String can be
187      * either CaseTransform's symbolic name or its index.
188      **/

189     public static CaseTransform from(String JavaDoc s)
190     {
191         if (s!=null && s.length()>1) {
192             s = Tk.lowercaseFrom(s);
193             if (Character.isDigit(s.charAt(0))) {
194                 try { return from(Integer.parseInt(s)); }
195                 catch(Exception JavaDoc nfx) {/*burp*/}
196             } else {
197                 if ("default".equals(s)) { return NONE; }
198                 if (NONE.value.equals(s)) { return NONE; }
199                 if (LOWERCASE.value.equals(s)) { return LOWERCASE; }
200                 if (UPPERCASE.value.equals(s)) { return UPPERCASE;}
201                 if (INVERTCASE.value.equals(s)) { return INVERTCASE;}
202                 if (CAPITALIZE.value.equals(s)) { return CAPITALIZE; }
203             }
204         }
205         return null;
206     }
207
208
209     /**
210      * Same as {@linkplain #from(java.lang.String) from(String)} but
211      * with a default value if value does not match any known
212      * CaseTransform's name.
213      * @param s the symbolic name to be matched
214      * @param dflt the default CaseTransform if necessary
215      **/

216     public static CaseTransform from(String JavaDoc s, CaseTransform dflt)
217     {
218         CaseTransform choice= from(s);
219         return (choice==null) ? dflt : choice;
220     }
221 }
222
223 /* end-of-CaseTransform.java */
224
Popular Tags