View Javadoc

1   /*
2    * $Id: PopulatorRegistry.java,v 1.4 2004/04/05 09:56:22 eelco12 Exp $
3    * $Revision: 1.4 $
4    * $Date: 2004/04/05 09:56:22 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31   
32  package nl.openedge.baritus;
33  
34  import java.util.HashMap;
35  import java.util.Map;
36  import java.util.regex.Pattern;
37  
38  import nl.openedge.baritus.population.FieldPopulator;
39  import nl.openedge.baritus.population.OgnlFieldPopulator;
40  
41  /***
42   * Registry for populators. Each instance of FormBeanBase has its own instance.
43   * 
44   * @author Eelco Hillenius
45   */
46  public final class PopulatorRegistry
47  {
48  
49  	private Map fieldPopulators = null;
50  	
51  	private Map regexFieldPopulators = null;
52  	
53  	private FieldPopulator defaultFieldPopulator = null;
54  	
55  	/***
56  	 * construct registry with the current instance of FormBeanBase
57  	 * @param formBeanCtrl
58  	 */
59  	public PopulatorRegistry(FormBeanCtrlBase formBeanCtrl)
60  	{
61  		defaultFieldPopulator = new OgnlFieldPopulator(formBeanCtrl);
62  	}
63  	
64  	/***
65  	 * Register a field populator for the given fieldName. 
66  	 * Field populators override the default population of a property on the current form
67  	 * @param fieldName name of field
68  	 * @param populator populator instance
69  	 */
70  	public void addPopulator(String fieldName, FieldPopulator populator)
71  	{
72  		if(fieldPopulators == null)
73  		{
74  			fieldPopulators = new HashMap();
75  		}
76  		fieldPopulators.put(fieldName, populator);
77  	}
78  
79  	
80  	/***
81  	 * de-register the field populator that was registered with the given fieldName
82  	 * @param fieldName name of field
83  	 */
84  	public void removePopulator(String fieldName)
85  	{
86  		if(fieldPopulators != null)
87  		{
88  			fieldPopulators.remove(fieldName);
89  			if(fieldPopulators.isEmpty()) fieldPopulators = null;
90  		}
91  	}
92  	
93  	/***
94  	 * Register a custom populator that overrides the default population
95  	 * process for all request parameters that match the regular expression stored in
96  	 * the provided pattern.
97  	 * 
98  	 * @param pattern regex pattern
99  	 * @param populator populator instance
100 	 */
101 	public void addPopulator(Pattern pattern, FieldPopulator populator)
102 	{
103 		if(regexFieldPopulators == null)
104 		{
105 			regexFieldPopulators = new HashMap();
106 		}
107 		regexFieldPopulators.put(pattern, populator);
108 	}
109 
110 	
111 	/***
112 	 * Remove a populator that was registered for the provided pattern
113 	 * @param pattern regex pattern
114 	 */
115 	public void removePopulator(Pattern pattern)
116 	{
117 		if(regexFieldPopulators != null)
118 		{
119 			regexFieldPopulators.remove(pattern);
120 		}
121 	}
122 	
123 	/***
124 	 * get the populators that were registered with regex patterns
125 	 * @return Map the populators that were registered with regex patterns
126 	 */
127 	public Map getRegexFieldPopulators()
128 	{
129 		return regexFieldPopulators;
130 	}
131 	
132 	/***
133 	 * get the field populator for the provided fieldName, null if none registered
134 	 * @param fieldName name of the field
135 	 * @return the field populator for the provided fieldName, null if none registered
136 	 */
137 	public FieldPopulator getFieldPopulator(String fieldName)
138 	{
139 		return (fieldPopulators != null) ? 
140 			(FieldPopulator)fieldPopulators.get(fieldName) : null;
141 	}
142 
143 	/***
144 	 * get the field populators
145 	 * @return Map the field populators
146 	 */
147 	public Map getFieldPopulators()
148 	{
149 		return fieldPopulators;
150 	}
151 	
152 	/***
153 	 * get the default field populator
154 	 * @return FieldPopulator the default field populator
155 	 */
156 	public FieldPopulator getDefaultFieldPopulator()
157 	{
158 		return defaultFieldPopulator;
159 	}
160 
161 	/***
162 	 * set the default field populator
163 	 * @param populator the default field populator
164 	 */
165 	public void setDefaultFieldPopulator(FieldPopulator populator)
166 	{
167 		defaultFieldPopulator = populator;
168 	}
169 
170 }