KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > project > ProjectDefinition


1 package net.sf.invicta.project;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import net.sf.invicta.InvictaConstants;
9 import net.sf.invicta.Logger;
10 import net.sf.invicta.api.Property;
11
12 /**
13  * A project definiton.
14  * Filled by digester with the elements of the project definition.
15  * Contains all define components of a project and also a ProjectSettings
16  * object.
17  */

18 public class ProjectDefinition {
19     // Resolve related members.
20
protected boolean isResolved = false;
21     
22     // Digester related members.
23
protected ProjectSettings projectSettings;
24     protected Map JavaDoc components = new HashMap JavaDoc(); // Map of BasicComponent objects.
25
protected ComponentDefinition globalComponent;
26
27     /**
28      * Constructor for Components.
29      */

30     public ProjectDefinition() {
31         super();
32         
33     }
34
35     /**
36      *
37      * @param projDef
38      * @throws InvictaProjectException
39      */

40     public void putAll(ProjectDefinition projDef) throws InvictaProjectException {
41         // Make sure that there are no duplicate project settings.
42
if ((this.projectSettings != null) &&
43              (projDef.getProjectSettings() != null))
44              throw InvictaProjectException.duplicateProjectSettings();
45         
46         // Copy the project settings.
47
if (projDef.projectSettings != null)
48             this.projectSettings = projDef.projectSettings;
49                     
50         // Copy the components of the given projDef while making sure that there
51
// are no duplicate components.
52
for (Iterator JavaDoc iterator = projDef.getComponentsMap().entrySet().iterator(); iterator.hasNext();) {
53             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
54             if (this.components.containsKey(entry.getKey()))
55                 throw InvictaProjectException.duplicateComponentDefinition((String JavaDoc)entry.getKey());
56             this.components.put(entry.getKey(), entry.getValue());
57         }
58             
59         this.isResolved = false;
60     }
61
62     /**
63      * Add a new component definition to the project.
64      * @param component
65      */

66     public void addComponent(ComponentDefinition component) throws InvictaProjectException {
67         if (this.components.get(component.getName()) != null)
68             throw InvictaProjectException.duplicateComponentDefinition(component.getName());
69         this.components.put(component.getName(), component);
70     }
71     
72     /**
73      * Set the project settings element of the project defintion.
74      * @param projectSettings
75      */

76     public void setProjectSettings(ProjectSettings projectSettings) {
77         this.projectSettings = projectSettings;
78     }
79     
80     /**
81      *
82      * @return ProjectSettings
83      */

84     public ProjectSettings getProjectSettings() {
85         return this.projectSettings;
86     }
87      
88     /**
89      * Returns the map of defined components.
90      *
91      * @return Map
92      */

93     public Map JavaDoc getComponentsMap() {
94         return this.components;
95     }
96
97     /**
98      *
99      * @return String
100      */

101     public String JavaDoc getName() {
102         return this.projectSettings.getName();
103     }
104     
105     /**
106      *
107      * @return String
108      */

109     public String JavaDoc getDir() {
110         return this.projectSettings.getDir();
111     }
112     
113     /**
114      *
115      * @return String
116      */

117     public String JavaDoc getVersion() {
118         return this.projectSettings.getVersion();
119     }
120
121     /**
122      *
123      * @param propertyName
124      * @return String
125      */

126     public String JavaDoc getProjectPropertyValue(String JavaDoc propertyName) {
127         return this.projectSettings.getPropertyValue(propertyName);
128     }
129
130     /**
131      *
132      * @param propertyName
133      * @return Property
134      */

135     public Property getProjectProperty(String JavaDoc propertyName) {
136         return this.projectSettings.getProperty(propertyName);
137     }
138
139     /**
140      *
141      * @param propertyName
142      * @param value
143      * @param description
144      */

145     public void setProjectProperty(String JavaDoc propertyName, String JavaDoc value, String JavaDoc description) {
146         Property previousProperty =
147             this.projectSettings.getProperty(propertyName);
148         if (previousProperty == null) {
149             this.projectSettings.setProperty(propertyName, value, description);
150         } else if (previousProperty.getDescription() == null) {
151             this.projectSettings.setProperty(propertyName, previousProperty.getValue(), description);
152         }
153     }
154
155     /**
156      *
157      * @return Map
158      */

159     public Map JavaDoc getProjectPropertiesMap() {
160         return this.projectSettings.getPropertiesMap();
161     }
162
163     /**
164      *
165      * @return List
166      */

167     public List JavaDoc getProjectProperties() {
168         return this.projectSettings.getProperties();
169     }
170
171     
172     /**
173      * String
174      */

175     public String JavaDoc toString() {
176         return components.toString();
177     }
178
179     /**
180      * Resolve the project definition by resolving all defined components.
181      *
182      * @throws InvictaProjectException
183      */

184     public void resolve() throws InvictaProjectException {
185         if (this.isResolved)
186             return;
187         
188         // Resolve the global component
189
resolveGlobalComponent();
190         
191         Logger.info("Resolving components: ");
192         // Go over all components and resolve them.
193
Iterator JavaDoc iterator = this.components.entrySet().iterator();
194         while(iterator.hasNext()) {
195             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
196             String JavaDoc key = (String JavaDoc) entry.getKey();
197             ComponentDefinition component = (ComponentDefinition)entry.getValue();
198                         
199             // Resolve the component.
200
component.resolve(this, 0);
201             
202         }
203         System.out.print("\n");
204         this.isResolved = true;
205     }
206                 
207     /**
208      * Resolve a component according to its given name while making sure that
209      * its defined.
210      *
211      * @param componentName
212      * @param level
213      * @return BasicComponent
214      */

215     public ComponentDefinition resolveComponent(String JavaDoc componentName, int level) throws InvictaProjectException {
216         ComponentDefinition component = (ComponentDefinition)this.components.get(componentName);
217         
218         if (component == null)
219             throw InvictaProjectException.componentUndefined(componentName);
220             
221         return component.resolve(this, level);
222     }
223
224     /**
225      *
226      * @return ComponentDefinition
227      */

228     public ComponentDefinition getGlobalComponent() {
229         return this.globalComponent;
230     }
231     
232     /**
233      *
234      * @return String
235      */

236     public String JavaDoc getGlobalComponentName() {
237         return this.globalComponent.getName();
238     }
239     
240     /**
241      *
242      */

243     protected void resolveGlobalComponent() throws InvictaProjectException {
244         // If a specific global component name was given, then set it to be
245
// the global component.
246
String JavaDoc globalComponentName = this.projectSettings.getGlobalComponent();
247         if (globalComponentName != null) {
248             this.globalComponent =
249                 (ComponentDefinition)this.components.get(globalComponentName);
250         
251             if (this.globalComponent == null)
252                 throw InvictaProjectException.componentUndefined(globalComponentName);
253         // If a specific name was not given, then create a global component
254
// with the default namd and type.
255
} else {
256             this.globalComponent = new ComponentDefinition();
257             this.globalComponent.setName(this.getName() +
258                                          InvictaConstants.DEFAULT_GLOBAL_COMPONENT_NAME);
259             this.globalComponent.setType(InvictaConstants.DEFAULT_GLOBAL_COMPONENT_TYPE);
260             this.components.put(globalComponent.getName(), globalComponent);
261         }
262     }
263
264 }
265
Popular Tags