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 static java.lang.String.format;
23
24 import java.util.Arrays;
25
26 import org.apache.commons.digester3.CallMethodRule;
27
28
29
30
31
32
33 public final class CallMethodBuilder
34 extends AbstractBackToLinkedRuleBuilder<CallMethodRule>
35 {
36
37 private final String methodName;
38
39 private final ClassLoader classLoader;
40
41 private int targetOffset;
42
43 private int paramCount = 0;
44
45 private Class<?>[] paramTypes = new Class<?>[]{};
46
47 private boolean useExactMatch = false;
48
49 CallMethodBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder, LinkedRuleBuilder mainBuilder,
50 String methodName, ClassLoader classLoader )
51 {
52 super( keyPattern, namespaceURI, mainBinder, mainBuilder );
53 this.methodName = methodName;
54 this.classLoader = classLoader;
55 }
56
57
58
59
60
61
62
63
64
65
66 public CallMethodBuilder withTargetOffset( int targetOffset )
67 {
68 this.targetOffset = targetOffset;
69 return this;
70 }
71
72
73
74
75
76
77
78
79
80
81 public CallMethodBuilder withParamTypes( String... paramTypeNames )
82 {
83 Class<?>[] paramTypes = null;
84 if ( paramTypeNames != null )
85 {
86 paramTypes = new Class<?>[paramTypeNames.length];
87 for ( int i = 0; i < paramTypeNames.length; i++ )
88 {
89 try
90 {
91 paramTypes[i] = classLoader.loadClass( paramTypeNames[i] );
92 }
93 catch ( ClassNotFoundException e )
94 {
95 this.reportError( format( "callMethod( \"%s\" ).withParamTypes( %s )", this.methodName,
96 Arrays.toString( paramTypeNames ) ),
97 format( "class '%s' cannot be load", paramTypeNames[i] ) );
98 }
99 }
100 }
101
102 return withParamTypes( paramTypes );
103 }
104
105
106
107
108
109
110
111
112
113
114 public CallMethodBuilder withParamTypes( Class<?>... paramTypes )
115 {
116 this.paramTypes = paramTypes;
117
118 if ( paramTypes != null )
119 {
120 this.paramCount = paramTypes.length;
121 }
122 else
123 {
124 paramCount = 0;
125 }
126
127 return this;
128 }
129
130
131
132
133
134
135
136 public CallMethodBuilder useExactMatch( boolean useExactMatch )
137 {
138 this.useExactMatch = useExactMatch;
139 return this;
140 }
141
142
143
144
145
146
147
148
149 public CallMethodBuilder withParamCount( int paramCount )
150 {
151 if ( paramCount < 0 )
152 {
153 this.reportError( format( "callMethod(\"%s\").withParamCount(int)", this.methodName ),
154 "negative parameters counter not allowed" );
155 }
156
157 this.paramCount = paramCount;
158
159 if ( this.paramCount == 0 )
160 {
161 if ( this.paramTypes == null || this.paramTypes.length != 1 )
162 {
163 this.paramTypes = new Class<?>[] { String.class };
164 }
165 }
166 else
167 {
168 this.paramTypes = new Class<?>[this.paramCount];
169 for ( int i = 0; i < paramTypes.length; i++ )
170 {
171 this.paramTypes[i] = String.class;
172 }
173 }
174 return this;
175 }
176
177
178
179
180
181
182 public CallMethodBuilder usingElementBodyAsArgument()
183 {
184 return withParamCount( 0 );
185 }
186
187
188
189
190 @Override
191 protected CallMethodRule createRule()
192 {
193 CallMethodRule callMethodRule = new CallMethodRule( targetOffset, methodName, paramCount, paramTypes );
194 callMethodRule.setUseExactMatch( useExactMatch );
195 return callMethodRule;
196 }
197
198 }