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 @ 40769
History | View | Annotate | Download (4.74 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 |
* This class implements a binary tree (not ordered) that is
|
28 |
* used to build a sintactic tree for the SQL language.
|
29 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
30 |
* @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
|
31 |
*/
|
32 |
public class BinaryTree { |
33 |
private Node root;
|
34 |
private Node currentNode;
|
35 |
|
36 |
public BinaryTree(){
|
37 |
root = null;
|
38 |
currentNode = null;
|
39 |
} |
40 |
|
41 |
/**
|
42 |
* This method is called for each term that is on
|
43 |
* the SQL query. It's supposed that the value is a valid expression, otherwise will launch an exception.
|
44 |
*
|
45 |
* @param value expression formatted
|
46 |
*/
|
47 |
public void addTerm(String value) { |
48 |
if (value.equals("(")) { |
49 |
if (currentNode == null) { |
50 |
currentNode = new Node();
|
51 |
|
52 |
if (root == null) { |
53 |
root = currentNode; |
54 |
} |
55 |
} |
56 |
else {
|
57 |
if (currentNode.leftNode == null) { |
58 |
currentNode.leftNode = new Node(currentNode);
|
59 |
currentNode = currentNode.leftNode; |
60 |
} |
61 |
else {
|
62 |
currentNode.rigthNode = new Node(currentNode);
|
63 |
currentNode = currentNode.rigthNode; |
64 |
} |
65 |
} |
66 |
} |
67 |
else {
|
68 |
if (value.equals(")")) { |
69 |
// Do nothing
|
70 |
} |
71 |
else {
|
72 |
// Add the expression or the operand
|
73 |
|
74 |
// Seeks for the first parent node without a value defined, if there isn't any, creates a new one
|
75 |
while (currentNode.value != null) { |
76 |
if (currentNode.parentNode == null) { |
77 |
currentNode.parentNode = new Node();
|
78 |
currentNode.parentNode.leftNode = root; |
79 |
root = currentNode.parentNode; |
80 |
currentNode = root; |
81 |
break;
|
82 |
} |
83 |
|
84 |
currentNode = currentNode.parentNode; |
85 |
} |
86 |
|
87 |
// Sets the expression or operand value
|
88 |
currentNode.value = value; |
89 |
} |
90 |
} |
91 |
} |
92 |
|
93 |
/**
|
94 |
* Adds a new term to the tree
|
95 |
* @param name
|
96 |
* @param value
|
97 |
* @param operationPair
|
98 |
* @param operationTree
|
99 |
*/
|
100 |
public void addTerm(String value, String operationTree ) { |
101 |
Node operationNode = new Node();
|
102 |
operationNode.value = value; |
103 |
if (root == null) { |
104 |
root = operationNode; |
105 |
}else{
|
106 |
Node newRoot = new Node();
|
107 |
newRoot.value = operationTree; |
108 |
newRoot.leftNode = root; |
109 |
newRoot.rigthNode = operationNode; |
110 |
root = newRoot; |
111 |
} |
112 |
} |
113 |
|
114 |
/**
|
115 |
* Print all the tree
|
116 |
*
|
117 |
*/
|
118 |
public void printTree(){ |
119 |
printNode(root,0);
|
120 |
} |
121 |
|
122 |
/**
|
123 |
* Print one node
|
124 |
* @param node
|
125 |
* Node to print
|
126 |
* @param level
|
127 |
* Level node
|
128 |
*/
|
129 |
private void printNode(Node node,int level){ |
130 |
if (node != null){ |
131 |
String tab = ""; |
132 |
for (int i=0 ; i<level ; i++){ |
133 |
tab = tab + "\t";
|
134 |
} |
135 |
level++; |
136 |
if (node.isField()){
|
137 |
System.out.print(tab + node.value + "\n"); |
138 |
}else{
|
139 |
System.out.print(tab + node.value + "\n"); |
140 |
printNode(node.leftNode,level); |
141 |
printNode(node.rigthNode,level); |
142 |
System.out.print(tab + "\\" + node.value + "\n"); |
143 |
} |
144 |
} |
145 |
} |
146 |
|
147 |
/**
|
148 |
* @return Returns the root.
|
149 |
*/
|
150 |
public Node getRoot() {
|
151 |
return root;
|
152 |
} |
153 |
|
154 |
/**
|
155 |
* This class represents a binary tree node.
|
156 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
157 |
*
|
158 |
*/
|
159 |
public class Node{ |
160 |
private String value; |
161 |
private Node leftNode;
|
162 |
private Node rigthNode;
|
163 |
private Node parentNode;
|
164 |
|
165 |
private Node(){
|
166 |
leftNode = null;
|
167 |
rigthNode = null;
|
168 |
parentNode = null;
|
169 |
} |
170 |
|
171 |
private Node(Node parentNode){
|
172 |
leftNode = null;
|
173 |
rigthNode = null;
|
174 |
this.parentNode = parentNode;
|
175 |
} |
176 |
|
177 |
private Node(Node parentNode, String value){ |
178 |
leftNode = null;
|
179 |
rigthNode = null;
|
180 |
this.value = value;
|
181 |
this.parentNode = parentNode;
|
182 |
} |
183 |
|
184 |
public boolean isField(){ |
185 |
if ((leftNode == null)&& |
186 |
(rigthNode == null)){
|
187 |
return true; |
188 |
} |
189 |
return false; |
190 |
} |
191 |
|
192 |
/**
|
193 |
* @return Returns the leftNode.
|
194 |
*/
|
195 |
public Node getLeftNode() {
|
196 |
return leftNode;
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* @return Returns the rigthNode.
|
201 |
*/
|
202 |
public Node getRigthNode() {
|
203 |
return rigthNode;
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* @return Returns the value.
|
208 |
*/
|
209 |
public String getValue() { |
210 |
return value;
|
211 |
} |
212 |
} |
213 |
|
214 |
|
215 |
} |
216 |
|