Revision 45620

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/spi/DynObjectSymbolTable.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 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 3
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.expressionevaluator.spi;
25

  
26
import java.util.AbstractMap;
27
import java.util.AbstractSet;
28
import java.util.Iterator;
29
import java.util.Map;
30
import java.util.Set;
31
import org.apache.commons.lang3.tuple.MutablePair;
32
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
33
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
34
import org.gvsig.expressionevaluator.MutableSymbolTable;
35
import org.gvsig.tools.ToolsLocator;
36
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.tools.dynobject.DynObjectManager;
38
import org.gvsig.tools.lang.Cloneable;
39

  
40
/**
41
 *
42
 * @author gvSIG Team
43
 */
44
public class DynObjectSymbolTable extends AbstractSymbolTable implements MutableSymbolTable {
45

  
46
    private DynObject obj;
47
    private Map<String, Object> wrapper;
48
    
49
    public DynObjectSymbolTable(DynObject obj) {
50
        this.obj = obj;
51
        this.wrapper = createDynObjectToMapAdapter(obj);
52

  
53
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
54
        manager.populateSymbolTable(this);
55
    }
56

  
57
    private static Map createDynObjectToMapAdapter(DynObject obj) {
58
        return new AbstractMap<String, Object>() {
59
            @Override
60
            public Set<Map.Entry<String, Object>> entrySet() {
61
                return new AbstractSet<Map.Entry<String, Object>>() {
62
                    @Override
63
                    public Iterator<Map.Entry<String, Object>> iterator() {
64
                        return new Iterator<Map.Entry<String, Object>>() {
65
                            int n=0;
66
                            @Override
67
                            public boolean hasNext() {
68
                                return n < obj.getDynClass().getDynFields().length;
69
                            }
70

  
71
                            @Override
72
                            public Map.Entry<String, Object> next() {
73
                                String name = obj.getDynClass().getDynFields()[n++].getName();
74
                                return new MutablePair<>(name, obj.getDynValue(name));
75
                            }
76
                        };
77
                    }
78

  
79
                    @Override
80
                    public int size() {
81
                        return obj.getDynClass().getDynFields().length;
82
                    }
83
                };
84
            }
85
        };
86
    }
87
    
88
    @Override
89
    protected Map<String, Object> getVars() {
90
        return this.wrapper;
91
    }
92

  
93
    @Override
94
    public void setVar(String name, Object value) {
95
        this.obj.setDynValue(name, value);
96
    }
97

  
98
    @Override
99
    public void removeVar(String name) {
100
        throw new UnsupportedOperationException("Not supported."); 
101
    }
102

  
103
    @Override
104
    public MutableSymbolTable clone() throws CloneNotSupportedException {
105
        DynObjectSymbolTable other = (DynObjectSymbolTable) super.clone();
106
        DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager();
107
        other.obj = (DynObject) Cloneable.cloneQuietly(this.obj);
108
        if( other.obj == null ) {
109
            other.obj = dynObjectManager.createDynObject(obj.getDynClass());
110
            dynObjectManager.copy(this.obj, other.obj);
111
        }
112
        other.wrapper = createDynObjectToMapAdapter(other.obj);
113
        return other;
114
    }
115

  
116
}
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/spi/AbstractSymbolTable.java
42 42
//
43 43
//    }
44 44

  
45
    private Map<String, Object> vars;
46

  
45 47
    protected final List<SymbolTable> symbolTables;
46
    protected Map<String, Object> vars;
47 48
    protected Map<String, Function> functions;
48 49
    protected Map<String, Function> functionAlias;
49 50
//    protected List<Script> scripts;
......
58 59
        this.vars = null;
59 60
        this.functions = null;
60 61
        this.functionAlias = null;
61
//        this.scripts = null;
62 62
    }
63 63

  
64 64
    @Override
......
66 66
        return name;
67 67
    }
68 68

  
69
    protected void addFunction(Function function) {
69
    public void addFunction(Function function) {
70 70
        if (function == null) {
71 71
            throw new IllegalArgumentException("function can't be null");
72 72
        }
......
83 83
        }
84 84
    }
85 85
    
86
    public void addFunctions(Collection<Function> functions) {
87
        for (Function function : functions) {
88
            this.addFunction(function);
89
        }
90
    }
