Statistics
| Revision:

svn-gvsig-desktop / 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 / symboltable / BookmarksSymbolTable.java @ 46831

History | View | Annotate | Download (5.24 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.expressionevaluator.impl.symboltable;
24

    
25
import java.util.AbstractMap;
26
import java.util.AbstractSet;
27
import java.util.ArrayList;
28
import java.util.Collections;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.Map;
32
import java.util.Set;
33
import org.apache.commons.lang3.StringUtils;
34
import org.gvsig.expressionevaluator.Code;
35
import org.gvsig.expressionevaluator.Expression;
36
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
37
import org.gvsig.expressionevaluator.Function;
38
import org.gvsig.expressionevaluator.SymbolTable;
39
import org.gvsig.expressionevaluator.impl.function.programming.CreateFnFunction;
40
import org.gvsig.expressionevaluator.impl.function.programming.VarFunction;
41
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
42
import org.gvsig.tools.bookmarksandhistory.Bookmark;
43
import org.gvsig.tools.bookmarksandhistory.Bookmarks;
44
import org.gvsig.tools.exception.BaseException;
45

    
46
/**
47
 *
48
 * @author gvSIG Team
49
 */
50
public class BookmarksSymbolTable
51
        extends AbstractSymbolTable
52
        implements SymbolTable {
53
    static final String NAME = "Bookmarks";
54

    
55
    private static class BookmarksFunctionsEntrySet
56
            extends AbstractSet<Map.Entry<String, Function>>
57
            implements Set<Map.Entry<String, Function>> {
58

    
59
        @Override
60
        public int size() {
61
            Bookmarks<Expression> bookmarks = ExpressionEvaluatorLocator.getExpressionEvaluatorManager().getBookmarks();
62
            int count = 0;
63
            for (Bookmark<Expression> bookmark : bookmarks) {
64
                if (StringUtils.startsWith(bookmark.getName(), "$")) {
65
                    count++;
66
                }
67
            }
68
            return count;
69
        }
70

    
71
        @Override
72
        public Iterator<Map.Entry<String, Function>> iterator() {
73
            final Bookmarks<Expression> bookmarks = ExpressionEvaluatorLocator.getExpressionEvaluatorManager().getBookmarks();
74
            Iterator<Map.Entry<String, Function>> it = new Iterator<Map.Entry<String, Function>>() {
75
                private final Iterator<Bookmark<Expression>> bookmarksIt = bookmarks.iterator();
76
                private Bookmark<Expression> current = null;
77

    
78
                @Override
79
                public boolean hasNext() {
80
                    while (bookmarksIt.hasNext()) {
81
                        this.current = this.bookmarksIt.next();
82
                        if (StringUtils.startsWith(this.current.getName(), "$")) {
83
                            return true;
84
                        }
85
                    }
86
                    return false;
87
                }
88

    
89
                @Override
90
                public Map.Entry<String, Function> next() {
91
                    if (this.current == null) {
92
                        return null;
93
                    }
94
                    Code code = this.current.getValue().getCode();
95
                    List<String> argNames = new ArrayList<>();
96
                    try {
97
                        code.accept((Object o) -> {
98
                            if (((Code) o).code() == Code.CALLABLE) {
99
                                Code.Callable c = (Code.Callable) o;
100
                                if(StringUtils.equalsIgnoreCase(c.name(), VarFunction.NAME)){
101
                                    argNames.add(((Code.Identifier) c.parameters().get(0)).name());
102
                                }
103
                            }
104
                        });
105
                    } catch (BaseException ex) {
106
                    }
107
                    Function function = new CreateFnFunction.UserFunction(
108
                            NAME,
109
                            this.current.getName(),
110
                            argNames,
111
                            code
112
                    );
113
                    return new AbstractMap.SimpleEntry(
114
                            this.current.getName().toUpperCase(),
115
                            function
116
                    );
117
                }
118
            };
119
            return it;
120
        }
121
    }
122

    
123
    public BookmarksSymbolTable() {
124
        super(NAME);
125
        this.functions = new AbstractMap<String, Function>() {
126
            @Override
127
            public Set<Map.Entry<String, Function>> entrySet() {
128
                return new BookmarksFunctionsEntrySet();
129
            }
130
        };
131
    }
132

    
133
    @Override
134
    protected Map<String, Object> getVars() {
135
        return Collections.EMPTY_MAP;
136
    }
137

    
138
}