1 7 package com.inversoft.verge.repository.config; 8 9 10 import java.util.Iterator ; 11 import java.util.List ; 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 35 public class ItemBuilder extends AbstractComponentConfigBuilder 36 implements Builder { 37 38 39 private final static Logger logger = Logger.getLogger(ItemBuilder.class); 40 41 42 51 public void build(Element element, RepositoryConfigRegistry registry) 52 throws ConfigurationException { 53 54 ErrorList errors = new ErrorList(); 55 String id = element.getAttributeValue(BuildConstants.ID_ATTRIBUTE); 56 String 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 (!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 97 protected boolean validateId(String 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 124 protected boolean setupProperties(Element item, ItemConfig config, ErrorList errors) { 125 126 List list = item.getChildren(BuildConstants.PROPERTY_ELEMENT); 127 if (list == null || list.isEmpty()) { 128 return true; 129 } 130 131 Iterator iter = list.iterator(); 132 Element prop; 133 String name; 134 String 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 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 175 protected boolean setupReferences(Element item, ItemConfig config, ErrorList errors) { 176 177 List list = item.getChildren(BuildConstants.REFERENCE_ELEMENT); 178 if (list == null || list.isEmpty()) { 179 return true; 180 } 181 182 Iterator iter = list.iterator(); 183 Element prop; 184 String name; 185 String 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 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 |