KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > runtime > parser > node > PropertyExecutor


1 package org.apache.velocity.runtime.parser.node;
2 /*
3  * Copyright 2000-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 import java.lang.reflect.InvocationTargetException JavaDoc;
19
20 import org.apache.velocity.util.introspection.Introspector;
21
22 import org.apache.velocity.runtime.RuntimeLogger;
23
24 /**
25  * Returned the value of object property when executed.
26  */

27 public class PropertyExecutor extends AbstractExecutor
28 {
29     protected Introspector introspector = null;
30
31     protected String JavaDoc methodUsed = null;
32
33     public PropertyExecutor(RuntimeLogger r, Introspector ispctr,
34                             Class JavaDoc clazz, String JavaDoc property)
35     {
36         rlog = r;
37         introspector = ispctr;
38
39         discover(clazz, property);
40     }
41
42     protected void discover(Class JavaDoc clazz, String JavaDoc property)
43     {
44         /*
45          * this is gross and linear, but it keeps it straightforward.
46          */

47
48         try
49         {
50             char c;
51             StringBuffer JavaDoc sb;
52
53             Object JavaDoc[] params = { };
54
55             /*
56              * start with get<property>
57              * this leaves the property name
58              * as is...
59              */

60             sb = new StringBuffer JavaDoc("get");
61             sb.append(property);
62
63             methodUsed = sb.toString();
64
65             method = introspector.getMethod(clazz, methodUsed, params);
66              
67             if (method != null)
68                 return;
69         
70             /*
71              * now the convenience, flip the 1st character
72              */

73          
74             sb = new StringBuffer JavaDoc("get");
75             sb.append(property);
76
77             c = sb.charAt(3);
78
79             if (Character.isLowerCase(c))
80             {
81                 sb.setCharAt(3, Character.toUpperCase(c));
82             }
83             else
84             {
85                 sb.setCharAt(3, Character.toLowerCase(c));
86             }
87
88             methodUsed = sb.toString();
89             method = introspector.getMethod(clazz, methodUsed, params);
90
91             if (method != null)
92                 return;
93             
94         }
95         catch(Exception JavaDoc e)
96         {
97             rlog.error("PROGRAMMER ERROR : PropertyExector() : " + e );
98         }
99     }
100
101
102     /**
103      * Execute method against context.
104      */

105     public Object JavaDoc execute(Object JavaDoc o)
106         throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
107     {
108         if (method == null)
109             return null;
110
111         return method.invoke(o, null);
112     }
113 }
114
115
116
Popular Tags