KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > ownhelpers > UEContainerProxy


1 /**
2  * $Id: UEContainerProxy.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.ownhelpers;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36 import org.apache.tools.ant.RuntimeConfigurable;
37 import org.apache.tools.ant.Task;
38 import org.apache.tools.ant.TaskContainer;
39 import org.apache.tools.ant.UnknownElement;
40
41 import com.idaremedia.antx.AntX;
42 import com.idaremedia.antx.helpers.Tk;
43
44 /**
45  * Helper when constructing UnknownElements(UEs) that contain other UEs.
46  *
47  * @since JWare/AntX 0.4
48  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
49  * @version 0.5
50  * @.safety single
51  * @.group impl,helper
52  * @see TaskExaminer#newUEProxy TaskExaminer.newUEProxy(&#8230;)
53  **/

54
55 public final class UEContainerProxy implements TaskContainer
56 {
57     /**
58      * The element that must be "filled-in" with other UE items.
59      **/

60     private final UnknownElement m_rootItem;
61
62
63
64     /**
65      * Initialize a new container proxy for an unknown element.
66      * @param impl the unknown element (acts like container)
67      **/

68     public UEContainerProxy(UnknownElement impl)
69     {
70         AntX.require_(impl!=null,AntX.nopackage,"ctor- nonzro UE");
71         m_rootItem= impl;
72     }
73
74
75
76
77     /**
78      * Ensures the incoming UE and its wrapper are added to our
79      * underlying UE properly.
80      * @param task the unknown element task proxy (non-null)
81      **/

82     public void addTask(Task task)
83     {
84         AntX.require_(task instanceof UnknownElement,
85                       AntX.nopackage, "addWrappedTask- UE");
86         UnknownElement ue = (UnknownElement)task;
87         m_rootItem.addChild(ue);
88         m_rootItem.getWrapper().addChild(ue.getWrapper());
89     }
90
91
92
93     /**
94      * Sets an attribute on the underlying UE element. Useful if
95      * a macro maker or custom macro definition needs to tweak
96      * the item's attributes as it is itself defined.
97      * @param name attribute's name (non-null)
98      * @param value attribute's value (non-null)
99      **/

100     public void setAttribute(String JavaDoc name, String JavaDoc value)
101     {
102         RuntimeConfigurable wrapper = m_rootItem.getWrapper();
103         wrapper.setAttribute(name,value);
104     }
105
106
107
108     /**
109      * Sets a collection of attributes on the underlying UE
110      * element. Shorthand for calling {@linkplain #setAttribute
111      * setAttribute()} repeatedly. The passed map must contain
112      * string-based name/value pairs.
113      * @param attrs map of attribute name/value pairs (non-null)
114      * @since JWare/AntX 0.5
115      **/

116     public void setAttributes(Map JavaDoc attrs)
117     {
118         RuntimeConfigurable wrapper = m_rootItem.getWrapper();
119         Iterator JavaDoc itr = attrs.entrySet().iterator();
120         while (itr.hasNext()) {
121             Map.Entry JavaDoc e = (Map.Entry JavaDoc)itr.next();
122             wrapper.setAttribute((String JavaDoc)e.getKey(),(String JavaDoc)e.getValue());
123         }
124     }
125
126
127
128     /**
129      * Like {@linkplain #addTask addTask()} except the new element
130      * is added to a nested child element. Incoming is therefore a
131      * grandchild to root element.
132      * @param task the unknown element grandchild proxy (non-null)
133      * @param parentname local type name of parent (non-null)
134      * @since JWare/AntX 0.5
135      **/

136     public void addGrandchildTask(Task task, String JavaDoc parentname)
137     {
138         AntX.require_(task instanceof UnknownElement,
139                       AntX.nopackage, "addWrappedTask- UE");
140         UnknownElement target = getBestTarget(parentname);
141         UnknownElement ue = (UnknownElement)task;
142         target.addChild(ue);
143         target.getWrapper().addChild(ue.getWrapper());
144     }
145
146
147
148     /**
149      * Like {@linkplain #setAttribute setAttribute()} except the new
150      * attribute is set on a nested child element.
151      * @param name attribute's name (non-null)
152      * @param value attribute's value (non-null)
153      * @param parentname local type name of parent (non-null)
154      * @since JWare/AntX 0.5
155      **/

156     public void setGrandchildAttribute(String JavaDoc name, String JavaDoc value,
157         String JavaDoc parentname)
158     {
159         UnknownElement target = getBestTarget(parentname);
160         RuntimeConfigurable wrapper = target.getWrapper();
161         wrapper.setAttribute(name,value);
162     }
163
164
165
166     /**
167      * Like {@linkplain #setAttributes setAttributes()} except the new
168      * attributes are set on a nested child element.
169      * @param attrs map of attribute name/value pairs (non-null)
170      * @param parentname local type name of parent (non-null)
171      * @since JWare/AntX 0.5
172      **/

173     public void setGrandchildAttributes(Map JavaDoc attrs, String JavaDoc parentname)
174     {
175         RuntimeConfigurable wrapper = getBestTarget(parentname).getWrapper();
176         Iterator JavaDoc itr = attrs.entrySet().iterator();
177         while (itr.hasNext()) {
178             Map.Entry JavaDoc e = (Map.Entry JavaDoc)itr.next();
179             wrapper.setAttribute((String JavaDoc)e.getKey(),(String JavaDoc)e.getValue());
180         }
181     }
182
183
184
185     /**
186      * Returns the main underlying unknown element. Never
187      * returns <i>null</i>.
188      * @since JWare/AntX 0.5
189      * @see TaskExaminer#copyUEProxy TaskExaminer.copyUEProxy(&#8230;)
190      **/

191     public UnknownElement getUE()
192     {
193         return m_rootItem;
194     }
195
196
197     private UnknownElement getBestTarget(String JavaDoc itempath)
198     {
199         UnknownElement match = m_rootItem;
200         if (!Tk.isWhitespace(itempath)) {
201             StringTokenizer JavaDoc st= new StringTokenizer JavaDoc(itempath,",");
202             while (st.hasMoreTokens()) {
203                 String JavaDoc nextname = st.nextToken();
204                 UnknownElement next = childFor(nextname,match);
205                 if (next==null) {
206                     return m_rootItem;
207                 }
208                 match = next;
209             }
210         }
211         return match;
212     }
213
214
215
216     private UnknownElement childFor(String JavaDoc childname, UnknownElement parent)
217     {
218         UnknownElement child = null;
219         if (!Tk.isWhitespace(childname)) {
220             List JavaDoc l = parent.getChildren();
221             if (l!=null && !l.isEmpty()) {
222                 for (int i=0,N=l.size();i<N;i++) {
223                     UnknownElement next = (UnknownElement)l.get(i);
224                     if (childname.equals(next.getTaskName())) {
225                         child = next;
226                         break;
227                     }
228                 }
229             }
230         }
231         return child;
232     }
233 }
234
235 /* end-of-UEContainerProxy.java */
236
Popular Tags