KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import com.inversoft.util.StringTools;
15 import com.inversoft.verge.mvc.config.BaseFormConfig;
16 import com.inversoft.verge.mvc.controller.LongTxnSetup;
17 import com.inversoft.verge.mvc.controller.actionflow.ActionFlowAction;
18
19
20 /**
21  * This class is the default implementation of the
22  * {@link Namespace Namespace} interface.
23  *
24  * @author Brian Pontarelli
25  * @since 2.0
26  * @version 2.0
27  */

28 public class BaseNamespace implements Namespace {
29
30     /**
31      * The Map used to store all the Node instances inside this namespace
32      */

33     protected Map JavaDoc nodes;
34
35     /**
36      * The default node
37      */

38     protected Node defaultNode;
39
40     /**
41      * The Map used to store all the Form instances
42      */

43     protected Map JavaDoc forms;
44
45     private String JavaDoc name;
46     private String JavaDoc type;
47     private String JavaDoc errorPage;
48     private LongTxnSetup longTxnSetup;
49
50
51     /**
52      * Constructs a new <code>BaseNamespace</code> that has the given
53      * name and type.
54      *
55      * @param name The name of the Namespace
56      * @param type The type of this namespace
57      * @asserts If name or type are empty or null
58      */

59     public BaseNamespace(String JavaDoc name, String JavaDoc type) {
60         this(name, type, null, null);
61     }
62
63     /**
64      * Constructs a new <code>BaseNamespace</code> that has the given
65      * name, type and error page
66      *
67      * @param name The name of the Namespace
68      * @param type The type of this namespace
69      * @param errorPage the error page for this namespace (if any)
70      * @asserts If name or type are empty or null
71      */

72     public BaseNamespace(String JavaDoc name, String JavaDoc type, String JavaDoc errorPage,
73             LongTxnSetup longTxnSetup) {
74         assert (!StringTools.isEmpty(name)) : "name is null or empty";
75         assert (!StringTools.isEmpty(type)) : "type is null or empty";
76
77         this.name = name;
78         this.type = type;
79         this.errorPage = errorPage;
80         this.nodes = new HashMap JavaDoc();
81         this.forms = new HashMap JavaDoc();
82         this.longTxnSetup = longTxnSetup;
83     }
84
85
86     /**
87      * Returns the name of this namespace.
88      *
89      * @return The name of this namespace
90      */

91     public String JavaDoc getName() {
92         return name;
93     }
94
95     /**
96      * Returns the type of this namespace
97      *
98      * @return The type of this namespace
99      */

100     public String JavaDoc getType() {
101         return type;
102     }
103
104     /**
105      * Returns the error page of this Namespace or null if one was not set
106      *
107      * @return The error page of this Namespace or null if one was not set
108      */

109     public String JavaDoc getErrorPage() {
110         return errorPage;
111     }
112
113     /**
114      * Returns the config for the Node in the namespace with the given name.
115      *
116      * @return The Node with the given name or null if it doesn't exist
117      */

118     public Node getNode(String JavaDoc name) {
119         return (Node) nodes.get(name);
120     }
121
122     /**
123      * Adds the given Node to the namespace. This is stored in the namespace
124      * under its name which is retrieved using the {@link
125      * com.inversoft.verge.mvc.controller.actionflow.config.Node#getName() getName()}
126      * method. If there was already a Node with the given name, it is
127      * returned
128      *
129      * @param node The Node to add to the namespace
130      * @return The previous Node with the same name as the new Node
131      * or null if there wasn't one.
132      * @throws IllegalArgumentException If node is null
133      */

134     public Node addNode(Node node) throws IllegalArgumentException JavaDoc {
135         if (node == null) {
136             throw new IllegalArgumentException JavaDoc("The Node is null");
137         }
138
139         Node oldNode = (Node) nodes.put(node.getName(), node);
140         if (node.isDefaultEntry()) {
141             defaultNode = node;
142         }
143
144         return oldNode;
145     }
146
147     /**
148      * Returns an iterator over all the Nodes in this namespace. This Iterator
149      * should be a live Iterator so that calling the remove method will remove
150      * the Node from the Namespace.
151      *
152      * @return An Iterator over all the Nodes
153      */

154     public Iterator JavaDoc nodeIterator() {
155         return nodes.values().iterator();
156     }
157
158     /**
159      * Finds an entry point, if one exists, for the given name. This first
160      * checks if there is an entry node with the given node. Next it iterates
161      * through all the entry nodes and determines if the a Node exists that
162      * will handle the name given. This is done by calling the {@link
163      * com.inversoft.verge.mvc.controller.actionflow.config.Node#acceptEntry(String)
164      * acceptEntry} method.
165      *
166      * @param name The name to locate an entry point for
167      * @return The entry point Node or null if one doesn't exist
168      */

169     public Node findEntry(String JavaDoc name) {
170         if (StringTools.isEmpty(name)) {
171             return defaultNode;
172         }
173
174         Node node = (Node) nodes.get(name);
175         if (node != null) {
176             return node;
177         }
178
179         // Handle wildcard entry nodes, etc
180
Iterator JavaDoc iter = nodes.values().iterator();
181         while (iter.hasNext()) {
182             node = (Node) iter.next();
183             if (node.acceptEntry(name)) {
184                 return node;
185             }
186         }
187
188         return null;
189     }
190
191     /**
192      * Adds the given form to the namespace. This is stored in the namespace
193      * under its name which is retrieved using the {@link BaseFormConfig#getName()
194      * getName()} method. If there was already a form with the given name, it is
195      * returned.
196      *
197      * @param form The BaseFormConfig to add to the namespace
198      * @return The previous BaseFormConfig with the same name as the new form
199      * or null if there wasn't one
200      * @throws IllegalArgumentException If node is null
201      */

202     public BaseFormConfig addForm(BaseFormConfig form) throws IllegalArgumentException JavaDoc {
203         if (form == null) {
204             throw new IllegalArgumentException JavaDoc("The form is null");
205         }
206
207         return (BaseFormConfig) forms.put(form.getName(), form);
208     }
209
210     /**
211      * Returns an iterator over all the Forms in this namespace. This Iterator
212      * should be a live Iterator so that calling the remove method will remove
213      * the Form from the Namespace.
214      *
215      * @return An Iterator over all the Forms
216      */

217     public Iterator JavaDoc formIterator() {
218         return forms.values().iterator();
219     }
220
221     /**
222      * Returns the BaseFormConfig stored in this namespace under the given name,
223      * which is also the name of the form.
224      *
225      * @param name The name of the form to lookup
226      * @return The form or null
227      */

228     public BaseFormConfig lookupForm(String JavaDoc name) {
229         return (BaseFormConfig) forms.get(name);
230     }
231
232     /**
233      * Returns the long transaction setup class defined in the configuration
234      * file for this namespace.
235      *
236      * @return The class
237      */

238     public LongTxnSetup getLongTxnSetup() {
239         return longTxnSetup;
240     }
241
242     /**
243      * Returns the long transaction begin URL however the namespace sees fit.
244      *
245      * @return The begin URL for long transactions
246      */

247     public String JavaDoc getLongTxnStartURL(ActionFlowAction action) {
248         return longTxnSetup.getLongTxnStartURL(action);
249     }
250
251     /**
252      * Returns the long transaction end URL however the namespace sees fit.
253      *
254      * @return The end URL for long transactions
255      */

256     public String JavaDoc getLongTxnEndURL(ActionFlowAction action) {
257         return longTxnSetup.getLongTxnEndURL(action);
258     }
259 }
Popular Tags