KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: ConditionalParameters.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2005 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.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import org.apache.tools.ant.Project;
36
37 import com.idaremedia.antx.helpers.GenericParameters;
38 import com.idaremedia.antx.helpers.InnerNameValuePair;
39 import com.idaremedia.antx.parameters.Conditional;
40
41 /**
42  * Extension of standard {@linkplain GenericParameters GenericParameters} that supports
43  * conditional parameters.
44  *
45  * @since JWare/AntX 0.5
46  * @author ssmc, &copy;2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
47  * @version 0.5
48  * @.safety single
49  * @.group impl,helper
50  * @see ConditionalInnerNameValuePair
51  **/

52
53 public final class ConditionalParameters extends GenericParameters
54 {
55     /**
56      * Initializes a new empty parameters collection bean.
57      **/

58     public ConditionalParameters()
59     {
60     }
61
62
63
64     /**
65      * Initializes a new empty parameters collection bean
66      * associated with project.
67      * @param project this parameter object's enclosing project
68      **/

69     public ConditionalParameters(Project project)
70     {
71         setProject(project);
72     }
73
74
75
76     /**
77      * Returns an independent map of this collection's
78      * name-value items. The returned map contains the
79      * {@linkplain InnerNameValuePair} items not excluded by their
80      * if/unless constraints.
81      * @param P [optional] project used to resolve property references.
82      **/

83     public Map JavaDoc copyOfParameterObjects(Project P)
84     {
85         if (P==null) {
86             P = getProject();
87         }
88         Map JavaDoc copy = super.copyOfParameterObjects();
89         if (!copy.isEmpty()) {
90             Iterator JavaDoc itr = copy.values().iterator();
91             while (itr.hasNext()) {
92                 InnerNameValuePair pair = (InnerNameValuePair)itr.next();
93                 if (pair instanceof Conditional) {
94                     if (!OptionalExecuteHelper.include(P,(Conditional)pair)) {
95                         itr.remove();
96                     }
97                 }
98             }
99         }
100         return copy;
101     }
102
103
104
105     /**
106      * Shortcut for {@linkplain #copyOfParameterObjects(Project)
107      * copyOfParameterObjects(Project)} using this item's own project
108      * to resolve references.
109      */

110     public Map JavaDoc copyOfParameterObjects()
111     {
112         return copyOfParameterObjects(null);
113     }
114
115
116
117     /**
118      * Returns an independent Properties map of this collection's
119      * name-value items as simplified name-stringvalue pairs.
120      * The returned map contains the item names and string
121      * values of those items not excluded by their if/unless constraints.
122      * Handles hetergeneous items fine.
123      * @param P [optional] project used to resolve property references.
124      * @param altForm alternative form of property references allowed.
125      **/

126     public Properties JavaDoc copyOfSimpleKeyValues(final Project P, boolean altForm)
127     {
128         Properties JavaDoc copy = new Properties JavaDoc();
129         if (!isEmpty()) {
130             Iterator JavaDoc itr= values().iterator();
131             while (itr.hasNext()) {
132                 InnerNameValuePair pair = (InnerNameValuePair)itr.next();
133                 if (pair instanceof Conditional) {
134                     if (OptionalExecuteHelper.include(P,(Conditional)pair)) {
135                         copy.put(pair.getName(),pair.getValue(P,altForm));
136                     }
137                 } else {
138                     copy.put(pair.getName(),pair.getValue(P,altForm));
139                 }
140             }
141         }
142         return copy;
143     }
144
145
146
147     /**
148      * Factory method to create a new conditional name-value pair
149      * for inclusion in this collection.
150      **/

151     protected InnerNameValuePair newNVPair()
152     {
153         return new ConditionalInnerNameValuePair();
154     }
155 }
156
157 /* end-of-ConditionalParameters.java */
Popular Tags