KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > Stringifier


1 /**
2  * $Id: Stringifier.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 as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * 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 (GNU Lesser General Public License) 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 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;
30
31 import java.io.File JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.util.Date JavaDoc;
34
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.types.EnumeratedAttribute;
37 import org.apache.tools.ant.types.Environment;
38 import org.apache.tools.ant.types.Path;
39 import org.apache.tools.ant.types.RegularExpression;
40 import org.apache.tools.ant.util.regexp.RegexpMatcher;
41
42 import com.idaremedia.antx.apis.FlexStringFriendly;
43 import com.idaremedia.antx.helpers.DateTimeFormat;
44 import com.idaremedia.antx.helpers.InnerString;
45 import com.idaremedia.antx.helpers.Tk;
46
47 /**
48  * Base utility for converting fixture components to their string form. This default
49  * stringifiers understand a subset of standard Ant elements (listed below) and any AntX
50  * component that implements the {@linkplain FlexStringFriendly} interface. AntX
51  * feedback tasks like &lt;print&gt; can fall back to Stringifiers if needed.
52  * <p/>
53  * A <i>lenient</i> stringifier is one that tries to make an intelligent decision about
54  * how to stringify an object but if all fails will just default to whatever
55  * <span class="src">Object.toString</span> returns. A strict stringifier will return
56  * <i>null</i> if it does not understand how to convert an object to a
57  * {@linkplain FlexString} friendly value.
58  * <p>
59  * <b>Recognized Types:</b><ul>
60  * <li>{@linkplain FlexStringFriendly}</li>
61  * <li>InnerStrings</li>
62  * <li>String</li>
63  * <li>Boolean (converted to "true" or "false")</li>
64  * <li>Dates (converted to GMT string)</li>
65  * <li>URLs</li>
66  * <li>Files</li>
67  * <li>RegularExpressions (converted to underlying pattern)</li>
68  * <li>Paths</li>
69  * <li>Numbers</li>
70  * <li>Throwable (converted to throwable's message)</li>
71  * <li>Class (converted to class's fully qualified name)</li>
72  * <li>EnumeratedAttribute</li>
73  * <li>Environment.Variable</li>
74  * </ul>
75  *
76  * @since JWare/AntX 0.3
77  * @author ssmc, &copy;2003-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
78  * @version 0.5
79  * @.safety guarded
80  * @.group api,helper
81  * @see FlexStringFriendly
82  **/

83
84 public class Stringifier implements FixtureCore
85 {
86     /**
87      * Pseudo factory method that returns the one of the current
88      * iteration's two class stringifier singletons. Never returns
89      * <i>null</i>.
90      * @param lenient <i>true</i> to return the lenient stringifier
91      * @see Iteration#lenientStringifer
92      * @see Iteration#strictStringifier
93      **/

94     public static final Stringifier get(boolean lenient)
95     {
96         return lenient ? Iteration.lenientStringifer()
97             : Iteration.strictStringifier();
98     }
99
100
101     /**
102      * Initializes a new lenient stringifier.
103      **/

104     public Stringifier()
105     {
106         m_isLenient = true;
107     }
108
109
110     /**
111      * Initializes a new stringifier.
112      * @param lenient <i>true</i> if this stringifier should
113      * do lenient stringification(tm)
114      **/

115     public Stringifier(boolean lenient)
116     {
117         m_isLenient = lenient;
118     }
119
120
121     /**
122      * Returns <i>true</i> if this stringifier will tolerate any
123      * type of object, defaulting to the standard 'toString()'
124      * method to stringify it. Defined when this stringifier is
125      * created.
126      **/

127     public final boolean isLenient()
128     {
129         return m_isLenient;
130     }
131
132
133     /**
134      * Tries to convert a generic reference object to a flex value
135      * friendly string. If the project is unspecified and this stringifier
136      * needs a project reference, this method will throw an
137      * IllegalArgumentException.
138      * @param o the thing to be stringified
139      * @param project [optional] project from which information read
140      * if necessary (unused by default)
141      * @throws IllegalArgumentException if project <i>null</i> and
142      * is required
143      **/

144     public String JavaDoc stringFrom(Object JavaDoc o, Project project)
145     {
146         if (o==null) {
147             return null;
148         }
149         if (o instanceof FlexStringFriendly) {
150             return ((FlexStringFriendly)o).stringFrom(project);
151         }
152
153         //Implementation Notes:
154
// => ugly (and slow) but works...
155
// => to extend this if ever necessary need to customize
156
// the stringifier used by the Iteration object...
157
//
158
if (o instanceof String JavaDoc || o instanceof Boolean JavaDoc) {
159             return o.toString();
160         }
161         if (o instanceof InnerString) {
162             return ((InnerString)o).toString(project);
163         }
164         if (o instanceof Throwable JavaDoc) {
165             return ((Throwable JavaDoc)o).getMessage();
166         }
167         if (o instanceof RegularExpression) {
168             return ((RegularExpression)o).getPattern(project);
169         }
170         if (o instanceof RegexpMatcher) {
171             return ((RegexpMatcher)o).getPattern();
172         }
173         if (o instanceof Path) {
174             return ((Path)o).toString();
175         }
176         if (o instanceof File JavaDoc || o instanceof URL JavaDoc || o instanceof Number JavaDoc) {
177             return o.toString();
178         }
179         if (o instanceof Date JavaDoc) {
180             return DateTimeFormat.GMTformat(((Date JavaDoc)o).getTime());
181         }
182         if (o instanceof EnumeratedAttribute) {
183             return ((EnumeratedAttribute)o).getValue();
184         }
185         if (o instanceof Environment.Variable) {
186             return ((Environment.Variable)o).getValue();
187         }
188         if (o instanceof Class JavaDoc) {
189             return ((Class JavaDoc)o).getName();
190         }
191
192         if (!isLenient()) {
193             return null;//abandon-ship!
194
}
195
196         //NB: Ant's default types aren't very 'toString'-friendly
197
// but let's assume that caller knows what they're doing
198
return Tk.stringFrom(o,project);
199     }
200
201
202     private final boolean m_isLenient;
203 }
204
205 /* end-of-Stringifier.java */
206
Popular Tags