KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > valueuri > info > ListFriendlyValueURIHandler


1 /**
2  * $Id: ListFriendlyValueURIHandler.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004-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.valueuri.info;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.apache.tools.ant.Project;
35
36 import com.idaremedia.antx.AntX;
37 import com.idaremedia.antx.FixtureExaminer;
38 import com.idaremedia.antx.apis.Requester;
39 import com.idaremedia.antx.helpers.Tk;
40 import com.idaremedia.antx.starters.ListFriendly;
41
42 /**
43  * Value URI handler that lets you extract information from a list friendly data
44  * object. This handler is not installed automatically by the AntX runtime, you must
45  * install it explicitly like the example below. List value URIs are often linked to the
46  * <span class="src">$list:</span> scheme. The general form of the URI:
47  * <span class="src"><nobr>$list:listrefid|comma-delimited-list[?[values|size|dump|<i>index</i>][,,delim]]</nobr></span>.
48  * The delim option is only relevant to the values operation.
49  * <p/>
50  * This handler assumes that the named item is a reference to a list-friendly
51  * data object unless the name is surrounded by square brackets or no such reference
52  * exists. If not a reference, this handler treats the name as an inlined comma-
53  * delimited list(as would be the case for a list stored in a property value).
54  * <p/>
55  * You can use this handler to extract a particular string from the targetted
56  * list. Set the URI's query string (the bit after the "&#63;") to the item's
57  * 0-based index; for example: <span class="src">$list:javadoc.servers?3</span>.
58  * You can also extract the size of a list by setting the query string to
59  * "<span class="src">size</span>" like: <span class="src">$list:logs?size</span>.
60  * <p>
61  * <b>Example Usage:</b><pre>
62  * &lt;urls id="javadoc.servers" prefix="http://" suffix="/apis/packagelist"&gt;
63  * &lt;url value="antxtras.org/thirdparty/junit"/&gt;
64  * &lt;url value="antxtras.org/stable/antunit/docs"/&gt;
65  * ...
66  * &lt;/urls&gt;
67  * ...
68  * &lt;echo message="There are ${<b>$list:</b>javadoc.servers?size}" servers"/&gt;
69  * &lt;foreach i="server_" list="${<b>$list:</b>javadoc.servers}".../&gt;
70  *
71  * &lt;macrodef name="run-programmertests"&gt;
72  * &lt;attribute name="jvm.args" default=""/&gt;
73  * ...
74  * &lt;do true="${$isdefined:@{jvm.args}}"&gt;
75  * &lt;foreach i="arg" in="0,${<b>$list:</b>@{jvm.args}?size}" mode="local"&gt;
76  * &lt;altertask name="myjunit" resolveproperties="yes"&gt;
77  * &lt;jvmarg value="${<b>$list:</b>@{jvm.args}?@(arg)}"/&gt;
78  * &lt;/altertask&gt;
79  * &lt;/foreach&gt;
80  * &lt;/do&gt;
81  *
82  * -- To Install --
83  * &lt;manageuris action="install"&gt;
84  * &lt;parameter name="list"
85  * value="com.idaremedia.antx.valueuri.info.ListFriendlyValueURIHandler"/&gt;
86  * &lt;/manageuris&gt;
87  * </pre>
88  *
89  * @since JWare/AntX 0.5
90  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
91  * @version 0.5
92  * @.safety multiple
93  * @.group api,helper
94  **/

95
96 public final class ListFriendlyValueURIHandler extends ManageCollectionValueURIHandler
97 {
98     /**
99      * Initializes a new list examiner URI handler.
100      **/

101     public ListFriendlyValueURIHandler()
102     {
103         super();
104     }
105
106
107     /**
108      * Returns "VALUES" always.
109      */

110     int getDefaultOp()
111     {
112         return VALUES;
113     }
114
115
116     /**
117      * Extracts the bit of list information requested. Currently supports
118      * three special operations: dump, values (also default if no fragment),
119      * and size. Any other query string is considered the lookup index.
120      **/

121     String JavaDoc valueFromTyped(String JavaDoc refid, int op, String JavaDoc delim, String JavaDoc key,
122         Requester clnt)
123     {
124         final Project P = clnt.getProject();
125
126         Object JavaDoc o;
127         if (refid.startsWith("[") && refid.endsWith("]")) {
128             refid = refid.substring(1,refid.length()-1);
129             o = null;
130         } else {
131             o = FixtureExaminer.trueReference(P,refid);
132         }
133         if (o instanceof ListFriendly) {
134             ListFriendly l = (ListFriendly)o;
135             switch(op) {
136                 case SIZE: {
137                     return String.valueOf(l.size());
138                 }
139                 case VALUES:
140                 case KEYSET: {
141                     if (delim!=null) {
142                         return listFrom(l.readonlyStringIterator(P),delim);
143                     }
144                     return l.stringFrom(P);
145                 }
146                 case LOOKUP: {
147                     int i = Tk.integerFrom(key,-1);
148                     if (i>=0) {
149                         Iterator JavaDoc itr= l.readonlyStringIterator(P);
150                          int n=0;
151                          while (itr.hasNext()) {
152                              String JavaDoc s = itr.next().toString();
153                              if (n==i) {
154                                  return s;
155                              }
156                              n++;
157                          }
158                     }
159                     break;
160                 }
161                 case DUMP: {
162                     return String.valueOf(o);
163                 }
164             }
165         } else if (o==null || (o instanceof List JavaDoc)) {
166             List JavaDoc l = o==null ? Tk.splitList(refid.trim()) : (List JavaDoc)o;
167             switch(op) {
168                 case SIZE: {
169                     return String.valueOf(l.size());
170                 }
171                 case LOOKUP: {
172                     int i = Tk.integerFrom(key,-1);
173                     if (i>=0 && i<l.size()) {
174                         return (String JavaDoc)l.get(i);
175                     }
176                     break;
177                 }
178                 case VALUES:
179                 case KEYSET: {
180                     if (delim==null) {
181                         delim = AntX.DEFAULT_DELIMITER;
182                     }
183                     return listFrom(l.iterator(),delim);
184                 }
185                 default: {
186                     return String.valueOf(l);
187                 }
188             }
189         }
190         return null;
191     }
192 }
193
194 /* end-of-ListFriendlyValueURIHandler.java */
Popular Tags