KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > go > IffAnt


1 /**
2  * $Id: IffAnt.java 187 2007-03-25 17:59:16Z ssmc $
3  * Copyright 2004-2005,2007 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://antxtras.sf.net/ EMAIL- jware[at]users[dot]sourceforge[dot]net
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.go;
30
31 import org.apache.tools.ant.Main;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.util.regexp.RegexpMatcher;
34 import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
35
36 import com.idaremedia.antx.helpers.Tk;
37
38 /**
39  * Implementation of the general <i>if-ant</i> test for all conditional components.
40  * The <span class="src">IffAnt</span> criteria passes if the current Ant runtime's
41  * version string matches the given regular expression.
42  *
43  * @since JWare/AntX 0.4
44  * @author ssmc, &copy;2004-2005,2007 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
45  * @version 0.5.1
46  * @.safety multiple
47  * @.group impl,helper
48  * @.pattern GoF.Strategy
49  * @see Go
50  * @see IffOs
51  * @see com.idaremedia.antx.parameters.PlatformConditional PlatformConditional
52  **/

53
54 public final class IffAnt
55 {
56     private static final RegexpMatcherFactory REMFactory =
57         new RegexpMatcherFactory();
58
59     private static final String JavaDoc DFLT_VERSION_PROPERTY =
60         "ant.version";
61
62     /**
63      * Returns <i>true</i> if the version information stored in
64      * the given property matches the version regular expression.
65      * @param pattern version pattern (non-null)
66      * @param property the name of the property storing version info
67      * @param P project from which properties read
68      * @param ifNot <i>true</i> if test should be for no-match
69      **/

70     public static boolean pass(String JavaDoc pattern, String JavaDoc property,
71                                Project P, boolean ifNot)
72     {
73         if (Tk.isWhitespace(pattern) || Tk.isWhitespace(property)) {
74             return false;
75         }
76         pattern = Tk.resolveString(P,pattern);
77
78         RegexpMatcher re = REMFactory.newRegexpMatcher(P);
79         re.setPattern(pattern);
80
81         String JavaDoc setting = null;
82         if (P!=null) {
83             setting = P.getProperty(property);
84         }
85         if (setting==null) {//NB:can happen in test|in-early-load environments!
86
setting = Main.getAntVersion();
87         }
88
89         boolean is = re.matches(setting);
90         re= null;
91         return ifNot ? !is : is;
92     }
93
94
95     /**
96      * Returns <i>true</i> if the current Ant runtime's version
97      * information matches the version regular expression.
98      * @param pattern version pattern (non-null)
99      * @param P project from which properties read
100      * @param ifNot <i>true</i> if test should be for no-match
101      **/

102     public static boolean pass(String JavaDoc pattern, Project P, boolean ifNot)
103     {
104         return pass(pattern, DFLT_VERSION_PROPERTY, P, ifNot);
105     }
106
107
108     /**
109      * Execute test for an "if-antLike" conditional parameter.
110      * @since JWare/AntX 0.4
111      * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
112      * @version 0.5
113      * @.safety single
114      * @.group impl,helper
115      **/

116     public static final class IsLike extends Go.TestSkeleton {
117         public IsLike() {
118         }
119         public IsLike(String JavaDoc pattern) {
120             super(pattern);
121         }
122         public boolean pass(Project P) {
123             verifyInited();
124             return IffAnt.pass(getParameter(),P,false);
125         }
126         public String JavaDoc getParameterName() {
127             return "ifAntLike";
128         }
129     }
130
131
132     /**
133      * Execute test for an "unless-antLike" conditional parameter.
134      * @since JWare/AntX 0.4
135      * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
136      * @version 0.5
137      * @.safety single
138      * @.group impl,helper
139      **/

140     public static final class IsNotLike extends Go.TestSkeleton {
141         public IsNotLike() {
142         }
143         public IsNotLike(String JavaDoc pattern) {
144             super(pattern);
145         }
146         public boolean pass(Project P) {
147             verifyInited();
148             return IffAnt.pass(getParameter(),P,true);
149         }
150         public String JavaDoc getParameterName() {
151             return "unlessAntLike";
152         }
153     }
154
155
156     /**
157      * Execute test for an "if-antLike-property" conditional parameter.
158      * @since JWare/AntX 0.4
159      * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
160      * @version 0.5
161      * @.safety single
162      * @.group impl,helper
163      **/

164     public static final class IsLikeProperty extends Go.TestSkeleton {
165         public IsLikeProperty() {
166         }
167         public IsLikeProperty(String JavaDoc pattern, String JavaDoc property) {
168             super(pattern);
169             setSourceProperty(property);
170         }
171         public boolean pass(Project P) {
172             verifyInited();
173             return IffAnt.pass(getParameter(),m_versionProperty,P,false);
174         }
175         public String JavaDoc getParameterName() {
176             return "ifAntLikeProperty";
177         }
178         public void setSourceProperty(String JavaDoc property)
179         {
180             m_versionProperty = property;
181         }
182         private String JavaDoc m_versionProperty= DFLT_VERSION_PROPERTY;
183     }
184
185
186     /**
187      * Prevent; only helpers public.
188      **/

189     private IffAnt() {
190     }
191 }
192
193 /* end-of-IffAnt.java */
194
Popular Tags