KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SetTextSegmentRule


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

16
17 import org.apache.commons.beanutils.MethodUtils;
18 import org.apache.commons.digester.Rule;
19
20 /**
21  * When a text segment is discovered, it calls a specific method on the top
22  * object on the stack.
23  */

24
25 public class SetTextSegmentRule extends Rule implements TextSegmentHandler {
26
27
28     // ----------------------------------------------------------- Constructors
29

30     public SetTextSegmentRule(String JavaDoc methodName) {
31         this.methodName = methodName;
32     }
33
34     // ----------------------------------------------------- Instance Variables
35

36
37     /**
38      * The method name to call on the parent object.
39      */

40     protected String JavaDoc methodName = null;
41
42     // --------------------------------------------------------- Public Methods
43

44     /**
45      * Process the end of this element.
46      */

47     public void textSegment(String JavaDoc text) throws Exception JavaDoc {
48
49         Object JavaDoc target = digester.peek(0);
50
51         // Call the specified method
52
Class JavaDoc paramTypes[] = new Class JavaDoc[] {String JavaDoc.class};
53         MethodUtils.invokeMethod(target, methodName,
54             new Object JavaDoc[]{ text }, paramTypes);
55     }
56 }
57
Popular Tags