KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > feedback > EmitInclude


1 /**
2  * $Id: EmitInclude.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 of the License, or (at your option) any later
9  * 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.feedback;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.apache.tools.ant.Project;
35 import org.apache.tools.ant.types.PropertySet;
36 import org.apache.tools.ant.types.Reference;
37
38 import com.idaremedia.antx.AntX;
39 import com.idaremedia.antx.AssertableProjectComponent;
40 import com.idaremedia.antx.ErrorSnapshot;
41 import com.idaremedia.antx.FixtureExaminer;
42 import com.idaremedia.antx.helpers.Strings;
43 import com.idaremedia.antx.parameters.FlexValueSupport;
44 import com.idaremedia.antx.print.DiagnosticsValue;
45
46 /**
47  * Helper used to nest Ant entities included with emitted messages. Allows inclusion
48  * of comment items like properties, exported-variables, and references while being
49  * extensible for additonal items later. Some examples:<pre>
50  * &lt;include property="ant.file"/&gt;
51  * &lt;include var="my.var"/&gt;
52  * &lt;include reference="compiling.err"/&gt;
53  * &lt;include taskdef="emitconfigure"/&gt;
54  * &lt;include typedef="all"/&gt;
55  * </pre>
56  *
57  * @since JWare/AntX 0.1
58  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
59  * @version 0.5
60  * @.safety guarded
61  * @.group impl,helper
62  * @see EmitTask#addConfiguredInclude
63  **/

