gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynObjectManager.java @ 1860
History | View | Annotate | Download (27.4 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.io.File; |
27 |
import java.io.FileNotFoundException; |
28 |
import java.io.IOException; |
29 |
import java.io.InputStream; |
30 |
import java.io.OutputStream; |
31 |
import java.util.ArrayList; |
32 |
import java.util.Arrays; |
33 |
import java.util.Collections; |
34 |
import java.util.HashMap; |
35 |
import java.util.HashSet; |
36 |
import java.util.Iterator; |
37 |
import java.util.List; |
38 |
import java.util.Map; |
39 |
import java.util.Set; |
40 |
|
41 |
import org.gvsig.tools.ToolsLocator; |
42 |
import org.gvsig.tools.dataTypes.DataTypes; |
43 |
import org.gvsig.tools.dynobject.AbstractDynMethod; |
44 |
import org.gvsig.tools.dynobject.DynClass; |
45 |
import org.gvsig.tools.dynobject.DynClassName; |
46 |
import org.gvsig.tools.dynobject.DynClass_v2; |
47 |
import org.gvsig.tools.dynobject.DynField; |
48 |
import org.gvsig.tools.dynobject.DynField_LabelAttribute; |
49 |
import org.gvsig.tools.dynobject.DynField_v2; |
50 |
import org.gvsig.tools.dynobject.DynMethod; |
51 |
import org.gvsig.tools.dynobject.DynObject; |
52 |
import org.gvsig.tools.dynobject.DynObjectEncoder; |
53 |
import org.gvsig.tools.dynobject.DynObjectManager; |
54 |
import org.gvsig.tools.dynobject.DynObjectPagingHelper; |
55 |
import org.gvsig.tools.dynobject.DynObjectRuntimeException; |
56 |
import org.gvsig.tools.dynobject.DynObjectSet; |
57 |
import org.gvsig.tools.dynobject.DynStruct; |
58 |
import org.gvsig.tools.dynobject.exception.DuplicateDynClassException; |
59 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
60 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
61 |
import org.gvsig.tools.dynobject.exception.DynMethodIllegalCodeException; |
62 |
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException; |
63 |
import org.gvsig.tools.dynobject.exception.IllegalDynMethodException; |
64 |
import org.gvsig.tools.dynobject.exception.IllegalDynMethodInvocationException; |
65 |
import org.gvsig.tools.exception.BaseException; |
66 |
import org.slf4j.Logger; |
67 |
import org.slf4j.LoggerFactory; |
68 |
import org.xmlpull.v1.XmlPullParser; |
69 |
import org.xmlpull.v1.XmlPullParserException; |
70 |
|
71 |
/**
|
72 |
* Default {@link DynObjectManager} implementation.
|
73 |
*
|
74 |
* @author gvSIG Team
|
75 |
* @version $Id$
|
76 |
*/
|
77 |
public class DefaultDynObjectManager implements DynObjectManager { |
78 |
|
79 |
private final static Logger LOG = LoggerFactory |
80 |
.getLogger(DefaultDynObjectManager.class); |
81 |
|
82 |
private static DefaultDynObjectManager manager = null; |
83 |
|
84 |
private class MethodInfo { |
85 |
|
86 |
int code;
|
87 |
DynClass dynClass; |
88 |
DynMethod dynMethod; |
89 |
Class theClass;
|
90 |
|
91 |
MethodInfo(Class theClass, DynClass dynClass, DynMethod dynMethod,
|
92 |
int code) {
|
93 |
this.code = code;
|
94 |
this.dynClass = dynClass;
|
95 |
this.dynMethod = dynMethod;
|
96 |
this.theClass = theClass;
|
97 |
} |
98 |
|
99 |
String getKey() {
|
100 |
return DefaultDynObjectManager
|
101 |
.getKey(theClass, dynClass, dynMethod); |
102 |
} |
103 |
|
104 |
void check(Class theClass, int code) throws DynMethodException { |
105 |
if (code != this.code) { |
106 |
throw new DynMethodIllegalCodeException(dynMethod.getName(), |
107 |
this.code, code);
|
108 |
} |
109 |
if (theClass != null) { |
110 |
if (this.theClass == null) { |
111 |
throw new IllegalDynMethodInvocationException( |
112 |
dynMethod.getName(), theClass); |
113 |
} |
114 |
if (!this.theClass.isAssignableFrom(theClass)) { |
115 |
throw new IllegalDynMethodInvocationException( |
116 |
dynMethod.getName(), theClass); |
117 |
} |
118 |
} |
119 |
} |
120 |
|
121 |
void check(DynClass dynClass, int code) throws DynMethodException { |
122 |
if (code != this.code) { |
123 |
throw new DynMethodIllegalCodeException(dynMethod.getName(), |
124 |
this.code, code);
|
125 |
} |
126 |
if (dynClass != null) { |
127 |
if (this.dynClass == null) { |
128 |
throw new IllegalDynMethodInvocationException( |
129 |
dynMethod.getName(), dynClass); |
130 |
} |
131 |
if (dynClass != this.dynClass |
132 |
|| !dynClass.getName().equalsIgnoreCase( |
133 |
this.dynClass.getName())) {
|
134 |
throw new IllegalDynMethodInvocationException( |
135 |
dynMethod.getName(), dynClass); |
136 |
} |
137 |
} |
138 |
} |
139 |
} |
140 |
|
141 |
private class ClassesNamespaces { |
142 |
|
143 |
private Map defaultNamespace; |
144 |
private Map namespaces; |
145 |
|
146 |
ClassesNamespaces() { |
147 |
this.namespaces = new HashMap(); |
148 |
this.defaultNamespace = new HashMap(); |
149 |
} |
150 |
|
151 |
public Map addNamespace(String name) { |
152 |
Map namespace = new HashMap(); |
153 |
this.namespaces.put(name.toLowerCase(), namespace);
|
154 |
return namespace;
|
155 |
} |
156 |
|
157 |
public Map getNamespace(String name) { |
158 |
return (Map) this.namespaces.get(name.toLowerCase()); |
159 |
} |
160 |
|
161 |
// public void clear() {
|
162 |
// this.defaultNamespace.clear();
|
163 |
// this.namespaces.clear();
|
164 |
// }
|
165 |
|
166 |
public boolean containsClass(String name) { |
167 |
name = name.toLowerCase(); |
168 |
if (this.defaultNamespace.containsKey(name)) { |
169 |
return true; |
170 |
} |
171 |
|
172 |
Iterator it = this.namespaces.values().iterator(); |
173 |
while (it.hasNext()) {
|
174 |
Map names = (Map) it.next(); |
175 |
if (names.containsKey(name)) {
|
176 |
return true; |
177 |
} |
178 |
} |
179 |
return false; |
180 |
} |
181 |
|
182 |
public boolean containsClass(String namespace, String name) { |
183 |
name = name.toLowerCase(); |
184 |
if (namespace == null) { |
185 |
return this.defaultNamespace.containsKey(name); |
186 |
} |
187 |
Map space = this.getNamespace(namespace); |
188 |
if (space == null) { |
189 |
return false; |
190 |
} |
191 |
return space.containsKey(name);
|
192 |
} |
193 |
|
194 |
// public boolean containsClass(DynClass dynClass) {
|
195 |
// if (this.defaultNamespace.containsValue(dynClass)) {
|
196 |
// return true;
|
197 |
// }
|
198 |
//
|
199 |
// Iterator it = this.namespaces.values().iterator();
|
200 |
// while (it.hasNext()) {
|
201 |
// Map names = (Map) it.next();
|
202 |
// if (names.containsValue(dynClass)) {
|
203 |
// return true;
|
204 |
// }
|
205 |
// }
|
206 |
// return false;
|
207 |
// }
|
208 |
|
209 |
public DynClass get(String name, String namespace) { |
210 |
if (namespace == null) { |
211 |
return (DynClass) this.defaultNamespace.get(name.toLowerCase()); |
212 |
} |
213 |
Map space = this.getNamespace(namespace); |
214 |
if (space == null) { |
215 |
return null; |
216 |
} |
217 |
DynClassName className = new DefaultDynClassName(name);
|
218 |
if (className.getNamespace()==null){ |
219 |
return (DynClass) space.get(name.toLowerCase());
|
220 |
} |
221 |
if (!namespace.equalsIgnoreCase(className.getNamespace())){
|
222 |
return null; |
223 |
} |
224 |
return (DynClass) space.get(className.getName().toLowerCase());
|
225 |
} |
226 |
|
227 |
public Set keySet() { |
228 |
Set keyset = new HashSet(); |
229 |
Iterator it = this.iterator(); |
230 |
while (it.hasNext()) {
|
231 |
DynClass dynClass = (DynClass) it.next(); |
232 |
keyset.add(dynClass.getFullName()); |
233 |
} |
234 |
return keyset;
|
235 |
} |
236 |
|
237 |
public Iterator iterator() { |
238 |
final class MyIterator implements Iterator { |
239 |
|
240 |
Iterator current;
|
241 |
Iterator others;
|
242 |
|
243 |
MyIterator(Iterator main, Iterator others) { |
244 |
this.current = main;
|
245 |
this.others = others;
|
246 |
} |
247 |
|
248 |
public boolean hasNext() { |
249 |
if (this.current.hasNext()) { |
250 |
return true; |
251 |
} |
252 |
while (this.others.hasNext()) { |
253 |
Object obj = others.next();
|
254 |
this.current = (Iterator) ((HashMap) obj).values().iterator(); |
255 |
if (this.current.hasNext()) { |
256 |
return true; |
257 |
} |
258 |
} |
259 |
return false; |
260 |
} |
261 |
|
262 |
public Object next() { |
263 |
if (this.current.hasNext()) { |
264 |
return this.current.next(); |
265 |
} |
266 |
while (this.others.hasNext()) { |
267 |
Object obj = others.next();
|
268 |
this.current = (Iterator) ((HashMap) obj).values().iterator(); |
269 |
if (this.current.hasNext()) { |
270 |
return this.current.next(); |
271 |
} |
272 |
} |
273 |
return null; |
274 |
} |
275 |
|
276 |
public void remove() { |
277 |
throw new UnsupportedOperationException(); |
278 |
} |
279 |
} |
280 |
|
281 |
return new MyIterator(this.defaultNamespace.values().iterator(), |
282 |
this.namespaces.values().iterator());
|
283 |
} |
284 |
|
285 |
public Object add(DynStruct dynClass) { |
286 |
String name = dynClass.getName().toLowerCase();
|
287 |
Map namespace;
|
288 |
if (dynClass.getNamespace() != null) { |
289 |
namespace = (Map) this.getNamespace(dynClass.getNamespace()); |
290 |
if (namespace == null) { |
291 |
namespace = this.addNamespace(dynClass.getNamespace());
|
292 |
} |
293 |
} else {
|
294 |
namespace = this.defaultNamespace;
|
295 |
} |
296 |
if (namespace.containsKey(name)) {
|
297 |
throw new DuplicateDynClassException(dynClass); |
298 |
} |
299 |
return namespace.put(name, dynClass);
|
300 |
} |
301 |
|
302 |
public void remove(DynStruct dynClass) { |
303 |
String name = dynClass.getName().toLowerCase();
|
304 |
Map namespace;
|
305 |
if (dynClass.getNamespace() != null) { |
306 |
namespace = (Map) this.getNamespace(dynClass.getNamespace()); |
307 |
if (namespace == null) { |
308 |
namespace = this.addNamespace(dynClass.getNamespace());
|
309 |
} |
310 |
} else {
|
311 |
namespace = this.defaultNamespace;
|
312 |
} |
313 |
if (namespace.containsKey(name)) {
|
314 |
namespace.remove(name); |
315 |
} |
316 |
} |
317 |
|
318 |
public int size() { |
319 |
int count = this.defaultNamespace.size(); |
320 |
|
321 |
Iterator it = this.namespaces.values().iterator(); |
322 |
while (it.hasNext()) {
|
323 |
Map names = (Map) it.next(); |
324 |
count += names.size(); |
325 |
} |
326 |
return count;
|
327 |
} |
328 |
|
329 |
} |
330 |
|
331 |
private Map anonymousClasses; |
332 |
private ClassesNamespaces classes;
|
333 |
private Map methodsMap; |
334 |
private List<MethodInfo> methods; |
335 |
|
336 |
public static DefaultDynObjectManager getManager() { |
337 |
if (manager == null) { |
338 |
manager = new DefaultDynObjectManager();
|
339 |
} |
340 |
return manager;
|
341 |
} |
342 |
|
343 |
static String getKey(Class theClass, DynClass dynClass, DynMethod dynMethod) { |
344 |
return DefaultDynObjectManager.getKey(theClass, dynClass,
|
345 |
dynMethod.getName()); |
346 |
} |
347 |
|
348 |
static String getKey(Class theClass, DynClass dynClass, String methodName) { |
349 |
if (dynClass == null) { |
350 |
if( theClass == null ) { |
351 |
return "__anonymous__:" + methodName; |
352 |
} else {
|
353 |
return theClass.getName() + ":" + methodName; |
354 |
} |
355 |
} else {
|
356 |
return dynClass.getName() + ":" + methodName; |
357 |
} |
358 |
} |
359 |
|
360 |
public DefaultDynObjectManager() {
|
361 |
this.classes = new ClassesNamespaces(); |
362 |
this.anonymousClasses = new HashMap(); |
363 |
this.methodsMap = new HashMap(); |
364 |
this.methods = null; |
365 |
} |
366 |
|
367 |
public DynClass_v2 createDynClass(String name, String description) { |
368 |
return new DefaultDynClass(this, name, description); |
369 |
} |
370 |
|
371 |
public DynClass_v2 createDynClass(String namespace, String name, |
372 |
String description) {
|
373 |
return new DefaultDynClass(this, namespace, name, description); |
374 |
} |
375 |
|
376 |
public void add(DynClass dynClass) { |
377 |
try {
|
378 |
((DefaultDynClass) dynClass).check(); |
379 |
} catch (Exception ex) { |
380 |
throw new DynObjectRuntimeException(ex); |
381 |
} |
382 |
this.classes.add(dynClass);
|
383 |
LOG.trace("Add DynClass definition {}.",
|
384 |
new Object[] { dynClass.getFullName() }); |
385 |
|
386 |
} |
387 |
|
388 |
public DynClass add(String name, String description) { |
389 |
DynClass dynClass = |
390 |
(DynClass) this.classes.get(name.toLowerCase(), null); |
391 |
if (dynClass == null) { |
392 |
dynClass = this.createDynClass(name, description);
|
393 |
this.add(dynClass);
|
394 |
} |
395 |
return dynClass;
|
396 |
} |
397 |
|
398 |
public DynClass add(String name) { |
399 |
return this.add(name, null); |
400 |
} |
401 |
|
402 |
public void remove(DynStruct dynClass) { |
403 |
this.classes.remove(dynClass);
|
404 |
} |
405 |
|
406 |
// public static String getFullName(String namespace, String name) {
|
407 |
// if( namespace == null ) {
|
408 |
// return name;
|
409 |
// }
|
410 |
// return namespace + ":" + name;
|
411 |
// }
|
412 |
// public static String[] splitFullName(String fullname) {
|
413 |
// String[] name = new String[] { null, fullname };
|
414 |
// int x=fullname.indexOf(':');
|
415 |
// if( x>-1 ) {
|
416 |
// name[0] = fullname.substring(0, x);
|
417 |
// name[1] = fullname.substring(x+1);
|
418 |
// }
|
419 |
// return name;
|
420 |
//
|
421 |
// }
|
422 |
|
423 |
public DynClass get(String theName) { |
424 |
DynClassName name = createDynClassName(theName); |
425 |
return this.get(name.getNamespace(), name.getName()); |
426 |
} |
427 |
|
428 |
public DynClass get(String namespace, String name) { |
429 |
return (DynClass) this.classes.get(name, namespace); |
430 |
} |
431 |
|
432 |
public DynClass get(DynClass[] superClasses) { |
433 |
StringBuffer name = new StringBuffer(); |
434 |
for (int i = 0; i < superClasses.length; i++) { |
435 |
name.append(superClasses[i].getName()).append("+");
|
436 |
} |
437 |
DefaultDynClass dynClass = |
438 |
(DefaultDynClass) this.anonymousClasses.get(name.toString());
|
439 |
if (dynClass == null) { |
440 |
dynClass = |
441 |
new DefaultDynClass(this, name.toString(), null, superClasses); |
442 |
dynClass.setAnonymous(true);
|
443 |
} |
444 |
return dynClass;
|
445 |
} |
446 |
|
447 |
public int getCount() { |
448 |
return this.classes.size(); |
449 |
} |
450 |
|
451 |
public List getNames() { |
452 |
String[] names = (String[]) this.classes.keySet().toArray(); |
453 |
Arrays.sort(names);
|
454 |
return Collections.unmodifiableList(Arrays.asList(names)); |
455 |
} |
456 |
|
457 |
public boolean has(String name) { |
458 |
return this.classes.containsClass(name); |
459 |
} |
460 |
|
461 |
public boolean has(String namespace, String name) { |
462 |
return this.classes.containsClass(namespace, name); |
463 |
} |
464 |
|
465 |
public Iterator iterator() { |
466 |
return this.classes.iterator(); |
467 |
} |
468 |
|
469 |
public DynObject createDynObject(String dynClassName) { |
470 |
DynClassName name = createDynClassName(dynClassName); |
471 |
return this.createDynObject(name.getName(), name.getNamespace()); |
472 |
} |
473 |
|
474 |
public DynObject createDynObject(String dynClassName, String namespace) { |
475 |
|
476 |
DynClass dynClass = |
477 |
(DynClass) this.classes.get(dynClassName, namespace);
|
478 |
if (dynClass == null) { |
479 |
throw new IllegalArgumentException("Can't locate class '" |
480 |
+ createDynClassName(namespace, dynClassName).getFullName() |
481 |
+ "'.");
|
482 |
} |
483 |
return this.createDynObject(dynClass); |
484 |
} |
485 |
|
486 |
public DynObject createDynObject(DynStruct dynClass) {
|
487 |
return new DefaultDynObject(dynClass); |
488 |
} |
489 |
|
490 |
public void consolide() { |
491 |
Iterator it = this.classes.iterator(); |
492 |
while (it.hasNext()) {
|
493 |
DefaultDynClass dc = (DefaultDynClass) it.next(); |
494 |
dc.consolide(); |
495 |
} |
496 |
it = this.anonymousClasses.values().iterator();
|
497 |
while (it.hasNext()) {
|
498 |
DefaultDynClass dc = (DefaultDynClass) it.next(); |
499 |
dc.consolide(); |
500 |
} |
501 |
} |
502 |
|
503 |
public int registerDynMethod(DynClass dynClass, DynMethod dynMethod) { |
504 |
((DefaultDynClass) dynClass).addMethod(dynMethod); |
505 |
return registerDynMethod(null, dynClass, dynMethod); |
506 |
} |
507 |
|
508 |
public int registerDynMethod(Class theClass, DynMethod dynMethod) { |
509 |
return registerDynMethod(theClass, null, dynMethod); |
510 |
} |
511 |
|
512 |
public int registerDynMethod(DynMethod dynMethod) { |
513 |
return registerDynMethod(null, null, dynMethod); |
514 |
} |
515 |
|
516 |
int registerDynMethod(Class theClass, DynClass dynClass, DynMethod dynMethod) { |
517 |
MethodInfo info = new MethodInfo(theClass, dynClass, dynMethod, 0); |
518 |
MethodInfo oldInfo = (MethodInfo) methodsMap.get(info.getKey()); |
519 |
if (oldInfo != null) { |
520 |
// Update the method info
|
521 |
oldInfo.dynClass = dynClass; |
522 |
oldInfo.dynMethod = dynMethod; |
523 |
return oldInfo.code;
|
524 |
} |
525 |
if (methods == null) { |
526 |
methods = new ArrayList<>(); |
527 |
} |
528 |
info.code = methods.size(); |
529 |
methods.add(info); |
530 |
methodsMap.put(info.getKey(), info); |
531 |
try {
|
532 |
if( dynMethod instanceof AbstractDynMethod && dynMethod.getCode()<0 ) { |
533 |
((AbstractDynMethod)dynMethod).setCode(info.code); |
534 |
} |
535 |
} catch (DynMethodNotSupportedException ex) {
|
536 |
} |
537 |
return info.code;
|
538 |
} |
539 |
|
540 |
public Object invokeDynMethod(Object self, int code, DynObject context) |
541 |
throws DynMethodException {
|
542 |
|
543 |
try {
|
544 |
/*
|
545 |
* Intentamos ejecutar la operacion, y si peta ya haremos las
|
546 |
* comprobaciones oportunas para lanzar la excepcion que toque.
|
547 |
*
|
548 |
* Asi evitamos codigo de comprobacion para los casos que valla bien
|
549 |
* que deberian ser la mayoria.
|
550 |
*/
|
551 |
return methods.get(code).dynMethod.invoke((DynObject)self, new Object[] {context}); |
552 |
} catch (RuntimeException e) { |
553 |
getDynMethod(self, code); |
554 |
throw e;
|
555 |
} catch (DynMethodException e) {
|
556 |
getDynMethod(self, code); |
557 |
throw e;
|
558 |
} |
559 |
|
560 |
} |
561 |
|
562 |
public int getDynMethodCode(DynClass dynClass, String methodName) |
563 |
throws DynMethodException {
|
564 |
String key = DefaultDynObjectManager.getKey(null, dynClass, methodName); |
565 |
MethodInfo info = (MethodInfo) methodsMap.get(key); |
566 |
if (info == null) { |
567 |
throw new IllegalDynMethodException(methodName, dynClass); |
568 |
} |
569 |
info.check(dynClass, info.code); |
570 |
return info.code;
|
571 |
} |
572 |
|
573 |
public int getDynMethodCode(Class theClass, String methodName) |
574 |
throws DynMethodException {
|
575 |
String key = DefaultDynObjectManager.getKey(theClass, null, methodName); |
576 |
MethodInfo info = (MethodInfo) methodsMap.get(key); |
577 |
if (info == null) { |
578 |
throw new IllegalDynMethodException(methodName, theClass); |
579 |
} |
580 |
info.check(theClass, info.code); |
581 |
return info.code;
|
582 |
} |
583 |
|
584 |
public DynMethod getDynMethod(int code) throws DynMethodException { |
585 |
if (code >= methods.size()) {
|
586 |
throw new DynMethodNotSupportedException(code, "{null}"); |
587 |
} |
588 |
MethodInfo info = methods.get(code); |
589 |
info.check((Class) null, code); |
590 |
return info.dynMethod;
|
591 |
} |
592 |
|
593 |
@Override
|
594 |
public DynMethod getDynMethod(String methodName) throws DynMethodException { |
595 |
String key = DefaultDynObjectManager.getKey(null, null, methodName); |
596 |
MethodInfo info = (MethodInfo) methodsMap.get(key); |
597 |
if (info == null) { |
598 |
throw new IllegalDynMethodException(methodName); |
599 |
} |
600 |
return info.dynMethod;
|
601 |
} |
602 |
|
603 |
public DynMethod getDynMethod(Object obj, int code) |
604 |
throws DynMethodException {
|
605 |
return getDynMethod(obj.getClass(), code);
|
606 |
} |
607 |
|
608 |
public DynMethod getDynMethod(Class theClass, int code) |
609 |
throws DynMethodException {
|
610 |
if (code >= methods.size()) {
|
611 |
throw new DynMethodNotSupportedException(code, theClass.getName()); |
612 |
} |
613 |
MethodInfo info = methods.get(code); |
614 |
info.check(theClass, code); |
615 |
return info.dynMethod;
|
616 |
} |
617 |
|
618 |
public DynMethod getDynMethod(DynClass dynClass, int code) |
619 |
throws DynMethodException {
|
620 |
if (code >= methods.size()) {
|
621 |
throw new DynMethodNotSupportedException(code, dynClass.getName()); |
622 |
} |
623 |
MethodInfo info = methods.get(code); |
624 |
info.check(dynClass, code); |
625 |
return info.dynMethod;
|
626 |
} |
627 |
|
628 |
public DynMethod getDynMethod(DynObject dynObject, int code) |
629 |
throws DynMethodException {
|
630 |
return getDynMethod(dynObject.getDynClass(), code);
|
631 |
} |
632 |
|
633 |
public void validate(DynObject object) { |
634 |
// TODO
|
635 |
return;
|
636 |
} |
637 |
|
638 |
public Class getDefaultClassOfType(int type) { |
639 |
return ToolsLocator.getDataTypesManager().getDefaultClass(type);
|
640 |
} |
641 |
|
642 |
public String exportSimpleDynClassDefinitions(DynClass dynClass) { |
643 |
DynClassExportHelper exporter = new DynClassExportHelper();
|
644 |
return exporter.exportSimpleDefinition(dynClass);
|
645 |
} |
646 |
|
647 |
public void exportSimpleDynClassDefinitions(File out, DynClass dynClass) throws FileNotFoundException { |
648 |
DynClassExportHelper exporter = new DynClassExportHelper();
|
649 |
exporter.exportSimpleDefinition(out, dynClass); |
650 |
} |
651 |
|
652 |
public void exportSimpleDynClassDefinitions(OutputStream out, DynClass dynClass) { |
653 |
DynClassExportHelper exporter = new DynClassExportHelper();
|
654 |
exporter.exportSimpleDefinition(out, dynClass); |
655 |
} |
656 |
|
657 |
public Map importDynClassDefinitions(InputStream resource, |
658 |
ClassLoader loader) throws XmlPullParserException, IOException { |
659 |
return new DynClassImportHelper().importDefinitions(resource, loader, |
660 |
null);
|
661 |
} |
662 |
|
663 |
public Map importDynClassDefinitions(XmlPullParser parser, |
664 |
ClassLoader loader, String defaultNamespace) |
665 |
throws XmlPullParserException, IOException { |
666 |
return new DynClassImportHelper().importDefinitions(parser, loader, |
667 |
defaultNamespace); |
668 |
} |
669 |
|
670 |
public Map importDynClassDefinitions(InputStream resource, |
671 |
ClassLoader loader, String defaultNamespace) |
672 |
throws XmlPullParserException, IOException { |
673 |
return new DynClassImportHelper().importDefinitions(resource, loader, |
674 |
defaultNamespace); |
675 |
} |
676 |
|
677 |
public DynObjectPagingHelper createDynObjectPagingHelper(DynObjectSet set)
|
678 |
throws BaseException {
|
679 |
return new DefaultDynObjectPagingHelper(set); |
680 |
} |
681 |
|
682 |
public DynObjectPagingHelper createDynObjectPagingHelper(DynObjectSet set,
|
683 |
int pageSize) throws BaseException { |
684 |
return new DefaultDynObjectPagingHelper(set, pageSize); |
685 |
} |
686 |
|
687 |
public DynClassName createDynClassName(String namespace, String name) { |
688 |
return new DefaultDynClassName(namespace, name); |
689 |
} |
690 |
|
691 |
public DynClassName createDynClassName(String name) { |
692 |
return new DefaultDynClassName(name); |
693 |
} |
694 |
|
695 |
public Iterator iterator(String nameSpace) { |
696 |
return iterator(nameSpace, false); |
697 |
} |
698 |
|
699 |
private Iterator iterator(String nameSpace, boolean exactMatchingRequired) { |
700 |
|
701 |
List list = new ArrayList(); |
702 |
Iterator it = this.classes.iterator(); |
703 |
|
704 |
if (nameSpace == null) { |
705 |
nameSpace = "";
|
706 |
} |
707 |
|
708 |
while( it.hasNext() ) {
|
709 |
|
710 |
Object obj = it.next();
|
711 |
DynStruct dynStruct = (DynStruct) obj; |
712 |
String dynNameSpace = dynStruct.getNamespace();
|
713 |
if ((dynNameSpace == null) || (dynNameSpace.equals(""))) { |
714 |
dynNameSpace = null;
|
715 |
} else {
|
716 |
dynNameSpace = dynNameSpace.toLowerCase(); |
717 |
} |
718 |
nameSpace = nameSpace.toLowerCase(); |
719 |
if (exactMatchingRequired) {
|
720 |
if (nameSpace.equalsIgnoreCase(dynNameSpace)) {
|
721 |
list.add(dynStruct); |
722 |
} |
723 |
} else {
|
724 |
if ((dynNameSpace != null) |
725 |
&& (nameSpace.indexOf(dynNameSpace) > -1)) {
|
726 |
list.add(dynStruct); |
727 |
} |
728 |
} |
729 |
} |
730 |
return list.iterator();
|
731 |
} |
732 |
|
733 |
public Object getAttributeValue(Object obj, String name) { |
734 |
if( "label".equalsIgnoreCase(name) ) { |
735 |
if( obj instanceof DynField_LabelAttribute ) { |
736 |
return ((DynField_LabelAttribute)obj).getLabel();
|
737 |
} |
738 |
} else if( "StructWhenTypeIsDynObject".equalsIgnoreCase(name) ) { |
739 |
if( obj instanceof DynField_v2 ) { |
740 |
return ((DynField_v2)obj).getDynClassOfValue();
|
741 |
} |
742 |
} |
743 |
return null; |
744 |
} |
745 |
|
746 |
public void setAttributeValue(Object obj, String name, Object value) { |
747 |
if( "label".equalsIgnoreCase(name) ) { |
748 |
if( obj instanceof DynField_LabelAttribute ) { |
749 |
((DynField_LabelAttribute)obj).setLabel((String) value);
|
750 |
} |
751 |
} else if( "StructWhenTypeIsDynObject".equalsIgnoreCase(name) ) { |
752 |
if( obj instanceof DynField_v2 ) { |
753 |
((DynField_v2)obj).setClassOfValue((DynStruct) value); |
754 |
} |
755 |
} |
756 |
} |
757 |
|
758 |
@Override
|
759 |
public DynObjectEncoder createSimpleDynObjectEncoder() {
|
760 |
return new SimpleDynObjectEncoder(); |
761 |
} |
762 |
|
763 |
@Override
|
764 |
public void copy(DynObject source, DynObject target) { |
765 |
for (DynField field : target.getDynClass().getDynFields() ) {
|
766 |
String name = field.getName();
|
767 |
try {
|
768 |
Object value = source.getDynValue(name);
|
769 |
target.setDynValue(name, value); |
770 |
} catch(DynFieldNotFoundException | DefaultDynObject.CoerceValueException ex) {
|
771 |
// if field not found in source or types are inconsistent, ignore it.
|
772 |
} |
773 |
} |
774 |
} |
775 |
|
776 |
public void clear(DynObject obj) { |
777 |
for (DynField field : obj.getDynClass().getDynFields() ) {
|
778 |
String name = field.getName();
|
779 |
try {
|
780 |
Object value = field.getDefaultValue();
|
781 |
obj.setDynValue(name, value); |
782 |
} catch(DefaultDynObject.CoerceValueException ex) {
|
783 |
// if types are inconsistent, ignore it.
|
784 |
} |
785 |
} |
786 |
} |
787 |
|
788 |
@Override
|
789 |
public DynField_v2 createDynField(String name) { |
790 |
return new DefaultDynField(name, DataTypes.STRING); |
791 |
} |
792 |
|
793 |
@Override
|
794 |
public DynClass_v2 createCopy(DynClass source) {
|
795 |
DynClass_v2 target = this.createDynClass(
|
796 |
source.getNamespace(), |
797 |
source.getName(), |
798 |
source.getDescription() |
799 |
); |
800 |
for (DynField sourceField : source.getDynFields()) {
|
801 |
DynField_v2 targetField = (DynField_v2) target.addDynField(sourceField.getName()); |
802 |
targetField.copyFrom(sourceField); |
803 |
} |
804 |
try {
|
805 |
for (DynMethod sourceMethod : source.getDynMethods()) {
|
806 |
target.addDynMethod(sourceMethod); |
807 |
} |
808 |
} catch (DynMethodException ex) {
|
809 |
} |
810 |
return target;
|
811 |
} |
812 |
|
813 |
} |