KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: ManageCollectionValueURIHandler.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.valueuri.info;
30
31 import java.util.Iterator JavaDoc;
32
33 import org.apache.tools.ant.Project;
34
35 import com.idaremedia.antx.apis.Requester;
36 import com.idaremedia.antx.helpers.Strings;
37 import com.idaremedia.antx.helpers.Tk;
38 import com.idaremedia.antx.starters.ValueURIHandlerSkeleton;
39
40 /**
41  * Starter class for handlers that manipulate common collection types like lists and
42  * maps. Provides support for a common set of operation selectors and uri fragment
43  * parsing.
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 multiple
49  * @.group api,helper
50  **/

51
52 abstract class ManageCollectionValueURIHandler extends ValueURIHandlerSkeleton
53 {
54     static final int UNKNOWN=-1;
55     static final int SIZE=UNKNOWN+1;
56     static final int KEYSET=SIZE+1;
57     static final int VALUES=KEYSET+1;
58     static final int LOOKUP=VALUES+1;
59     static final int DUMP=LOOKUP+1;
60
61
62     /**
63      * Initializes a new collection examiner value uri handler.
64      **/

65     protected ManageCollectionValueURIHandler()
66     {
67         super();
68     }
69
70
71     /**
72      * Returns the default operation if one is not set explicitly.
73      * Will use "DUMP" by default.
74      **/

75     int getDefaultOp()
76     {
77         return DUMP;
78     }
79
80
81
82     /**
83      * Extracts the bit of collection information requested. Currently supports
84      * four special operations: dump (also default if no fragment), keyset,
85      * values, and size.
86      */

87     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
88     {
89         final Project P = clnt.getProject();
90             
91         String JavaDoc refid = uriFragment;
92         int op = getDefaultOp();
93         String JavaDoc key = null;
94         String JavaDoc delim = null;
95
96         int i = uriFragment.lastIndexOf("?");
97         if (i>=0) {
98             refid = uriFragment.substring(0,i++);
99             if (i<uriFragment.length()) {
100                 String JavaDoc instr = Tk.resolveString(P,uriFragment.substring(i),true);
101                 int j = instr.lastIndexOf(",,");
102                 if (j>=0) {
103                     delim = instr.substring(j+2);
104                     instr = instr.substring(0,j);
105                     if ("\\n".equals(delim)) {
106                         delim= Strings.NL;
107                     }
108                 }
109                 op = opFrom(instr);
110                 if (op==LOOKUP) {
111                     key = instr;
112                 }
113             }
114         }
115         refid = Tk.resolveString(P,refid,true);
116         return valueFromTyped(refid,op,delim,key,clnt);
117     }
118
119
120
121     /**
122      * Does query work that is specific to the particular collection or map that
123      * is being handled.
124      * @param refid extracted (normalized) reference id (or inline collection)
125      * @param op the determined operation from original URI fragment
126      * @param delim the client-supplied delimiter or <i>null</i> if none in URI
127      * @param key the client-supplied "key" or <i>null</i> if none in URI
128      * @param clnt call controls (non-null
129      * @return the handler's results or <i>null</i> if cannot determine.
130      **/

131     abstract String JavaDoc valueFromTyped(String JavaDoc refid, int op, String JavaDoc delim, String JavaDoc key,
132         Requester clnt);
133
134
135
136     /**
137      * Determine the requested collection operation for the handler.
138      * @param opstring incoming operation or key value (non-null)
139      * @return operation id
140      **/

141     int opFrom(String JavaDoc opstring)
142     {
143         if ("size".equals(opstring)) {
144             return SIZE;
145         }
146         if ("keys".equals(opstring)) {
147             return KEYSET;
148         }
149         if ("values".equals(opstring)) {
150             return VALUES;
151         }
152         if ("dump".equals(opstring)) {
153             return DUMP;
154         }
155         return opstring.length()>0 ? LOOKUP : getDefaultOp();
156     }
157
158
159
160     /**
161      * Generates a listing string of the items returned by
162      * the given iterator. The listing elements are delimited
163      * by the incoming marker string.
164      * @param itr iterator that returns listing elements (non-null)
165      * @param delim delimiter used between elements (non-null)
166      * @return listing string (never <i>null</i>)
167      **/

168     String JavaDoc listFrom(Iterator JavaDoc itr, String JavaDoc delim)
169     {
170         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
171         int i=0;
172         while (itr.hasNext()) {
173             if (i>0) {
174                 sb.append(delim);
175             }
176             sb.append(itr.next());
177             i++;
178         }
179         return sb.toString();
180     }
181 }
182
183 /* end-of-ManageCollectionValueURIHandler.java */
Popular Tags