KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: ValueTransform.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004-2005 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 the type of value transformations a task might
36  * implement. This enumeration is an extension of the more limited
37  * <span class="src">{@linkplain CaseTransform}</span> class. The following list
38  * explains what the various settings mean:<ul>
39  * <li><span class="src">none</span>: The value should be left as-is.</li>
40  * <li><span class="src">duration</span>: The value (a number string) should
41  * be converted to a human-readable duration string.</li>
42  * <li><span class="src">datetime</span>: The value (a number string) should
43  * be converted to an abbreviated human-readable date-time string.</li>
44  * <li><span class="src">longdatetime</span>: The value (a number string)
45  * should be converted to a full (or verbose) human-readable
46  * date-time string.</li>
47  * <li><span class="src">ospath</span>: The value (a path string) should
48  * be converted to a normalized platform-specific path.</li>
49  * <li><span class="src">ospathurl</span>: The value (a path string) should
50  * be converted to a normalized platform-specific path URL.</li>
51  * <li><span class="src">checkpoint</span>: The value (a number string) should
52  * be converted to a normalized GMT-based timestamp string.</li>
53  * <li><span class="src">lowercase</span>: The value should be lower-cased
54  * using the current locale.</li>
55  * <li><span class="src">uppercase</span>: The value should be UPPER-cased
56  * using the current locale.</li>
57  * <li><span class="src">trim</span>: The value should be trimmed of whitespace
58  * before and after (in-betweens not touched).</li>
59  * <li><span class="src">decimal</span>: The value (a number) should be converted
60  * to a simple fixed decimal format (at most three places after decimal).</li>
61  * <li><span class="src">stripws</span>: The value should be stripped of all
62  * whitespace. Result is a <em>condensed</em> string.</li>
63  * </ul>
64  *
65  * @since JWare/AntX 0.4
66  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
67  * @version 0.5
68  * @.safety multiple
69  * @.group api,helper
70  * @see TransformHelper
71  **/

