View Javadoc

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 for use by a surrounding <code>CallMethodRule<code>.
29   * </p>
30   * <p>
31   * This parameter may be:
32   * <ul>
33   * <li>an arbitrary Object defined programatically, assigned when the element pattern associated with the Rule is
34   * matched. See {@link #ObjectParamRule(int paramIndex, Object param)}.
35   * <li>an arbitrary Object defined programatically, assigned if the element pattern AND specified attribute name are
36   * matched. See {@link #ObjectParamRule(int paramIndex, String attributeName, Object param)}.
37   * </ul>
38   * </p>
39   * 
40   * @since 1.4
41   */
42  public class ObjectParamRule
43      extends Rule
44  {
45      // ----------------------------------------------------------- Constructors
46      /**
47       * Construct a "call parameter" rule that will save the given Object as the parameter value.
48       * 
49       * @param paramIndex The zero-relative parameter number
50       * @param param the parameter to pass along
51       */
52      public ObjectParamRule( int paramIndex, Object param )
53      {
54          this( paramIndex, null, param );
55      }
56  
57      /**
58       * Construct a "call parameter" rule that will save the given Object as the parameter value, provided that the
59       * specified attribute exists.
60       * 
61       * @param paramIndex The zero-relative parameter number
62       * @param attributeName The name of the attribute to match
63       * @param param the parameter to pass along
64       */
65      public ObjectParamRule( int paramIndex, String attributeName, Object param )
66      {
67          this.paramIndex = paramIndex;
68          this.attributeName = attributeName;
69          this.param = param;
70      }
71  
72      // ----------------------------------------------------- Instance Variables
73  
74      /**
75       * The attribute which we are attempting to match
76       */
77      protected String attributeName = null;
78  
79      /**
80       * The zero-relative index of the parameter we are saving.
81       */
82      protected int paramIndex = 0;
83  
84      /**
85       * The parameter we wish to pass to the method call
86       */
87      protected Object param = null;
88  
89      // --------------------------------------------------------- Public Methods
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public void begin( String namespace, String name, Attributes attributes )
96          throws Exception
97      {
98          Object anAttribute = null;
99          Object parameters[] = getDigester().peekParams();
100 
101         if ( attributeName != null )
102         {
103             anAttribute = attributes.getValue( attributeName );
104             if ( anAttribute != null )
105             {
106                 parameters[paramIndex] = param;
107             }
108             // note -- if attributeName != null and anAttribute == null, this rule
109             // will pass null as its parameter!
110         }
111         else
112         {
113             parameters[paramIndex] = param;
114         }
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public String toString()
122     {
123         return format( "ObjectParamRule[paramIndex=%s, attributeName=%s, param=%s]", paramIndex, attributeName, param );
124     }
125 
126 }