KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: TransformHelper.java 187 2007-03-25 17:59:16Z ssmc $
3  * Copyright 2004-2005,2007 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://antxtras.sf.net/ EMAIL- jware[at]users[dot]sourceforge[dot]net
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.parameters;
30
31 import java.text.DecimalFormat JavaDoc;
32
33 import org.apache.tools.ant.Project;
34 import org.apache.tools.ant.util.FileUtils;
35
36 import com.idaremedia.antx.helpers.DateTimeFormat;
37 import com.idaremedia.antx.helpers.DurationFormat;
38 import com.idaremedia.antx.helpers.Strings;
39 import com.idaremedia.antx.helpers.Tk;
40
41
42 /**
43  * Utilities to do simple formatting of (raw) string values based on our various
44  * transform instructions.
45  * <p>
46  * <b>Example Usage:</b><pre>
47  * public class MyClass ...
48  * {
49  * private ValueTransform m_T= ValueTransform.NONE;
50  * private String m_value;
51  *
52  * public final void setValue(String s) {
53  * m_value = s;
54  * }
55  * public final void setTransform(String t) {
56  * m_T = (t==null) ? ValueTransform.NONE : ValueTransform.from(t,m_T);
57  * }
58  *
59  * public final String getValue() {
60  * return <b>TransformHelper.apply</b>(m_T,m_value,getProject());
61  * }
62  * ...
63  * }
64  * </pre>
65  *
66  * @since JWare/AntX 0.4
67  * @author ssmc, &copy;2004-2005,2007 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
68  * @version 0.5.1
69  * @.safety multiple
70  * @.group impl,helper
71  * @see CaseTransform
72  * @see ValueTransform
73  **/

74
75 public final class TransformHelper
76 {
77     /**
78      * Converts a string value to a long number or returns a
79      * symbolic {@linkplain Tk#NO_NUM NO_NUM} if string could
80      * not be converted.
81      * @param s number string (non-null)
82      * @since JWare/AntX 0.4
83      **/

84     public static final long toNumber(String JavaDoc s)
85     {
86         return Tk.longFrom(s,Tk.NO_NUM);
87     }
88
89
90
91     /**
92      * Like {@linkplain #toNumber toNumber(&#8230;)} except if
93      * the incoming string equals "<span class="src">now</span>"
94      * the system's current time is returned as a long
95      * (milliseconds).
96      * @param s number string or the value "now" (non-null)
97      * @since JWare/AntX 0.5
98      **/

99     public static final long toTimeNumber(String JavaDoc s)
100     {
101         if (Strings.NOW.equals(s)) {
102             return System.currentTimeMillis();
103         }
104         return Tk.longFrom(s,Tk.NO_NUM);
105     }
106
107
108
109     /**
110      * Inverts the case of every character in a string. Will
111      * invert characters according to current locale response
112      * to 'isLowerCase' and 'isUpperCase'. Returns <i>null</i>
113      * if incoming string is <i>null</i>.
114      * @param s string to be inverted
115      * @since JWare/AntX 0.4
116      **/

117     public static final String JavaDoc toInvertCase(String JavaDoc s)
118     {
119         if (s==null) {
120             return s;
121         }
122         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s);
123         for (int i=0,n=s.length();i<n;i++) {
124             char c = s.charAt(i);
125             if (Character.isLowerCase(c)) {
126                 sb.setCharAt(i,Character.toUpperCase(c));
127             }
128             else if (Character.isUpperCase(c)) {
129                 sb.setCharAt(i,Character.toLowerCase(c));
130             }
131         }
132         s = sb.substring(0);
133         return s;
134     }
135
136
137
138     /**
139      * Converts a general value transform instruction into a specific
140      * case transform. Returns <i>null</i> if unable to match by name.
141      * @param gt the general transform instruction (non-null)
142      * @since JWare/AntX 0.4
143      **/

144     public static CaseTransform toCaseTransform(ValueTransform gt)
145     {
146         if (gt==null) {
147             throw new IllegalArgumentException JavaDoc();
148         }
149         return CaseTransform.from(gt.getValue());
150     }
151
152
153
154
155     /**
156      * Converts a case transform instruction into a generic value
157      * transform. Returns <i>null</i> if unable to match by name.
158      * @param ct the case transform instruction (non-null)
159      * @since JWare/AntX 0.4
160      **/

161     public static ValueTransform toValueTransform(CaseTransform ct)
162     {
163         if (ct==null) {
164             throw new IllegalArgumentException JavaDoc();
165         }
166         return ValueTransform.from(ct.getValue());
167     }
168
169
170
171     /**
172      * Short hand for transforming a file system path or file URL
173      * into a normalized (possibly project-based) full path. Common
174      * operation within AntX tools.
175      * @param fileRef description of file (path or URL)
176      * @param P project to use if fileref is not absolute
177      * @return normalized absolute path (non-null)
178      * @since JWare/AntX 0.5
179      **/

