KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: SetTopRule.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
23 import org.apache.tomcat.util.IntrospectionUtils;
24
25
26 /**
27  * <p>Rule implementation that calls a "set parent" method on the top (child)
28  * object, passing the (top-1) (parent) object as an argument.</p>
29  *
30  * <p>This rule now supports more flexible method matching by default.
31  * It is possible that this may break (some) code
32  * written against release 1.1.1 or earlier.
33  * See {@link #isExactMatch()} for more details.</p>
34  */

35
36 public class SetTopRule extends Rule {
37
38
39     // ----------------------------------------------------------- Constructors
40

41
42     /**
43      * Construct a "set parent" rule with the specified method name. The
44      * "set parent" method's argument type is assumed to be the class of the
45      * parent object.
46      *
47      * @param digester The associated Digester
48      * @param methodName Method name of the "set parent" method to call
49      *
50      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
51      * Use {@link #SetTopRule(String methodName)} instead.
52      */

53     public SetTopRule(Digester digester, String JavaDoc methodName) {
54
55         this(methodName);
56
57     }
58
59
60     /**
61      * Construct a "set parent" rule with the specified method name.
62      *
63      * @param digester The associated Digester
64      * @param methodName Method name of the "set parent" method to call
65      * @param paramType Java class of the "set parent" method's argument
66      * (if you wish to use a primitive type, specify the corresonding
67      * Java wrapper class instead, such as <code>java.lang.Boolean</code>
68      * for a <code>boolean</code> parameter)
69      *
70      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
71      * Use {@link #SetTopRule(String methodName, String paramType)} instead.
72      */

73     public SetTopRule(Digester digester, String JavaDoc methodName,
74                       String JavaDoc paramType) {
75
76         this(methodName, paramType);
77
78     }
79
80     /**
81      * Construct a "set parent" rule with the specified method name. The
82      * "set parent" method's argument type is assumed to be the class of the
83      * parent object.
84      *
85      * @param methodName Method name of the "set parent" method to call
86      */

87     public SetTopRule(String JavaDoc methodName) {
88
89         this(methodName, null);
90
91     }
92
93
94     /**
95      * Construct a "set parent" rule with the specified method name.
96      *
97      * @param methodName Method name of the "set parent" method to call
98      * @param paramType Java class of the "set parent" method's argument
99      * (if you wish to use a primitive type, specify the corresonding
100      * Java wrapper class instead, such as <code>java.lang.Boolean</code>
101      * for a <code>boolean</code> parameter)
102      */

103     public SetTopRule(String JavaDoc methodName,
104                       String JavaDoc paramType) {
105
106         this.methodName = methodName;
107         this.paramType = paramType;
108
109     }
110
111
112     // ----------------------------------------------------- Instance Variables
113

114
115     /**
116      * The method name to call on the child object.
117      */

118     protected String JavaDoc methodName = null;
119
120
121     /**
122      * The Java class name of the parameter type expected by the method.
123      */

124     protected String JavaDoc paramType = null;
125     
126     /**
127      * Should we use exact matching. Default is no.
128      */

129     protected boolean useExactMatch = false;
130
131
132     // --------------------------------------------------------- Public Methods
133

134     /**
135      * <p>Is exact matching being used.</p>
136      *
137      * <p>This rule uses <code>org.apache.commons.beanutils.MethodUtils</code>
138      * to introspect the relevent objects so that the right method can be called.
139      * Originally, <code>MethodUtils.invokeExactMethod</code> was used.
140      * This matches methods very strictly
141      * and so may not find a matching method when one exists.
142      * This is still the behaviour when exact matching is enabled.</p>
143      *
144      * <p>When exact matching is disabled, <code>MethodUtils.invokeMethod</code> is used.
145      * This method finds more methods but is less precise when there are several methods
146      * with correct signatures.
147      * So, if you want to choose an exact signature you might need to enable this property.</p>
148      *
149      * <p>The default setting is to disable exact matches.</p>
150      *
151      * @return true iff exact matching is enabled
152      * @since Digester Release 1.1.1
153      */

154     public boolean isExactMatch() {
155     
156         return useExactMatch;
157     }
158     
159     /**
160      * <p>Set whether exact matching is enabled.</p>
161      *
162      * <p>See {@link #isExactMatch()}.</p>
163      *
164      * @param useExactMatch should this rule use exact method matching
165      * @since Digester Release 1.1.1
166      */

167     public void setExactMatch(boolean useExactMatch) {
168
169         this.useExactMatch = useExactMatch;
170     }
171     
172     /**
173      * Process the end of this element.
174      */

175     public void end() throws Exception JavaDoc {
176
177         // Identify the objects to be used
178
Object JavaDoc child = digester.peek(0);
179         Object JavaDoc parent = digester.peek(1);
180         
181         if (digester.log.isDebugEnabled()) {
182             if (child == null) {
183                 digester.log.debug("[SetTopRule]{" + digester.match +
184                         "} Call [NULL CHILD]." +
185                         methodName + "(" + parent + ")");
186             } else {
187                 digester.log.debug("[SetTopRule]{" + digester.match +
188                         "} Call " + child.getClass().getName() + "." +
189                         methodName + "(" + parent + ")");
190             }
191         }
192
193         // Call the specified method
194
IntrospectionUtils.callMethod1(child, methodName,
195                 parent, paramType, digester.getClassLoader());
196
197     }
198
199
200     /**
201      * Render a printable version of this Rule.
202      */

203     public String JavaDoc toString() {
204
205         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("SetTopRule[");
206         sb.append("methodName=");
207         sb.append(methodName);
208         sb.append(", paramType=");
209         sb.append(paramType);
210         sb.append("]");
211         return (sb.toString());
212
213     }
214
215
216 }
217
Popular Tags