Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wfs / filters / BinaryTree.java @ 8528

History | View | Annotate | Download (3.98 KB)

1
package org.gvsig.remoteClient.wfs.filters;
2

    
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: BinaryTree.java 8528 2006-11-06 13:10:14Z jorpiell $
47
 * $Log$
48
 * Revision 1.2  2006-11-06 13:09:39  jorpiell
49
 * Hay que quitarle las cmillas dobles a los nombres de los atributos en el Filtro
50
 *
51
 * Revision 1.1  2006/10/05 10:25:15  jorpiell
52
 * Implementado el filter encoding
53
 *
54
 *
55
 */
56
/**
57
 * This class implements a binary tree (not ordered) that is
58
 * used to build a sintactic tree for the SQL language.
59
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
60
 */
61
public class BinaryTree {
62
        private Node root;
63
        private Node currentNode;
64
        
65
        public BinaryTree(){
66
                root = null;
67
                currentNode = null;
68
        }
69
        
70
        /**
71
         * This method is called for each term that is on 
72
         * the SQL query
73
         * @param value
74
         */
75
        public void addTerm(String value){
76
                if (value.equals("(")){
77
                        if (root == null){
78
                                root = new Node();
79
                                currentNode = root;
80
                        }else{
81
                                if (currentNode.leftNode == null){
82
                                        currentNode.leftNode = new Node(currentNode);
83
                                        currentNode = currentNode.leftNode;
84
                                }else if(currentNode.rigthNode == null){
85
                                        currentNode.rigthNode = new Node(currentNode);
86
                                        currentNode = currentNode.rigthNode;
87
                                }
88
                        }
89
                }else if(value.equals(")")){
90
                        currentNode = currentNode.parentNode;
91
                }else{
92
                        currentNode.value = value.replaceAll("\"","");
93
                }                        
94
        }
95

    
96
        /**
97
         * Print all the tree
98
         *
99
         */
100
        public void printTree(){
101
                printNode(root,0);
102
        }        
103
        
104
        /**
105
         * Print one node
106
         * @param node
107
         * Node to print
108
         * @param level
109
         * Level node
110
         */
111
        private void printNode(Node node,int level){
112
                if (node != null){
113
                        String tab = "";
114
                        for (int i=0 ; i<level ; i++){
115
                                tab = tab + "\t";
116
                        }
117
                        level++;
118
                        if (node.isField()){
119
                                System.out.print(tab + node.value + "\n");
120
                        }else{
121
                                System.out.print(tab + node.value + "\n");
122
                                printNode(node.leftNode,level);
123
                                printNode(node.rigthNode,level);
124
                                System.out.print(tab + "\\" + node.value + "\n");
125
                        }
126
                }                
127
        }
128
        
129
        /**
130
         * @return Returns the root.
131
         */
132
        public Node getRoot() {
133
                return root;
134
        }        
135
        
136
        /**
137
         * This class represents a binary tree node.
138
         * @author Jorge Piera Llodr? (piera_jor@gva.es)
139
         *
140
         */
141
        public class Node{
142
                private String value;
143
                private Node leftNode;
144
                private Node rigthNode;
145
                private Node parentNode;
146
                
147
                private Node(){
148
                        leftNode = null;
149
                        rigthNode = null;
150
                        parentNode = null;
151
                }
152
                
153
                private Node(Node parentNode){
154
                        leftNode = null;
155
                        rigthNode = null;
156
                        this.parentNode = parentNode;
157
                }
158
                
159
                public boolean isField(){
160
                        if ((leftNode == null)&&
161
                                        (rigthNode == null)){
162
                                return true;
163
                        }
164
                        return false;
165
                }
166

    
167
                /**
168
                 * @return Returns the leftNode.
169
                 */
170
                public Node getLeftNode() {
171
                        return leftNode;
172
                }
173

    
174
                /**
175
                 * @return Returns the rigthNode.
176
                 */
177
                public Node getRigthNode() {
178
                        return rigthNode;
179
                }
180

    
181
                /**
182
                 * @return Returns the value.
183
                 */
184
                public String getValue() {
185
                        return value;
186
                }
187
        }
188

    
189
        
190
}
191