Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2020 / libraries / libRemoteServices / src / org / gvsig / remoteclient / wfs / filters / BinaryTree.java @ 33910

History | View | Annotate | Download (5.37 KB)

1 29677 jpiera
package org.gvsig.remoteclient.wfs.filters;
2 7897 jorpiell
3
4
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id$
47
 * $Log$
48 8699 jorpiell
 * Revision 1.3  2006-11-13 11:35:02  jorpiell
49
 * Comitados los cambios que tienen en cuanta las comillas simples y dobles
50
 *
51
 * Revision 1.2  2006/11/06 13:09:39  jorpiell
52 8528 jorpiell
 * Hay que quitarle las cmillas dobles a los nombres de los atributos en el Filtro
53
 *
54
 * Revision 1.1  2006/10/05 10:25:15  jorpiell
55 7897 jorpiell
 * Implementado el filter encoding
56
 *
57
 *
58
 */
59
/**
60
 * This class implements a binary tree (not ordered) that is
61
 * used to build a sintactic tree for the SQL language.
62
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
63 17104 vcaballero
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
64 7897 jorpiell
 */
65
public class BinaryTree {
66
        private Node root;
67
        private Node currentNode;
68
69
        public BinaryTree(){
70
                root = null;
71
                currentNode = null;
72
        }
73 17104 vcaballero
74 7897 jorpiell
        /**
75
         * This method is called for each term that is on
76 17104 vcaballero
         * the SQL query. It's supposed that the value is a valid expression, otherwise will launch an exception.
77
         *
78
         * @param value expression formatted
79 7897 jorpiell
         */
80 17104 vcaballero
        public void addTerm(String value) {
81
                if (value.equals("(")) {
82
                        if (currentNode == null) {
83
                                currentNode = new Node();
84
85
                                if (root == null) {
86
                                        root = currentNode;
87
                                }
88
                        }
89
                        else {
90
                                if (currentNode.leftNode == null) {
91 7897 jorpiell
                                        currentNode.leftNode = new Node(currentNode);
92
                                        currentNode = currentNode.leftNode;
93 17104 vcaballero
                                }
94
                                else {
95 7897 jorpiell
                                        currentNode.rigthNode = new Node(currentNode);
96
                                        currentNode = currentNode.rigthNode;
97
                                }
98
                        }
99 17104 vcaballero
                }
100
                else {
101
                        if (value.equals(")")) {
102
                                // Do nothing
103
                        }
104
                        else {
105
                                // Add the expression or the operand
106
107
                                // Seeks for the first parent node without a value defined, if there isn't any, creates a new one
108
                                while (currentNode.value != null) {
109
                                        if (currentNode.parentNode == null) {
110
                                                currentNode.parentNode = new Node();
111
                                                currentNode.parentNode.leftNode = root;
112
                                                root = currentNode.parentNode;
113
                                                currentNode = root;
114
                                                break;
115
                                        }
116
117
                                        currentNode = currentNode.parentNode;
118
                                }
119
120
                                // Sets the expression or operand value
121
                                currentNode.value = value;
122
                        }
123
                }
124 7897 jorpiell
        }
125 18271 jpiera
126
        /**
127
         * Adds a new term to the tree
128
         * @param name
129
         * @param value
130
         * @param operationPair
131
         * @param operationTree
132
         */
133
        public void addTerm(String value, String operationTree ) {
134
                Node operationNode = new Node();
135
                operationNode.value = value;
136
                if (root == null) {
137
                        root = operationNode;
138
                }else{
139
                        Node newRoot = new Node();
140
                        newRoot.value = operationTree;
141
                        newRoot.leftNode = root;
142
                        newRoot.rigthNode = operationNode;
143
                        root = newRoot;
144
                }
145
        }
146 7897 jorpiell
147
        /**
148
         * Print all the tree
149
         *
150
         */
151
        public void printTree(){
152
                printNode(root,0);
153
        }
154
155
        /**
156
         * Print one node
157
         * @param node
158
         * Node to print
159
         * @param level
160
         * Level node
161
         */
162
        private void printNode(Node node,int level){
163
                if (node != null){
164
                        String tab = "";
165
                        for (int i=0 ; i<level ; i++){
166
                                tab = tab + "\t";
167
                        }
168
                        level++;
169
                        if (node.isField()){
170
                                System.out.print(tab + node.value + "\n");
171
                        }else{
172
                                System.out.print(tab + node.value + "\n");
173
                                printNode(node.leftNode,level);
174
                                printNode(node.rigthNode,level);
175
                                System.out.print(tab + "\\" + node.value + "\n");
176
                        }
177
                }
178
        }
179
180
        /**
181
         * @return Returns the root.
182
         */
183
        public Node getRoot() {
184
                return root;
185
        }
186
187
        /**
188
         * This class represents a binary tree node.
189
         * @author Jorge Piera Llodr? (piera_jor@gva.es)
190
         *
191
         */
192
        public class Node{
193
                private String value;
194
                private Node leftNode;
195
                private Node rigthNode;
196
                private Node parentNode;
197
198
                private Node(){
199
                        leftNode = null;
200
                        rigthNode = null;
201
                        parentNode = null;
202
                }
203
204
                private Node(Node parentNode){
205
                        leftNode = null;
206
                        rigthNode = null;
207
                        this.parentNode = parentNode;
208
                }
209
210 18271 jpiera
                private Node(Node parentNode, String value){
211
                        leftNode = null;
212
                        rigthNode = null;
213
                        this.value = value;
214
                        this.parentNode = parentNode;
215
                }
216
217 7897 jorpiell
                public boolean isField(){
218
                        if ((leftNode == null)&&
219
                                        (rigthNode == null)){
220
                                return true;
221
                        }
222
                        return false;
223
                }
224
225
                /**
226
                 * @return Returns the leftNode.
227
                 */
228
                public Node getLeftNode() {
229
                        return leftNode;
230
                }
231
232
                /**
233
                 * @return Returns the rigthNode.
234
                 */
235
                public Node getRigthNode() {
236
                        return rigthNode;
237
                }
238
239
                /**
240
                 * @return Returns the value.
241
                 */
242
                public String getValue() {
243
                        return value;
244
                }
245
        }
246
247
248
}