001package org.apache.commons.digester3.binder;
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
022import org.apache.commons.digester3.CallParamRule;
023
024/**
025 * Builder chained when invoking {@link LinkedRuleBuilder#callParam()}.
026 *
027 * @since 3.0
028 */
029public final class CallParamBuilder
030    extends AbstractBackToLinkedRuleBuilder<CallParamRule>
031{
032
033    private int paramIndex = 0;
034
035    private int stackIndex = 0;
036
037    private boolean fromStack = false;
038
039    private String attributeName;
040
041    CallParamBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder, LinkedRuleBuilder mainBuilder )
042    {
043        super( keyPattern, namespaceURI, mainBinder, mainBuilder );
044    }
045
046    /**
047     * Sets the zero-relative parameter number.
048     *
049     * @param paramIndex The zero-relative parameter number
050     * @return this builder instance
051     */
052    public CallParamBuilder ofIndex( int paramIndex )
053    {
054        if ( paramIndex < 0 )
055        {
056            reportError( "callParam().ofIndex( int )", "negative index argument not allowed" );
057        }
058
059        this.paramIndex = paramIndex;
060        return this;
061    }
062
063    /**
064     * Sets the attribute from which to save the parameter value.
065     *
066     * @param attributeName The attribute from which to save the parameter value
067     * @return this builder instance
068     */
069    public CallParamBuilder fromAttribute( /* @Nullable */String attributeName )
070    {
071        this.attributeName = attributeName;
072        return this;
073    }
074
075    /**
076     * Flags the parameter to be set from the stack.
077     *
078     * @param fromStack the parameter flag to be set from the stack
079     * @return this builder instance
080     */
081    public CallParamBuilder fromStack( boolean fromStack )
082    {
083        this.fromStack = fromStack;
084        return this;
085    }
086
087    /**
088     * Sets the position of the object from the top of the stack.
089     *
090     * @param stackIndex The position of the object from the top of the stack
091     * @return this builder instance
092     */
093    public CallParamBuilder withStackIndex( int stackIndex )
094    {
095        this.stackIndex = stackIndex;
096        this.fromStack = true;
097        return this;
098    }
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    protected CallParamRule createRule()
105    {
106        CallParamRule rule;
107
108        if ( fromStack )
109        {
110            rule = new CallParamRule( paramIndex, stackIndex );
111        }
112        else
113        {
114            rule = new CallParamRule( paramIndex );
115        }
116
117        rule.setAttributeName( attributeName );
118
119        return rule;
120    }
121
122}