View Javadoc

1   package org.apache.commons.digester3.binder;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Builder chained when invoking {@link LinkedRuleBuilder#callMethod(String)}.
30   *
31   * @since 3.0
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       * Sets the location of the target object.
59       *
60       * Positive numbers are relative to the top of the digester object stack.
61       * Negative numbers are relative to the bottom of the stack. Zero implies the top object on the stack.
62       *
63       * @param targetOffset location of the target object.
64       * @return this builder instance
65       */
66      public CallMethodBuilder withTargetOffset( int targetOffset )
67      {
68          this.targetOffset = targetOffset;
69          return this;
70      }
71  
72      /**
73       * Sets the Java class names that represent the parameter types of the method arguments.
74       *
75       * If you wish to use a primitive type, specify the corresonding Java wrapper class instead,
76       * such as {@code java.lang.Boolean.TYPE} for a {@code boolean} parameter.
77       *
78       * @param paramTypeNames The Java classes names that represent the parameter types of the method arguments
79       * @return this builder instance
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      * Sets the Java classes that represent the parameter types of the method arguments.
107      *
108      * If you wish to use a primitive type, specify the corresonding Java wrapper class instead,
109      * such as {@code java.lang.Boolean.TYPE} for a {@code boolean} parameter.
110      *
111      * @param paramTypes The Java classes that represent the parameter types of the method arguments
112      * @return this builder instance
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      * Should <code>MethodUtils.invokeExactMethod</code> be used for the reflection.
132      *
133      * @param useExactMatch Flag to mark exact matching or not
134      * @return this builder instance
135      */
136     public CallMethodBuilder useExactMatch( boolean useExactMatch )
137     {
138         this.useExactMatch = useExactMatch;
139         return this;
140     }
141 
142     /**
143      * The number of parameters to collect, or zero for a single argument from the body of this element.
144      *
145      * @param paramCount The number of parameters to collect, or zero for a single argument
146      *        from the body of this element.
147      * @return this builder instance
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      * Prepare the {@link CallMethodRule} to be invoked using the matching element body as argument.
179      *
180      * @return this builder instance
181      */
182     public CallMethodBuilder usingElementBodyAsArgument()
183     {
184         return withParamCount( 0 );
185     }
186 
187     /**
188      * {@inheritDoc}
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 }