KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > helpers > InnerNameValuePair


1 /**
2  * $Id: InnerNameValuePair.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2003-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.1 of the License, or (at your option) any
9  * 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 (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.helpers;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33
34 import com.idaremedia.antx.apis.Nameable;
35
36 /**
37  * Brain-dead luggage Name-Value bean that allows nested elements and other simple
38  * "key-value holder" elements. The name-value pair's value should be specified
39  * as either a single "<span class="src">value</span>" attribute or as nested text.
40  * Note that these name-value pairs have empty string names and values by default. To
41  * clear a pair's name and/or value to <i>null</i> you must set it to <i>null</i>
42  * explicitly (see {@linkplain #setNull()}.
43  *
44  * @since JWare/AntX 0.3
45  * @author ssmc, &copy;2003-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
46  * @version 0.5
47  * @.safety single
48  * @.group impl,helper
49  **/

50
51 public class InnerNameValuePair implements Nameable, Cloneable JavaDoc
52 {
53     /**
54      * Creates new empty pair (name and value are empty).
55      **/

56     public InnerNameValuePair()
57     {
58     }
59
60
61     /**
62      * Creates new predefined pair.
63      **/

64     public InnerNameValuePair(String JavaDoc name, String JavaDoc value)
65     {
66         setNV(name,value);
67     }
68
69
70
71     /**
72      * Returns independent clone of this name-value pair.
73      * @since JWare/AntX 0.5
74      **/

75     public Object JavaDoc clone()
76     {
77         try {
78             return super.clone();
79         } catch(CloneNotSupportedException JavaDoc clnX) {
80             throw new Error JavaDoc();
81         }
82     }
83
84
85
86     /**
87      * <em>Replaces</em> this pair's current name.
88      * @param s the new name (non-null)
89      **/

90     public void setName(String JavaDoc s)
91     {
92         m_name = s;
93     }
94
95
96
97     /**
98      * Returns this pair's current name. Will return
99      * the empty string if this property never set.
100      **/

101     public String JavaDoc getName()
102     {
103         return m_name;
104     }
105
106
107
108     /**
109      * <em>Replaces</em> this pair's current value.
110      * @param s the new value (non-null)
111      **/

112     public void setValue(String JavaDoc s)
113     {
114         m_value = s;
115     }
116
117
118
119     /**
120      * Adds more text to this pair's current value.
121      * @param text additional text (non-null)
122      **/

123     public void addText(String JavaDoc text)
124     {
125         m_value += text;
126     }
127
128
129
130     /**
131      * Returns this pair's current value.
132      **/

133     public String JavaDoc getValue()
134     {
135         return m_value;
136     }
137
138
139
140     /**
141      * Returns this pair's current value with property substition
142      * first applied.
143      * @param P project from which property values read
144      **/

145     public String JavaDoc getValue(final Project P)
146     {
147         String JavaDoc s = getValue();
148         if (P!=null) {
149             s = Tk.resolveString(P,s);
150         }
151         return s;
152     }
153
154
155
156     /**
157      * Returns this pair's current value with property substition
158      * first applied.
159      * @param P project from which property values read
160      * @param altForm alternative form of property references allowed
161      * @since JWare/AntX 0.5
162      **/

163     public String JavaDoc getValue(final Project P, boolean altForm)
164     {
165         String JavaDoc s = getValue();
166         if (P!=null) {
167             s = Tk.resolveString(P,s,altForm);
168         }
169         return s;
170     }
171
172
173
174     /**
175      * Forces both this item's name and its value to <i>null</i>.
176      * @since JWare/AntX 0.5
177      **/

178     public void setNull()
179     {
180         m_name = null;
181         m_value = null;
182     }
183
184
185
186     /**
187      * Utility method that sets both name and value in a
188      * single call. Incoming values can be <i>null</i>.
189      **/

190     public void setNV(String JavaDoc name, String JavaDoc value)
191     {
192         m_name = name;
193         m_value = value;
194     }
195
196
197
198     /**
199      * Returns a template string like: "<span class="src">name=value</span>"
200      * where "<span class="src">name</span>" is this pair's current
201      * name and "<span class="src">value</span>" is this pair's current
202      * value.
203      **/

204     public String JavaDoc toString()
205     {
206         return ""+getName()+"="+getValue();
207     }
208
209
210
211     /**
212      * Same as {@linkplain #toString toString()} but with embedded
213      * properties replaced with supplied project's definitions.
214      * @param P project from which properties resolved
215      **/

216     public String JavaDoc toString(final Project P)
217     {
218         String JavaDoc s = getValue();
219         if (P!=null) {
220             s = Tk.resolveString(P,s);
221         }
222         return ""+getName()+"="+s;
223     }
224
225
226
227     /**
228      * Ensures this item has a non-null, non-empty name assigned.
229      * @throws BuildException if name malformed.
230      * @since JWare/AntX 0.5
231      **/

232     public void verifyNamed()
233     {
234         if (m_name==null || m_name.length()==0) {
235             throw new BuildException("A non-empty name is required.");
236         }
237     }
238
239     private String JavaDoc m_name="", m_value="";
240 }
241
242 /* end-of-InnerNameValuePair.java */
243
Popular Tags