KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > repository > config > ItemBuilder


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.repository.config;
8
9
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 import org.apache.log4j.Logger;
14 import org.jdom.Element;
15
16 import com.inversoft.beans.BeanException;
17 import com.inversoft.config.ConfigurationException;
18 import com.inversoft.config.component.AbstractComponentConfigBuilder;
19 import com.inversoft.error.BasicError;
20 import com.inversoft.error.ErrorList;
21 import com.inversoft.verge.util.ScopeTools;
22
23
24 /**
25  * This class is the Builder for the items in the repository.
26  * It reads the information from the XML elements in the
27  * config file and creates Config objects that are stored
28  * in a Map. This also builds a list of all the errors that
29  * are encountered, if any.
30  *
31  * @author Brian Pontarelli
32  * @since 2.0
33  * @version 2.0
34  */

35 public class ItemBuilder extends AbstractComponentConfigBuilder
36 implements Builder {
37
38     /** The logger for this class */
39     private final static Logger logger = Logger.getLogger(ItemBuilder.class);
40
41
42     /**
43      * Builds the repository items from the given Element and stores them
44      * in the given Map.
45      *
46      * @param element The Element to build the items from
47      * @param registry The RepositoryConfigRegistry to store the items in
48      * @throws ConfigurationException If anything went wrong while building.
49      * This Exception contains an ErrorList that holds all the errors
50      */

51     public void build(Element element, RepositoryConfigRegistry registry)
52     throws ConfigurationException {
53
54         ErrorList errors = new ErrorList();
55         String JavaDoc id = element.getAttributeValue(BuildConstants.ID_ATTRIBUTE);
56         String JavaDoc scope = element.getAttributeValue(BuildConstants.SCOPE_ATTRIBUTE);
57         int scopeInt = ScopeTools.convertScope(scope);
58
59         validateId(id, registry, errors);
60
61         if (scopeInt == -1) {
62             errors.addError( new BasicError("Invalid scope of: " + scope +
63                 " for item with id: " + id) );
64         }
65
66         ItemConfig config = null;
67         try {
68             config = new ItemConfig(id, scopeInt,
69                 element.getAttributeValue(BuildConstants.CLASS_ATTRIBUTE));
70             setupProperties(element, config, errors);
71             setupReferences(element, config, errors);
72         } catch (BeanException be) {
73             errors.addError(new BasicError(be.getMessage()));
74         }
75
76         // If there were errors, throw an exception
77
if (!errors.isEmpty()) {
78             throw new ConfigurationException(errors);
79         }
80
81         if (logger.isDebugEnabled()) {
82             logger.debug("Storing item:" + config.getID());
83         }
84
85         registry.register(id, config);
86     }
87
88     /**
89      * Validates that the given id does not contain any bad characters
90      *
91      * @param id The id to validate
92      * @param registry The RepositoryConfigRegistry of configs to use when
93      * checking if a config already exists
94      * @param errors The ErrorList to store errors
95      * @return True if the id is okay, false if it is invalid
96      */

97     protected boolean validateId(String JavaDoc id, RepositoryConfigRegistry registry,
98             ErrorList errors) {
99
100         if (id.indexOf(".") != -1) {
101             errors.addError( new BasicError("The item with an id of: " +
102                 id + " is invalid because it contains the '.' character") );
103             return false;
104         }
105
106         if (registry.lookup(id) != null) {
107             errors.addError( new BasicError("Item with an id of: " + id
108                 + " is defined twice in configuration file") );
109             return false;
110         }
111
112         return true;
113     }
114
115     /**
116      * Sets up the initial properties for the given item and stores any errors in
117      * the given ErrorList.
118      *
119      * @param item The item Element from the config file
120      * @param config The Config object to store the properties in
121      * @param errors The ErrorList to store the errors in
122      * @return True if the properties contained no errors, false otherwise
123      */

124     protected boolean setupProperties(Element item, ItemConfig config, ErrorList errors) {
125
126         List JavaDoc list = item.getChildren(BuildConstants.PROPERTY_ELEMENT);
127         if (list == null || list.isEmpty()) {
128             return true;
129         }
130
131         Iterator JavaDoc iter = list.iterator();
132         Element prop;
133         String JavaDoc name;
134         String JavaDoc value;
135         boolean retValue = true;
136
137         while (iter.hasNext()) {
138             prop = (Element) iter.next();
139             name = prop.getAttributeValue(BuildConstants.NAME_ATTRIBUTE);
140             value = prop.getAttributeValue(BuildConstants.VALUE_ATTRIBUTE);
141
142             // Check if this property name has already been used on this item
143
if (config.hasProperty(name)) {
144                 errors.addError( new BasicError("The item with an id of: " + config.getID()
145                     + " defines the property named: " + name + " twice") );
146                 retValue = false;
147                 continue;
148             }
149
150             try {
151                 if (logger.isDebugEnabled()) {
152                     logger.debug("Adding property: " + name + " with value: " + value);
153                 }
154
155                 config.addProperty(name, value);
156             } catch (BeanException be) {
157                 errors.addError( new BasicError(be.toString()) );
158                 retValue = false;
159                 continue;
160             }
161         }
162
163         return retValue;
164     }
165
166     /**
167      * Sets up the initial references for the given item and stores any errors in
168      * the given ErrorList.
169      *
170      * @param item The item Element from the config file
171      * @param config The Config object to store the references in
172      * @param errors The ErrorList to store the errors in
173      * @return True if the references contained no errors, false otherwise
174      */

175     protected boolean setupReferences(Element item, ItemConfig config, ErrorList errors) {
176
177         List JavaDoc list = item.getChildren(BuildConstants.REFERENCE_ELEMENT);
178         if (list == null || list.isEmpty()) {
179             return true;
180         }
181
182         Iterator JavaDoc iter = list.iterator();
183         Element prop;
184         String JavaDoc name;
185         String JavaDoc value;
186         boolean retValue = true;
187
188         while (iter.hasNext()) {
189             prop = (Element) iter.next();
190             name = prop.getAttributeValue(BuildConstants.NAME_ATTRIBUTE);
191             value = prop.getAttributeValue(BuildConstants.VALUE_ATTRIBUTE);
192
193             // Check if this reference name has already been used on this item
194
if (config.hasProperty(name)) {
195                 errors.addError( new BasicError("The item with an id of: " + config.getID()
196                     + " defines the reference with id of: " + name + " twice") );
197                 retValue = false;
198                 continue;
199             }
200
201             try {
202                 if (logger.isDebugEnabled()) {
203                     logger.debug("Adding reference: " + name + " with value: " + value);
204                 }
205
206                 config.addReference(name, value);
207             } catch (BeanException be) {
208                 errors.addError( new BasicError(be.toString()) );
209                 retValue = false;
210                 continue;
211             }
212         }
213
214         return retValue;
215     }
216 }
217
Popular Tags