Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wfs / filters / BinaryTree.java @ 40559

History | View | Annotate | Download (6.36 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 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.remoteclient.wfs.filters;
25

    
26

    
27
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
28
 *
29
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
30
 *
31
 * This program is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU General Public License
33
 * as published by the Free Software Foundation; either version 2
34
 * of the License, or (at your option) any later version.
35
 *
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39
 * GNU General Public License for more details.
40
 *
41
 * You should have received a copy of the GNU General Public License
42
 * along with this program; if not, write to the Free Software
43
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
44
 *
45
 * For more information, contact:
46
 *
47
 *  Generalitat Valenciana
48
 *   Conselleria d'Infraestructures i Transport
49
 *   Av. Blasco Ib??ez, 50
50
 *   46010 VALENCIA
51
 *   SPAIN
52
 *
53
 *      +34 963862235
54
 *   gvsig@gva.es
55
 *      www.gvsig.gva.es
56
 *
57
 *    or
58
 *
59
 *   IVER T.I. S.A
60
 *   Salamanca 50
61
 *   46005 Valencia
62
 *   Spain
63
 *
64
 *   +34 963163400
65
 *   dac@iver.es
66
 */
67
/* CVS MESSAGES:
68
 *
69
 * $Id: BinaryTree.java 29677 2009-06-29 17:21:04Z jpiera $
70
 * $Log$
71
 * Revision 1.3  2006-11-13 11:35:02  jorpiell
72
 * Comitados los cambios que tienen en cuanta las comillas simples y dobles
73
 *
74
 * Revision 1.2  2006/11/06 13:09:39  jorpiell
75
 * Hay que quitarle las cmillas dobles a los nombres de los atributos en el Filtro
76
 *
77
 * Revision 1.1  2006/10/05 10:25:15  jorpiell
78
 * Implementado el filter encoding
79
 *
80
 *
81
 */
82
/**
83
 * This class implements a binary tree (not ordered) that is
84
 * used to build a sintactic tree for the SQL language.
85
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
86
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
87
 */
88
public class BinaryTree {
89
        private Node root;
90
        private Node currentNode;
91
        
92
        public BinaryTree(){
93
                root = null;
94
                currentNode = null;
95
        }
96

    
97
        /**
98
         * This method is called for each term that is on 
99
         * the SQL query. It's supposed that the value is a valid expression, otherwise will launch an exception.
100
         * 
101
         * @param value expression formatted
102
         */
103
        public void addTerm(String value) {
104
                if (value.equals("(")) {
105
                        if (currentNode == null) {
106
                                currentNode = new Node();
107
                                
108
                                if (root == null) {
109
                                        root = currentNode;
110
                                }
111
                        }
112
                        else {
113
                                if (currentNode.leftNode == null) {
114
                                        currentNode.leftNode = new Node(currentNode);
115
                                        currentNode = currentNode.leftNode;
116
                                }
117
                                else {
118
                                        currentNode.rigthNode = new Node(currentNode);
119
                                        currentNode = currentNode.rigthNode;
120
                                }
121
                        }
122
                }
123
                else {
124
                        if (value.equals(")")) {
125
                                // Do nothing
126
                        }
127
                        else {
128
                                // Add the expression or the operand
129

    
130
                                // Seeks for the first parent node without a value defined, if there isn't any, creates a new one
131
                                while (currentNode.value != null) {
132
                                        if (currentNode.parentNode == null) {
133
                                                currentNode.parentNode = new Node();
134
                                                currentNode.parentNode.leftNode = root;
135
                                                root = currentNode.parentNode;
136
                                                currentNode = root;
137
                                                break;
138
                                        }
139

    
140
                                        currentNode = currentNode.parentNode;
141
                                }
142
                                
143
                                // Sets the expression or operand value
144
                                currentNode.value = value;
145
                        }
146
                }
147
        }
148
        
149
        /**
150
         * Adds a new term to the tree
151
         * @param name
152
         * @param value
153
         * @param operationPair
154
         * @param operationTree
155
         */
156
        public void addTerm(String value, String operationTree ) {
157
                Node operationNode = new Node();
158
                operationNode.value = value;
159
                if (root == null) {
160
                        root = operationNode;                        
161
                }else{
162
                        Node newRoot = new Node();
163
                        newRoot.value = operationTree;
164
                        newRoot.leftNode = root;
165
                        newRoot.rigthNode = operationNode;
166
                        root = newRoot;
167
                }
168
        }
169

    
170
        /**
171
         * Print all the tree
172
         *
173
         */
174
        public void printTree(){
175
                printNode(root,0);
176
        }        
177
        
178
        /**
179
         * Print one node
180
         * @param node
181
         * Node to print
182
         * @param level
183
         * Level node
184
         */
185
        private void printNode(Node node,int level){
186
                if (node != null){
187
                        String tab = "";
188
                        for (int i=0 ; i<level ; i++){
189
                                tab = tab + "\t";
190
                        }
191
                        level++;
192
                        if (node.isField()){
193
                                System.out.print(tab + node.value + "\n");
194
                        }else{
195
                                System.out.print(tab + node.value + "\n");
196
                                printNode(node.leftNode,level);
197
                                printNode(node.rigthNode,level);
198
                                System.out.print(tab + "\\" + node.value + "\n");
199
                        }
200
                }                
201
        }
202
        
203
        /**
204
         * @return Returns the root.
205
         */
206
        public Node getRoot() {
207
                return root;
208
        }        
209
        
210
        /**
211
         * This class represents a binary tree node.
212
         * @author Jorge Piera Llodr? (piera_jor@gva.es)
213
         *
214
         */
215
        public class Node{
216
                private String value;
217
                private Node leftNode;
218
                private Node rigthNode;
219
                private Node parentNode;
220
                
221
                private Node(){
222
                        leftNode = null;
223
                        rigthNode = null;
224
                        parentNode = null;
225
                }
226
                
227
                private Node(Node parentNode){
228
                        leftNode = null;
229
                        rigthNode = null;
230
                        this.parentNode = parentNode;
231
                }
232
                
233
                private Node(Node parentNode, String value){
234
                        leftNode = null;
235
                        rigthNode = null;
236
                        this.value = value;
237
                        this.parentNode = parentNode;
238
                }
239
                
240
                public boolean isField(){
241
                        if ((leftNode == null)&&
242
                                        (rigthNode == null)){
243
                                return true;
244
                        }
245
                        return false;
246
                }
247

    
248
                /**
249
                 * @return Returns the leftNode.
250
                 */
251
                public Node getLeftNode() {
252
                        return leftNode;
253
                }
254

    
255
                /**
256
                 * @return Returns the rigthNode.
257
                 */
258
                public Node getRigthNode() {
259
                        return rigthNode;
260
                }
261

    
262
                /**
263
                 * @return Returns the value.
264
                 */
265
                public String getValue() {
266
                        return value;
267
                }
268
        }
269

    
270
        
271
}
272