64
65 public class EmitInclude extends AssertableProjectComponent implements FlexValueSupport
66 {
67     /**
68      * Initializes new EmitInclude instance.
69      **/

70     public EmitInclude()
71     {
72         super(AntX.feedback+"include");
73     }
74
75     /**
76      * Sets this include's item's name.
77      **/

78     protected final void setKey(String JavaDoc key)
79     {
80         m_itemName = key;
81     }
82
83
84     /**
85      * Returns this include's item's name. Returns <i>null</i>
86      * if never set (through one of various 'set' methods).
87      **/

88     public final String JavaDoc getKey()
89     {
90         return m_itemName;
91     }
92
93
94     /**
95      * Marks this as a project property value inclusion.
96      **/

97     public void setProperty(String JavaDoc property)
98     {
99         require_(property!=null,"setProp- nonzro name");
100         m_xstring.set(property);
101         m_xstring.setIsProperty(true);
102         setKey(property);
103     }
104
105
106     /**
107      * Marks this as a project reference value inclusion.
108      **/

109     public void setReference(String JavaDoc refId)
110     {
111         require_(refId!=null,"setRefId- nonzro name");
112         m_xstring.set(refId);
113         m_xstring.setIsReference(true);
114         setKey(refId);
115     }
116
117
118     /**
119      * Marks this as a project exported property value inclusion.
120      **/

121     public void setVariable(String JavaDoc variable)
122     {
123         require_(variable!=null,"setVar- nonzro name");
124         m_xstring.set(variable);
125         m_xstring.setIsExported(true);
126         setKey(variable);
127     }
128
129
130     /**
131      * Synonym for {@linkplain #setVariable setVariable()}.
132      **/

133     public final void setVar(String JavaDoc variable)
134     {
135         setVariable(variable);
136     }
137
138
139     /**
140      * Marks this as a property set inclusion.
141      * @param r existing property set reference (non-null)
142      * @since JWare/AntX 0.4
143      **/

144     public void setPropertySet(Reference r)
145     {
146         require_(r!=null,"setPropSet- nonzro ref");
147         require_(r.getRefId()!=null,"setPropSet- nonzro refid");
148         setKey(r.getRefId());
149         m_Is = IS_PSET; //xclusive
150
}
151
152
153     /**
154      * Marks this as a task-definition class name inclusion.
155      **/

156     public void setTaskdef(String JavaDoc taskName)
157     {
158         require_(taskName!=null,"setTaskdef- nonzro name");
159         setKey(taskName);
160         m_Is = IS_TASK; //xclusive
161
}
162
163
164     /**
165      * Marks this as a type-definition class name inclusion.
166      **/

167     public void setTypedef(String JavaDoc typeName)
168     {
169         require_(typeName!=null,"setTypedef- nonzro name");
170         setKey(typeName);
171         m_Is = IS_TYPE; //xclusive
172
}
173
174
175     /**
176      * Includes whatever this inclusion refers to the given
177      * snapshot.
178      * @param es the snapshot to be updated (non-null)
179      **/

180     public void apply(ErrorSnapshot es)
181     {
182         require_(es!=null,"apply- nonzro snapshot");
183         if (!putFlexString(es)) {
184
185             final Project P= getProject();
186             final String JavaDoc item = getKey();
187             Map JavaDoc mp;
188
189             if (isPropertySet()) {
190                 es.addConfiguredPropertySet
191                     ((PropertySet)FixtureExaminer.getReferencedObject
192                      (P,null,item,PropertySet.class));
193             }
194             else if (isTaskDef()) {
195                 mp= P.getTaskDefinitions();
196                 if (Strings.ALL.equals(item)) {
197                     putDefiners(es,mp,"TaskDef");
198                 } else {
199                     putDefiner(es,item,(Class JavaDoc)mp.get(item),"TaskDef");
200                 }
201
202             }
203             else if (isTypeDef()) {
204                 mp= P.getDataTypeDefinitions();
205                 if (Strings.ALL.equals(item)) {
206                     putDefiners(es,mp,"TypeDef");
207                 } else {
208                     putDefiner(es,item,(Class JavaDoc)mp.get(item),"TypeDef");
209                 }
210             }
211         }
212     }
213
214
215     /**
216      * Captures a single type or task definition to snapshot.
217      **/

218     private void putDefiner(ErrorSnapshot es, String JavaDoc key, Class JavaDoc c, String JavaDoc prefix)
219     {
220         if (c!=null) {
221             es.setProperty(prefix+"."+key,c.getName());
222         } else {
223             es.setProperty(prefix+"."+key,null);
224         }
225     }
226
227
228     /**
229      * Captures all type or task definitions to snapshot.
230      **/

231     private void putDefiners(ErrorSnapshot es, Map JavaDoc mp, String JavaDoc prefix)
232     {
233         Iterator JavaDoc itr= mp.entrySet().iterator();
234         while (itr.hasNext()) {
235             Map.Entry JavaDoc mE= (Map.Entry JavaDoc)itr.next();
236             if (mE!=null) {
237                 es.setProperty(prefix+"."+mE.getKey(),((Class JavaDoc)mE.getValue()).getName());
238             }
239         }
240     }
241
242
243     /**
244      * If active, captures contents of flex string to snapshot.
245      * Returns <i>false</i> if flex string was activated by any
246      * setter method.
247      **/

248     private boolean putFlexString(ErrorSnapshot es)
249     {
250         if (!m_xstring.isUndefined()) {
251             es.setProperty(m_xstring.get(), m_xstring.getValue(getProject()));
252             return true;
253         }
254         return false;
255     }
256
257
258     /**
259      * Returns <i>true</i> if this is task-definition inclusion.
260      **/

261     protected final boolean isTaskDef()
262     {
263         return (m_Is&IS_TASK)==IS_TASK;
264     }
265
266
267     /**
268      * Returns <i>true</i> if this is type-definition inclusion.
269      **/

270     protected final boolean isTypeDef()
271     {
272         return (m_Is&IS_TYPE)==IS_TYPE;
273     }
274
275
276     /**
277      * Returns <i>true</i> if this is &lt;propertyset&gt; inclusion.
278      * @since JWare/AntX 0.4
279      **/

280     protected final boolean isPropertySet()
281     {
282         return (m_Is&IS_PSET)==IS_PSET;
283     }
284
285
286
287     private static final int IS_NONE=0;
288     private static final int IS_TYPE=0x01;
289     private static final int IS_TASK=0x02;
290     private static final int IS_PSET=0x04;
291
292     private String JavaDoc m_itemName;
293     private int m_Is= IS_NONE;
294     private DiagnosticsValue m_xstring = new DiagnosticsValue(true);//NB:most often done
295
}
296
297 /* end-of-EmitInclude.java */
298
Popular Tags