001/* $Id: PathCallParamRule.java 992060 2010-09-02 19:09:47Z simonetripodi $
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
020package org.apache.commons.digester;
021
022
023import 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
034public 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    @Override
072    public void begin(String namespace, String name, Attributes attributes) throws Exception {
073
074        String param = getDigester().getMatch();
075        
076        if(param != null) {
077            Object parameters[] = (Object[]) digester.peekParams();
078            parameters[paramIndex] = param;
079        }
080        
081    }
082
083    /**
084     * Render a printable version of this Rule.
085     */
086    @Override
087    public String toString() {
088
089        StringBuffer sb = new StringBuffer("PathCallParamRule[");
090        sb.append("paramIndex=");
091        sb.append(paramIndex);
092        sb.append("]");
093        return (sb.toString());
094
095    }
096}