KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: NoiseLevel.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-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 org.apache.tools.ant.BuildEvent;
32 import org.apache.tools.ant.Project;
33
34 import com.idaremedia.antx.helpers.Tk;
35 import com.idaremedia.antx.parameters.EnumSkeleton;
36
37 /**
38  * Enumeration of (log) noise levels. Exposed as standalone class since reused by
39  * multiple AntX tasks and conditions.
40  *
41  * @since JWare/AntX 0.1
42  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
43  * @version 0.5
44  * @.safety single
45  * @.group impl,helper
46  **/

47
48 public final class NoiseLevel extends EnumSkeleton
49 {
50     /** Index of {@linkplain #ERROR ERROR}. Implementation: must be indice 0.**/
51     public static final int ERROR_INDEX= 0;
52     /** Index of {@linkplain #WARNING WARNING}. **/
53     public static final int WARNING_INDEX= ERROR_INDEX+1;
54     /** Index of {@linkplain #INFO INFO}. **/
55     public static final int INFO_INDEX= WARNING_INDEX+1;
56     /** Index of {@linkplain #VERBOSE VERBOSE}. **/
57     public static final int VERBOSE_INDEX= INFO_INDEX+1;
58     /** Index of {@linkplain #DEBUG DEBUG}. **/
59     public static final int DEBUG_INDEX= VERBOSE_INDEX+1;
60     /** Index of {@linkplain #FATAL FATAL}. **/
61     public static final int FATAL_INDEX= DEBUG_INDEX+1;
62
63     /** Singleton "fatal" noise level. **/
64     public static final NoiseLevel FATAL = new NoiseLevel("fatal",FATAL_INDEX);
65     /** Singleton "error" noise level. **/
66     public static final NoiseLevel ERROR = new NoiseLevel("error",ERROR_INDEX);
67     /** Singleton "warning" noise level. **/
68     public static final NoiseLevel WARNING = new NoiseLevel("warning",WARNING_INDEX);
69     /** Singleton "info" noise level. **/
70     public static final NoiseLevel INFO = new NoiseLevel("info",INFO_INDEX);
71     /** Singleton "verbose" noise level. **/
72     public static final NoiseLevel VERBOSE = new NoiseLevel("verbose",VERBOSE_INDEX);
73     /** Singleton "debug" noise level. **/
74     public static final NoiseLevel DEBUG = new NoiseLevel("debug",DEBUG_INDEX);
75
76
77
78     /**
79      * Required bean void constructor for Ant's introspector.
80      **/

81     public NoiseLevel()
82     {
83         super();
84     }
85
86
87     /**
88      * Use to create public singletons. Ensure it's initialized
89      * as with default Ant Introspector helper thingy.
90      **/

91     private NoiseLevel(String JavaDoc v, int i)
92     {
93         super(v);
94     }
95
96
97     /**
98      * Returns copy of all possible noise level values as an ordered
99      * string array. Note: should be same ordering as EchoLevel.
100      **/

101     public String JavaDoc[] getValues()
102     {
103         return new String JavaDoc[] {"error","warning","info","verbose","debug","fatal"};
104     };
105
106
107
108     /**
109      * Helper that converts a scalar to a known noise level.
110      * Returns <i>null</i> if value does not match any of expected
111      * noise levels index.
112      * @param i the index to be matched
113      **/

114     public static NoiseLevel from(int i)
115     {
116         switch(i) {
117             case ERROR_INDEX:
118                 return ERROR;
119             case WARNING_INDEX:
120                 return WARNING;
121             case INFO_INDEX:
122                 return INFO;
123             case VERBOSE_INDEX:
124                 return VERBOSE;
125             case DEBUG_INDEX:
126                 return DEBUG;
127             case FATAL_INDEX:
128                 return FATAL;
129         }
130         return null;
131     }
132
133
134     /**
135      * Same as {@linkplain #from(int) from(int)} but with a
136      * default value if value does not match any known noise level
137      * index.
138      * @param i the index to be matched
139      * @param dfltNL the default NoiseLevel if necessary
140      **/

141     public static NoiseLevel from(int i, NoiseLevel dfltNL)
142     {
143         NoiseLevel nl= from(i);
144         return (nl==null) ? dfltNL : nl;
145     }
146
147
148     /**
149      * Helper that converts a string to a known noise level
150      * singleton. Returns <i>null</i> if string unrecognized.
151      * String can be either noise level's symbolic name or
152      * its index.
153      **/

154     public static NoiseLevel from(String JavaDoc s)
155     {
156         if (s!=null && s.length()>1) {
157             s = Tk.lowercaseFrom(s);
158             if (Character.isDigit(s.charAt(0))) {
159                 try { return from(Integer.parseInt(s)); }
160                 catch(Exception JavaDoc nfx) {/*burp*/}
161             } else {
162                 if ("default".equals(s)) { return getAntXDefault(); }
163                 if (ERROR.value.equals(s)) { return ERROR; }
164                 if (WARNING.value.equals(s)) { return WARNING; }
165                 if (INFO.value.equals(s)) { return INFO; }
166                 if (VERBOSE.value.equals(s)) { return VERBOSE; }
167                 if (DEBUG.value.equals(s)) { return DEBUG; }
168                 if (FATAL.value.equals(s)) { return FATAL; }
169             }
170         }
171         return null;
172     }
173
174
175     /**
176      * Same as {@linkplain #from(java.lang.String) from(String)} but
177      * with a default value if value does not match any known
178      * noise level name.
179      * @param s the symbolic name to be matched
180      * @param dfltNL the default NoiseLevel if necessary
181      **/

182     public static NoiseLevel from(String JavaDoc s, NoiseLevel dfltNL)
183     {
184         NoiseLevel nl= from(s);
185         return (nl==null) ? dfltNL : nl;
186     }
187
188
189
190     /**
191      * Helper that converts a standard Ant message priority to a
192      * known noise level. Returns <i>null</i> if priority does not
193      * match any of expected project message levels.
194      * @param msgLevel the standard Ant message level
195      * @since JWare/AntX 0.5
196      **/

197     public static NoiseLevel fromNativeIndex(int msgLevel)
198     {
199         switch(msgLevel) {
200             case Project.MSG_ERR:
201                 return ERROR;
202             case Project.MSG_WARN:
203                 return WARNING;
204             case Project.MSG_INFO:
205                 return INFO;
206             case Project.MSG_VERBOSE:
207                 return VERBOSE;
208             case Project.MSG_DEBUG:
209                 return DEBUG;
210         }
211         return null;
212     }
213
214
215     /**
216      * Helper that converts a BuildEvent's priority to a known noise
217      * level. Returns <i>null</i> if priority does not match any of
218      * expected noise levels.
219      * @param e the event (non-null)
220      **/

221     public static NoiseLevel from(BuildEvent e)
222     {
223         return fromNativeIndex(e.getPriority());
224     }
225
226
227     /**
228      * Returns a "pinned" level that's within the standard Ant
229      * &lt;echo&gt; and Project MSG_* level set.
230      **/

231     public final int getNativeIndex()
232     {
233         int i= getIndex();
234         if (i==FATAL_INDEX) {
235             return ERROR_INDEX;
236         }
237         return i;
238     }
239
240
241     /**
242      * Returns the <i>AntX</i> default noise level (INFO).
243      * @see #getDefault
244      **/

245     public static final NoiseLevel getAntXDefault()
246     {
247         return NoiseLevel.INFO;
248     }
249
250
251
252     /**
253      * Helper that returns some context's default noise level.
254      * Looks in the project to see if a script-specified
255      * {@linkplain AntX#DEFAULT_NOISELEVEL_PROP default} noise
256      * level has been defined. If none specified in project (which
257      * also includes System properties) the returns the AntX
258      * {@linkplain #getAntXDefault default}. Never returns
259      * <i>null</i>.
260      * @param P [optional] caller's project
261      * @see #getAntXDefault
262      **/

263     public static final NoiseLevel getDefault(final Project P)
264     {
265         String JavaDoc slvl=null;
266         if (P!=null) {
267             slvl = P.getProperty(AntX.DEFAULT_NOISELEVEL_PROP);
268         }
269         if (slvl==null) {
270             slvl = System.getProperty(AntX.DEFAULT_NOISELEVEL_PROP);
271         }
272         NoiseLevel nl = getAntXDefault();
273         if (slvl!=null) {
274             nl= NoiseLevel.from(slvl,nl);
275         }
276         return nl;
277     }
278
279
280
281     /**
282      * Determine if the given noise level is as bad (in effect) as the
283      * named threshold.
284      * @param unk noise level to be tested (non-null)
285      * @param threshold threshold level (non-null)
286      * @return <i>true</i> if is as bad (in effect) as threshold
287      * @since JWare/AntX 0.5
288      **/

289     public static final boolean isAsBadAs(NoiseLevel unk, NoiseLevel threshold)
290     {
291         return unk.getNativeIndex() <= threshold.getNativeIndex();
292     }
293
294
295
296     /**
297      * Determine if the given Ant log level is as bad (in effect) as the
298      * named noise threshold.
299      * @param loglevel Ant log level to be tested (non-null)
300      * @param threshold threshold level (non-null)
301      * @return <i>true</i> if is as bad (in effect) as threshold
302      * @since JWare/AntX 0.5
303      **/

304     public static final boolean isAsBadAs(int loglevel, NoiseLevel threshold)
305     {
306         return loglevel <= threshold.getNativeIndex();
307     }
308
309
310
311     /**
312      * Convenient (and visually clearer) instance variant of
313      * {@linkplain #isAsBadAs(NoiseLevel, NoiseLevel)
314      * isAsBadAs(NoiseLevel,NoiseLevel)}.
315      * @param threshold threshold level (non-null)
316      * @return <i>true</i> if is as bad (in effect) as threshold
317      * @since JWare/AntX 0.5
318      **/

319     public final boolean isAsBadAs(NoiseLevel threshold)
320     {
321         return isAsBadAs(this,threshold);
322     }
323 }
324
325 /* end-of-NoiseLevel.java */
326
Popular Tags