KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: PropertyExaminer.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 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 java.util.Map JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.PropertyHelper;
37
38 import com.idaremedia.antx.AntX;
39 import com.idaremedia.antx.helpers.NameValuePair;
40
41 /**
42  * Little helper that can evaluate a project property definition for unresolved
43  * variable references. The helper can be setup to sniff for "simple" all-in-one
44  * unresolved references (the default), or for complex, nested references (more
45  * memory/time costly).
46  *
47  * @since JWare/AntX 0.4
48  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
49  * @version 0.5
50  * @.safety multiple, after examiner configured
51  * @.group impl,helper
52  * @see PropertiesIterator
53  **/

54
55 public class PropertyExaminer extends ProjectDependentSkeleton
56 {
57     private static final String JavaDoc IAM_ = AntX.utilities+"PropertyExaminer:";
58
59
60     /** Simple unresolved property check. **/
61     private static final int DONTCHECK= 0;
62     /** Simple unresolved property check. **/
63     private static final int SIMPLE= DONTCHECK+100;
64     /** Anal unresolved property check. **/
65     private static final int THOROUGH= SIMPLE+1;
66
67
68     /**
69      * Initializes new property examiner instance. The target
70      * project must be suppled on a call-by-call basis or
71      * <span class="src">setProject</span> must be called.
72      **/

73     public PropertyExaminer()
74     {
75     }
76
77
78     /**
79      * Initializes new project-specific property examiner instance.
80      * @param P target project for property checks (non-null)
81      **/

82     public PropertyExaminer(Project P)
83     {
84         setProject(P);
85     }
86
87
88     /**
89      * Tells this examiner to check the property for an all-in-one
90      * failed variable substitution. If called, this examiner will
91      * either skip bad properties or return a proxy if one defined.
92      * @see #setBrokenSubstitutionProxy setBrokenSubstitutionProxy(String)
93      **/

94     public final void setCheckSimpleBrokenSubstitution()
95     {
96         m_checkBadSubst = SIMPLE;
97     }
98
99
100     /**
101      * Tells this examiner to thoroughly check the property for any
102      * bad (failed) variable substitution. If called, this examiner
103      * will either skip bad properties or return a proxy if one
104      * defined.
105      * @see #setBrokenSubstitutionProxy setBrokenSubstitutionProxy(String)
106      **/

107     public final void setCheckBrokenSubstitution()
108     {
109         m_checkBadSubst = THOROUGH;
110     }
111
112
113     /**
114      * Tells this examiner to return property candidate as-is.
115      * Effectively makes this examiner a no-op.
116      **/

117     public final void clearCheckBrokenSubstitution()
118     {
119         m_checkBadSubst = DONTCHECK;
120     }
121
122
123     /**
124      * Returns <i>true</i> if this examiner will check for
125      * unresolved property definitions (simple or complex). Is
126      * <i>true</i> by default.
127      * @see #clearCheckBrokenSubstitution
128      **/

129     public final boolean willCheckBrokenSubstitution()
130     {
131         return m_checkBadSubst!=DONTCHECK;
132     }
133
134
135     /**
136      * Define a proxy value string for failed substitution
137      * properties. This proxy has no affect unless this examiner
138      * is setup to check for bad property substitution.
139      * @param valueProxy string to use for property value (non-null)
140      * @see UnresolvedProperty#VALUE
141      **/

142     public final void setBrokenSubstitutionProxy(String JavaDoc valueProxy)
143     {
144         AntX.require_(valueProxy!=null,IAM_,"setProxy- nonzro valu");
145         m_valueProxy = valueProxy;
146     }
147
148
149     /**
150      * Common work that filters the given value through this examiner's
151      * bad-substitution check. Will return <i>false</i> if entire
152      * value is unresolved or this is a complex examiner and
153      * part of value is unresolved.
154      **/

155     private boolean isBroken(String JavaDoc value, Project P, String JavaDoc name)
156     {
157         boolean broken = value==null;
158         if (!broken && m_checkBadSubst!=DONTCHECK) {
159             value = value.trim();
160             broken = value.startsWith("${") && value.endsWith("}");
161
162             if (!broken && m_checkBadSubst!=SIMPLE) {//tst nested broken
163
int i = value.indexOf("${");
164                 if (i>=0) {
165                     PropertyHelper Ph= PropertyHelper.getPropertyHelper(P);
166                     Vector JavaDoc ignore = new Vector JavaDoc(); /*icky-API*/
167                     Vector JavaDoc misses = new Vector JavaDoc(); /* " " */
168                     try {
169                         Ph.parsePropertyString(value,ignore,misses);
170                         if (!misses.isEmpty()) {
171                             broken= true;
172                         }
173                     } catch(BuildException malformX) {
174                         String JavaDoc warning = AntX.uistrs().get
175                             ("fixture.bad.propdef.found", name,value);
176                         P.log(warning,Project.MSG_WARN);
177                         broken= true;
178                     }
179                 }//might-be
180
}
181         }
182         return broken;
183     }
184
185
186     /**
187      * Filters the incoming value through this examiner's
188      * bad-substitution check. Will return <i>false</i> if could
189      * not verify value.
190      * @param value value to check (non-null)
191      * @param P target project (non-null)
192      **/

193     public boolean verifiedLiteral(String JavaDoc value, Project P)
194     {
195         AntX.require_(value!=null,IAM_,"verifyLiteral- nonzro value");
196         AntX.require_(P!=null,IAM_,"verifyLiteral- nonzro project");
197         return !isBroken(value,P,"");
198     }
199
200
201     /**
202      * Shortcut for {@linkplain #verifiedLiteral(String,Project)
203      * verifiedLiteral(String,Project)} that uses this examiner's
204      * own project.
205      **/

206     public final boolean verifiedLiteral(String JavaDoc value)
207     {
208         return verifiedLiteral(value,getProject());
209     }
210
211
212     /**
213      * Filters the incoming property through this examiner's
214      * bad-substitution check. Will return a replacement entry if
215      * incoming entry fails check and a value-proxy was defined.
216      * Otherwise will return <i>null</i> if the entry fails check.
217      * @param entry property key-value pair (non-null)
218      * @param P target project (non-null)
219      * @see #setCheckBrokenSubstitution
220      **/

221     public Map.Entry JavaDoc verifiedPropertyValue(Map.Entry JavaDoc entry, Project P)
222     {
223         AntX.require_(entry!=null && entry.getKey()!=null,IAM_,
224                       "verifyProp- nonzro property");
225         AntX.require_(P!=null,IAM_,"verifyProp- nonzro project");
226
227         Map.Entry JavaDoc result = entry;
228         boolean broken = isBroken((String JavaDoc)entry.getValue(),P,
229                                   entry.getKey().toString());
230         if (broken) {
231             //?replace
232
if (m_valueProxy!=null) {
233                 NameValuePair nvp = new NameValuePair();
234                 result = nvp;
235                 nvp.setName(entry.getKey().toString());
236                 nvp.setValue(m_valueProxy);
237             }
238             //?or skip
239
else {
240                 result = null;
241             }
242         }
243         return result;
244     }
245
246
247     /**
248      * Shortcut for {@linkplain #verifiedPropertyValue(Map.Entry,Project)
249      * verifiedPropertyValue(Entry,Project)} that uses this examiner's
250      * own project.
251      **/

252     public final Map.Entry JavaDoc verifiedPropertyValue(Map.Entry JavaDoc entry)
253     {
254         return verifiedPropertyValue(entry,getProject());
255     }
256
257
258     private int m_checkBadSubst=SIMPLE;//NB:=> return as-is
259
private String JavaDoc m_valueProxy;//NB:=> skip-bad
260
}
261
262
263 /* end-of-PropertyExaminer.java */
264
Popular Tags