91

  
92
    public void removeFunction(String name) {
93
        if( StringUtils.isEmpty(name) ) {
94
            throw new IllegalArgumentException("name can't be null");
95
        }
96
        this.functions.remove(name.toUpperCase());
97
    }
98
    
86 99
    @Override
87 100
    public boolean addSymbolTable(SymbolTable symbolTable) {
88 101
        if( symbolTable==null ) {
......
113 126
        return this.vars;
114 127
    }
115 128

  
129
    public void setVar(String name, Object value) {
130
        if( StringUtils.isEmpty(name) ) {
131
            throw new IllegalArgumentException("name can't be null");
132
        }
133
        this.getVars().put(name.toUpperCase(), value);
134
    }
135

  
136
    public void removeVar(String name) {
137
        if( StringUtils.isEmpty(name) ) {
138
            throw new IllegalArgumentException("name can't be null");
139
        }
140
        this.getVars().remove(name.toUpperCase());
141
    }
142

  
116 143
    protected Map<String, Function> getFunctions() {
117 144
        if (this.functions == null) {
118 145
            this.functions = new HashMap<>();
......
127 154
        return this.functionAlias;
128 155
    }
129 156

  
130
//    protected List<Script> getScripts() {
131
//        if (this.scripts == null) {
132
//            this.scripts = new ArrayList<>();
133
//        }
134
//        return this.scripts;
135
//    }
136
//
137 157
    @Override
138 158
    public boolean exists(String name) {
139 159
        if (StringUtils.isEmpty(name)) {
140 160
            return false;
141 161
        }
142
        if (this.vars != null) {
143
            if (this.vars.containsKey(name.toUpperCase())) {
144
                return true;
145
            }
162
        if (this.getVars().containsKey(name.toUpperCase())) {
163
            return true;
146 164
        }
147 165
        for (SymbolTable other : this.symbolTables) {
148 166
            if (other.exists(name)) {
......
157 175
        if (StringUtils.isEmpty(name)) {
158 176
            return null;
159 177
        }
160
        if (this.vars != null) {
161
            name = name.toUpperCase();
162
            if (this.vars.containsKey(name)) {
163
                return this.vars.get(name);
164
            }
178
        name = name.toUpperCase();
179
        if (this.getVars().containsKey(name)) {
180
            return this.getVars().get(name);
165 181
        }
166 182
        for (SymbolTable other : this.symbolTables) {
167 183
            if (other.exists(name)) {
......
191 207
        for (SymbolTable other : this.symbolTables) {
192 208
            Function fn = other.function(name);
193 209
            if (fn != null) {
194
//                if (fn instanceof ScriptFunction) {
195
//                    continue;
196
//                }
197 210
                return fn;
198 211
            }
199 212
        }
200
//        if (this.scripts != null && ! this.scripts.isEmpty()) {
201
//            return new ScriptFunction(name);
202
//        }
203 213
        return null;
204 214
    }
205 215

  
......
235 245

  
236 246
    @Override
237 247
    public Collection<String> localvariables() {
238
        if( this.vars == null ) {
248
        if( this.getVars().size() == 0) {
239 249
            return Collections.EMPTY_LIST;
240 250
        }
241 251
        return Collections.unmodifiableCollection(this.vars.keySet());
242 252
    }
243 253

  
244
//    @Override
245
//    public Collection<Script> scripts() {
246
//        Set<Script> theScripts = new HashSet<>();
247
//        for (SymbolTable symbolTable : this.symbolTables) {
248
//            theScripts.addAll(symbolTable.scripts());
249
//        }
250
//        if (this.scripts != null) {
251
//            theScripts.addAll(this.scripts);
252
//        }
253
//        return Collections.unmodifiableCollection(theScripts);
254
//    }
255
//
256 254
    @Override
257 255
    public Iterator<Function> iterator() {
258 256
        return this.functions().iterator();
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/ExpressionUtils.java
12 12
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
13 13
import static org.gvsig.expressionevaluator.ExpressionEvaluatorManager.DYNAMICTEXT_ENDTAG;
14 14
import static org.gvsig.expressionevaluator.ExpressionEvaluatorManager.DYNAMICTEXT_STARTTAG;
15
import org.gvsig.expressionevaluator.spi.DynObjectSymbolTable;
16
import org.gvsig.tools.dynobject.DynObject;
15 17
import org.gvsig.tools.util.ListBuilder;
16 18

  
17 19
/**
......
171 173
        return symbolTable;
172 174
    }
173 175
    
176
    public static MutableSymbolTable createSymbolTable(DynObject obj) {
177
        DynObjectSymbolTable symbolTable = new DynObjectSymbolTable(obj);
178
        return symbolTable;
179
    }
180
    
174 181
    public static String surroundByDynamicTextTag(String source) {
175 182
        return surroundByDynamicTextTag(source, true);
176 183
    }
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/MutableSymbolTable.java
1 1
package org.gvsig.expressionevaluator;
2 2

  
3 3
import java.util.Collection;
4
import org.gvsig.tools.script.Script;
5 4

  
6

  
7 5
public interface MutableSymbolTable extends SymbolTable {
8 6

  
9 7
    public void setVar(String name, Object value);
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/DefaultSymbolTable.java
1 1
package org.gvsig.expressionevaluator.impl;
2 2

  
3 3
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
4
import java.util.Collection;
5
import java.util.HashMap;
6
import org.apache.commons.lang3.StringUtils;
7 4
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
8
import org.gvsig.expressionevaluator.Function;
9 5
import org.gvsig.expressionevaluator.MutableSymbolTable;
10 6

  
11 7
/**
......
20 16

  
21 17
    public DefaultSymbolTable(String name) {
22 18
        super(name);
23
        this.vars = new HashMap<>();
24
        this.functions = new HashMap<>();
25
//        this.scripts = new ArrayList<>();
19
//        this.functions = new HashMap<>();
26 20
        this.init();
27 21
    }
28 22
    
......
35 29
        manager.populateSymbolTable(this);
36 30
    }
37 31
    
38
    @Override
39
    public void setVar(String name, Object value) {
40
        if( StringUtils.isEmpty(name) ) {
41
            throw new IllegalArgumentException("name can't be null");
42
        }
43
        this.vars.put(name.toUpperCase(), value);
44
    }
45

  
46
    @Override
47
    public void addFunction(Function function) {
48
        super.addFunction(function);
49
    }
50

  
51
    @Override
52
    public void addFunctions(Collection<Function> functions) {
53
        for (Function function : functions) {
54
            this.addFunction(function);
55
        }
56
    }
57

  
58
    @Override
59
    public void removeVar(String name) {
60
        if( StringUtils.isEmpty(name) ) {
61
            throw new IllegalArgumentException("name can't be null");
62
        }
63
        this.vars.remove(name.toUpperCase());
64
    }
65

  
66
    @Override
67
    public void removeFunction(String name) {
68
        if( StringUtils.isEmpty(name) ) {
69
            throw new IllegalArgumentException("name can't be null");
70
        }
71
        this.functions.remove(name.toUpperCase());
72
    }
73

  
74 32
//    @Override
75
//    public void addScript(Script script) {
76
//        if( script == null ) {
77
//            throw new IllegalArgumentException("script can't be null");
33
//    public void setVar(String name, Object value) {
34
//        if( StringUtils.isEmpty(name) ) {
35
//            throw new IllegalArgumentException("name can't be null");
78 36
//        }
79
//        if (this.scripts.contains(script)) {
80
//            return;
37
//        this.getVars().put(name.toUpperCase(), value);
38
//    }
39
//
40
//    @Override
41
//    public void addFunction(Function function) {
42
//        super.addFunction(function);
43
//    }
44
//
45
//    @Override
46
//    public void addFunctions(Collection<Function> functions) {
47
//        for (Function function : functions) {
48
//            this.addFunction(function);
81 49
//        }
82
//        this.scripts.add(script);
83 50
//    }
84 51
//
85 52
//    @Override
86
//    public void removeScript(Script script) {
87
//        if( script == null ) {
88
//            throw new IllegalArgumentException("script can't be null");
53
//    public void removeVar(String name) {
54
//        if( StringUtils.isEmpty(name) ) {
55
//            throw new IllegalArgumentException("name can't be null");
89 56
//        }
90
//        this.scripts.remove(script);
57
//        this.getVars().remove(name.toUpperCase());
91 58
//    }
59
//
60
//    @Override
61
//    public void removeFunction(String name) {
62
//        if( StringUtils.isEmpty(name) ) {
63
//            throw new IllegalArgumentException("name can't be null");
64
//        }
65
//        this.functions.remove(name.toUpperCase());
66
//    }
92 67

  
93 68
    @Override
94 69
    public MutableSymbolTable clone() throws CloneNotSupportedException {

Also available in: Unified diff