72
73 public class ValueTransform extends EnumSkeleton
74 {
75     //@.impl ORDERING of static declarations is important! ------
76

77     //1) Indices (in order)
78

79     /** Index of {@linkplain #NONE NONE}. **/
80     public static final int NONE_INDEX = 0;
81     /** Index of {@linkplain #DATETIME DATETIME}. **/
82     public static final int DATETIME_INDEX = NONE_INDEX+1;
83     /** Index of {@linkplain #LONGDATETIME LONGDATETIME}. **/
84     public static final int LONGDATETIME_INDEX = DATETIME_INDEX+1;
85     /** Index of {@linkplain #DURATION DURATION}. **/
86     public static final int DURATION_INDEX = LONGDATETIME_INDEX+1;
87     /** Index of {@linkplain #OSPATH OSPATH}. **/
88     public static final int OSPATH_INDEX = DURATION_INDEX+1;
89     /** Index of {@linkplain #CHECKPOINT CHECKPOINT}. **/
90     public static final int CHECKPOINT_INDEX = OSPATH_INDEX+1;
91     /** Index of {@linkplain #LOWERCASE LOWERCASE}. **/
92     public static final int LOWERCASE_INDEX = CHECKPOINT_INDEX+1;
93     /** Index of {@linkplain #UPPERCASE UPPERCASE}. **/
94     public static final int UPPERCASE_INDEX = LOWERCASE_INDEX+1;
95     /** Index of {@linkplain #INVERTCASE INVERTCASE}. **/
96     public static final int INVERTCASE_INDEX = UPPERCASE_INDEX+1;
97     
98     /** Index of {@linkplain #OSPATHURL OSPATHURL}.
99      * @since JWare/AntX 0.5
100      */

101     public static final int OSPATHURL_INDEX = INVERTCASE_INDEX+1;
102     
103     /** Index of {@linkplain #DECIMAL DECIMAL}.
104      * @since JWare/AntX 0.5
105      */

106     public static final int DECIMAL_INDEX = OSPATHURL_INDEX+1;
107
108     /** Index of {@linkplain #TRIM TRIM}.
109      * @since JWare/AntX 0.5
110      */

111     public static final int TRIM_INDEX = DECIMAL_INDEX+1;
112
113     /** Index of {@linkplain #STRIPWS STRIPWS}.
114      * @since JWare/AntX 0.5
115      */

116     public static final int STRIPWS_INDEX = TRIM_INDEX+1;
117
118     /** The number of base value transform values. **/
119     protected static final int BASE_VALUE_COUNT= STRIPWS_INDEX+1;
120
121
122     //2) Values (in order)
123

124     /** Values in same order as public indices. **/
125     private static final String JavaDoc[] VALUES_= new String JavaDoc[] {
126         "none", "datetime", "longdatetime", "duration", "ospath",
127         "checkpoint", "lowercase", "uppercase", "invertcase",
128         "ospathurl", "decimal", "trim", "stripws"
129     };
130
131
132     //3) Singletons (depend on Indices and Values already existing!)
133

134     /** Singleton "<span class="src">none</span>" transform. **/
135     public static final ValueTransform NONE=
136         new ValueTransform(VALUES_[NONE_INDEX],NONE_INDEX);
137
138     /** Singleton "<span class="src">datetime</span>" choice. **/
139     public static final ValueTransform DATETIME=
140         new ValueTransform(VALUES_[DATETIME_INDEX],DATETIME_INDEX);
141
142     /** Singleton "<span class="src">longdatetime</span>" choice. **/
143     public static final ValueTransform LONGDATETIME=
144         new ValueTransform(VALUES_[LONGDATETIME_INDEX],LONGDATETIME_INDEX);
145
146     /** Singleton "<span class="src">duration</span>" choice. **/
147     public static final ValueTransform DURATION=
148         new ValueTransform(VALUES_[DURATION_INDEX],DURATION_INDEX);
149
150     /** Singleton "<span class="src">ospath</span>" choice. **/
151     public static final ValueTransform OSPATH=
152         new ValueTransform(VALUES_[OSPATH_INDEX],OSPATH_INDEX);
153
154     /** Singleton "<span class="src">checkpoint</span>" choice. **/
155     public static final ValueTransform CHECKPOINT=
156         new ValueTransform(VALUES_[CHECKPOINT_INDEX],CHECKPOINT_INDEX);
157
158     /** Singleton "<span class="src">lowercase</span>" transform. **/
159     public static final ValueTransform LOWERCASE=
160         new ValueTransform(VALUES_[LOWERCASE_INDEX],LOWERCASE_INDEX);
161
162     /** Singleton "<span class="src">uppercase</span>" transform. **/
163     public static final ValueTransform UPPERCASE=
164         new ValueTransform(VALUES_[UPPERCASE_INDEX],UPPERCASE_INDEX);
165
166     /** Singleton "<span class="src">invertcase</span>" transform. **/
167     public static final ValueTransform INVERTCASE=
168         new ValueTransform(VALUES_[INVERTCASE_INDEX],INVERTCASE_INDEX);
169
170
171     /** Singleton "<span class="src">pathurl</span>" transform.
172      * @since JWare/AntX 0.5
173      */

174     public static final ValueTransform OSPATHURL=
175         new ValueTransform(VALUES_[OSPATHURL_INDEX],OSPATHURL_INDEX);
176
177     /** Singleton "<span class="src">decimal</span>" transform.
178      * @since JWare/AntX 0.5
179      */

180     public static final ValueTransform DECIMAL=
181         new ValueTransform(VALUES_[DECIMAL_INDEX],DECIMAL_INDEX);
182
183     /** Singleton "<span class="src">trim</span>" transform.
184      * @since JWare/AntX 0.5
185      */

186     public static final ValueTransform TRIM=
187         new ValueTransform(VALUES_[TRIM_INDEX],TRIM_INDEX);
188
189     /** Singleton "<span class="src">stripws</span>" transform.
190      * @since JWare/AntX 0.5
191      */

192     public static final ValueTransform STRIPWS=
193         new ValueTransform(VALUES_[STRIPWS_INDEX],STRIPWS_INDEX);
194
195
196     /**
197      * Required bean void constructor for Ant's introspector.
198      **/

199     public ValueTransform()
200     {
201         super();
202     }
203
204
205     /**
206      * Use to create public singletons. Ensures this enum is
207      * initialized as if with the default Ant Introspector
208      * helper thingy.
209      **/

210     private ValueTransform(String JavaDoc v, int i)
211     {
212         super(v);
213     }
214
215
216
217     /**
218      * Returns a <em>copy</em> of the standard transform values
219      * in order. Subclasses can use this method to pre-fill their
220      * value arrays with the inherited list.
221      * @param fillin [optional] array of strings to update with values.
222      **/

223     public static String JavaDoc[] copyOfDefaultValues(String JavaDoc[] fillin)
224     {
225         if (fillin==null) {
226             fillin = new String JavaDoc[VALUES_.length];
227         }
228         System.arraycopy(VALUES_,0,fillin,0,VALUES_.length);
229         return fillin;
230     }
231
232
233
234     /**
235      * Returns copy of all possible source values as an ordered
236      * string array. Note: ordering should be same as our
237      * singleton indices.
238      **/

239     public String JavaDoc[] getValues()
240     {
241         return ValueTransform.copyOfDefaultValues(null);
242     };
243
244
245
246     /**
247      * Helper that converts a scalar to a known ValueTransform.
248      * Returns <i>null</i> if value does not match any of expected
249      * source.
250      * @param i the index to be matched
251      **/

252     public static ValueTransform from(int i)
253     {
254         if (i==NONE.index) { return NONE; }
255         if (i==OSPATH.index) { return OSPATH; }
256         if (i==OSPATHURL.index) { return OSPATHURL; }
257         if (i==DURATION.index) { return DURATION; }
258         if (i==DATETIME.index) { return DATETIME; }
259         if (i==DECIMAL.index) { return DECIMAL; }
260         if (i==LOWERCASE.index) { return LOWERCASE; }
261         if (i==UPPERCASE.index) { return UPPERCASE; }
262         if (i==TRIM.index) { return TRIM; }
263         if (i==STRIPWS.index) { return STRIPWS; }
264         if (i==CHECKPOINT.index) { return CHECKPOINT; }
265         if (i==LONGDATETIME.index){ return LONGDATETIME; }
266         if (i==INVERTCASE.index) { return INVERTCASE; }
267         return null;
268     }
269
270
271     /**
272      * Same as {@linkplain #from(int) from(int)} but with a
273      * default value if value does not match any known
274      * ValueTransform's index.
275      * @param i the index to be matched
276      * @param dflt the default ValueTransform if necessary
277      **/

278     public static ValueTransform from(int i, ValueTransform dflt)
279     {
280         ValueTransform choice= from(i);
281         return (choice==null) ? dflt : choice;
282     }
283
284
285     /**
286      * Helper that converts a string to a known ValueTransform
287      * singleton. Returns <i>null</i> if string unrecognized. String
288      * can be either ValueTransform's symbolic name or its index.
289      **/

290     public static ValueTransform from(String JavaDoc s)
291     {
292         if (s!=null && s.length()>1) {
293             s = Tk.lowercaseFrom(s);
294             if (Character.isDigit(s.charAt(0))) {
295                 try { return from(Integer.parseInt(s)); }
296                 catch(Exception JavaDoc nfx) {/*burp*/}
297             } else {
298                 if (Strings.DEFAULT.equals(s)) { return NONE; }/*safest*/
299                 if (DURATION.value.equals(s)) { return DURATION; }
300                 if (DATETIME.value.equals(s)) { return DATETIME; }
301                 if (OSPATH.value.equals(s)) { return OSPATH; }
302                 if ("path".equals(s)) { return OSPATH; }
303                 if (OSPATHURL.value.equals(s)) { return OSPATHURL; }
304                 if ("pathurl".equals(s)) { return OSPATHURL; }
305                 if (DECIMAL.value.equals(s)) { return DECIMAL; }
306                 if ("fixed".equals(s)) { return DECIMAL; }
307                 if (CHECKPOINT.value.equals(s)) { return CHECKPOINT; }
308                 if (LOWERCASE.value.equals(s)) { return LOWERCASE; }
309                 if (UPPERCASE.value.equals(s)) { return UPPERCASE; }
310                 if (TRIM.value.equals(s)) { return TRIM; }
311                 if (STRIPWS.value.equals(s)) { return STRIPWS; }
312                 if (LONGDATETIME.value.equals(s)){ return LONGDATETIME; }
313                 if (INVERTCASE.value.equals(s)) { return INVERTCASE; }
314                 if (NONE.value.equals(s)) { return NONE; }
315             }
316         }
317         return null;
318     }
319
320
321     /**
322      * Same as {@linkplain #from(String) from(String)} but with a
323      * default value if supplied value does not match any known
324      * ValueTransform's name.
325      * @param s the symbolic name to be matched
326      * @param dflt the default ValueTransform if necessary
327      **/

328     public static ValueTransform from(String JavaDoc s, ValueTransform dflt)
329     {
330         ValueTransform choice= from(s);
331         return (choice==null) ? dflt : choice;
332     }
333 }
334
335 /* end-of-ValueTransform.java */
336
Popular Tags