KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: UrlList.java 186 2007-03-16 13:42:35Z 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.solo;
30
31 import java.io.File JavaDoc;
32 import java.net.MalformedURLException JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 import org.apache.tools.ant.BuildException;
39 import org.apache.tools.ant.Project;
40
41 import com.idaremedia.antx.AntX;
42 import com.idaremedia.antx.apis.AntLibFriendly;
43 import com.idaremedia.antx.helpers.InnerString;
44 import com.idaremedia.antx.helpers.Tk;
45 import com.idaremedia.antx.starters.ListFriendly;
46 import com.idaremedia.antx.starters.StringItemList;
47
48 /**
49  * A list of URLs as a structured Ant type. Useful to verify URLs as they are declared
50  * instead of when they're used for the first time. A URL list's iterator returns URL
51  * objects not URL strings.
52  * <p>
53  * <b>Examples:</b><pre>
54  * &lt;<b>urls</b> id="default.webservers" prefix="http://www." suffix=".info/"&gt;
55  * &lt;url value="idaremedia"/&gt;
56  * &lt;url value="jware"/&gt;
57  * &lt;url value="antxtras"/&gt;
58  * &lt;/urls&gt;
59  *
60  * -OR- (for use in AntX looping construct)
61  *
62  * &lt;<b>urls</b> id="test.urls" delim="||;||"&gt;
63  * &lt;url value="http://localhost:9090/builds/"/&gt;
64  * &lt;url value="http://dev.mycompany.com/builds/"/&gt;
65  * &lt;/urls&gt;
66  * &#46;&#46;&#46;
67  *
68  * <i>-WHERE-</i>
69  * &lt;callforeach items="test.urls" &#46;&#46;&#46;/&gt;
70  *
71  * <i>-IS-SAME-AS-</i>
72  * &lt;copyproperty name="testurls" reference="test.urls"/&gt;
73  * &lt;callforeach list="${testurls}" delim="||;||" &#46;&#46;&#46;/&gt;
74  * </pre>
75  *
76  * @since JWare/AntX 0.3
77  * @author ssmc, &copy;2003-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
78  * @version 0.5
79  * @.safety multiple (after fully configured)
80  * @.group api,helper
81  * @.caveat URL lists cannot handle AntX property references because they
82  * are verified as they are built.
83  **/

