1 package org.apache.commons.digester3;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import static java.lang.String.format;
23
24 import org.xml.sax.Attributes;
25
26 /**
27 * <p>
28 * Rule implementation that saves a parameter containing the <code>Digester</code> matching path for use by a
29 * surrounding <code>CallMethodRule</code>. This Rule is most useful when making extensive use of wildcards in rule
30 * patterns.
31 * </p>
32 *
33 * @since 1.6
34 */
35 public class PathCallParamRule
36 extends Rule
37 {
38
39 // ----------------------------------------------------------- Constructors
40
41 /**
42 * Construct a "call parameter" rule that will save the body text of this element as the parameter value.
43 *
44 * @param paramIndex The zero-relative parameter number
45 */
46 public PathCallParamRule( int paramIndex )
47 {
48 this.paramIndex = paramIndex;
49 }
50
51 // ----------------------------------------------------- Instance Variables
52
53 /**
54 * The zero-relative index of the parameter we are saving.
55 */
56 protected int paramIndex = 0;
57
58 // --------------------------------------------------------- Public Methods
59
60 /**
61 * {@inheritDoc}
62 */
63 @Override
64 public void begin( String namespace, String name, Attributes attributes )
65 throws Exception
66 {
67 String param = getDigester().getMatch();
68
69 if ( param != null )
70 {
71 Object parameters[] = getDigester().peekParams();
72 parameters[paramIndex] = param;
73 }
74 }
75
76 /**
77 * {@inheritDoc}
78 */
79 @Override
80 public String toString()
81 {
82 return format( "PathCallParamRule[paramIndex=%s]", paramIndex );
83 }
84
85 }