1 7 package com.inversoft.verge.mvc.controller.form.config; 8 9 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Map ; 13 14 import org.jdom.Document; 15 import org.jdom.Element; 16 17 import com.inversoft.config.ConfigBuilder; 18 import com.inversoft.config.ConfigRegistry; 19 import com.inversoft.config.ConfigurationException; 20 import com.inversoft.config.component.ComponentConfigBuilderRegistry; 21 import com.inversoft.error.ErrorList; 22 import com.inversoft.verge.config.VergeConfigConstants; 23 import com.inversoft.verge.mvc.config.BaseConfig; 24 import com.inversoft.verge.mvc.config.BaseConfigBuilder; 25 import com.inversoft.verge.repository.config.RepositoryConfigRegistry; 26 27 28 43 public class FormMVCConfigBuilder implements ConfigBuilder { 44 45 48 public static final String BUILDERS_BUNDLE = 49 "com.inversoft.verge.mvc.controller.form.config.FormMVCConfigBuilders"; 50 51 55 private ComponentConfigBuilderRegistry builderRegistry; 56 57 58 64 public FormMVCConfigBuilder() throws ConfigurationException { 65 builderRegistry = new ComponentConfigBuilderRegistry(BUILDERS_BUNDLE); 66 } 67 68 69 87 public synchronized void build(Document document, ConfigRegistry registry) 88 throws ConfigurationException { 89 90 ErrorList errors = new ErrorList(); 91 FormMVCConfigRegistry formRegistry = (FormMVCConfigRegistry) registry; 92 93 Iterator iter = document.getRootElement().getChildren().iterator(); 94 Element element; 95 BaseConfigBuilder configBuilder; 96 BaseConfig configObject; 97 98 while (iter.hasNext()) { 99 element = (Element) iter.next(); 100 configBuilder = (BaseConfigBuilder) builderRegistry.lookup(element.getName()); 101 if (configBuilder == null) { 102 errors.addError("element named: " + element.getName() + 103 " is not a valid element"); 104 continue; 105 } 106 107 try { 108 configObject = configBuilder.build(element); 109 } catch (ConfigurationException ce) { 110 errors.addErrorList(ce.getErrors()); 111 continue; 112 } 113 114 if (configObject instanceof ActionConfig) { 115 ActionConfig config = (ActionConfig) configObject; 116 if (formRegistry.lookupAction(config.getName()) != null) { 117 errors.addError("action named: " + config.getName() + 118 " is defined twice"); 119 continue; 120 } 121 122 formRegistry.register(config.getName(), config); 123 } else if (configObject instanceof FormConfig) { 124 FormConfig config = (FormConfig) configObject; 125 if (formRegistry.lookupForm(config.getName()) != null) { 126 errors.addError("form named: " + config.getName() + 127 " is defined twice"); 128 continue; 129 } 130 131 formRegistry.register(config.getName(), config); 132 } else if (configObject instanceof MappingConfig) { 133 MappingConfig config = (MappingConfig) configObject; 134 if (formRegistry.lookupMapping(config.getName()) != null) { 135 errors.addError("mapping named: " + config.getName() + 136 " is defined twice"); 137 continue; 138 } 139 140 formRegistry.register(config.getName(), config); 141 } 142 } 143 144 if (!errors.isEmpty()) { 145 throw new ConfigurationException(errors); 146 } 147 } 148 149 157 public void rebuild(Document document, ConfigRegistry registry) 158 throws ConfigurationException { 159 build(document, registry); 160 } 161 162 166 public void validate(ConfigRegistry registry, Map registries) 167 throws ConfigurationException { 168 169 assert (registries != null) : "registries == null"; 171 172 ActionConfigBuilder actionBuilder = 173 (ActionConfigBuilder) builderRegistry.lookup(Constants.ACTION_ELEMENT); 174 FormConfigBuilder formBuilder = 175 (FormConfigBuilder) builderRegistry.lookup(Constants.FORM_BUILDER_NAME); 176 177 RepositoryConfigRegistry repositoryRegistry = (RepositoryConfigRegistry) 178 registries.get(VergeConfigConstants.REPOSITORY_KEY); 179 180 ErrorList errors = new ErrorList(); 181 FormMVCConfigRegistry formRegistry = (FormMVCConfigRegistry) registry; 182 List configs = formRegistry.getAllConfigs(); 183 Iterator iter = configs.iterator(); 184 Object config; 185 186 while (iter.hasNext()) { 187 config = iter.next(); 188 189 try { 190 if (config instanceof ActionConfig) { 191 actionBuilder.validate((ActionConfig) config, repositoryRegistry); 192 } else if (config instanceof FormConfig) { 193 formBuilder.validate((FormConfig) config, repositoryRegistry, 194 formRegistry); 195 } 196 } catch (ConfigurationException ce) { 197 errors.addErrorList(ce.getErrors()); 198 } 199 } 200 201 if (!errors.isEmpty()) { 202 throw new ConfigurationException(errors); 203 } 204 } 205 206 213 public void commit(ConfigRegistry registry, Map otherRegistries) { 214 FormMVCConfigRegistry.setInstance((FormMVCConfigRegistry) registry); 215 } 216 } 217 | Popular Tags |