001    package 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    import static java.lang.String.format;
023    
024    import org.xml.sax.Attributes;
025    
026    /**
027     * <p>
028     * Rule implementation that saves a parameter containing the <code>Digester</code> matching path for use by a
029     * surrounding <code>CallMethodRule</code>. This Rule is most useful when making extensive use of wildcards in rule
030     * patterns.
031     * </p>
032     * 
033     * @since 1.6
034     */
035    public class PathCallParamRule
036        extends Rule
037    {
038    
039        // ----------------------------------------------------------- Constructors
040    
041        /**
042         * Construct a "call parameter" rule that will save the body text of this element as the parameter value.
043         * 
044         * @param paramIndex The zero-relative parameter number
045         */
046        public PathCallParamRule( int paramIndex )
047        {
048            this.paramIndex = paramIndex;
049        }
050    
051        // ----------------------------------------------------- Instance Variables
052    
053        /**
054         * The zero-relative index of the parameter we are saving.
055         */
056        protected int paramIndex = 0;
057    
058        // --------------------------------------------------------- Public Methods
059    
060        /**
061         * {@inheritDoc}
062         */
063        @Override
064        public void begin( String namespace, String name, Attributes attributes )
065            throws Exception
066        {
067            String param = getDigester().getMatch();
068    
069            if ( param != null )
070            {
071                Object parameters[] = getDigester().peekParams();
072                parameters[paramIndex] = param;
073            }
074        }
075    
076        /**
077         * {@inheritDoc}
078         */
079        @Override
080        public String toString()
081        {
082            return format( "PathCallParamRule[paramIndex=%s]", paramIndex );
083        }
084    
085    }