180     public static final String JavaDoc toPath(String JavaDoc fileRef, Project P)
181     {
182         return apply(ValueTransform.OSPATH,fileRef,P);
183     }
184
185
186
187     /**
188      * Utility that implements simple lowercasing, uppercasing,
189      * and invertcasing transformation.
190      * @param t the transform instruction (non-null)
191      * @param s the value to be transformed
192      * @since JWare/AntX 0.4
193      **/

194     public static final String JavaDoc apply(CaseTransform t, String JavaDoc s)
195     {
196         if (t==null) {
197             throw new IllegalArgumentException JavaDoc();
198         }
199         if (s==null) {
200             return null;
201         }
202         switch(t.getIndex()) {
203             case CaseTransform.LOWERCASE_INDEX: {
204                 s = s.toLowerCase();
205                 break;
206             }
207             case CaseTransform.UPPERCASE_INDEX: {
208                 s = s.toUpperCase();
209                 break;
210             }
211             case CaseTransform.INVERTCASE_INDEX:{
212                 s = toInvertCase(s);
213                 break;
214             }
215         }
216         return s;
217     }
218
219
220
221     /**
222      * Utility that implements simple interpretation of value
223      * transformation instructions. Good as a generic fallback.
224      * @param t the transform instruction (non-null)
225      * @param s the value to be transformed
226      * @param P controlling project for problem feedback (non-null)
227      * @see DateTimeFormat
228      * @see DurationFormat
229      * @since JWare/AntX 0.4
230      **/

231     public static final String JavaDoc apply(ValueTransform t, String JavaDoc s, Project P)
232     {
233         if (t==null || P==null) {
234             throw new IllegalArgumentException JavaDoc();
235         }
236         if (s==null) {
237             return null;
238         }
239         switch(t.getIndex()) {
240             case ValueTransform.TRIM_INDEX: {
241                 s = s.trim();
242                 break;
243             }
244             case ValueTransform.STRIPWS_INDEX: {
245                 s = Tk.stripWhitespace(s);
246                 break;
247             }
248             case ValueTransform.LOWERCASE_INDEX: {
249                 s = s.toLowerCase();
250                 break;
251             }
252             case ValueTransform.UPPERCASE_INDEX: {
253                 s = s.toUpperCase();
254                 break;
255             }
256             case ValueTransform.INVERTCASE_INDEX: {
257                 s = toInvertCase(s);
258                 break;
259             }
260             case ValueTransform.DURATION_INDEX: {
261                 long l= toNumber(s);
262                 if (l!=Tk.NO_NUM) {
263                     s = DurationFormat.INSTANCE.format(l,true);
264                 } else {
265                     P.log("Unable to convert '"+s+"' to time duration.",
266                           Project.MSG_WARN);
267                 }
268                 break;
269             }
270             case ValueTransform.DATETIME_INDEX: {
271                 long l= toTimeNumber(s);
272                 if (l!=Tk.NO_NUM) {
273                     s = DateTimeFormat.shortformat(l);
274                 } else {
275                     P.log("Unable to convert '"+s+"' to datetime.",
276                           Project.MSG_WARN);
277                 }
278                 break;
279             }
280             case ValueTransform.CHECKPOINT_INDEX: {
281                 long l= toTimeNumber(s);
282                 if (l!=Tk.NO_NUM) {
283                     s = DateTimeFormat.GMTformat(l);
284                 } else {
285                     P.log("Unable to convert '"+s+"' to GMT timestamp.",
286                           Project.MSG_WARN);
287                 }
288                 break;
289             }
290             case ValueTransform.LONGDATETIME_INDEX: {
291                 long l= toTimeNumber(s);
292                 if (l!=Tk.NO_NUM) {
293                     s = DateTimeFormat.format(l);
294                 } else {
295                     P.log("Unable to convert '"+s+"' to full datetime.",
296                           Project.MSG_WARN);
297                 }
298                 break;
299             }
300             case ValueTransform.OSPATH_INDEX: {
301                 FileUtils fu= FileUtils.getFileUtils();
302                 if (s.startsWith("file:")) {
303                     s = fu.fromURI(s);
304                 } else {
305                     //s = fu.normalize(s).getPath();
306
s = P.resolveFile(s).getPath();
307                 }
308                 break;
309             }
310             case ValueTransform.OSPATHURL_INDEX: {
311                 FileUtils fu= FileUtils.getFileUtils();
312                 if (s.startsWith("file:")) {
313                     s = fu.fromURI(s);
314                 } else {
315                     s = P.resolveFile(s).getPath();
316                 }
317                 s = fu.toURI(s);
318                 break;
319             }
320             case ValueTransform.DECIMAL_INDEX: {
321                 Number JavaDoc n = Tk.numberFrom(s);
322                 if (n==Tk.NO_NUM_NUM) {
323                     s = "";
324                 } else {
325                     synchronized(DFI) {
326                         s = DFI.format(n);
327                     }
328                 }
329                 break;
330             }
331         }
332         return s;
333     }
334
335     private static final DecimalFormat JavaDoc DFI = new DecimalFormat JavaDoc("#.###");//NB:90+% expects
336
}
337
338 /* end-of-TransformHelper.java */
339
Popular Tags