1 package org.apache.commons.digester3.binder;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24 import java.util.Map.Entry;
25
26 import org.apache.commons.digester3.plugins.PluginCreateRule;
27 import org.apache.commons.digester3.plugins.RuleLoader;
28
29
30
31
32
33
34 public final class PluginCreateRuleBuilder
35 extends AbstractBackToLinkedRuleBuilder<PluginCreateRule>
36 {
37
38 private final Map<String, String> pluginClassAttributes = new LinkedHashMap<String, String>();
39
40 private final Map<String, String> pluginIdAttributes = new LinkedHashMap<String, String>();
41
42 private Class<?> baseClass;
43
44 private Class<?> dfltPluginClass;
45
46 private RuleLoader dfltPluginRuleLoader;
47
48 PluginCreateRuleBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder,
49 LinkedRuleBuilder mainBuilder )
50 {
51 super( keyPattern, namespaceURI, mainBinder, mainBuilder );
52 }
53
54
55
56
57
58
59
60
61 public <T> PluginCreateRuleBuilder ofType( Class<T> type )
62 {
63 if ( type == null )
64 {
65 reportError( "createPlugin().ofType( Class<?> )", "NULL Java type not allowed" );
66 return this;
67 }
68
69 this.baseClass = type;
70
71 return this;
72 }
73
74
75
76
77
78
79
80
81 public <T> PluginCreateRuleBuilder usingDefaultPluginClass(
82 {
83 this.dfltPluginClass = type;
84 return this;
85 }
86
87
88
89
90
91
92
93
94
95 public <RL extends RuleLoader> PluginCreateRuleBuilder usingRuleLoader(
96 {
97 this.dfltPluginRuleLoader = ruleLoader;
98 return this;
99 }
100
101
102
103
104
105
106
107
108
109 public PluginCreateRuleBuilder setPluginClassAttribute( String attrName )
110 {
111 if ( attrName == null )
112 {
113 reportError( "createPlugin().setPluginClassAttribute( String )", "NULL attribute name not allowed" );
114 return this;
115 }
116
117 return this.setPluginClassAttribute( null, attrName );
118 }
119
120
121
122
123
124
125
126
127
128 public PluginCreateRuleBuilder setPluginClassAttribute(
129 {
130 if ( attrName == null )
131 {
132 reportError( "createPlugin().setPluginClassAttribute( String, String )",
133 "NULL attribute name not allowed" );
134 return this;
135 }
136
137 return addToMap( pluginClassAttributes, namespaceUri, attrName );
138 }
139
140
141
142
143
144
145
146
147 public PluginCreateRuleBuilder setPluginIdAttribute( String attrName )
148 {
149 if ( attrName == null )
150 {
151 reportError( "createPlugin().setPluginIdAttribute( String )", "NULL attribute name not allowed" );
152 return this;
153 }
154
155 return setPluginIdAttribute( null, attrName );
156 }
157
158
159
160
161
162
163
164
165
166 public PluginCreateRuleBuilder setPluginIdAttribute(
167 {
168 if ( attrName == null )
169 {
170 reportError( "createPlugin().setPluginIdAttribute( String, String )", "NULL attribute name not allowed" );
171 return this;
172 }
173
174 return addToMap( pluginIdAttributes, namespaceUri, attrName );
175 }
176
177
178
179
180
181
182
183
184
185 private PluginCreateRuleBuilder addToMap( Map<String, String> map, String namespaceUri, String attrName )
186 {
187 map.put( namespaceUri, attrName );
188 return this;
189 }
190
191
192
193
194 @Override
195 protected PluginCreateRule createRule()
196 {
197 if ( baseClass == null )
198 {
199 reportError( "createPlugin()", "'baseClass' has to be specified" );
200 }
201
202 PluginCreateRule rule;
203 if ( dfltPluginClass != null )
204 {
205 if ( dfltPluginRuleLoader != null )
206 {
207 rule = new PluginCreateRule( baseClass, dfltPluginClass, dfltPluginRuleLoader );
208 }
209 else
210 {
211 rule = new PluginCreateRule( baseClass, dfltPluginClass );
212 }
213 }
214 else
215 {
216 rule = new PluginCreateRule( baseClass );
217 }
218
219 for ( Entry<String, String> entry : pluginClassAttributes.entrySet() )
220 {
221 rule.setPluginClassAttribute( entry.getKey(), entry.getValue() );
222 }
223
224 for ( Entry<String, String> entry : pluginIdAttributes.entrySet() )
225 {
226 rule.setPluginIdAttribute( entry.getKey(), entry.getValue() );
227 }
228
229 return rule;
230 }
231
232 }