View Javadoc

1   package org.apache.commons.digester3.binder;
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 org.apache.commons.digester3.CallParamRule;
23  
24  /**
25   * Builder chained when invoking {@link LinkedRuleBuilder#callParam()}.
26   *
27   * @since 3.0
28   */
29  public final class CallParamBuilder
30      extends AbstractBackToLinkedRuleBuilder<CallParamRule>
31  {
32  
33      private int paramIndex = 0;
34  
35      private int stackIndex = 0;
36  
37      private boolean fromStack = false;
38  
39      private String attributeName;
40  
41      CallParamBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder, LinkedRuleBuilder mainBuilder )
42      {
43          super( keyPattern, namespaceURI, mainBinder, mainBuilder );
44      }
45  
46      /**
47       * Sets the zero-relative parameter number.
48       *
49       * @param paramIndex The zero-relative parameter number
50       * @return this builder instance
51       */
52      public CallParamBuilder ofIndex( int paramIndex )
53      {
54          if ( paramIndex < 0 )
55          {
56              reportError( "callParam().ofIndex( int )", "negative index argument not allowed" );
57          }
58  
59          this.paramIndex = paramIndex;
60          return this;
61      }
62  
63      /**
64       * Sets the attribute from which to save the parameter value.
65       *
66       * @param attributeName The attribute from which to save the parameter value
67       * @return this builder instance
68       */
69      public CallParamBuilder fromAttribute( /* @Nullable */String attributeName )
70      {
71          this.attributeName = attributeName;
72          return this;
73      }
74  
75      /**
76       * Flags the parameter to be set from the stack.
77       *
78       * @param fromStack the parameter flag to be set from the stack
79       * @return this builder instance
80       */
81      public CallParamBuilder fromStack( boolean fromStack )
82      {
83          this.fromStack = fromStack;
84          return this;
85      }
86  
87      /**
88       * Sets the position of the object from the top of the stack.
89       *
90       * @param stackIndex The position of the object from the top of the stack
91       * @return this builder instance
92       */
93      public CallParamBuilder withStackIndex( int stackIndex )
94      {
95          this.stackIndex = stackIndex;
96          this.fromStack = true;
97          return this;
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     protected CallParamRule createRule()
105     {
106         CallParamRule rule;
107 
108         if ( fromStack )
109         {
110             rule = new CallParamRule( paramIndex, stackIndex );
111         }
112         else
113         {
114             rule = new CallParamRule( paramIndex );
115         }
116 
117         rule.setAttributeName( attributeName );
118 
119         return rule;
120     }
121 
122 }