001    /* $Id: PathCallParamRule.java 471661 2006-11-06 08:09:25Z skitching $
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     * 
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */ 
018    
019    
020    package org.apache.commons.digester;
021    
022    
023    import org.xml.sax.Attributes;
024    
025    /**
026     * <p>Rule implementation that saves a parameter containing the 
027     * <code>Digester</code> matching path for use by a surrounding 
028     * <code>CallMethodRule</code>. This Rule is most useful when making 
029     * extensive use of wildcards in rule patterns.</p>
030     *
031     * @since 1.6
032     */
033    
034    public class PathCallParamRule extends Rule {
035    
036        // ----------------------------------------------------------- Constructors
037    
038        /**
039         * Construct a "call parameter" rule that will save the body text of this
040         * element as the parameter value.
041         *
042         * @param paramIndex The zero-relative parameter number
043         */
044        public PathCallParamRule(int paramIndex) {
045    
046            this.paramIndex = paramIndex;
047    
048        }
049     
050        // ----------------------------------------------------- Instance Variables
051    
052        /**
053         * The zero-relative index of the parameter we are saving.
054         */
055        protected int paramIndex = 0;
056    
057        // --------------------------------------------------------- Public Methods
058    
059    
060        /**
061         * Process the start of this element.
062         *
063         * @param namespace the namespace URI of the matching element, or an 
064         *   empty string if the parser is not namespace aware or the element has
065         *   no namespace
066         * @param name the local name if the parser is namespace aware, or just 
067         *   the element name otherwise
068         * @param attributes The attribute list for this element
069    
070         */
071        public void begin(String namespace, String name, Attributes attributes) throws Exception {
072    
073            String param = getDigester().getMatch();
074            
075            if(param != null) {
076                Object parameters[] = (Object[]) digester.peekParams();
077                parameters[paramIndex] = param;
078            }
079            
080        }
081    
082        /**
083         * Render a printable version of this Rule.
084         */
085        public String toString() {
086    
087            StringBuffer sb = new StringBuffer("PathCallParamRule[");
088            sb.append("paramIndex=");
089            sb.append(paramIndex);
090            sb.append("]");
091            return (sb.toString());
092    
093        }
094    }