KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > valueuri > NewRefIdValueURIHandler


1 /**
2  * $Id: NewRefIdValueURIHandler.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;
30
31 import java.util.Map JavaDoc;
32
33 import org.apache.tools.ant.Project;
34
35 import com.idaremedia.antx.ExportedProperties;
36 import com.idaremedia.antx.FixtureExaminer;
37 import com.idaremedia.antx.apis.Requester;
38 import com.idaremedia.antx.helpers.SIDs;
39 import com.idaremedia.antx.helpers.Tk;
40 import com.idaremedia.antx.starters.ValueURIHandlerSkeleton;
41
42 /**
43  * Value URI handler that generates a new reference id that does not already exist in
44  * the named project. Very useful in tests and SAMS artifact definition methods that
45  * need to add data objects to the current project's fixture without worrying about name
46  * collisons. Be careful when using this value URI as part of an overlaid property's
47  * value&#8212; the value <em>will change everytime</em> the property is dereferenced!
48  * <p/>
49  * The general URI format: <span class="src">$newrefid:[prefix][?variable-name]</span>
50  * where <span class="src">prefix</span> is an optional prefix that is used in new
51  * reference name (useful for debugging), and <span class="src">variable-name</span> is
52  * the name of the mutable variable that should be updated with new id.
53  * <p/>
54  * <b>Example Usage:</b><pre>
55  * &lt;assign var="myjunit" value="${<b>$newrefid:junit</b>}"/&gt;
56  * &lt;createtask name="${$var:myjunit}"...&gt;
57
58  * -or better-
59  * &lt;createtask name="${<b>$newrefid:junit?myjunit</b>}"&gt;
60  * &lt;junit.../&gt;
61  * &lt;/createtask&gt;
62  *
63  * -- To Install --
64  * &lt;manageuris action="install"&gt;
65  * &lt;parameter name="newrefid"
66  * value="com.idaremedia.antx.valueuri.NewRefIdValueURIHandler"/&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  * @see SIDsValueURIHandler
76  **/

77
78 public final class NewRefIdValueURIHandler extends ValueURIHandlerSkeleton
79 {
80     private static final String JavaDoc DEFAULT_PREFIX= "refid";
81
82     /**
83      * Initalizes a new (unique) refid value generator.
84      **/

85     public NewRefIdValueURIHandler()
86     {
87         super();
88     }
89
90
91     /**
92      * Returns a project-unique reference identifier. If the name of
93      * a variable is not provided, the reference object should be
94      * inserted ASAP to minimize the collison potential.
95      **/

96     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
97     {
98         final Project project = clnt.getProject();
99         String JavaDoc variable = null;
100
101         String JavaDoc prefix = DEFAULT_PREFIX;
102         int i = uriFragment.indexOf("?");
103         if (i>=0) {
104             prefix = uriFragment.substring(0,i++);
105             if (prefix.length()==0) {
106                 prefix = DEFAULT_PREFIX;
107             } else {
108                 prefix = Tk.resolveString(project,prefix,true);
109             }
110             if (i<uriFragment.length()) {
111                 variable = Tk.resolveString(project,uriFragment.substring(i),true);
112             }
113         } else if (uriFragment.length()>0) {
114             prefix = Tk.resolveString(project,uriFragment,true);
115         }
116
117         final Map JavaDoc refs = clnt.getProject().getReferences();
118         SIDs.Finder refidFinder = new SIDs.Finder() {
119                 public boolean exists(String JavaDoc sid) {
120                     return refs.containsKey(sid);
121                 }
122             };
123         String JavaDoc refid;
124         synchronized(refs) {
125             refid = SIDs.next(refidFinder,prefix);
126             if (!Tk.isWhitespace(variable)) {
127                 refs.put(refid,FixtureExaminer.PENDING_REFERENCE);
128                 ExportedProperties.set(variable,refid);
129             }
130         }
131         return refid;
132     }
133 }
134
135 /* end-of-NewRefIdValueURIHandler.java */
Popular Tags