View Javadoc
1   package org.andromda.core.namespace;
2   
3   import java.net.URL;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.LinkedHashMap;
7   import java.util.Map;
8   
9   /**
10   * Represents a namespace registry.  This is where
11   * all components within a namespace are registered.
12   *
13   * @author Chad Brandon
14   * @author Bob Fields
15   */
16  public class NamespaceRegistry
17  {
18      /**
19       * The name of the namespace registry
20       */
21      private String name;
22  
23      /**
24       * Gets the name of the namespace registry.
25       *
26       * @return Returns the name.
27       */
28      public String getName()
29      {
30          return this.name;
31      }
32  
33      /**
34       * SEts the name of the namespace registry.
35       *
36       * @param name The name to set.
37       */
38      public void setName(String name)
39      {
40          this.name = name;
41      }
42  
43      /**
44       * Whether or not this is a shared namespace.
45       */
46      private boolean shared = false;
47  
48      /**
49       * Gets whether or not the namespace defined by this registry
50       * is shared. By default namespaces are <strong>NOT </strong> shared.
51       *
52       * @return Returns the shared.
53       */
54      public boolean isShared()
55      {
56          return shared;
57      }
58  
59      /**
60       * Sets whether or not the namespace defined by this registry is shared.
61       *
62       * @param shared The shared to set.
63       */
64      public void setShared(final boolean shared)
65      {
66          this.shared = shared;
67      }
68  
69      /**
70       * Stores the names of the components registered
71       * within this namespace registry and the paths from which
72       * they can be initialized.
73       */
74      private final Map<String, String[]> components = new LinkedHashMap<String, String[]>();
75  
76      /**
77       * Registers the component with the
78       * give name in this registry.
79       *
80       * @param component the component of the registry.
81       */
82      public void registerComponent(final Component component)
83      {
84          if (component != null)
85          {
86              this.components.put(
87                  component.getName(),
88                  component.getPaths());
89          }
90      }
91  
92      /**
93       * Gets the names registered components.
94       *
95       * @return the names of the registered components.
96       */
97      public String[] getRegisteredComponents()
98      {
99          return this.components.keySet().toArray(new String[this.components.size()]);
100     }
101 
102     /**
103      * Gets the initialization paths for the given component name.
104      *
105      * @param name the name of the component.
106      * @return the paths or null if none are found.
107      */
108     public String[] getPaths(final String name)
109     {
110         return this.components.get(name);
111     }
112 
113     /**
114      * Stores the property definitions.
115      */
116     private final Map<String, PropertyDefinition> definitions = new LinkedHashMap<String, PropertyDefinition>();
117 
118     /**
119      * Attempts to retrieve the property definition for the given
120      * <code>name</code>.
121      * @param name
122      * @return the property definition or null if one could not be found.
123      */
124     public PropertyDefinition getPropertyDefinition(final String name)
125     {
126         return this.definitions.get(name);
127     }
128 
129     /**
130      * Adds all property definitions to the current property definitions.
131      *
132      * @param propertyDefinitions the collection of property definitions.
133      */
134     public void addPropertyDefinitions(final PropertyDefinition[] propertyDefinitions)
135     {
136         for (PropertyDefinition propertyDefinition : propertyDefinitions)
137         {
138             this.addPropertyDefinition(propertyDefinition);
139         }
140     }
141 
142     /**
143      * Copies all contents from the <code>registry</code>
144      * to this instance.
145      *
146      * @param registry the registry to copy.
147      */
148     final void copy(final NamespaceRegistry registry)
149     {
150         if (registry != null)
151         {
152             this.addPropertyDefinitions(registry.getPropertyDefinitions());
153             this.components.putAll(registry.components);
154             if (registry.isShared())
155             {
156                 this.shared = registry.isShared();
157             }
158             this.resourceRoots.addAll(registry.resourceRoots);
159         }
160     }
161 
162     /**
163      * Gets all property definitions belonging to this registry.
164      *
165      * @return all property definitions.
166      */
167     public PropertyDefinition[] getPropertyDefinitions()
168     {
169         return this.definitions.values().toArray(new PropertyDefinition[this.definitions.size()]);
170     }
171 
172     /**
173      * Adds a property definition to the group of defintions.
174      *
175      * @param propertyDefinition the property definition.
176      */
177     public void addPropertyDefinition(final PropertyDefinition propertyDefinition)
178     {
179         if (propertyDefinition != null)
180         {
181             this.definitions.put(
182                 propertyDefinition.getName(),
183                 propertyDefinition);
184         }
185     }
186 
187     /**
188      * The root of this namespace which stores all resources.
189      */
190     private Collection<URL> resourceRoots = new ArrayList<URL>();
191 
192     /**
193      * Gets the resource root of this namespace.
194      *
195      * @return Returns the resource.
196      */
197     public URL[] getResourceRoots()
198     {
199         return this.resourceRoots.toArray(new URL[this.resourceRoots.size()]);
200     }
201 
202     /**
203      * Adds a resource root to this namespace (since a namespace can consist of multiple
204      * locations)
205      *
206      * @param resourceRoot The resource root to set.
207      */
208     final void addResourceRoot(final URL resourceRoot)
209     {
210         this.resourceRoots.add(resourceRoot);
211     }
212 
213     /**
214      * @see Object#toString()
215      */
216     public String toString()
217     {
218         return super.toString() + '[' + this.getName() + ']';
219     }
220 }