Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynObjectManager.java @ 1405

History | View | Annotate | Download (26.5 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.io.UnsupportedEncodingException;
32
import java.net.URLEncoder;
33
import java.util.ArrayList;
34
import java.util.Arrays;
35
import java.util.Collections;
36
import java.util.HashMap;
37
import java.util.HashSet;
38
import java.util.Iterator;
39
import java.util.List;
40
import java.util.Map;
41
import java.util.Set;
42

    
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dataTypes.DataTypes;
45
import org.gvsig.tools.dynobject.DelegatedDynObject;
46
import org.gvsig.tools.dynobject.DynClass;
47
import org.gvsig.tools.dynobject.DynClassName;
48
import org.gvsig.tools.dynobject.DynField;
49
import org.gvsig.tools.dynobject.DynField_LabelAttribute;
50
import org.gvsig.tools.dynobject.DynField_v2;
51
import org.gvsig.tools.dynobject.DynMethod;
52
import org.gvsig.tools.dynobject.DynObject;
53
import org.gvsig.tools.dynobject.DynObjectEncoder;
54
import org.gvsig.tools.dynobject.DynObjectManager;
55
import org.gvsig.tools.dynobject.DynObjectPagingHelper;
56
import org.gvsig.tools.dynobject.DynObjectRuntimeException;
57
import org.gvsig.tools.dynobject.DynObjectSet;
58
import org.gvsig.tools.dynobject.DynStruct;
59
import org.gvsig.tools.dynobject.exception.DuplicateDynClassException;
60
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
61
import org.gvsig.tools.dynobject.exception.DynMethodException;
62
import org.gvsig.tools.dynobject.exception.DynMethodIllegalCodeException;
63
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
64
import org.gvsig.tools.dynobject.exception.IllegalDynMethodException;
65
import org.gvsig.tools.dynobject.exception.IllegalDynMethodInvocationException;
66
import org.gvsig.tools.exception.BaseException;
67
import org.slf4j.Logger;
68
import org.slf4j.LoggerFactory;
69
import org.xmlpull.v1.XmlPullParser;
70
import org.xmlpull.v1.XmlPullParserException;
71

    
72
/**
73
 * Default {@link DynObjectManager} implementation.
74
 * 
75
 * @author gvSIG Team
76
 * @version $Id$
77
 */
78
public class DefaultDynObjectManager implements DynObjectManager {
79

    
80
    private final static Logger LOG = LoggerFactory
81
        .getLogger(DefaultDynObjectManager.class);
82

    
83
    private static DefaultDynObjectManager manager = null;
84

    
85
    private class MethodInfo {
86

    
87
        int code;
88
        DynClass dynClass;
89
        DynMethod dynMethod;
90
        Class theClass;
91

    
92
        MethodInfo(Class theClass, DynClass dynClass, DynMethod dynMethod,
93
            int code) {
94
            this.code = code;
95
            this.dynClass = dynClass;
96
            this.dynMethod = dynMethod;
97
            this.theClass = theClass;
98
        }
99

    
100
        String getKey() {
101
            return DefaultDynObjectManager
102
                .getKey(theClass, dynClass, dynMethod);
103
        }
104

    
105
        void check(Class theClass, int code) throws DynMethodException {
106
            if (code != this.code) {
107
                throw new DynMethodIllegalCodeException(dynMethod.getName(),
108
                    this.code, code);
109
            }
110
            if (theClass != null) {
111
                if (this.theClass == null) {
112
                    throw new IllegalDynMethodInvocationException(
113
                        dynMethod.getName(), theClass);
114
                }
115
                if (!this.theClass.isAssignableFrom(theClass)) {
116
                    throw new IllegalDynMethodInvocationException(
117
                        dynMethod.getName(), theClass);
118
                }
119
            }
120
        }
121

    
122
        void check(DynClass dynClass, int code) throws DynMethodException {
123
            if (code != this.code) {
124
                throw new DynMethodIllegalCodeException(dynMethod.getName(),
125
                    this.code, code);
126
            }
127
            if (dynClass != null) {
128
                if (this.dynClass == null) {
129
                    throw new IllegalDynMethodInvocationException(
130
                        dynMethod.getName(), dynClass);
131
                }
132
                if (dynClass != this.dynClass
133
                    || !dynClass.getName().equalsIgnoreCase(
134
                        this.dynClass.getName())) {
135
                    throw new IllegalDynMethodInvocationException(
136
                        dynMethod.getName(), dynClass);
137
                }
138
            }
139
        }
140
    }
141

    
142
    private class ClassesNamespaces {
143

    
144
        private Map defaultNamespace;
145
        private Map namespaces;
146

    
147
        ClassesNamespaces() {
148
            this.namespaces = new HashMap();
149
            this.defaultNamespace = new HashMap();
150
        }
151

    
152
        public Map addNamespace(String name) {
153
            Map namespace = new HashMap();
154
            this.namespaces.put(name.toLowerCase(), namespace);
155
            return namespace;
156
        }
157

    
158
        public Map getNamespace(String name) {
159
            return (Map) this.namespaces.get(name.toLowerCase());
160
        }
161

    
162
//        public void clear() {
163
//            this.defaultNamespace.clear();
164
//            this.namespaces.clear();
165
//        }
166

    
167
        public boolean containsClass(String name) {
168
            name = name.toLowerCase();
169
            if (this.defaultNamespace.containsKey(name)) {
170
                return true;
171
            }
172

    
173
            Iterator it = this.namespaces.values().iterator();
174
            while (it.hasNext()) {
175
                Map names = (Map) it.next();
176
                if (names.containsKey(name)) {
177
                    return true;
178
                }
179
            }
180
            return false;
181
        }
182

    
183
        public boolean containsClass(String namespace, String name) {
184
            name = name.toLowerCase();
185
            if (namespace == null) {
186
                return this.defaultNamespace.containsKey(name);
187
            }
188
            Map space = this.getNamespace(namespace);
189
            if (space == null) {
190
                return false;
191
            }
192
            return space.containsKey(name);
193
        }
194

    
195
//        public boolean containsClass(DynClass dynClass) {
196
//            if (this.defaultNamespace.containsValue(dynClass)) {
197
//                return true;
198
//            }
199
//
200
//            Iterator it = this.namespaces.values().iterator();
201
//            while (it.hasNext()) {
202
//                Map names = (Map) it.next();
203
//                if (names.containsValue(dynClass)) {
204
//                    return true;
205
//                }
206
//            }
207
//            return false;
208
//        }
209

    
210
        public DynClass get(String name, String namespace) {
211
            if (namespace == null) {
212
                return (DynClass) this.defaultNamespace.get(name.toLowerCase());
213
            }
214
            Map space = this.getNamespace(namespace);
215
            if (space == null) {
216
                return null;
217
            }
218
            DynClassName className = new DefaultDynClassName(name);
219
            if (className.getNamespace()==null){
220
                return (DynClass) space.get(name.toLowerCase());
221
            }
222
            if (!namespace.equalsIgnoreCase(className.getNamespace())){
223
                    return null;
224
            }
225
            return (DynClass) space.get(className.getName().toLowerCase());
226
        }
227

    
228
        public Set keySet() {
229
            Set keyset = new HashSet();
230
            Iterator it = this.iterator();
231
            while (it.hasNext()) {
232
                DynClass dynClass = (DynClass) it.next();
233
                keyset.add(dynClass.getFullName());
234
            }
235
            return keyset;
236
        }
237

    
238
        public Iterator iterator() {
239
            final class MyIterator implements Iterator {
240

    
241
                Iterator current;
242
                Iterator others;
243

    
244
                MyIterator(Iterator main, Iterator others) {
245
                    this.current = main;
246
                    this.others = others;
247
                }
248

    
249
                public boolean hasNext() {
250
                    if (this.current.hasNext()) {
251
                        return true;
252
                    }
253
                    while (this.others.hasNext()) { 
254
                        Object obj = others.next();
255
                        this.current = (Iterator) ((HashMap) obj).values().iterator();
256
                        if (this.current.hasNext()) {
257
                            return true;
258
                        }
259
                    }
260
                    return false;
261
                }
262

    
263
                public Object next() {
264
                    if (this.current.hasNext()) {
265
                        return this.current.next();
266
                    }
267
                    while (this.others.hasNext()) {
268
                        Object obj = others.next();
269
                        this.current = (Iterator) ((HashMap) obj).values().iterator();
270
                        if (this.current.hasNext()) {
271
                            return this.current.next();
272
                        }
273
                    }
274
                    return null;
275
                }
276

    
277
                public void remove() {
278
                    throw new UnsupportedOperationException();
279
                }
280
            }
281

    
282
            return new MyIterator(this.defaultNamespace.values().iterator(),
283
                this.namespaces.values().iterator());
284
        }
285

    
286
        public Object add(DynStruct dynClass) {
287
            String name = dynClass.getName().toLowerCase();
288
            Map namespace;
289
            if (dynClass.getNamespace() != null) {
290
                namespace = (Map) this.getNamespace(dynClass.getNamespace());
291
                if (namespace == null) {
292
                    namespace = this.addNamespace(dynClass.getNamespace());
293
                }
294
            } else {
295
                namespace = this.defaultNamespace;
296
            }
297
            if (namespace.containsKey(name)) {
298
                throw new DuplicateDynClassException(dynClass);
299
            }
300
            return namespace.put(name, dynClass);
301
        }
302

    
303
        public void remove(DynStruct dynClass) {
304
            String name = dynClass.getName().toLowerCase();
305
            Map namespace;
306
            if (dynClass.getNamespace() != null) {
307
                namespace = (Map) this.getNamespace(dynClass.getNamespace());
308
                if (namespace == null) {
309
                    namespace = this.addNamespace(dynClass.getNamespace());
310
                }
311
            } else {
312
                namespace = this.defaultNamespace;
313
            }
314
            if (namespace.containsKey(name)) {
315
                namespace.remove(name);
316
            }
317
        }
318

    
319
        public int size() {
320
            int count = this.defaultNamespace.size();
321

    
322
            Iterator it = this.namespaces.values().iterator();
323
            while (it.hasNext()) {
324
                Map names = (Map) it.next();
325
                count += names.size();
326
            }
327
            return count;
328
        }
329

    
330
    }
331

    
332
    private Map anonymousClasses;
333
    private ClassesNamespaces classes;
334
    private Map methodsMap;
335
    private List<MethodInfo> methods;
336

    
337
    public static DefaultDynObjectManager getManager() {
338
        if (manager == null) {
339
            manager = new DefaultDynObjectManager();
340
        }
341
        return manager;
342
    }
343

    
344
    static String getKey(Class theClass, DynClass dynClass, DynMethod dynMethod) {
345
        return DefaultDynObjectManager.getKey(theClass, dynClass,
346
            dynMethod.getName());
347
    }
348

    
349
    static String getKey(Class theClass, DynClass dynClass, String methodName) {
350
        if (dynClass == null) {
351
            if( theClass == null ) {
352
                return "__anonymous__:" + methodName;
353
            } else {
354
                return theClass.getName() + ":" + methodName;
355
            }
356
        } else {
357
            return dynClass.getName() + ":" + methodName;
358
        }
359
    }
360

    
361
    public DefaultDynObjectManager() {
362
        this.classes = new ClassesNamespaces();
363
        this.anonymousClasses = new HashMap();
364
        this.methodsMap = new HashMap();
365
        this.methods = null;
366
    }
367

    
368
    public DynClass createDynClass(String name, String description) {
369
        return new DefaultDynClass(this, name, description);
370
    }
371

    
372
    public DynClass createDynClass(String namespace, String name,
373
        String description) {
374
        return new DefaultDynClass(this, namespace, name, description);
375
    }
376

    
377
    public void add(DynClass dynClass) {
378
        try {
379
            ((DefaultDynClass) dynClass).check();
380
        } catch (Exception ex) {
381
            throw new DynObjectRuntimeException(ex);
382
        }
383
        this.classes.add(dynClass);
384
        LOG.trace("Add DynClass definition {}.",
385
            new Object[] { dynClass.getFullName() });
386

    
387
    }
388

    
389
    public DynClass add(String name, String description) {
390
        DynClass dynClass =
391
            (DynClass) this.classes.get(name.toLowerCase(), null);
392
        if (dynClass == null) {
393
            dynClass = this.createDynClass(name, description);
394
            this.add(dynClass);
395
        }
396
        return dynClass;
397
    }
398

    
399
    public DynClass add(String name) {
400
        return this.add(name, null);
401
    }
402

    
403
    public void remove(DynStruct dynClass) {
404
        this.classes.remove(dynClass);
405
    }
406

    
407
    // public static String getFullName(String namespace, String name) {
408
    // if( namespace == null ) {
409
    // return name;
410
    // }
411
    // return namespace + ":" + name;
412
    // }
413
    // public static String[] splitFullName(String fullname) {
414
    // String[] name = new String[] { null, fullname };
415
    // int x=fullname.indexOf(':');
416
    // if( x>-1 ) {
417
    // name[0] = fullname.substring(0, x);
418
    // name[1] = fullname.substring(x+1);
419
    // }
420
    // return name;
421
    //
422
    // }
423

    
424
    public DynClass get(String theName) {
425
        DynClassName name = createDynClassName(theName);
426
        return this.get(name.getNamespace(), name.getName());
427
    }
428

    
429
    public DynClass get(String namespace, String name) {
430
        return (DynClass) this.classes.get(name, namespace);
431
    }
432

    
433
    public DynClass get(DynClass[] superClasses) {
434
        StringBuffer name = new StringBuffer();
435
        for (int i = 0; i < superClasses.length; i++) {
436
            name.append(superClasses[i].getName()).append("+");
437
        }
438
        DefaultDynClass dynClass =
439
            (DefaultDynClass) this.anonymousClasses.get(name.toString());
440
        if (dynClass == null) {
441
            dynClass =
442
                new DefaultDynClass(this, name.toString(), null, superClasses);
443
            dynClass.setAnonymous(true);
444
        }
445
        return dynClass;
446
    }
447

    
448
    public int getCount() {
449
        return this.classes.size();
450
    }
451

    
452
    public List getNames() {
453
        String[] names = (String[]) this.classes.keySet().toArray();
454
        Arrays.sort(names);
455
        return Collections.unmodifiableList(Arrays.asList(names));
456
    }
457

    
458
    public boolean has(String name) {
459
        return this.classes.containsClass(name);
460
    }
461

    
462
    public boolean has(String namespace, String name) {
463
        return this.classes.containsClass(namespace, name);
464
    }
465

    
466
    public Iterator iterator() {
467
        return this.classes.iterator();
468
    }
469

    
470
    public DynObject createDynObject(String dynClassName) {
471
        DynClassName name = createDynClassName(dynClassName);
472
        return this.createDynObject(name.getName(), name.getNamespace());
473
    }
474

    
475
    public DynObject createDynObject(String dynClassName, String namespace) {
476

    
477
        DynClass dynClass =
478
            (DynClass) this.classes.get(dynClassName, namespace);
479
        if (dynClass == null) {
480
            throw new IllegalArgumentException("Can't locate class '"
481
                + createDynClassName(namespace, dynClassName).getFullName()
482
                + "'.");
483
        }
484
        return this.createDynObject(dynClass);
485
    }
486

    
487
    public DynObject createDynObject(DynStruct dynClass) {
488
        return new DefaultDynObject(dynClass);
489
    }
490

    
491
    public void consolide() {
492
        Iterator it = this.classes.iterator();
493
        while (it.hasNext()) {
494
            DefaultDynClass dc = (DefaultDynClass) it.next();
495
            dc.consolide();
496
        }
497
        it = this.anonymousClasses.values().iterator();
498
        while (it.hasNext()) {
499
            DefaultDynClass dc = (DefaultDynClass) it.next();
500
            dc.consolide();
501
        }
502
    }
503

    
504
    public int registerDynMethod(DynClass dynClass, DynMethod dynMethod) {
505
        ((DefaultDynClass) dynClass).addMethod(dynMethod);
506
        return registerDynMethod(null, dynClass, dynMethod);
507
    }
508

    
509
    public int registerDynMethod(Class theClass, DynMethod dynMethod) {
510
        return registerDynMethod(theClass, null, dynMethod);
511
    }
512

    
513
    public int registerDynMethod(DynMethod dynMethod) {
514
        return registerDynMethod(null, null, dynMethod);
515
    }
516

    
517
    int registerDynMethod(Class theClass, DynClass dynClass, DynMethod dynMethod) {
518
        MethodInfo info = new MethodInfo(theClass, dynClass, dynMethod, 0);
519
        MethodInfo oldInfo = (MethodInfo) methodsMap.get(info.getKey());
520
        if (oldInfo != null) {
521
            // Update the method info
522
            oldInfo.dynClass = dynClass;
523
            oldInfo.dynMethod = dynMethod;
524
            return oldInfo.code;
525
        }
526
        if (methods == null) {
527
            methods = new ArrayList<MethodInfo>();
528
        }
529
        info.code = methods.size();
530
        methods.add(info);
531
        methodsMap.put(info.getKey(), info);
532

    
533
        return info.code;
534
    }
535

    
536
    public Object invokeDynMethod(Object self, int code, DynObject context)
537
        throws DynMethodException {
538

    
539
        try {
540
            /*
541
             * Intentamos ejecutar la operacion, y si peta ya haremos las
542
             * comprobaciones oportunas para lanzar la excepcion que toque.
543
             * 
544
             * Asi evitamos codigo de comprobacion para los casos que valla bien
545
             * que deberian ser la mayoria.
546
             */
547
            return methods.get(code).dynMethod.invoke((DynObject)self, new Object[] {context});
548
        } catch (RuntimeException e) {
549
            getDynMethod(self, code);
550
            throw e;
551
        } catch (DynMethodException e) {
552
            getDynMethod(self, code);
553
            throw e;
554
        }
555

    
556
    }
557

    
558
    public int getDynMethodCode(DynClass dynClass, String methodName)
559
        throws DynMethodException {
560
        String key = DefaultDynObjectManager.getKey(null, dynClass, methodName);
561
        MethodInfo info = (MethodInfo) methodsMap.get(key);
562
        if (info == null) {
563
            throw new IllegalDynMethodException(methodName, dynClass);
564
        }
565
        info.check(dynClass, info.code);
566
        return info.code;
567
    }
568

    
569
    public int getDynMethodCode(Class theClass, String methodName)
570
        throws DynMethodException {
571
        String key = DefaultDynObjectManager.getKey(theClass, null, methodName);
572
        MethodInfo info = (MethodInfo) methodsMap.get(key);
573
        if (info == null) {
574
            throw new IllegalDynMethodException(methodName, theClass);
575
        }
576
        info.check(theClass, info.code);
577
        return info.code;
578
    }
579

    
580
    public DynMethod getDynMethod(int code) throws DynMethodException {
581
        if (code >= methods.size()) {
582
            throw new DynMethodNotSupportedException(code, "{null}");
583
        }
584
        MethodInfo info = methods.get(code);
585
        info.check((Class) null, code);
586
        return info.dynMethod;
587
    }
588
    
589
    @Override
590
    public DynMethod getDynMethod(String methodName) throws DynMethodException {
591
        String key = DefaultDynObjectManager.getKey(null, null, methodName);
592
        MethodInfo info = (MethodInfo) methodsMap.get(key);
593
        if (info == null) {
594
            throw new IllegalDynMethodException(methodName);
595
        }
596
        return info.dynMethod;
597
    }
598
    
599
    public DynMethod getDynMethod(Object obj, int code)
600
        throws DynMethodException {
601
        return getDynMethod(obj.getClass(), code);
602
    }
603

    
604
    public DynMethod getDynMethod(Class theClass, int code)
605
        throws DynMethodException {
606
        if (code >= methods.size()) {
607
            throw new DynMethodNotSupportedException(code, theClass.getName());
608
        }
609
        MethodInfo info = methods.get(code);
610
        info.check(theClass, code);
611
        return info.dynMethod;
612
    }
613

    
614
    public DynMethod getDynMethod(DynClass dynClass, int code)
615
        throws DynMethodException {
616
        if (code >= methods.size()) {
617
            throw new DynMethodNotSupportedException(code, dynClass.getName());
618
        }
619
        MethodInfo info = methods.get(code);
620
        info.check(dynClass, code);
621
        return info.dynMethod;
622
    }
623

    
624
    public DynMethod getDynMethod(DynObject dynObject, int code)
625
        throws DynMethodException {
626
        return getDynMethod(dynObject.getDynClass(), code);
627
    }
628

    
629
    public void validate(DynObject object) {
630
        // TODO
631
        return;
632
    }
633

    
634
    public Class getDefaultClassOfType(int type) {
635
        return ToolsLocator.getDataTypesManager().getDefaultClass(type);
636
    }
637

    
638
    public String exportSimpleDynClassDefinitions(DynClass dynClass) {
639
        DynClassExportHelper exporter = new DynClassExportHelper();
640
        return exporter.exportSimpleDefinition(dynClass);
641
    }
642
    
643
    public void exportSimpleDynClassDefinitions(File out, DynClass dynClass) throws FileNotFoundException {
644
        DynClassExportHelper exporter = new DynClassExportHelper();
645
        exporter.exportSimpleDefinition(out, dynClass);
646
    }
647
    
648
    public void exportSimpleDynClassDefinitions(OutputStream out, DynClass dynClass) {
649
        DynClassExportHelper exporter = new DynClassExportHelper();
650
        exporter.exportSimpleDefinition(out, dynClass);
651
    }
652
    
653
    public Map importDynClassDefinitions(InputStream resource,
654
        ClassLoader loader) throws XmlPullParserException, IOException {
655
        return new DynClassImportHelper().importDefinitions(resource, loader,
656
            null);
657
    }
658

    
659
    public Map importDynClassDefinitions(XmlPullParser parser,
660
        ClassLoader loader, String defaultNamespace)
661
        throws XmlPullParserException, IOException {
662
        return new DynClassImportHelper().importDefinitions(parser, loader,
663
            defaultNamespace);
664
    }
665

    
666
    public Map importDynClassDefinitions(InputStream resource,
667
        ClassLoader loader, String defaultNamespace)
668
        throws XmlPullParserException, IOException {
669
        return new DynClassImportHelper().importDefinitions(resource, loader,
670
            defaultNamespace);
671
    }
672

    
673
    public DynObjectPagingHelper createDynObjectPagingHelper(DynObjectSet set)
674
        throws BaseException {
675
        return new DefaultDynObjectPagingHelper(set); 
676
    }
677

    
678
    public DynObjectPagingHelper createDynObjectPagingHelper(DynObjectSet set,
679
        int pageSize) throws BaseException {
680
        return new DefaultDynObjectPagingHelper(set, pageSize);
681
    }
682

    
683
    public DynClassName createDynClassName(String namespace, String name) {
684
        return new DefaultDynClassName(namespace, name);
685
    }
686

    
687
    public DynClassName createDynClassName(String name) {
688
        return new DefaultDynClassName(name);
689
    }
690

    
691
    public Iterator iterator(String nameSpace) {
692
        return iterator(nameSpace, false);
693
    }
694

    
695
    private Iterator iterator(String nameSpace, boolean exactMatchingRequired) {
696

    
697
        List list = new ArrayList();
698
        Iterator it = this.classes.iterator();
699

    
700
        if (nameSpace == null) {
701
            nameSpace = "";
702
        }
703

    
704
        while( it.hasNext() ) {
705
            
706
            Object obj = it.next();
707
            DynStruct dynStruct = (DynStruct) obj;
708
            String dynNameSpace = dynStruct.getNamespace();
709
            if ((dynNameSpace == null) || (dynNameSpace.equals(""))) {
710
                dynNameSpace = null;
711
            } else {
712
                dynNameSpace = dynNameSpace.toLowerCase();
713
            }
714
            nameSpace = nameSpace.toLowerCase();
715
            if (exactMatchingRequired) {
716
                if (nameSpace.equalsIgnoreCase(dynNameSpace)) {
717
                    list.add(dynStruct);
718
                }
719
            } else {
720
                if ((dynNameSpace != null)
721
                    && (nameSpace.indexOf(dynNameSpace) > -1)) {
722
                    list.add(dynStruct);
723
                }
724
            }
725
        }
726
        return list.iterator();
727
    }
728

    
729
    public Object getAttributeValue(Object obj, String name) {
730
        if( "label".equalsIgnoreCase(name) ) {
731
            if( obj instanceof DynField_LabelAttribute ) {
732
                return ((DynField_LabelAttribute)obj).getLabel();
733
            }
734
            } else if( "StructWhenTypeIsDynObject".equalsIgnoreCase(name) ) {
735
            if( obj instanceof DynField_v2 ) {
736
                return ((DynField_v2)obj).getDynClassOfValue();
737
            }
738
            }
739
            return null;
740
    }
741

    
742
    public void setAttributeValue(Object obj, String name, Object value) {
743
        if( "label".equalsIgnoreCase(name) ) {
744
            if( obj instanceof DynField_LabelAttribute ) {
745
                ((DynField_LabelAttribute)obj).setLabel((String) value);
746
            }
747
            } else if( "StructWhenTypeIsDynObject".equalsIgnoreCase(name) ) {
748
            if( obj instanceof DynField_v2 ) {
749
                ((DynField_v2)obj).setClassOfValue((DynStruct) value);
750
            }
751
            }
752
    }
753

    
754
    @Override
755
    public DynObjectEncoder createSimpleDynObjectEncoder() {
756
        return new SimpleDynObjectEncoder();
757
    }
758

    
759
    @Override
760
    public void copy(DynObject source, DynObject target) {
761
        for (DynField field : target.getDynClass().getDynFields() ) {
762
            String name = field.getName();
763
            try {
764
                Object value = source.getDynValue(name);
765
                target.setDynValue(name, value);
766
            } catch(DynFieldNotFoundException | DefaultDynObject.CoerceValueException ex) {
767
                // if field not found in source or types are inconsistent, ignore it.
768
            }
769
        }
770
    }
771
    
772
    public void clear(DynObject obj) {
773
        for (DynField field : obj.getDynClass().getDynFields() ) {
774
            String name = field.getName();
775
            try {
776
                Object value = field.getDefaultValue();
777
                obj.setDynValue(name, value);
778
            } catch(DefaultDynObject.CoerceValueException ex) {
779
                // if types are inconsistent, ignore it.
780
            }
781
        }        
782
    }
783

    
784
    @Override
785
    public DynField createDynField(String name) {
786
        return new DefaultDynField(name, DataTypes.STRING);
787
    }
788
    
789
    
790
}