KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > solo > StringList


1 /**
2  * $Id: StringList.java 186 2007-03-16 13:42:35Z ssmc $
3  * Copyright 2003-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 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.solo;
30
31 import java.io.File JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import org.apache.tools.ant.Project;
35
36 import com.idaremedia.antx.AntX;
37 import com.idaremedia.antx.apis.AntLibFriendly;
38 import com.idaremedia.antx.helpers.InnerString;
39 import com.idaremedia.antx.starters.ListFriendly;
40 import com.idaremedia.antx.starters.StringItemList;
41 import com.idaremedia.antx.starters.StringItemListHandle;
42
43 /**
44  * A list of strings as a formal Ant type. Useful to minimize those "comma-delimited"
45  * lists of string, filenames, etc. that seem to sprout up everywhere.
46  * <p>
47  * <b>Examples:</b><pre>
48  * &lt;<b>strings</b> id="default.extensions" prefix="*."&gt;
49  * &lt;string value="java"/&gt;
50  * &lt;string value="xml"/&gt;
51  * &lt;string value="ent"/&gt;
52  * &lt;string value="properties"/&gt;
53  * &lt;/strings&gt;
54  *
55  * &lt;<b>strings</b> id="my.extensions" prefix="*." file="${user.home}/.buildext"/&gt;
56  *
57  * &lt;<b>strings</b> id="my.extensions" prefix="*." file="${user.home}/.buildext"&gt;
58  * &lt;string value="xsl"/&gt;
59  * &lt;string value="php"/&gt;
60  * &lt;strings listref="defaults.extensions" raw="yes"/&gt;
61  * &lt;/strings&gt;
62  * </pre>
63  *
64  * @since JWare/AntX 0.3
65  * @author ssmc, &copy;2003-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
66  * @version 0.5
67  * @.safety multiple (after fully configured)
68  * @.group api,helper
69  **/

70
71 public final class StringList extends StringItemList
72     implements ListFriendly, AntLibFriendly
73 {
74     /**
75      * Initializes a new StringList instance.
76      **/

77     public StringList()
78     {
79         super(AntX.fixture+"StringList:");
80     }
81
82
83     /**
84      * Initializes a new pre-initialized StringList instance.
85      **/

86     public StringList(String JavaDoc list)
87     {
88         super(AntX.fixture+"StringList:",list);
89
90     }
91
92
93     /**
94      * Sets a custom delimiter for this string list. This
95      * delimiter is used by stringification(TM) methods.
96      * @param delimiter new delimiter
97      **/

98     public void setDelim(String JavaDoc delimiter)
99     {
100         require_(delimiter!=null,"setDelim- nonzro delim");
101         if (isReference()) {
102             throw tooManyAttributes();
103         }
104         m_delim = delimiter;
105         edited();
106     }
107
108
109     /**
110      * Returns the delimiter this strings list will use for
111      * any stringification(TM) of its contents. Defaults
112      * to a comma. Never returns <i>null</i>.
113      * @see AntX#DEFAULT_DELIMITER
114      **/

115     public String JavaDoc getDelim()
116     {
117         if (isReference()) {
118             return getOtherList().getDelim();
119         }
120         return m_delim;
121     }
122
123
124     /**
125      * Sets a prefix that will be prepended to all strings
126      * returned by this type.
127      * @param prefix the prefix (non-null)
128      * @throws BuildException if this item is a reference
129      **/

130     public void setPrefix(String JavaDoc prefix)
131     {
132         setPrefixString(prefix);
133     }
134
135
136     /**
137      * Returns this string list's prefix or the empty
138      * string if never set. Never returns <i>null</i>.
139      **/

140     public String JavaDoc getPrefix()
141     {
142         if (isReference()) {
143             return getOtherList().getPrefix();
144         }
145         return getPrefixString();
146     }
147
148
149     /**
150      * Set a suffix that will be appended to all strings
151      * returned by this type.
152      * @param suffix the suffix (non-null)
153      * @throws BuildException if this item is a reference
154      **/

155     public void setSuffix(String JavaDoc suffix)
156     {
157         setSuffixString(suffix);
158     }
159
160
161     /**
162      * Returns this string list's suffix or the empty
163      * string if never set. Never returns <i>null</i>.
164      **/

165     public String JavaDoc getSuffix()
166     {
167         if (isReference()) {
168             return getOtherList().getSuffix();
169         }
170         return getSuffixString();
171     }
172
173
174     /**
175      * Appends the contents of a newline-delimited file to
176      * this string list. No-op if file is missing or empty.
177      * @param f the file (non-null)
178      * @since JWare/AntX 0.4
179      **/

180     public void setFile(File JavaDoc f)
181     {
182         require_(f!=null,"setfil- nonzro fil");
183         addFileOrURL(f.getPath());
184     }
185
186
187     /**
188      * Appends the contents of a newline-delimited resource
189      * file to this string list. No-op if file is missing or
190      * empty.
191      * @param rn the resource path (non-null)
192      * @since JWare/AntX 0.4
193      **/

194     public void setResource(String JavaDoc rn)
195     {
196         require_(rn!=null,"setRez- nonzro reznam");
197         addResource(rn);
198     }
199
200
201     /**
202      * Appends given string (with property substitution) to
203      * this string list.
204      * @param string new string
205      **/

206     public void addConfiguredString(InnerString string)
207     {
208         require_(string!=null,"addStr- nonzro itm");
209         addItem(string,getProject());
210     }
211
212
213
214     /**
215      * Appends (in place) the strings referred to be the
216      * given handle. The strings are immediately inserted!
217      * @param listH reference to another list-friendly data object.
218      * @since JWare/AntX 0.5
219      **/

220     public void addConfiguredStrings(StringItemListHandle listH)
221     {
222         require_(listH!=null,"addStrings- nonzro listref");
223         if (isReference()) {
224             throw tooManyAttributes();
225         }
226         Iterator JavaDoc itr = listH.readonlyStringIterator(getProject());
227         while (itr.hasNext()) {
228             String JavaDoc value = itr.next().toString();
229             addItemFinal(value);
230             edited();
231         }
232     }
233
234
235
236     /**
237      * Returns a comma-delimited list of this list's contents.
238      **/

239     public String JavaDoc stringFrom(Project theProject)
240     {
241         if (isReference()) {
242             return getOtherList().stringFrom(theProject);
243         }
244         final String JavaDoc theDelimiter = getDelim();
245         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(150);
246
247         int N=0;
248         Iterator JavaDoc itr= readonlyStringIterator(theProject);
249         while (itr.hasNext()) {
250             if (N>0) {
251                 sb.append(theDelimiter);
252             }
253             sb.append(itr.next());
254             N++;
255         }
256         itr=null;
257         return sb.substring(0);
258     }
259
260
261     /**
262      * Synonym for {@linkplain #stringFrom stringFrom()}.
263      **/

264     public String JavaDoc toString()
265     {
266         return stringFrom(getProject());
267     }
268
269
270     private final StringList getOtherList()
271     {
272         return (StringList)getOtherItemList(StringList.class);
273     }
274
275     private String JavaDoc m_delim = AntX.DEFAULT_DELIMITER;
276 }
277
278 /* end-of-StringList.java */
279
Popular Tags