KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: ExportScope.java 186 2007-03-16 13:42:35Z 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.Project;
32
33 import com.idaremedia.antx.helpers.Tk;
34 import com.idaremedia.antx.parameters.EnumSkeleton;
35
36 /**
37  * An export's range of influence. Three options: <i>all</i>, <i>thread</i>, or
38  * <i>project</i>.
39  *
40  * @since JWare/AntX 0.2 (Moved out of ExportTask AntX 0.3)
41  * @author ssmc, &copy;2002-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  * @see ExportedProperties
46  * @see com.idaremedia.antx.solo.ExportTask
47  * @see com.idaremedia.antx.solo.ImportTask
48  **/

49
50 public final class ExportScope extends EnumSkeleton
51 {
52     /** Index of {@linkplain #ALL ALL} scope. **/
53     public final int ALL_INDEX = 0;
54     /** Index of {@linkplain #THREAD THREAD} scope. **/
55     public final int THREAD_INDEX = ALL_INDEX+1;
56     /** Index of {@linkplain #PROJECT PROJECT} scope. **/
57     public final int PROJECT_INDEX = THREAD_INDEX+1;
58
59
60     /**
61      * VM-shareable instance of <i>thread</i> scope.
62      **/

63     public static final ExportScope THREAD = new ExportScope("thread");
64
65     /**
66      * VM-shareable instance of <i>project</i> scope.
67      **/

68     public static final ExportScope PROJECT = new ExportScope("project");
69
70     /**
71      * VM-shareable instance of <i>all</i> scope.
72      **/

73     public static final ExportScope ALL = new ExportScope("all");
74
75
76
77     /**
78      * Required void constructor for Ant's introspector.
79      **/

80     public ExportScope()
81     {
82         super();
83     }
84
85
86     /**
87        Private constructor to create our own singletons.
88     **/

89     private ExportScope(String JavaDoc v)
90     {
91         super(v);
92     }
93
94
95     /**
96      * Returns copy of all possible values as ordered string
97      * array. Ordering must be same as the _INDEX constants.
98      **/

99     public String JavaDoc[] getValues()
100     {
101         return new String JavaDoc[]{"all","thread","project"};
102     }
103
104
105
106     /**
107      * The default scope used by all exporting tasks
108      * within AntX.
109      **/

110     public static final ExportScope DEFAULT_SCOPE= ExportScope.THREAD;
111
112
113
114     /**
115      * Updates a modifiable property with given value
116      * depending on the specified scope.
117      * @param S the variable's scope (null => {@linkplain #DEFAULT_SCOPE})
118      * @param P the active project (non-null if scope != THREAD)
119      * @param name the variable's name (non-null)
120      * @param value the variable's value (non-null)
121      * @param erase <i>true</i> if an empty value should remove variable
122      **/

123     public static void setTheProperty(ExportScope S, Project P,
124                                       String JavaDoc name, String JavaDoc value,
125                                       boolean erase)
126     {
127         if (S==null) {
128             S= ExportScope.DEFAULT_SCOPE;
129         }
130         if (erase) {
131             erase = value.length()==0;//NB: only vars can be zapped (ssmc)
132
}
133         if (ExportScope.THREAD.equals(S)) {
134             if (erase) {
135                 ExportedProperties.delete(name);
136             } else {
137                 ExportedProperties.set(name,value);
138             }
139         }
140         else {
141             if (P==null) {
142                 throw new IllegalArgumentException JavaDoc
143                     ("Export.setTheProperty- nonNULL project required");
144             }
145             if (ExportScope.PROJECT.equals(S)) {
146                 P.addReference(name,value);
147             }
148             else {
149                 P.setInheritedProperty(name,value);
150             }
151         }
152     }
153
154
155     /**
156      * Returns a modifiable property's current value given its
157      * declared scope. Will return <i>null</i> if property never
158      * set. Uses a strict {@linkplain Stringifier} if a non-string
159      * reference is specified.
160      * @param S the variable's scope (<i>null</i> => {@linkplain #DEFAULT_SCOPE})
161      * @param P the active project (non-null if scope != THREAD)
162      * @param name the variable's name (non-null)
163      **/

164     public static String JavaDoc getTheProperty(ExportScope S, Project P, String JavaDoc name)
165     {
166         if (S==null) {
167             S= ExportScope.DEFAULT_SCOPE;
168         }
169         if (ExportScope.THREAD.equals(S)) {
170             return ExportedProperties.readstring(name);
171         }
172         if (P==null) {
173             throw new IllegalArgumentException JavaDoc
174                 ("Export.getTheProperty- nonNULL project required");
175         }
176         if (ExportScope.PROJECT.equals(S)) {
177             return Stringifier.get(false).stringFrom(P.getReference(name),P);
178         }
179         return P.getProperty(name);
180     }
181
182
183
184
185     /**
186      * Helper that converts a scalar to a known scope. Returns
187      * <i>null</i> if value does not match any of expected indices.
188      * @param i the index to be matched
189      * @since JWare/AntX 0.5
190      **/

191     public static ExportScope from(int i)
192     {
193         if (i==ALL.index) { return ALL; }
194         if (i==PROJECT.index) { return PROJECT; }
195         if (i==THREAD.index) { return THREAD; }
196         return null;
197     }
198
199
200
201     /**
202      * Same as {@linkplain #from(int) from(int)} but with a
203      * default value if value does not match any known indices.
204      * @param i the index to be matched
205      * @param dflt the default scope if necessary
206      * @since JWare/AntX 0.5
207      **/

208     public static ExportScope from(int i, ExportScope dflt)
209     {
210         ExportScope xs= from(i);
211         return (xs==null) ? dflt : xs;
212     }
213
214
215
216     /**
217      * Helper that converts a string to a known ExportScope singleton.
218      * Returns <i>null</i> if string unrecognized. String can be
219      * either ExportScope's symbolic name or its index.
220      * @since JWare/AntX 0.5
221      **/

222     public static ExportScope from(String JavaDoc s)
223     {
224         if (s!=null && s.length()>1) {
225             s = Tk.lowercaseFrom(s);
226             if (Character.isDigit(s.charAt(0))) {
227                 try { return from(Integer.parseInt(s)); }
228                 catch(Exception JavaDoc nfx) {/*burp*/}
229             } else {
230                 if ("default".equals(s)) { return DEFAULT_SCOPE; }
231                 if (PROJECT.value.equals(s)) { return PROJECT; }
232                 if (THREAD.value.equals(s)) { return THREAD; }
233                 if (ALL.value.equals(s)) { return ALL;}
234                 if ("system".equals(s)) { return ALL;}
235             }
236         }
237         return null;
238     }
239
240
241     /**
242      * Same as {@linkplain #from(java.lang.String) from(String)} but
243      * with a default value if value does not match any known
244      * ExportScope's name.
245      * @param s the symbolic name to be matched
246      * @param dflt the default ExportScope if necessary
247      * @since JWare/AntX 0.5
248      **/

249     public static ExportScope from(String JavaDoc s, ExportScope dflt)
250     {
251         ExportScope xs= from(s);
252         return (xs==null) ? dflt : xs;
253     }
254 }
255
256 /* end-of-ExportScope.java */
257
Popular Tags