1 /*
2 * Copyright (c) 2003, Henri Yandell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the
7 * following conditions are met:
8 *
9 * + Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * + Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * + Neither the name of Genjava-Core nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 package com.generationjava.beans;
33
34 import java.util.List;
35 import java.util.Enumeration;
36 import java.util.Iterator;
37
38 import org.apache.commons.collections.CollectionUtils;
39
40 /***
41 * Helps us deal with such wonderful things as array notation
42 * and object notation. Array notation may use associative array notation.
43 *
44 * @date 2001-06-14
45 */
46 abstract public class AbstractBeanViewer {
47
48 /***
49 * Get the property of myBean with the name of the key's toString.
50 *
51 * The key may use dotted notation to imply a property in a property
52 * and array notation to get the nth property in a list of propertys.
53 */
54 public Object get(Object key, Object myBean) {
55 if( key == null ) {
56 return null;
57 }
58
59 if(myBean == null) {
60 return null;
61 }
62
63 String name = key.toString();
64
65 BeanViewRuntime runtime = new BeanViewRuntime();
66 runtime.pushObject(myBean);
67
68 int idx = 0;
69 Object obj = myBean;
70 while( (idx = name.indexOf('.', idx)) != -1) {
71 String pre = name.substring(0, idx);
72 name = name.substring(idx+1);
73 if(obj != null) {
74 obj = handleGet(runtime, obj, pre);
75 runtime.pushObject(obj);
76 } else {
77 return null;
78 }
79 }
80
81 return handleGet(runtime, obj, name);
82 }
83
84 /***
85 * Give a bean, and a String name, get the property with that name
86 * from the bean.
87 * Does not handle dot notation, but does handle array notation
88 * and associative array notation.
89 */
90 public Object handleGet(BeanViewRuntime runtime, Object bean, String name) {
91 // deal with array
92 int i = name.indexOf('[');
93 Object index = null;
94 if(i != -1) {
95 String pre = name.substring(i+1);
96 if(pre.endsWith("]")) {
97 pre = pre.substring(0, pre.length() -1);
98 }
99 try {
100 index = Integer.valueOf(pre);
101 } catch(NumberFormatException nfe) {
102 index = pre;
103 }
104 name = name.substring(0,i);
105 }
106
107 // shouldn't be null
108 if(name.equals("")) {
109 return CollectionUtils.index(bean, index);
110 } else {
111 return invokeGet( runtime, bean, name, index );
112 }
113 }
114
115 /***
116 * Get a value from a bean with the specified name.
117 * If the bean is indexed, an int index is passed. Else it is -1.
118 * It is recommmended that implementors of this method use the
119 * index method to handle the array notation.
120 */
121 abstract public Object invokeGet(BeanViewRuntime runtime, Object bean, String name, Object idx);
122
123 public Object set(Object key, Object bean, Object value) {
124 if(key == null) {
125 return null;
126 }
127
128 if(bean == null) {
129 return null;
130 }
131
132 String name = key.toString();
133
134 int idx = name.lastIndexOf(".");
135 if(idx != -1) {
136 String pre = name.substring(0, idx);
137 String post = name.substring(idx+1);
138 Object parent = get(pre, bean);
139 // TODO: remove [] notation from name.
140 // equally, need to pass idx as an Object, so implies that
141 // invokeGet shuld have Object idx and so should index.
142 bean = parent;
143 name = post;
144 }
145 // call invokeGet to get the current value, then
146 // call invokeSet
147 invokeSet(null, bean, name, null, value);
148
149 // return old value
150 return null;
151 }
152
153 abstract public void invokeSet(BeanViewRuntime runtime, Object bean, String name, Object idx, Object value);
154
155 }
This page was automatically generated by Maven