Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/cerberus/component/descriptor/ComponentDefinition.java


1   /*
2    * ComponentDefinition.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.cerberus.component.descriptor;
11  
12  import java.util.ArrayList;
13  import java.util.HashMap;
14  import java.util.Collection;
15  import java.util.Iterator;
16  
17  /**
18   * <p>Describes the various properties of a component.</p>
19   *
20   * <p>The messages and attributes for a component are described by this class.</p>
21   *
22   * <p>A component may extend one or more definitions. The messages and attributes of the definition
23   * are inherited.</p>
24   *
25   * @author  Trevor Milne
26   *
27   */
28  
29  public class ComponentDefinition
30  {
31    /** The name of this component. */
32    protected String name;
33  
34    /** The description of this component. */
35    protected String description;
36  
37    /** The backing class for this component. */
38    protected String backingClass;
39  
40    /** The definitions this component extends. */
41    protected ArrayList parents;
42  
43    /** The attributes for this component. */
44    protected HashMap attributes;
45  
46    /** The messages for this component. */
47    protected HashMap messages;
48  
49    /** The components within this component. */
50    protected ArrayList components;
51  
52  
53    /* Constructors. */
54  
55  
56    /**
57     * Constructs a <code>ComponentDefinition</code> instance.
58     *
59     */
60  
61    public ComponentDefinition()
62    {
63      name = null;
64      description = null;
65  
66      backingClass = null;
67  
68      parents = new ArrayList();
69  
70      attributes = new HashMap();
71      messages = new HashMap();
72      components = new ArrayList();
73    }
74  
75    /**
76     * Constructs a <code>ComponentDefinition</code> instance as a copy of the one supplied.
77     *
78     * @param    definition          The <code>ComponentDefinition</code> to copy.
79     *
80     */
81  
82    public ComponentDefinition(ComponentDefinition definition)
83    {
84      name = definition.getName();
85      description = definition.getDescription();
86  
87      backingClass = definition.getBackingClass();
88  
89      parents = new ArrayList();
90  
91      attributes = new HashMap();
92      messages = new HashMap();
93      components = new ArrayList();
94  
95      // copy parents
96      Iterator parentIterator = definition.getParents().iterator();
97  
98      while (parentIterator.hasNext())
99      {
100       String parent = (String)parentIterator.next();
101       addParent(parent);
102     }
103 
104     // copy attributes
105     Iterator attributeIterator = definition.getAttributes().iterator();
106 
107     while (attributeIterator.hasNext())
108     {
109       ComponentAttribute copyAttribute = (ComponentAttribute)attributeIterator.next();
110       ComponentAttribute newAttribute = new ComponentAttribute(copyAttribute);
111 
112       addAttribute(newAttribute);
113     }
114 
115     // copy messages
116     Iterator messageIterator = definition.getMessages().iterator();
117 
118     while (messageIterator.hasNext())
119     {
120       ComponentMessage copyMessage = (ComponentMessage)messageIterator.next();
121       ComponentMessage newMessage = new ComponentMessage(copyMessage);
122 
123       addMessage(newMessage);
124     }
125 
126     // copy components
127     Iterator componentIterator = definition.getComponents().iterator();
128 
129     while (componentIterator.hasNext())
130     {
131       ComponentInstance copyComponent = (ComponentInstance)componentIterator.next();
132       ComponentInstance newComponent = new ComponentInstance(copyComponent);
133 
134       addComponent(newComponent);
135     }
136   }
137 
138 
139   /* Accessors. */
140 
141 
142   /* Name. */
143 
144   /**
145    * Sets the name of this component.
146    *
147    * @param    setName            The name of this component.
148    *
149    */
150 
151   public void setName(String setName)
152   {
153     name = setName;
154   }
155 
156   /**
157    * Returns the name of this component.
158    *
159    * @return                  The name of this component.
160    *
161    */
162 
163   public String getName()
164   {
165     return name;
166   }
167 
168   /* Description. */
169 
170   /**
171    * Sets the description of this component.
172    *
173    * @param    setDescription        The description of this component.
174    *
175    */
176 
177   public void setDescription(String setDescription)
178   {
179     description = setDescription;
180   }
181 
182   /**
183    * Returns the description of this component.
184    *
185    * @return                  The description of this component.
186    *
187    */
188 
189   public String getDescription()
190   {
191     return description;
192   }
193 
194   /* Class. */
195 
196   /**
197    * Sets the class of this component.
198    *
199    * @param    setClass          The component's class.
200    *
201    */
202 
203   public void setBackingClass(String setClass)
204   {
205     backingClass = setClass;
206   }
207 
208   /**
209    * Returns the class of this component.
210    *
211    * @return                  The class of this component.
212    *
213    */
214 
215   public String getBackingClass()
216   {
217     return backingClass;
218   }
219 
220   /* Parents. */
221 
222   /**
223    * Adds a parent definition that this component extends.
224    *
225    * @param    definition          The name of a component definition for this component to extend.
226    *
227    */
228 
229   public void addParent(String definition)
230   {
231     if (parents.contains(definition)) return;
232     parents.add(definition);
233   }
234 
235   /**
236    * Returns the definitions this component extends.
237    *
238    * @return                  A <code>Collection</code> of names (<code>Strings</code>) of
239    *                      definitions this component extends.
240    *
241    */
242 
243   public Collection getParents()
244   {
245     return parents;
246   }
247 
248   /* Attributes. */
249 
250   /**
251    * Adds an attribute to this component.
252    *
253    * @param    attribute          An attribute for this component.
254    *
255    */
256 
257   public void addAttribute(ComponentAttribute attribute)
258   {
259     attributes.put(attribute.getName(), attribute);
260   }
261 
262   /**
263    * Updates the specified attribute. This allows the attribute to be renamed.
264    *
265    * @param    name            The name of the attribute to update.
266    * @param    attribute          A new attribute for this component.
267    *
268    */
269 
270   public void updateAttribute(String name, ComponentAttribute attribute)
271   {
272     // remove old attribute
273     attributes.remove(name);
274 
275     // add new attribute
276     addAttribute(attribute);
277   }
278 
279   /**
280    * Returns the specified attribute.
281    *
282    * @param    name            The name of the attribute.
283    *
284    * @return                  The {@link ComponentAttribute} having the supplied name.
285    *
286    */
287 
288   public ComponentAttribute getAttribute(String name)
289   {
290     return (ComponentAttribute)attributes.get(name);
291   }
292 
293   /**
294    * Returns the attributes of this component.
295    *
296    * @return                  A <code>Collection</code> of {@link ComponentAttribute ComponentAttributes}.
297    *
298    */
299 
300   public Collection getAttributes()
301   {
302     return attributes.values();
303   }
304 
305   /* Messages. */
306 
307   /**
308    * Adds a message to this component.
309    *
310    * @param    message            A message for this component.
311    *
312    */
313 
314   public void addMessage(ComponentMessage message)
315   {
316     messages.put(message.getName(), message);
317   }
318 
319   /**
320    * Updates the specified message. This allows the message to be renamed.
321    *
322    * @param    name            The name of the message to update.
323    * @param    message            A new message for this component.
324    *
325    */
326 
327   public void updateMessage(String name, ComponentMessage message)
328   {
329     // remove old message
330     messages.remove(name);
331 
332     // add new message
333     addMessage(message);
334   }
335 
336   /**
337    * Returns the specified message.
338    *
339    * @param    name            The name of the message.
340    *
341    * @return                  The {@link ComponentMessage} having the supplied name.
342    *
343    */
344 
345   public ComponentMessage getMessage(String name)
346   {
347     return (ComponentMessage)messages.get(name);
348   }
349 
350   /**
351    * Returns the messages of this component.
352    *
353    * @return                  A <code>Collection</code> of {@link ComponentMessage ComponentMessages}.
354    *
355    */
356 
357   public Collection getMessages()
358   {
359     return messages.values();
360   }
361 
362   /* Components. */
363 
364   /**
365    * Adds a component to this composite.
366    *
367    * @param    component          A component for this composite.
368    *
369    */
370 
371   public void addComponent(ComponentInstance component)
372   {
373     components.add(component);
374   }
375 
376   /**
377    * Returns the components of this composite.
378    *
379    * @return                  A <code>Collection</code> of {@link ComponentInstance ComponentInstances}.
380    *
381    */
382 
383   public Collection getComponents()
384   {
385     return components;
386   }
387 
388 
389   /* Debug. */
390 
391 
392   public String toString()
393   {
394     StringBuffer buffer = new StringBuffer();
395 
396     buffer.append("ComponentDefinition=[");
397 
398     buffer.append("name=");
399     buffer.append(name);
400 
401     buffer.append("; class=");
402     buffer.append(backingClass);
403 
404     buffer.append("; parents=[");
405 
406     Iterator parentIterator = parents.iterator();
407 
408     while (parentIterator.hasNext())
409     {
410       String parent = (String)parentIterator.next();
411 
412       buffer.append(parent);
413 
414       if (parentIterator.hasNext())
415       {
416         buffer.append("; ");
417       }
418     }
419 
420     buffer.append("]; attributes=[");
421 
422     Iterator attributeIterator = attributes.values().iterator();
423 
424     while (attributeIterator.hasNext())
425     {
426       ComponentAttribute attribute = (ComponentAttribute)attributeIterator.next();
427 
428       buffer.append(attribute.getName());
429       buffer.append("=");
430       buffer.append(attribute.getValue());
431 
432       if (attributeIterator.hasNext())
433       {
434         buffer.append("; ");
435       }
436     }
437 
438     buffer.append("]; messages=[");
439 
440     Iterator messageIterator = messages.values().iterator();
441 
442     while (messageIterator.hasNext())
443     {
444       ComponentMessage message = (ComponentMessage)messageIterator.next();
445 
446       buffer.append(message.getName());
447       buffer.append("=");
448       buffer.append(message.getTopic());
449 
450       if (messageIterator.hasNext())
451       {
452         buffer.append("; ");
453       }
454     }
455 
456     buffer.append("]; components=[");
457 
458     Iterator componentIterator = components.iterator();
459 
460     while (componentIterator.hasNext())
461     {
462       ComponentInstance component = (ComponentInstance)componentIterator.next();
463 
464       buffer.append(component.getName());
465 
466       if (componentIterator.hasNext())
467       {
468         buffer.append("; ");
469       }
470     }
471 
472     buffer.append("]]");
473 
474     return buffer.toString();
475   }
476 }
477