84
85 public final class UrlList extends StringItemList
86     implements ListFriendly, AntLibFriendly
87 {
88     /** Default delimiter used by all URL lists. **/
89     public static final String JavaDoc DEFAULT_DELIMITER = ";";
90
91
92     /**
93      * Initializes a new UrlList instance.
94      **/

95     public UrlList()
96     {
97         super(AntX.fixture+"UrlList:");
98     }
99
100
101     /**
102      * Sets a custom delimiter for this URL list. This
103      * delimiter will be used by all stringification(TM) methods.
104      * @param delimiter new delimiter (non-null)
105      * @throws BuildException if this list is a reference
106      **/

107     public void setDelim(String JavaDoc delimiter)
108     {
109         require_(delimiter!=null,"setDelim- nonzro delim");
110         if (isReference()) {
111             throw tooManyAttributes();
112         }
113         m_delim = delimiter;
114         edited();
115     }
116
117
118     /**
119      * Returns the delimiter this URL list will use for
120      * any stringification(TM) of its contents. Defaults
121      * to the semi-colon. Never returns <i>null</i>.
122      **/

123     public String JavaDoc getDelim()
124     {
125         if (isReference()) {
126             return getOtherList().getDelim();
127         }
128         return m_delim;
129     }
130
131
132     /**
133      * Sets a prefix that will be prepended to all URLs
134      * strings processed by this list. Usually something like:
135      * "<code>http://www.</code>".
136      * @param prefix the prefix (non-null)
137      * @throws BuildException if this item is a reference
138      **/

139     public void setPrefix(String JavaDoc prefix)
140     {
141         setPrefixString(prefix);
142     }
143
144
145     /**
146      * Returns this URL list's prefix or the empty
147      * string if never set. Never returns <i>null</i>.
148      **/

149     public String JavaDoc getPrefix()
150     {
151         if (isReference()) {
152             return getOtherList().getPrefix();
153         }
154         return getPrefixString();
155     }
156
157
158     /**
159      * Set a suffix that will be appended to all URLs
160      * strings processed by this list. Usually something like:
161      * "<code>.com/index.html</code>".
162      * @param suffix the suffix (non-null)
163      * @throws BuildException if this item is a reference
164      **/

165     public void setSuffix(String JavaDoc suffix)
166     {
167         setSuffixString(suffix);
168     }
169
170
171     /**
172      * Returns this string list's suffix or the empty
173      * string if never set. Never returns <i>null</i>.
174      **/

175     public String JavaDoc getSuffix()
176     {
177         if (isReference()) {
178             return getOtherList().getSuffix();
179         }
180         return getSuffixString();
181     }
182
183
184     /**
185      * Appends the contents of a newline-delimited file to
186      * this string list. No-op if file is missing or empty.
187      * @param f the file (non-null)
188      * @since JWare/AntX 0.4
189      **/

190     public void setFile(File JavaDoc f)
191     {
192         require_(f!=null,"setfil- nonzro fil");
193         addFileOrURL(f.getPath());
194     }
195
196
197     /**
198      * Appends the contents of a newline-delimited resource
199      * file to this string list. No-op if file is missing or
200      * empty.
201      * @param rn the resource path (non-null)
202      * @since JWare/AntX 0.4
203      **/

204     public void setResource(String JavaDoc rn)
205     {
206         require_(rn!=null,"setRez- nonzro reznam");
207         addResource(rn);
208     }
209
210
211     /**
212      * Appends given URL string (with property substitution)
213      * to this URL list. URL string is immediately converted
214      * to a URL (so errors will fail quick during parse).
215      * @param urlstring new URL string
216      * @throws BuildException if invalid URL string or this list
217      * is a reference
218      **/

219     public void addConfiguredURL(InnerString urlstring)
220     {
221         require_(urlstring!=null,"addURL- nonzro urlstr");
222         addItem(urlstring,getProject());
223         addedURLstring();
224     }
225
226
227     /**
228      * Returns a delimited list of this URL list's contents.
229      * Stringifies the original declared strings not the URL
230      * external forms.
231      * @see #setDelim
232      **/

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

259     public String JavaDoc toString()
260     {
261         return stringFrom(getProject());
262     }
263
264
265     /**
266      * Filter <i>null</i> strings and any string that
267      * resolves as empty or all whitespace.
268      * @param string candidate string
269      **/

270     protected boolean includeItem(String JavaDoc string)
271     {
272         return !Tk.isWhitespace(string);
273     }
274
275
276     /**
277      * Returns a copy of this URL list's underlying URLs. Each
278      * URL is copied since we don't know how URLs work internally
279      * so it's not safe to assume they can be shared. Caller
280      * assumes ownership of returned array and contents. Never
281      * returns <i>null</i>.
282      **/

283     protected List JavaDoc copyOfItems(Project p)
284     {
285         ArrayList JavaDoc l = (ArrayList JavaDoc)m_URLs.clone();
286         if (!l.isEmpty()) {
287             try {
288                 for (int i=0,N=l.size();i<N;i++) {
289                     l.set(i, new URL JavaDoc(l.get(i).toString()));
290                 }
291             } catch(MalformedURLException JavaDoc mfx) {
292                 throw new BuildException(mfx);
293             }
294         }
295         return l;
296     }
297
298
299     /**
300      * Ensures clone object has its own copy of any matching
301      * URLs.
302      * @param cloned the newly cloned object (non-null)
303      * @see #copyOfItems
304      **/

305     protected void cloneInternals(StringItemList cloned)
306     {
307         super.cloneInternals(cloned);
308         ((UrlList)cloned).m_URLs = (ArrayList JavaDoc)copyOfItems(null);
309     }
310
311
312     /**
313      * Converts a URL string to a URL object triggering protocol
314      * handler verification by JVM.
315      * @return URL based on string
316      * @throws BuildException if unable to parse url string
317      **/

318     private URL JavaDoc convertURL(String JavaDoc urlstr)
319     {
320         try {
321             if (shouldModifyStrings()) {
322                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(urlstr.length()+20);
323                 sb.append(getPrefixString());
324                 sb.append(urlstr);
325                 sb.append(getSuffixString());
326                 urlstr = sb.substring(0);
327             }
328             return new URL JavaDoc(urlstr);
329         } catch (MalformedURLException JavaDoc mfx) {
330             String JavaDoc error = uistrs().get("solo.urls.malformed",getId(),urlstr);
331             log(error,Project.MSG_ERR);
332             throw new BuildException(error);
333         }
334     }
335
336
337     /**
338      * Converts the last URL string to a live URL object.
339      **/

340     private void addedURLstring()
341     {
342         List JavaDoc sl= rawStringsList();
343         String JavaDoc urlstr = (String JavaDoc)sl.get(sl.size()-1);
344         m_URLs.add(convertURL(urlstr));
345         ensure_(sl.size()==m_URLs.size(),"addedURL- synced lists");
346     }
347
348
349     private final UrlList getOtherList()
350     {
351         return (UrlList)getOtherItemList(UrlList.class);
352     }
353
354
355     private String JavaDoc m_delim = DEFAULT_DELIMITER;
356     private ArrayList JavaDoc m_URLs = new ArrayList JavaDoc(5);
357 }
358
359 /* end-of-UrlList.java */
360
Popular Tags