KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > digester > ObjectParamRule


1 /* $Id: ObjectParamRule.java 467222 2006-10-24 03:17:11Z markt $
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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

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

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

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

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

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

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

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

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

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

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