KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: MapValueURIHandler.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.Properties JavaDoc;
32
33 import org.apache.tools.ant.Project;
34
35 import com.idaremedia.antx.AntX;
36 import com.idaremedia.antx.FixtureExaminer;
37 import com.idaremedia.antx.apis.Requester;
38
39 /**
40  * Value URI handler that stringifies various aspects of a map data object. Is meant
41  * as the map equivalent of the AntX <span class="src">$list:</span> value URI handler.
42  * When generating a listing (keyset,values) you can specify a custom delimiter by
43  * setting a second fragment argument after a "<span class="src">,,</span>" like:
44  * <span class="src">$map:labels?keys,,|||"</span>. The general form of the URI:
45  * <span class="src"><nobr>$map:maprefid[?[keys|values|size|dump|<i>key</i>][,,delim]]</nobr></span>.
46  * The delim option is only relevant to the set operations.
47  * <p/>
48  * <b>Example Usage:</b><pre>
49  * &lt;foreach i="key" list="${<b>$map:</b>@{testproperties}?keys}" mode="local"&gt;
50  * &lt;altertask name="junitrunner" resolveproperties="yes"&gt;
51  * &lt;sysproperty key="${key}" value="${<b>$map:</b>@{testproperties}?@(key)}"/&gt;
52  * &lt;/altertask&gt;
53  * &lt;/foreach&gt;
54  *
55  * &lt;foreach i="tool" list="${<b>$map:</b>@{toolnames}?values}" mode="local"&gt;
56  * &lt;mkdir dir="${reports.dir}/${tool}/${DSTAMP}"/&gt;
57  *
58  * &lt;echo message="Javadoc Configuration: ${<b>$map:</b>javadoc.args}"/&gt;
59  * &lt;echo message="Metrics Tool Count: ${<b>$map:</b>toolnames?size}"/&gt;
60  *
61  * -- To Install --
62  * &lt;manageuris action="install"&gt;
63  * &lt;parameter name="map"
64  * value="com.idaremedia.antx.valueuri.info.MapValueURIHandler"/&gt;
65  * &lt;parameter name="itemmap"
66  * value="com.idaremedia.antx.valueuri.info.MapValueURIHandler"/&gt;
67  * &lt;/manageuris&gt;
68  * </pre>
69  *
70  * @since JWare/AntX 0.5
71  * @author ssmc, &copy;2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
72  * @version 0.5
73  * @.safety multiple
74  * @.group api,helper
75  **/

76
77 public final class MapValueURIHandler extends ManageCollectionValueURIHandler
78 {
79     /**
80      * Initializes a new map examiner value uri handler.
81      **/

82     public MapValueURIHandler()
83     {
84         super();
85     }
86
87
88     /**
89      * Extracts the bit of map information requested. Currently supports
90      * four special operations: dump (also default if no fragment), keyset,
91      * values, and size.
92      */

93     String JavaDoc valueFromTyped(String JavaDoc refid, int op, String JavaDoc delim, String JavaDoc key,
94         Requester clnt)
95     {
96         final Project P = clnt.getProject();
97
98         if (delim==null) {
99             delim = AntX.DEFAULT_DELIMITER;
100         }
101
102         if (FixtureExaminer.usableReference(P,refid)) {
103             Properties JavaDoc all = FixtureExaminer.getReferencedProperties(P,refid,null);
104             if (all!=null) {
105                 switch(op) {
106                     case SIZE: {
107                         return String.valueOf(all.size());
108                     }
109                     case KEYSET: {
110                         return listFrom(all.keySet().iterator(),delim);
111                     }
112                     case VALUES: {
113                         return listFrom(all.values().iterator(),delim);
114                     }
115                     case LOOKUP: {
116                         return all.getProperty(key);
117                     }
118                     case DUMP: {
119                         return String.valueOf(all);
120                     }
121                 }
122             }//all!=null
123
}//refid-usable
124

125         return null;
126     }
127 }
128
129 /* end-of-MapValueURIHandler.java */
Popular Tags