svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / wfs / filters / ParseExpressions.java @ 40769
History | View | Annotate | Download (3.47 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 |
import java.util.ArrayList; |
27 |
|
28 |
/**
|
29 |
* It parses a SQL expression (just the where part) and seperates
|
30 |
* it in "sub-expresion". A sub-expresion could be a separator ('(' or ')'),
|
31 |
* a logical operator ('And', 'Not' or 'Or') or a expresion (A op B).
|
32 |
*
|
33 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
34 |
*/
|
35 |
public class ParseExpressions { |
36 |
|
37 |
/**
|
38 |
* It parses a expression and return one arrayList with
|
39 |
* the separated words
|
40 |
* @param expression
|
41 |
* @return
|
42 |
*/
|
43 |
public ArrayList parseExpression(String expression){ |
44 |
ArrayList expressions = new ArrayList(); |
45 |
|
46 |
char[] chars = expression.toCharArray(); |
47 |
int i=0; |
48 |
while (i<chars.length){
|
49 |
if (chars[i] == '('){ |
50 |
expressions.add("(");
|
51 |
i++; |
52 |
}else if(chars[i] == ')'){ |
53 |
expressions.add(")");
|
54 |
i++; |
55 |
}else if(chars[i] == ' '){ |
56 |
i++; |
57 |
}else{
|
58 |
int desp = getShift(chars,i);
|
59 |
expressions.add(expression.substring(i,i + desp)); |
60 |
i = i + desp; |
61 |
} |
62 |
} |
63 |
return expressions;
|
64 |
} |
65 |
|
66 |
/**
|
67 |
* Gets the shift of the next expression.
|
68 |
* @param chars
|
69 |
* @param position
|
70 |
* @return
|
71 |
*/
|
72 |
private int getShift(char[] chars,int position){ |
73 |
int shift = 0; |
74 |
shift = isAndOperator(chars,position); |
75 |
if (shift == 0){ |
76 |
shift = isOrOperator(chars,position); |
77 |
if (shift == 0){ |
78 |
shift = isNotOperator(chars,position); |
79 |
if (shift == 0){ |
80 |
shift = isExpression(chars,position); |
81 |
} |
82 |
} |
83 |
} |
84 |
return shift;
|
85 |
} |
86 |
|
87 |
private int isExpression(char[] chars,int position){ |
88 |
int desp = 0; |
89 |
for (int i=position ;i<chars.length ; i++){ |
90 |
if (chars[i] == ')'){ |
91 |
break;
|
92 |
} |
93 |
desp++; |
94 |
} |
95 |
return desp;
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* Return true if the next array is a AND operator
|
100 |
* @param chars
|
101 |
* @param position
|
102 |
* @return
|
103 |
*/
|
104 |
private int isAndOperator(char[] chars,int position){ |
105 |
if (chars[position] == 'A' && |
106 |
chars[position+1] == 'N' && |
107 |
chars[position+2] == 'D' && |
108 |
chars[position+3] == ' '){ |
109 |
return 3; |
110 |
} |
111 |
return 0; |
112 |
} |
113 |
|
114 |
/**
|
115 |
* Return true if the next array is a OR operator
|
116 |
* @param chars
|
117 |
* @param position
|
118 |
* @return
|
119 |
*/
|
120 |
private int isOrOperator(char[] chars,int position){ |
121 |
if (chars[position] == 'O' && |
122 |
chars[position+1] == 'R' && |
123 |
chars[position+2] == ' '){ |
124 |
return 2; |
125 |
} |
126 |
return 0; |
127 |
} |
128 |
|
129 |
/**
|
130 |
* Return true if the next array is a NOT operator
|
131 |
* @param chars
|
132 |
* @param position
|
133 |
* @return
|
134 |
*/
|
135 |
private int isNotOperator(char[] chars,int position){ |
136 |
if (chars[position] == 'N' && |
137 |
chars[position+1] == 'O' && |
138 |
chars[position+2] == 'T' && |
139 |
chars[position+3] == ' '){ |
140 |
return 3; |
141 |
} |
142 |
return 0; |
143 |
} |
144 |
|
145 |
} |