KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: AssertableDataType.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 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;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Stack JavaDoc;
34
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.types.DataType;
38 import org.apache.tools.ant.types.Reference;
39
40 import com.idaremedia.apis.UIStringManager;
41
42 import com.idaremedia.antx.apis.ProjectDependent;
43 import com.idaremedia.antx.helpers.Strings;
44 import com.idaremedia.antx.helpers.Tk;
45
46 /**
47  * Extension of basic Ant <i>DataType</i> that adds builtin assertions and helpers for
48  * working with the 'refid' attribute.
49  *
50  * @since JWare/AntX 0.2
51  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
52  * @version 0.5
53  * @.safety single
54  * @.group impl,helper
55  **/

56
57 public abstract class AssertableDataType extends DataType
58     implements ProjectDependent
59 {
60     /**
61      * Initializes a new unlabeled datatype.
62      **/

63     protected AssertableDataType()
64     {
65         super();
66         Iam_="";
67     }
68
69
70     /**
71      * Initializes new CV-labeled datatype.
72      * @param iam CV-label (non-null)
73      **/

74     protected AssertableDataType(String JavaDoc iam)
75     {
76         super();
77         Iam_= Tk.cvlabelFrom(iam);
78     }
79
80
81     /**
82      * Returns this datatype's CV-label. Never <i>null</i>.
83      **/

84     protected final String JavaDoc cvlabel_()
85     {
86         return Iam_;
87     }
88
89
90     /**
91      * Shortcut that returns this datatypes's internal AntX UI
92      * strings manager. Never returns <i>null</i>.
93      * @see AntX#uistrs
94      **/

95     public final UIStringManager uistrs()
96     {
97         return AntX.uistrs();
98     }
99
100
101     /**
102      * Throws assertion error if pre-condtion is not met.
103      * @param c pre-condition
104      * @param msg [optional] failure message (if not met)
105      * @throws IllegalArgumentException if condition not met
106      **/

107     protected final void require_(boolean c, String JavaDoc msg)
108     {
109         if (!c) {
110             String JavaDoc ermsg = uistrs().get("cv.require",Iam_,msg);
111             log(ermsg, Project.MSG_ERR);
112             throw new IllegalArgumentException JavaDoc(ermsg);
113         }
114     }
115
116
117     /**
118      * Throws assertion error if post-condition is not met. Used
119      * for post-condition verification.
120      * @param c post-condition
121      * @param msg [optional] failure message (if not met)
122      * @throws IllegalStateException if condition not met
123      **/

124     protected final void ensure_(boolean c, String JavaDoc msg)
125     {
126         if (!c) {
127             String JavaDoc ermsg = uistrs().get("cv.ensure",Iam_,msg);
128             log(ermsg, Project.MSG_ERR);
129             throw new IllegalStateException JavaDoc(ermsg);
130         }
131     }
132
133
134     /**
135      * Throws assertion error if condition is not met. Used for
136      * block and invariant verification.
137      * @param c condition
138      * @param msg [optional] failure message (if not met)
139      * @throws IllegalStateException if condition not met
140      **/

141     protected final void verify_(boolean c, String JavaDoc msg)
142     {
143         if (!c) {
144             String JavaDoc ermsg = uistrs().get("cv.verify",Iam_,msg);
145             log(ermsg, Project.MSG_ERR);
146             throw new IllegalStateException JavaDoc(ermsg);
147         }
148     }
149
150
151     /**
152      * Notes an unexpected but manageable problem has occured.
153      * Just logs a warning by default.
154      * @param t [optional] causing throwable
155      * @param msg caller's additional (context) message
156      **/

157     protected final void unexpected_(Throwable JavaDoc t, String JavaDoc msg)
158     {
159         String JavaDoc ermsg = uistrs().get("cv.unexpected",Iam_,msg,t);
160         log(ermsg, Project.MSG_WARN);
161     }
162
163
164
165     /**
166      * Verifies we're in a live project (created from build
167      * process).
168      **/

169     protected final void verifyInProject_(String JavaDoc calr)
170     {
171         if (getProject()==null) {
172             String JavaDoc ermsg = uistrs().get("cv.verifyInP",Iam_,calr);
173             log(ermsg, Project.MSG_ERR);
174             throw new IllegalStateException JavaDoc(ermsg);
175         }
176     }
177
178
179
180     /**
181      * Tries to return an interesting identifier for this type.
182      **/

183     public String JavaDoc getId()
184     {
185         return getClass().getName()+"@"+
186             String.valueOf(System.identityHashCode(this));
187     }
188
189
190
191
192     /**
193      * Set this instance as a reference to an existing item.
194      * @throws BuildException if any other attribute or nested element
195      * has been defined
196      **/

197     public void setRefid(Reference r)
198         throws BuildException
199     {
200         require_(r!=null,"setRef- nonzro ref");
201         if (isIndependentlyEdited()) {
202             throw tooManyAttributes();
203         }
204         super.setRefid(r);
205     }
206
207
208
209     /**
210      * Increments the non-refid attribute modified count; if user tries
211      * to subsequently set this item as a reference, it veill die
212      * very big.
213      **/

214     protected void edited(String JavaDoc what)
215     {
216         m_attrsEdited++;
217     }
218
219     /**
220      * Shortcut of {@linkplain #edited(String) edited(&#46;&#46;&#46;)}
221      * when the caller's identity isn't passed.
222      **/

223     protected final void edited()
224     {
225         edited("");
226     }
227
228
229     /**
230      * Returns <i>true</i> if attributes other than the refid have
231      * been defined.
232      **/

233     protected final boolean isIndependentlyEdited()
234     {
235         return m_attrsEdited>0;
236     }
237
238
239
240     /**
241      * Returns a type-checked referenced object. Common utility that
242      * generates resource bundle based messages if reference broken.
243      * @param theProject the source project (null=> this task's project)
244      * @param refid the referred-to thing's identifier (non-null)
245      * @param requiredClass the required class of referred-to thing (non-null)
246      * @throws BuildException if no such reference or object is not compatible
247      * @since JWare/AntX 0.3
248      **/

249     public final Object JavaDoc getReferencedObject(Project theProject, String JavaDoc refid,
250                                             Class JavaDoc requiredClass)
251     {
252         require_(refid!=null,"getRefObj- nonzro id");
253         if (theProject==null) {
254             theProject= getProject();
255             verify_(theProject!=null,"getRefObj- hav project");
256         }
257
258         Object JavaDoc o = theProject.getReference(refid);
259         String JavaDoc error = null;
260
261         if (o==null) {
262             error = uistrs().get("task.missing.refid", refid);
263         }
264         else if (!requiredClass.isInstance(o)) {
265             error = uistrs().get("task.bad.refid", refid,
266                                  requiredClass.getName(),
267                                  o.getClass().getName());
268         }
269         if (error!=null) {
270             log(error,Project.MSG_ERR);
271             throw new BuildException(error);
272         }
273         return o;
274     }
275
276
277
278     /**
279      * Returns a circular-dependency/type-checked reference. Like inherited
280      * method except uses resource bundle-based error messages.
281      **/

282     protected Object JavaDoc getCheckedRef(Class JavaDoc requiredClass, String JavaDoc typeName)
283     {
284         if (!isChecked()) {
285             Stack JavaDoc stk = new Stack JavaDoc();
286             stk.push(this);
287             dieOnCircularReference(stk,getProject());
288         }
289         return getReferencedObject(getProject(),getRefid().getRefId(),requiredClass);
290     }
291
292
293
294     /**
295      * Tries to determine the (fully qualified) script name of
296      * this object. Will return "<span class="src">none</span>" if
297      * not able to determine proper name.
298      * @since JWare/AntX 0.4
299      * @.bug Does not work for two declarations of same type in
300      * different namespaces!
301      **/

302     public synchronized String JavaDoc getTypeName()
303     {
304         if (m_typeName==null) {
305             verifyInProject_("typeName");
306             Class JavaDoc myClass = getClass();
307             Iterator JavaDoc itr= getProject().getDataTypeDefinitions().
308                 entrySet().iterator();//lock|clone?
309
while (itr.hasNext()) {
310                 Map.Entry JavaDoc mE= (Map.Entry JavaDoc)itr.next();
311                 if (myClass==mE.getValue()) {
312                     m_typeName = (String JavaDoc)mE.getKey();
313                     break;
314                 }
315             }
316         }
317         return m_typeName==null ? Strings.NONE : m_typeName;
318     }
319
320
321     private int m_attrsEdited;//NB: to zap refid+others
322
private final String JavaDoc Iam_;
323     private String JavaDoc m_typeName;//NB: lazy-inited
324
}
325
326 /* end-of-AssertableDataType.java */
327
Popular Tags