001: package org.andromda.core.namespace;
002:
003: import java.net.URL;
004: import java.util.ArrayList;
005: import java.util.LinkedHashMap;
006: import java.util.List;
007: import java.util.Map;
008:
009: /**
010: * Represents a namespace registry. This is where
011: * all components within a namespace are registered.
012: *
013: * @author Chad Brandon
014: */
015: public class NamespaceRegistry {
016: /**
017: * The name of the namespace registry
018: */
019: private String name;
020:
021: /**
022: * Gets the name of the namespace registry.
023: *
024: * @return Returns the name.
025: */
026: public String getName() {
027: return this .name;
028: }
029:
030: /**
031: * SEts the name of the namespace registry.
032: *
033: * @param name The name to set.
034: */
035: public void setName(String name) {
036: this .name = name;
037: }
038:
039: /**
040: * Whether or not this is a shared namespace.
041: */
042: private boolean shared = false;
043:
044: /**
045: * Gets whether or not the namespace defined by this registry
046: * is shared. By default namespaces are <strong>NOT </strong> shared.
047: *
048: * @return Returns the shared.
049: */
050: public boolean isShared() {
051: return shared;
052: }
053:
054: /**
055: * Sets whether or not the namespace defined by this registry is shared.
056: *
057: * @param shared The shared to set.
058: */
059: public void setShared(final boolean shared) {
060: this .shared = shared;
061: }
062:
063: /**
064: * Stores the names of the components registered
065: * within this namespace registry and the paths from which
066: * they can be initialized.
067: */
068: private final Map components = new LinkedHashMap();
069:
070: /**
071: * Registers the component with the
072: * give name in this registry.
073: *
074: * @param component the component of the registry.
075: */
076: public void registerComponent(final Component component) {
077: if (component != null) {
078: this .components.put(component.getName(), component
079: .getPaths());
080: }
081: }
082:
083: /**
084: * Gets the names registered components.
085: *
086: * @return the names of the registered components.
087: */
088: public String[] getRegisteredComponents() {
089: return (String[]) this .components.keySet().toArray(
090: new String[0]);
091: }
092:
093: /**
094: * Gets the initialization paths for the given component name.
095: *
096: * @param name the name of the component.
097: * @return the paths or null if none are found.
098: */
099: public String[] getPaths(final String name) {
100: return (String[]) this .components.get(name);
101: }
102:
103: /**
104: * Stores the property definitions.
105: */
106: private final Map definitions = new LinkedHashMap();
107:
108: /**
109: * Attempts to retrieve the property definition for the given
110: * <code>name</code>.
111: *
112: * @return the property definition or null if one could not be found.
113: */
114: public PropertyDefinition getPropertyDefinition(final String name) {
115: return (PropertyDefinition) this .definitions.get(name);
116: }
117:
118: /**
119: * Adds all property definitions to the current property definitions.
120: *
121: * @param propertyDefinitions the collection of property definitions.
122: */
123: public void addPropertyDefinitions(
124: final PropertyDefinition[] propertyDefinitions) {
125: for (int ctr = 0; ctr < propertyDefinitions.length; ctr++) {
126: this .addPropertyDefinition(propertyDefinitions[ctr]);
127: }
128: }
129:
130: /**
131: * Copies all contents from the <code>registry</code>
132: * to this instance.
133: *
134: * @param registry the registry to copy.
135: */
136: final void copy(final NamespaceRegistry registry) {
137: if (registry != null) {
138: this .addPropertyDefinitions(registry
139: .getPropertyDefinitions());
140: this .components.putAll(registry.components);
141: if (registry.isShared()) {
142: this .shared = registry.isShared();
143: }
144: this .resourceRoots.addAll(registry.resourceRoots);
145: }
146: }
147:
148: /**
149: * Gets all property definitions belonging to this registry.
150: *
151: * @return all property definitions.
152: */
153: public PropertyDefinition[] getPropertyDefinitions() {
154: return (PropertyDefinition[]) this .definitions.values()
155: .toArray(new PropertyDefinition[0]);
156: }
157:
158: /**
159: * Adds a property definition to the group of defintions.
160: *
161: * @param propertyDefinition the property definition.
162: */
163: public void addPropertyDefinition(
164: final PropertyDefinition propertyDefinition) {
165: if (propertyDefinition != null) {
166: this .definitions.put(propertyDefinition.getName(),
167: propertyDefinition);
168: }
169: }
170:
171: /**
172: * The root of this namespace which stores all resources.
173: */
174: private List resourceRoots = new ArrayList();
175:
176: /**
177: * Gets the resource root of this namespace.
178: *
179: * @return Returns the resource.
180: */
181: public URL[] getResourceRoots() {
182: return (URL[]) this .resourceRoots.toArray(new URL[0]);
183: }
184:
185: /**
186: * Adds a resource root to this namespace (since a namespace can consist of multiple
187: * locations)
188: *
189: * @param resourceRoot The resource root to set.
190: */
191: final void addResourceRoot(final URL resourceRoot) {
192: this .resourceRoots.add(resourceRoot);
193: }
194:
195: /**
196: * @see java.lang.Object#toString()
197: */
198: public String toString() {
199: return super .toString() + "[" + this .getName() + "]";
200: }
201: }
|