KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > domain > AbstractObjectHandler


1 package org.sapia.magnet.domain;
2
3 // Import of Sun's JDK classes
4
// ---------------------------
5
import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12
13 // Import of Sapia's utility classes
14
// ---------------------------------
15
import org.sapia.util.xml.confix.ObjectHandlerIF;
16
17 // Import of Sapia's Corus classes
18
// --------------------------------
19
import org.sapia.magnet.render.AbstractRenderable;
20 import org.sapia.magnet.render.MagnetContext;
21 import org.sapia.magnet.render.RenderableIF;
22 import org.sapia.magnet.render.RenderingException;
23
24
25 /**
26  *
27  *
28  * @author Jean-Cedric Desrochers
29  *
30  * <dl>
31  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
32  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
33  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
34  * </dl>
35  */

36 public abstract class AbstractObjectHandler extends AbstractRenderable implements ObjectHandlerIF {
37
38   /////////////////////////////////////////////////////////////////////////////////////////
39
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
40
/////////////////////////////////////////////////////////////////////////////////////////
41

42   /** Defines an empty immutable list. */
43   private static final List JavaDoc EMPTY_LIST = Collections.unmodifiableList(new ArrayList JavaDoc(0));
44
45   /////////////////////////////////////////////////////////////////////////////////////////
46
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
47
/////////////////////////////////////////////////////////////////////////////////////////
48

49   /** The list of script handler definitions. */
50   private List JavaDoc _theScriptHandlerDefs;
51
52   /** The list of protocol handler definitions. */
53   private List JavaDoc _theProtocolHandlerDefs;
54
55   /** The list of launch handler definitions. */
56   private List JavaDoc _theLaunchHandlerDefs;
57
58   /** The map of objects identified by the element name. */
59   private Map JavaDoc _theObjects;
60
61   /////////////////////////////////////////////////////////////////////////////////////////
62
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
63
/////////////////////////////////////////////////////////////////////////////////////////
64

65   /**
66    * Creates a new AbstractObjectHandler instance.
67    */

68   protected AbstractObjectHandler() {
69     _theLaunchHandlerDefs = new ArrayList JavaDoc();
70     _theProtocolHandlerDefs = new ArrayList JavaDoc();
71     _theScriptHandlerDefs = new ArrayList JavaDoc();
72   }
73
74   /////////////////////////////////////////////////////////////////////////////////////////
75
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
76
/////////////////////////////////////////////////////////////////////////////////////////
77

78   /**
79    * Returns the list of launch handler definition objects.
80    *
81    * @return The list of <CODE>LaunchHandlerDef</CODE> objects.
82    */

83   public Collection JavaDoc getLaunchHandlerDefs() {
84     return _theLaunchHandlerDefs;
85   }
86
87   /**
88    * Returns the list of protocol handler definition objects.
89    *
90    * @return The list of <CODE>ProtocolHandlerDef</CODE> objects.
91    */

92   public Collection JavaDoc getProtocolHandlerDefs() {
93     return _theProtocolHandlerDefs;
94   }
95
96   /**
97    * Returns the list of script handler definition objects.
98    *
99    * @return The list of <CODE>ScriptHandlerDef</CODE> objects.
100    */

101   public Collection JavaDoc getScriptHandlerDefs() {
102     return _theScriptHandlerDefs;
103   }
104
105   /**
106    * Returns a collection of objects associated to the element name passed in.
107    * If there's no mapping, it returns an empty collection.
108    *
109    * @param anElementName The element name for which to retrieve the objects.
110    * @return A collection of <CODE>Object</CODE> objects.
111    */

112   public Collection JavaDoc getObjectsFor(String JavaDoc anElementName) {
113     List JavaDoc someObjects = EMPTY_LIST;
114
115     if (_theObjects != null) {
116       someObjects = (List JavaDoc) _theObjects.get(anElementName.toLowerCase());
117
118       if (someObjects == null) {
119         someObjects = EMPTY_LIST;
120       }
121     }
122
123     return someObjects;
124   }
125
126   /**
127    * Returns the collection of element names that represents the keys
128    * of the object of this object handler.
129    *
130    * @return The collection of element name.
131    */

132   public Collection JavaDoc getElementNames() {
133     Collection JavaDoc someNames = EMPTY_LIST;
134
135     if (_theObjects != null) {
136       someNames = _theObjects.keySet();
137     }
138
139     return someNames;
140   }
141
142   /////////////////////////////////////////////////////////////////////////////////////////
143
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
144
/////////////////////////////////////////////////////////////////////////////////////////
145

146   /**
147    * Adds the launch handler definition to this abstract object handler.
148    *
149    * @param aHandlerDef The launch handler definition to add.
150    */

151   public void addLaunchHandlerDef(LaunchHandlerDef aHandlerDef) {
152     _theLaunchHandlerDefs.add(aHandlerDef);
153   }
154
155   /**
156    * Adds the protocol handler definition to this abstract object handler.
157    *
158    * @param aHandlerDef The protocol handler definition to add.
159    */

160   public void addProtocolHandlerDef(ProtocolHandlerDef aHandlerDef) {
161     _theProtocolHandlerDefs.add(aHandlerDef);
162   }
163
164   /**
165    * Adds the script handler definition to this abstract object handler.
166    *
167    * @param aHandlerDef The script handler definition to add.
168    */

169   public void addScriptHandlerDef(ScriptHandlerDef aHandlerDef) {
170     _theScriptHandlerDefs.add(aHandlerDef);
171   }
172
173   /////////////////////////////////////////////////////////////////////////////////////////
174
/////////////////////////////// INTERACE IMPLEMENTATION ///////////////////////////////
175
/////////////////////////////////////////////////////////////////////////////////////////
176

177   /**
178    * Render the handler definitions of this abstract object handler.
179    *
180    * @param aContext The magnet context to use.
181    * @exception RenderingException If an error occurs while rendering the handlers.
182    */

183   public void renderHandlerDefs(MagnetContext aContext) throws RenderingException {
184     // Render the launch handler definitions
185
for (Iterator JavaDoc it = _theLaunchHandlerDefs.iterator(); it.hasNext(); ) {
186       LaunchHandlerDef aHandlerDef = (LaunchHandlerDef) it.next();
187       aHandlerDef.render(aContext);
188     }
189
190     // Render the protocol handler definitions
191
for (Iterator JavaDoc it = _theProtocolHandlerDefs.iterator(); it.hasNext(); ) {
192       ProtocolHandlerDef aHandlerDef = (ProtocolHandlerDef) it.next();
193       aHandlerDef.render(aContext);
194     }
195
196     // Render the script handler definitions
197
for (Iterator JavaDoc it = _theScriptHandlerDefs.iterator(); it.hasNext(); ) {
198       ScriptHandlerDef aHandlerDef = (ScriptHandlerDef) it.next();
199       aHandlerDef.render(aContext);
200     }
201   }
202
203   /**
204    * Renders this objects to the magnet context passed in.
205    *
206    * @param aContext The magnet context to use.
207    * @exception RenderingException If an error occurs while rendering this object.
208    */

209   public void render(MagnetContext aContext) throws RenderingException {
210     for (Iterator JavaDoc it = getElementNames().iterator(); it.hasNext(); ) {
211       String JavaDoc anElementName = (String JavaDoc) it.next();
212
213       for (Iterator JavaDoc someObjects = getObjectsFor(anElementName).iterator(); someObjects.hasNext(); ) {
214         Object JavaDoc anObject = someObjects.next();
215
216         if (anObject instanceof RenderableIF) {
217           ((RenderableIF) anObject).render(aContext);
218         }
219       }
220     }
221   }
222
223   /**
224    * Handles the passed in object that was created for the element name passed in.
225    *
226    * @param anElementName The xml element name for which the object was created.
227    * @param anObject The object to handle.
228    */

229   public void handleObject(String JavaDoc anElementName, Object JavaDoc anObject) {
230     // Lazy creation of the map
231
if (_theObjects == null) {
232       _theObjects = new HashMap JavaDoc();
233     }
234
235     // Retrieve the list associated with the element name
236
List JavaDoc someObjects = (List JavaDoc) _theObjects.get(anElementName.toLowerCase());
237     if (someObjects == null) {
238       someObjects = new ArrayList JavaDoc();
239       _theObjects.put(anElementName.toLowerCase(), someObjects);
240     }
241
242     // Add the object to its list
243
someObjects.add(anObject);
244   }
245
246   /////////////////////////////////////////////////////////////////////////////////////////
247
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
248
/////////////////////////////////////////////////////////////////////////////////////////
249

250   /**
251    * Returns a string representation of this magnet.
252    *
253    * @return A string representation of this magnet.
254    */

255   public String JavaDoc toString() {
256     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(super.toString());
257     aBuffer.append("[launchHandlerDefs=").append(_theLaunchHandlerDefs).
258             append(" protocolHandlerDefs=").append(_theProtocolHandlerDefs).
259             append(" scriptHandlerDefs=").append(_theScriptHandlerDefs).
260             append(" objects=").append(_theObjects).
261             append("]");
262
263     return aBuffer.toString();
264   }
265 }
266
Popular Tags