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

Quick Search    Search Deep

Source code: com/aendvari/cerberus/component/directory/ComponentDirectory.java


1   /*
2    * ComponentDirectory.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.directory;
11  
12  import java.util.Collection;
13  import java.util.ArrayList;
14  
15  import com.aendvari.cerberus.component.descriptor.*;
16  
17  import com.aendvari.common.osm.*;
18  
19  /**
20   * <p>The component directory provides an interface for accessing component
21   * attributes and messages.</p>
22   *
23   * <p>Components are stored in the directory using their instance names. When
24   * a component is part of a composite, the instance name has the parent's name
25   * as a prefix.</p>
26   *
27   * @author  Trevor Milne
28   *
29   */
30  
31  public class ComponentDirectory
32  {
33    /** The OSM to store component information. */
34    protected Osm directory;
35  
36  
37    /* Constructors. */
38  
39  
40    /**
41     * Constructs a <code>ComponentDirectory</code> instance.
42     *
43     */
44  
45    public ComponentDirectory()
46    {
47      directory = new Osm();
48    }
49  
50  
51    /* Components. */
52  
53  
54    /**
55     * Adds a new {@link ComponentDescriptor} to the directory.
56     *
57     * @param    descriptor          The new {@link ComponentDescriptor}.
58     *
59     */
60  
61    public void addComponent(ComponentDescriptor descriptor)
62    {
63      // get node for property
64      OsmNode node = SimpleOsmPath.getNode(directory, descriptor.getName(), true);
65  
66      // set value of node
67      node.setNodeValue(descriptor);
68    }
69  
70    /**
71     * Returns the specified {@link ComponentDescriptor}.
72     *
73     * @param    name            The name of the component.
74     *
75     * @return                  The {@link ComponentDescriptor} specified.
76     *
77     */
78  
79    public ComponentDescriptor getComponent(String name)
80    {
81      // get node for property
82      OsmNode node = SimpleOsmPath.getNode(directory, name, false);
83  
84      // check for non existant property
85      if (node == null) return null;
86  
87      // get value of node
88      return (ComponentDescriptor)node.getNodeValue();
89    }
90  
91    /**
92     * Returns all {@link ComponentDescriptor ComponentDescriptors}.
93     *
94     * @return                  A <code>Collection</code> containing all
95     *                      {@link ComponentDescriptor ComponentDescriptors}.
96     *
97     */
98  
99    public Collection getComponents()
100   {
101     ArrayList components = new ArrayList();
102 
103     getComponents(directory, components);
104 
105     return components;
106   }
107 
108   /**
109    * Helper method for {@link getComponents}.
110    * Collects all {@link ComponentDescriptor ComponentDescriptors}.
111    *
112    * @param    node            The current node containing components.
113    * @param    components          The list of all components.
114    *
115    */
116 
117   private void getComponents(OsmNode node, Collection components)
118   {
119     OsmNode childNode = node.getFirstChild();
120 
121     while (childNode != null)
122     {
123       // get component of node, if any
124       ComponentDescriptor descriptor = (ComponentDescriptor)childNode.getNodeValue();
125 
126       if (descriptor != null)
127       {
128         components.add(descriptor);
129       }
130 
131       // traverse child nodes
132       getComponents(childNode, components);
133 
134       // traverse to next node
135       childNode = childNode.getNextSibling();
136     }
137   }
138 }
139