KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > actionflow > config > BaseNamespaceBuilder


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.mvc.controller.actionflow.config;
8
9
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.jdom.Element;
14
15 import com.inversoft.config.ConfigurationException;
16 import com.inversoft.config.component.AbstractComponentConfigBuilder;
17 import com.inversoft.config.component.ComponentConfigBuilderRegistry;
18 import com.inversoft.error.ErrorList;
19 import com.inversoft.util.ReflectionException;
20 import com.inversoft.util.ReflectionTools;
21 import com.inversoft.util.StringTools;
22 import com.inversoft.verge.config.VergeConfigConstants;
23 import com.inversoft.verge.mvc.config.BaseFormConfig;
24 import com.inversoft.verge.mvc.config.BaseFormConfigBuilder;
25 import com.inversoft.verge.mvc.config.FormConfigRegistry;
26 import com.inversoft.verge.mvc.controller.LongTxnSetup;
27 import com.inversoft.verge.repository.config.RepositoryConfigRegistry;
28
29
30 /**
31  * This class is the base implementation of the NamespaceBuilder
32  * interface.
33  *
34  * @author Brian Pontarelli
35  * @since 2.0
36  * @version 2.0
37  */

38 public class BaseNamespaceBuilder extends AbstractComponentConfigBuilder
39 implements NamespaceBuilder {
40
41     /**
42      * Constructs a new <code>BaseNamespaceBuilder</code>.
43      */

44     public BaseNamespaceBuilder() {
45     }
46
47
48     /**
49      * Builds an <code>Namespace</code> instance from the Element given.
50      * The Element should contain all the information necessary to build the
51      * instance.
52      *
53      * @param element The Element to build the Namespace instance from
54      * @return The newly constructed Namespace
55      * @throws ConfigurationException If there was any problems during the build
56      */

57     public Namespace build(Element element) throws ConfigurationException {
58
59         String JavaDoc name = element.getAttributeValue(Constants.NAME_ATTRIBUTE);
60         String JavaDoc type = element.getAttributeValue(Constants.TYPE_ATTRIBUTE);
61         String JavaDoc errorPage = element.getAttributeValue(Constants.ERROR_PAGE_ATTRIBUTE);
62         String JavaDoc longTxnSetup = element.getAttributeValue(Constants.LONG_TXN_SETUP_ATTRIBUTE);
63
64         if (StringTools.isEmpty(name)) {
65             throw new ConfigurationException(Constants.NAME_ATTRIBUTE +
66                 " is a required attribute of the " + Constants.NAMESPACE_ELEMENT);
67         }
68
69         if (StringTools.isEmpty(type)) {
70             type = DefaultTypes.NAMESPACE;
71         }
72
73         // Setup the long transaction support if needed
74
LongTxnSetup lts = null;
75         if (!StringTools.isEmpty(longTxnSetup)) {
76             try {
77                 Class JavaDoc klass = ReflectionTools.findClass(longTxnSetup);
78                 if (!LongTxnSetup.class.isAssignableFrom(klass)) {
79                     throw new ConfigurationException("Invalid longTxnSetup" +
80                         "attribute. Must reference a class of " +
81                         "type com.inversoft.verge.mvc.controller.LongTxnSetup");
82                 }
83
84                 lts = (LongTxnSetup) ReflectionTools.instantiate(klass);
85             } catch (ReflectionException re) {
86                 throw new ConfigurationException(re);
87             }
88         }
89
90         return new BaseNamespace(name, type, errorPage, lts);
91     }
92
93     /**
94      * Goes through all the nodes and make certain that there is at most one
95      * default node
96      *
97      * @param namespace The Namespace to postBuild
98      * @throws ConfigurationException If anything went wrong while postBuilding
99      */

100     public void validate(Namespace namespace, ActionFlowConfigRegistry actionRegistry,
101             Map JavaDoc registries, ComponentConfigBuilderRegistry builderRegistry)
102     throws ConfigurationException {
103
104         // Validate all the nodes
105
RepositoryConfigRegistry repository =
106             (RepositoryConfigRegistry) registries.get(VergeConfigConstants.REPOSITORY_KEY);
107         Iterator JavaDoc iter = namespace.nodeIterator();
108         Node node;
109         boolean found = false;
110         NodeBuilder nodeBuilder;
111         ErrorList errors = new ErrorList();
112
113         while (iter.hasNext()) {
114             node = (Node) iter.next();
115             if (node.isDefaultEntry() && found) {
116                 errors.addError("Only one default entry node can be specified");
117             } else if (node.isDefaultEntry()) {
118                 found = true;
119             }
120
121             // Call post build on all the nodes
122
nodeBuilder = (NodeBuilder) builderRegistry.lookup(
123                 ActionFlowConfigBuilder.NODE_PREFIX + node.getType());
124             try {
125                 nodeBuilder.postBuild(node, actionRegistry, repository);
126             } catch (ConfigurationException ce) {
127                 errors.addErrorList(ce.getErrors());
128             }
129         }
130
131         // Validate all the forms
132
BaseFormConfigBuilder formBuilder =
133             (BaseFormConfigBuilder) builderRegistry.lookup(
134                 com.inversoft.verge.mvc.config.Constants.FORM_BUILDER_NAME);
135         FormConfigRegistry formRegistry =
136             (FormConfigRegistry) registries.get(VergeConfigConstants.FORM_KEY);
137         iter = namespace.formIterator();
138         BaseFormConfig formConfig;
139         while (iter.hasNext()) {
140             formConfig = (BaseFormConfig) iter.next();
141             try {
142                 formBuilder.validate(formConfig, repository, formRegistry);
143             } catch (ConfigurationException ce) {
144                 errors.addErrorList(ce.getErrors());
145             }
146         }
147
148         if (!errors.isEmpty()) {
149             throw new ConfigurationException(errors);
150         }
151     }
152 }
153
154
Popular Tags