KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > ownhelpers > FeedbackSink


1 /**
2  * $Id: FeedbackSink.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.ownhelpers;
30
31 import com.idaremedia.antx.helpers.Strings;
32 import com.idaremedia.antx.helpers.Tk;
33 import com.idaremedia.antx.parameters.EnumSkeleton;
34
35 /**
36  * Enumeration that represents the possible types of (feedback) output sinks within
37  * the Ant runtime. Includes elements like the standard Ant log and the System
38  * stdio streams <i>err</i> and <i>out</i>.
39  *
40  * @since JWare/AntX 0.3
41  * @author ssmc, &copy;2003-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
42  * @version 0.5
43  * @.safety multiple
44  * @.group api,helper
45  **/

46
47 public class FeedbackSink extends EnumSkeleton
48 {
49     //@.impl ORDERING of static declarations is important! ------
50

51     //1) Indices (in order)
52

53     /** Index of {@linkplain #ALL ALL}. **/
54     public static final int ALL_INDEX = 0; //@.impl must be zero!
55
/** Index of {@linkplain #NONE NONE}. **/
56     public static final int NONE_INDEX = ALL_INDEX+1;
57     /** Index of {@linkplain #STDOUT STDOUT}. **/
58     public static final int STDOUT_INDEX = NONE_INDEX+1;
59     /** Index of {@linkplain #STDERR STDERR}. **/
60     public static final int STDERR_INDEX = STDOUT_INDEX+1;
61     /** Index of {@linkplain #STDIO STDIO}. **/
62     public static final int STDIO_INDEX = STDERR_INDEX+1;
63     /** Index of {@linkplain #ANTLOG ANTLOG}. **/
64     public static final int ANTLOG_INDEX = STDIO_INDEX+1;
65     /** Index of {@linkplain #EXTERNAL EXTERNAL}. **/
66     public static final int EXTERNAL_INDEX = ANTLOG_INDEX+1;
67
68     /** The number of base sink values. **/
69     protected static final int BASE_COUNT = EXTERNAL_INDEX+1;
70
71
72     //2) Values (in order)
73

74     /** Values in same order as public indices. **/
75     private static final String JavaDoc[] VALUES_= new String JavaDoc[]
76     {"all", "none", "stdout","stderr","stdio", "antlog", "external"};
77
78
79     //3) Singletons (depend on Indices and Values already existing!)
80

81     /** Singleton "<span class="src">all</span>" choice. **/
82     public static final FeedbackSink ALL=
83         new FeedbackSink(VALUES_[ALL_INDEX],ALL_INDEX);
84     /** Singleton "<span class="src">none</span>" choice. **/
85     public static final FeedbackSink NONE=
86         new FeedbackSink(VALUES_[NONE_INDEX],NONE_INDEX);
87
88     /** Singleton "<span class="src">stdout</span>" choice. **/
89     public static final FeedbackSink STDOUT=
90         new FeedbackSink(VALUES_[STDOUT_INDEX],STDOUT_INDEX);
91     /** Singleton "<span class="src">stderr</span>" choice. **/
92     public static final FeedbackSink STDERR=
93         new FeedbackSink(VALUES_[STDERR_INDEX],STDERR_INDEX);
94     /** Singleton "<span class="src">stdio</span>" choice. **/
95     public static final FeedbackSink STDIO=
96         new FeedbackSink(VALUES_[STDIO_INDEX],STDIO_INDEX);
97
98
99     /** Singleton "<span class="src">antlog</span>" choice. **/
100     public static final FeedbackSink ANTLOG=
101         new FeedbackSink(VALUES_[ANTLOG_INDEX],ANTLOG_INDEX);
102     /** Singleton "<span class="src">external</span>" choice. **/
103     public static final FeedbackSink EXTERNAL=
104         new FeedbackSink(VALUES_[EXTERNAL_INDEX],EXTERNAL_INDEX);
105
106
107     /**
108      * Required bean void constructor for Ant's introspector.
109      **/

110     public FeedbackSink()
111     {
112         super();
113     }
114
115
116     /**
117      * Use to create public singletons. Ensure this enum is
118      * initialized as if with the default Ant Introspector
119      * helper thingy.
120      **/

121     protected FeedbackSink(String JavaDoc v, int i)
122     {
123         super(v);
124     }
125
126
127
128     /**
129      * Returns a <em>copy</em> of the standard feedback sink values
130      * in order. Subclasses can use this method to pre-fill their
131      * value arrays with the inherited list.
132      * @param fillin [optional] array of strings to update with values.
133      **/

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

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

161     public static FeedbackSink from(int i)
162     {
163         if (i==ANTLOG.index) { return ANTLOG; }
164         if (i==STDIO.index) { return STDIO; }
165         if (i==STDOUT.index) { return STDOUT; }
166         if (i==STDERR.index) { return STDERR; }
167         if (i==EXTERNAL.index) { return EXTERNAL; }
168         if (i==ALL.index) { return ALL; }
169         if (i==NONE.index) { return NONE; }
170         return null;
171     }
172
173
174     /**
175      * Same as {@linkplain #from(int) from(int)} but with a
176      * default value if value does not match any known FeedbackSink's
177      * index.
178      * @param i the index to be matched
179      * @param dflt the default FeedbackSink if necessary
180      **/

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

193     public static FeedbackSink from(String JavaDoc s)
194     {
195         if (s!=null && s.length()>1) {
196             s = Tk.lowercaseFrom(s);
197             if (Character.isDigit(s.charAt(0))) {
198                 try { return from(Integer.parseInt(s)); }
199                 catch(Exception JavaDoc nfx) {/*burp*/}
200             } else {
201                 if (STDIO.value.equals(s)) { return STDIO; }
202                 if (STDERR.value.equals(s)) { return STDERR; }
203                 if (STDOUT.value.equals(s)) { return STDOUT; }
204                 if (ANTLOG.value.equals(s)) { return ANTLOG; }
205                 if ("stdlog".equals(s)) { return ANTLOG; }
206                 if (EXTERNAL.value.equals(s)) { return EXTERNAL; }
207                 if (ALL.value.equals(s)) { return ALL; }
208                 if (NONE.value.equals(s)) { return NONE; }
209                 if (Strings.DEFAULT.equals(s)) { return STDIO; }
210                 if ("both".equals(s)) { return STDIO; }
211             }
212         }
213         return null;
214     }
215
216
217     /**
218      * Same as {@linkplain #from(String) from(String)} but with a
219      * default value if supplied value does not match any known
220      * FeedbackSink's name.
221      * @param s the symbolic name to be matched
222      * @param dflt the default FeedbackSink if necessary
223      **/

224     public static FeedbackSink from(String JavaDoc s, FeedbackSink dflt)
225     {
226         FeedbackSink choice= from(s);
227         return (choice==null) ? dflt : choice;
228     }
229
230
231
232     /**
233      * Utility method that verifies the given sink is one of the standard
234      * system output streams (err|out). A <i>null</i> sink value always
235      * returns <i>false</i>.
236      * @param ss the feedback sink value
237      **/

238     public static final boolean isSystem(FeedbackSink ss)
239     {
240         if (ss!=null) {
241             int i= ss.getIndex();
242             if (i==FeedbackSink.STDIO_INDEX ||
243                 i==FeedbackSink.STDOUT_INDEX || i==FeedbackSink.STDERR_INDEX) {
244                 return true;
245             }
246         }
247         return false;
248     }
249
250
251     /**
252      * Utility method that verifies the given sink is one of the internal
253      * Ant process output streams (err|out|antlogs). A <i>null</i> sink
254      * value always returns <i>false</i>.
255      * @param ss the feedback sink value
256      **/

257     public static boolean isKnown(FeedbackSink ss)
258     {
259         if (ss!=null) {
260             int i= ss.getIndex();
261             if (i==FeedbackSink.STDIO_INDEX || i==FeedbackSink.ANTLOG_INDEX ||
262                 i==FeedbackSink.STDOUT_INDEX || i==FeedbackSink.STDERR_INDEX) {
263                 return true;
264             }
265         }
266         return false;
267     }
268
269
270
271     /**
272      * Utility method that verifies the given sink is one of the general
273      * sources of output streams (antlogs|stdio). A <i>null</i> sink
274      * value always returns <i>false</i>.
275      * @param ss the feedback sink value
276      **/

277     public static boolean isGrouping(FeedbackSink ss)
278     {
279         if (ss!=null) {
280             int i= ss.getIndex();
281             if (i==FeedbackSink.STDIO_INDEX || i==FeedbackSink.ANTLOG_INDEX) {
282                 return true;
283             }
284         }
285         return false;
286     }
287 }
288
289 /* end-of-FeedbackSink.java */
290
Popular Tags