gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynObject.java @ 1119
History | View | Annotate | Download (10.6 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.dynobject.impl; |
25 |
|
26 |
import java.util.HashMap; |
27 |
import java.util.Map; |
28 |
import org.gvsig.tools.ToolsLocator; |
29 |
|
30 |
import org.gvsig.tools.dataTypes.CoercionException; |
31 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
32 |
import org.gvsig.tools.dynobject.DynClass; |
33 |
import org.gvsig.tools.dynobject.DynField; |
34 |
import org.gvsig.tools.dynobject.DynMethod; |
35 |
import org.gvsig.tools.dynobject.DynObject; |
36 |
import org.gvsig.tools.dynobject.DynObjectManager; |
37 |
import org.gvsig.tools.dynobject.DynObjectRuntimeException; |
38 |
import org.gvsig.tools.dynobject.DynStruct; |
39 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
40 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
41 |
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException; |
42 |
import org.gvsig.tools.dynobject.impl.DefaultDynClass.FieldAndIndex; |
43 |
|
44 |
public class DefaultDynObject implements DelegatedDynObject { |
45 |
|
46 |
protected DynClass dynClass;
|
47 |
protected Map values; |
48 |
protected DynObject[] delegateds; |
49 |
|
50 |
public DefaultDynObject(DynStruct dynClass) {
|
51 |
this.dynClass = (DynClass) dynClass;
|
52 |
this.delegateds = null; |
53 |
this.values = createValues(null); |
54 |
} |
55 |
|
56 |
private Map createValues(Map oldValues) { |
57 |
HashMap extended = new HashMap(); |
58 |
if ( oldValues != null ) { |
59 |
extended.putAll(oldValues); |
60 |
} |
61 |
return extended;
|
62 |
} |
63 |
|
64 |
public void implement(DynClass dynClass) { |
65 |
this.dynClass = (DefaultDynClass) ((DefaultDynObjectManager) ((DefaultDynClass) dynClass)
|
66 |
.getManager()).get(new DynClass[]{this.dynClass, dynClass}); |
67 |
this.values = createValues(this.values); |
68 |
} |
69 |
|
70 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
71 |
boolean defined = false; |
72 |
Object defaultValue = null; |
73 |
name = name.toLowerCase(); |
74 |
|
75 |
|
76 |
DynField field = dynClass.getDynField(name); |
77 |
if( field != null ) { |
78 |
if( values.containsKey(name) ) {
|
79 |
return values.get(name);
|
80 |
} |
81 |
defined = true;
|
82 |
defaultValue = field.getDefaultValue(); |
83 |
} |
84 |
//
|
85 |
// FieldAndIndex fieldAndIndex = dynClass.getDynFieldAndIndex(name);
|
86 |
// if ( fieldAndIndex != null ) {
|
87 |
// if ( values.containsKey(name) ) {
|
88 |
// return values.get(name);
|
89 |
// }
|
90 |
// defined = true;
|
91 |
// defaultValue = fieldAndIndex.getDynField().getDefaultValue();
|
92 |
// }
|
93 |
if ( delegateds != null ) { |
94 |
for ( int i = 0; i < delegateds.length; i++ ) { |
95 |
DynObject dynObj = delegateds[i]; |
96 |
try {
|
97 |
if ( dynObj.hasDynValue(name) ) {
|
98 |
return dynObj.getDynValue(name);
|
99 |
} else {
|
100 |
defined = true;
|
101 |
defaultValue = dynObj.getDynValue(name); |
102 |
} |
103 |
} catch (DynFieldNotFoundException ex) {
|
104 |
; |
105 |
} |
106 |
} |
107 |
} |
108 |
if ( defined ) {
|
109 |
return defaultValue;
|
110 |
} |
111 |
throw new DynFieldNotFoundException(name, dynClass.getName()); |
112 |
} |
113 |
|
114 |
public void setDynValue(String name, Object value) |
115 |
throws DynFieldNotFoundException {
|
116 |
name = name.toLowerCase(); |
117 |
|
118 |
if ( this.dynClass.getDynField(name) == null ) { |
119 |
throw new DynFieldNotFoundException(name, this.getDynClass() |
120 |
.getName()); |
121 |
} |
122 |
|
123 |
try {
|
124 |
values.put(name, this.dynClass.getDynField(name).coerce(value));
|
125 |
} catch (CoercionException e) {
|
126 |
throw new CoerceValueException(this.dynClass, name, value, e); |
127 |
} |
128 |
} |
129 |
|
130 |
public class CoerceValueException extends DynObjectRuntimeException { |
131 |
|
132 |
/**
|
133 |
*
|
134 |
*/
|
135 |
private static final long serialVersionUID = 8974502669097158348L; |
136 |
|
137 |
public CoerceValueException(DynStruct dynStruct, String fieldName, |
138 |
Object value, Throwable cause) { |
139 |
super(
|
140 |
"Can't convert value %(value) for field %(field) of class %(class).",
|
141 |
cause, |
142 |
"Cant_convert_value_XvalueX_for_field_XfieldX_of_class_XclassX",
|
143 |
serialVersionUID); |
144 |
setValue("field", fieldName);
|
145 |
setValue("class", dynStruct.getFullName());
|
146 |
try {
|
147 |
setValue("value", value.toString());
|
148 |
} catch (Exception e1) { |
149 |
setValue("value", "???"); |
150 |
} |
151 |
} |
152 |
|
153 |
} |
154 |
|
155 |
public boolean instanceOf(DynClass dynClass) { |
156 |
return dynClass.isInstance(this); |
157 |
} |
158 |
|
159 |
public DynClass getDynClass() {
|
160 |
return this.dynClass; |
161 |
} |
162 |
|
163 |
public boolean hasDynValue(String name) throws DynFieldNotFoundException { |
164 |
boolean defined = false; |
165 |
name = name.toLowerCase(); |
166 |
|
167 |
DynField field = dynClass.getDynField(name); |
168 |
if( field!=null ) { |
169 |
if ( this.values.containsKey(name) ) { |
170 |
return true; |
171 |
} |
172 |
defined = true;
|
173 |
} |
174 |
|
175 |
// int index = dynClass.getFieldIndex(name); xxxx
|
176 |
// if ( index >= 0 ) {
|
177 |
// if ( this.values.containsKey(name) ) {
|
178 |
// return true;
|
179 |
// }
|
180 |
// defined = true;
|
181 |
// }
|
182 |
if ( delegateds != null ) { |
183 |
for ( int i = 0; i < delegateds.length; i++ ) { |
184 |
DynObject dynObj = delegateds[i]; |
185 |
try {
|
186 |
if ( dynObj.hasDynValue(name) ) {
|
187 |
return true; |
188 |
} else {
|
189 |
defined = true;
|
190 |
} |
191 |
} catch (DynFieldNotFoundException ex) {
|
192 |
; |
193 |
} |
194 |
} |
195 |
} |
196 |
if ( defined ) {
|
197 |
return false; |
198 |
} |
199 |
throw new DynFieldNotFoundException(name, dynClass.getName()); |
200 |
} |
201 |
|
202 |
public void delegate(DynObject dynObjects) { |
203 |
if ( delegateds == null ) { |
204 |
this.delegateds = new DynObject[1]; |
205 |
this.delegateds[0] = dynObjects; |
206 |
return;
|
207 |
} |
208 |
DynObject[] newValues = new DynObject[this.delegateds.length + 1]; |
209 |
System.arraycopy(delegateds, 0, newValues, 0, delegateds.length); |
210 |
newValues[delegateds.length] = dynObjects; |
211 |
this.delegateds = newValues;
|
212 |
} |
213 |
|
214 |
public Object invokeDynMethod(String name, DynObject context) |
215 |
throws DynMethodException {
|
216 |
throw new IllegalArgumentException("self required"); |
217 |
} |
218 |
|
219 |
public Object invokeDynMethod(int code, DynObject context) throws DynMethodException { |
220 |
throw new IllegalArgumentException("self required"); |
221 |
} |
222 |
|
223 |
public Object invokeDynMethod(Object self, String methodName, |
224 |
DynObject context) throws DynMethodException {
|
225 |
DynMethod method = this.dynClass.getDynMethod(methodName);
|
226 |
if ( method == null ) { |
227 |
if ( delegateds != null ) { |
228 |
for ( int i = 0; i < delegateds.length; i++ ) { |
229 |
try {
|
230 |
return delegateds[i].invokeDynMethod(methodName,
|
231 |
context); |
232 |
} catch (DynMethodNotSupportedException e) {
|
233 |
// continue next delegated
|
234 |
} |
235 |
} |
236 |
|
237 |
} |
238 |
throw new DynMethodNotSupportedException(methodName, self |
239 |
.getClass().getName()); |
240 |
|
241 |
} |
242 |
return method.invoke(self, context);
|
243 |
} |
244 |
|
245 |
public Object invokeDynMethod(Object self, int methodCode, DynObject context) throws DynMethodException { |
246 |
DynMethod method = this.dynClass.getDynMethod(methodCode);
|
247 |
if ( method == null ) { |
248 |
if ( delegateds != null ) { |
249 |
for ( int i = 0; i < delegateds.length; i++ ) { |
250 |
try {
|
251 |
return delegateds[i].invokeDynMethod(methodCode,
|
252 |
context); |
253 |
} catch (DynMethodNotSupportedException e) {
|
254 |
// continue next delegated
|
255 |
} |
256 |
} |
257 |
throw new DynMethodNotSupportedException(methodCode, self |
258 |
.getClass().getName()); |
259 |
|
260 |
} else {
|
261 |
throw new DynMethodNotSupportedException(methodCode, self |
262 |
.getClass().getName()); |
263 |
} |
264 |
} |
265 |
DynObjectManager manager = ToolsLocator.getDynObjectManager(); |
266 |
return manager.invokeDynMethod(self, methodCode, context);
|
267 |
} |
268 |
|
269 |
public void clear() { |
270 |
DynField[] fields = getDynClass().getDeclaredDynFields();
|
271 |
|
272 |
for ( int i = 0; i < fields.length; i++ ) { |
273 |
this.setDynValue(fields[i].getName(), fields[i].getDefaultValue());
|
274 |
} |
275 |
} |
276 |
|
277 |
public String toString() { |
278 |
return DefaultDynObject.toString(this); |
279 |
} |
280 |
|
281 |
public static String toString(DynObject obj) { |
282 |
StringBuffer buffer = new StringBuffer(); |
283 |
|
284 |
DynClass dynClass = obj.getDynClass(); |
285 |
buffer.append("DynClass name: ").append(dynClass.getName()).append(
|
286 |
"; Fields: ");
|
287 |
|
288 |
DynField[] fields = dynClass.getDeclaredDynFields();
|
289 |
|
290 |
if ( fields == null || fields.length == 0 ) { |
291 |
buffer.append("(none)");
|
292 |
} else {
|
293 |
buffer.append("[");
|
294 |
for ( int i = 0; i < fields.length; i++ ) { |
295 |
if ( i != 0 ) { |
296 |
buffer.append(", ");
|
297 |
} |
298 |
buffer.append(fields[i].getName()).append(" = ")
|
299 |
.append(obj.getDynValue(fields[i].getName())); |
300 |
} |
301 |
buffer.append("]");
|
302 |
} |
303 |
return buffer.toString();
|
304 |
} |
305 |
|
306 |
public boolean hasEmptyValues() { |
307 |
return this.values.isEmpty(); |
308 |
} |
309 |
} |