You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/05/14 17:06:22 UTC

svn commit: r1103122 - /commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/binder/CallMethodBuilder.java

Author: simonetripodi
Date: Sat May 14 15:06:22 2011
New Revision: 1103122

URL: http://svn.apache.org/viewvc?rev=1103122&view=rev
Log:
first checkin of CallMethodBuilder class

Added:
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/binder/CallMethodBuilder.java   (with props)

Added: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/binder/CallMethodBuilder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/binder/CallMethodBuilder.java?rev=1103122&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/binder/CallMethodBuilder.java (added)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/binder/CallMethodBuilder.java Sat May 14 15:06:22 2011
@@ -0,0 +1,187 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.digester3.binder;
+
+import java.util.Arrays;
+
+import org.apache.commons.digester3.CallMethodRule;
+
+/**
+ * Builder chained when invoking {@link LinkedRuleBuilder#callMethod(String)}.
+ */
+public final class CallMethodBuilder
+    extends AbstractBackToLinkedRuleBuilder<CallMethodRule>
+{
+
+    private final String methodName;
+
+    private final ClassLoader classLoader;
+
+    private int targetOffset;
+
+    private int paramCount = 0;
+
+    private Class<?>[] paramTypes = new Class<?>[]{};
+
+    private boolean useExactMatch = false;
+
+    public CallMethodBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder,
+                              LinkedRuleBuilder mainBuilder, String methodName, ClassLoader classLoader )
+    {
+        super( keyPattern, namespaceURI, mainBinder, mainBuilder );
+        this.methodName = methodName;
+        this.classLoader = classLoader;
+    }
+
+    /**
+     * Sets the location of the target object.
+     *
+     * Positive numbers are relative to the top of the digester object stack.
+     * Negative numbers are relative to the bottom of the stack. Zero implies the top object on the stack.
+     *
+     * @param targetOffset location of the target object.
+     * @return this builder instance
+     */
+    public CallMethodBuilder withTargetOffset( int targetOffset )
+    {
+        this.targetOffset = targetOffset;
+        return this;
+    }
+
+    /**
+     * Sets the Java class names that represent the parameter types of the method arguments.
+     *
+     * If you wish to use a primitive type, specify the corresonding Java wrapper class instead,
+     * such as {@code java.lang.Boolean.TYPE} for a {@code boolean} parameter.
+     *
+     * @param The Java classe names that represent the parameter types of the method arguments
+     * @return this builder instance
+     */
+    public CallMethodBuilder withParamTypes( String... paramTypeNames )
+    {
+        if ( paramTypeNames != null )
+        {
+            this.paramTypes = new Class[paramTypeNames.length];
+            for ( int i = 0; i < paramTypeNames.length; i++ )
+            {
+                try
+                {
+                    this.paramTypes[i] = classLoader.loadClass( paramTypeNames[i] );
+                }
+                catch ( ClassNotFoundException e )
+                {
+                    this.reportError( String.format( "callMethod(\"%s\").withParamTypes(%s)", this.methodName,
+                                                     Arrays.toString( paramTypeNames ) ),
+                                      String.format( "class '%s' cannot be load", paramTypeNames[i] ) );
+                }
+            }
+        }
+
+        return this;
+    }
+
+    /**
+     * Sets the Java classes that represent the parameter types of the method arguments.
+     *
+     * If you wish to use a primitive type, specify the corresonding Java wrapper class instead,
+     * such as {@code java.lang.Boolean.TYPE} for a {@code boolean} parameter.
+     *
+     * @param paramTypes The Java classes that represent the parameter types of the method arguments
+     * @return this builder instance
+     */
+    public CallMethodBuilder withParamTypes( Class<?>... paramTypes )
+    {
+        this.paramTypes = paramTypes;
+
+        if ( paramTypes != null )
+        {
+            this.paramCount = paramTypes.length;
+        }
+
+        return this;
+    }
+
+    /**
+     * Should <code>MethodUtils.invokeExactMethod</code> be used for the reflection.
+     *
+     * @param useExactMatch Flag to mark exact matching or not
+     * @return this builder instance
+     */
+    public CallMethodBuilder useExactMatch( boolean useExactMatch )
+    {
+        this.useExactMatch = useExactMatch;
+        return this;
+    }
+
+    /**
+     * The number of parameters to collect, or zero for a single argument from the body of this element.
+     *
+     * @param paramCount The number of parameters to collect, or zero for a single argument
+     *        from the body of this element.
+     * @return this builder instance
+     */
+    public CallMethodBuilder withParamCount( int paramCount )
+    {
+        if ( paramCount < 0 )
+        {
+            this.reportError( String.format( "callMethod(\"%s\").withParamCount(int)", this.methodName ),
+                              "negative parameters counter not allowed" );
+        }
+
+        this.paramCount = paramCount;
+
+        if ( this.paramCount == 0 )
+        {
+            if ( this.paramTypes == null || this.paramTypes.length != 1 )
+            {
+                this.paramTypes = new Class<?>[] { String.class };
+            }
+        }
+        else
+        {
+            this.paramTypes = new Class<?>[this.paramCount];
+            for ( int i = 0; i < paramTypes.length; i++ )
+            {
+                this.paramTypes[i] = String.class;
+            }
+        }
+        return this;
+    }
+
+    /**
+     * Prepare the {@link CallMethodRule} to be invoked using the matching element body as argument.
+     *
+     * @return this builder instance
+     */
+    public CallMethodBuilder usingElementBodyAsArgument()
+    {
+        return withParamCount( 0 );
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected CallMethodRule createRule()
+    {
+        CallMethodRule callMethodRule = new CallMethodRule( targetOffset, methodName, paramCount, paramTypes );
+        callMethodRule.setUseExactMatch( useExactMatch );
+        return callMethodRule;
+    }
+
+}

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/binder/CallMethodBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/binder/CallMethodBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/binder/CallMethodBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain