001package org.apache.commons.digester3;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022
023/**
024 * <p>
025 * Rule implementation that calls a method on the (top-1) (parent) object, passing the top object (child) as an
026 * argument. It is commonly used to establish parent-child relationships.
027 * </p>
028 * <p>
029 * This rule now supports more flexible method matching by default. It is possible that this may break (some) code
030 * written against release 1.1.1 or earlier. See {@link #isExactMatch()} for more details.
031 * </p>
032 * <p>
033 * Note that while CallMethodRule uses commons-beanutils' data-conversion functionality (ConvertUtils class) to convert
034 * parameter values into the appropriate type for the parameter to the called method, this rule does not. Needing to use
035 * ConvertUtils functionality when building parent-child relationships is expected to be very rare; however if you do
036 * need this then instead of using this rule, create a CallMethodRule specifying targetOffset of 1 in the constructor.
037 * </p>
038 */
039public class SetNextRule
040    extends AbstractMethodRule
041{
042
043    // ----------------------------------------------------------- Constructors
044
045    /**
046     * Construct a "set next" rule with the specified method name. The method's argument type is assumed to be the class
047     * of the child object.
048     * 
049     * @param methodName Method name of the parent method to call
050     */
051    public SetNextRule( String methodName )
052    {
053        super( methodName );
054    }
055
056    /**
057     * Construct a "set next" rule with the specified method name.
058     * 
059     * @param methodName Method name of the parent method to call
060     * @param paramType Java class name of the parent method's argument (if you wish to use a primitive type,
061     *                  specify the corresonding Java wrapper class instead, such as <code>java.lang.Boolean</code>
062     *                  for a <code>boolean</code> parameter)
063     */
064    public SetNextRule( String methodName, String paramType )
065    {
066        super( methodName, paramType );
067    }
068
069    /**
070     * Construct a "set next" rule with the specified method name.
071     * 
072     * @param methodName Method name of the parent method to call
073     * @param paramType Java class of the parent method's argument (if you wish to use a primitive type, specify the
074     *                  corresonding Java wrapper class instead, such as <code>java.lang.Boolean</code> for a
075     *                  <code>boolean</code> parameter)
076     */
077    public SetNextRule( String methodName, Class<?> paramType )
078    {
079        super( methodName, paramType );
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    protected Object getChild()
087    {
088        return getDigester().peek( 0 );
089    }
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    protected Object getParent()
096    {
097        return getDigester().peek( 1 );
098    }
099
100}