KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > ObjectParamRule


1 /* $Id: ObjectParamRule.java 178355 2005-05-25 03:58:11Z skitching $
2  *
3  * Copyright 2002-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.commons.digester;
20
21 import org.xml.sax.Attributes JavaDoc;
22
23 /**
24  * <p>Rule implementation that saves a parameter for use by a surrounding
25  * <code>CallMethodRule<code>.</p>
26  *
27  * <p>This parameter may be:
28  * <ul>
29  * <li>an arbitrary Object defined programatically, assigned when the element
30  * pattern associated with the Rule is matched. See
31  * {@link #ObjectParamRule(int paramIndex, Object param)}.
32  * <li>an arbitrary Object defined programatically, assigned if the element
33  * pattern AND specified attribute name are matched. See
34  * {@link #ObjectParamRule(int paramIndex, String attributeName, Object param)}.
35  * </ul>
36  * </p>
37  *
38  * @since 1.4
39  */

40
41 public class ObjectParamRule extends Rule {
42     // ----------------------------------------------------------- Constructors
43
/**
44      * Construct a "call parameter" rule that will save the given Object as
45      * the parameter value.
46      *
47      * @param paramIndex The zero-relative parameter number
48      * @param param the parameter to pass along
49      */

50     public ObjectParamRule(int paramIndex, Object JavaDoc param) {
51         this(paramIndex, null, param);
52     }
53
54
55     /**
56      * Construct a "call parameter" rule that will save the given Object as
57      * the parameter value, provided that the specified attribute exists.
58      *
59      * @param paramIndex The zero-relative parameter number
60      * @param attributeName The name of the attribute to match
61      * @param param the parameter to pass along
62      */

63     public ObjectParamRule(int paramIndex, String JavaDoc attributeName, Object JavaDoc param) {
64         this.paramIndex = paramIndex;
65         this.attributeName = attributeName;
66         this.param = param;
67     }
68
69
70     // ----------------------------------------------------- Instance Variables
71

72     /**
73      * The attribute which we are attempting to match
74      */

75     protected String JavaDoc attributeName = null;
76
77     /**
78      * The zero-relative index of the parameter we are saving.
79      */

80     protected int paramIndex = 0;
81
82     /**
83      * The parameter we wish to pass to the method call
84      */

85     protected Object JavaDoc param = null;
86
87
88     // --------------------------------------------------------- Public Methods
89

90     /**
91      * Process the start of this element.
92      *
93      * @param attributes The attribute list for this element
94      */

95     public void begin(String JavaDoc namespace, String JavaDoc name,
96                       Attributes JavaDoc attributes) throws Exception JavaDoc {
97         Object JavaDoc anAttribute = null;
98         Object JavaDoc parameters[] = (Object JavaDoc[]) digester.peekParams();
99
100         if (attributeName != null) {
101             anAttribute = attributes.getValue(attributeName);
102             if(anAttribute != null) {
103                 parameters[paramIndex] = param;
104             }
105             // note -- if attributeName != null and anAttribute == null, this rule
106
// will pass null as its parameter!
107
}else{
108             parameters[paramIndex] = param;
109         }
110     }
111
112     /**
113      * Render a printable version of this Rule.
114      */

115     public String JavaDoc toString() {
116         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("ObjectParamRule[");
117         sb.append("paramIndex=");
118         sb.append(paramIndex);
119         sb.append(", attributeName=");
120         sb.append(attributeName);
121         sb.append(", param=");
122         sb.append(param);
123         sb.append("]");
124         return (sb.toString());
125     }
126 }
127
Popular Tags