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 @ 40559
History | View | Annotate | Download (4.84 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | package org.gvsig.remoteclient.wfs.filters; |
25 | |||
26 | import java.util.ArrayList; |
||
27 | |||
28 | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
29 | *
|
||
30 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
31 | *
|
||
32 | * This program is free software; you can redistribute it and/or
|
||
33 | * modify it under the terms of the GNU General Public License
|
||
34 | * as published by the Free Software Foundation; either version 2
|
||
35 | * of the License, or (at your option) any later version.
|
||
36 | *
|
||
37 | * This program is distributed in the hope that it will be useful,
|
||
38 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
39 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
40 | * GNU General Public License for more details.
|
||
41 | *
|
||
42 | * You should have received a copy of the GNU General Public License
|
||
43 | * along with this program; if not, write to the Free Software
|
||
44 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
45 | *
|
||
46 | * For more information, contact:
|
||
47 | *
|
||
48 | * Generalitat Valenciana
|
||
49 | * Conselleria d'Infraestructures i Transport
|
||
50 | * Av. Blasco Ib??ez, 50
|
||
51 | * 46010 VALENCIA
|
||
52 | * SPAIN
|
||
53 | *
|
||
54 | * +34 963862235
|
||
55 | * gvsig@gva.es
|
||
56 | * www.gvsig.gva.es
|
||
57 | *
|
||
58 | * or
|
||
59 | *
|
||
60 | * IVER T.I. S.A
|
||
61 | * Salamanca 50
|
||
62 | * 46005 Valencia
|
||
63 | * Spain
|
||
64 | *
|
||
65 | * +34 963163400
|
||
66 | * dac@iver.es
|
||
67 | */
|
||
68 | /* CVS MESSAGES:
|
||
69 | *
|
||
70 | * $Id: ParseExpressions.java 29677 2009-06-29 17:21:04Z jpiera $
|
||
71 | * $Log$
|
||
72 | * Revision 1.1 2006-10-05 10:25:15 jorpiell
|
||
73 | * Implementado el filter encoding
|
||
74 | *
|
||
75 | *
|
||
76 | */
|
||
77 | /**
|
||
78 | * It parses a SQL expression (just the where part) and seperates
|
||
79 | * it in "sub-expresion". A sub-expresion could be a separator ('(' or ')'),
|
||
80 | * a logical operator ('And', 'Not' or 'Or') or a expresion (A op B).
|
||
81 | *
|
||
82 | * @author Jorge Piera Llodr? (piera_jor@gva.es)
|
||
83 | */
|
||
84 | public class ParseExpressions { |
||
85 | |||
86 | /**
|
||
87 | * It parses a expression and return one arrayList with
|
||
88 | * the separated words
|
||
89 | * @param expression
|
||
90 | * @return
|
||
91 | */
|
||
92 | public ArrayList parseExpression(String expression){ |
||
93 | ArrayList expressions = new ArrayList(); |
||
94 | |||
95 | char[] chars = expression.toCharArray(); |
||
96 | int i=0; |
||
97 | while (i<chars.length){
|
||
98 | if (chars[i] == '('){ |
||
99 | expressions.add("(");
|
||
100 | i++; |
||
101 | }else if(chars[i] == ')'){ |
||
102 | expressions.add(")");
|
||
103 | i++; |
||
104 | }else if(chars[i] == ' '){ |
||
105 | i++; |
||
106 | }else{
|
||
107 | int desp = getShift(chars,i);
|
||
108 | expressions.add(expression.substring(i,i + desp)); |
||
109 | i = i + desp; |
||
110 | } |
||
111 | } |
||
112 | return expressions;
|
||
113 | } |
||
114 | |||
115 | /**
|
||
116 | * Gets the shift of the next expression.
|
||
117 | * @param chars
|
||
118 | * @param position
|
||
119 | * @return
|
||
120 | */
|
||
121 | private int getShift(char[] chars,int position){ |
||
122 | int shift = 0; |
||
123 | shift = isAndOperator(chars,position); |
||
124 | if (shift == 0){ |
||
125 | shift = isOrOperator(chars,position); |
||
126 | if (shift == 0){ |
||
127 | shift = isNotOperator(chars,position); |
||
128 | if (shift == 0){ |
||
129 | shift = isExpression(chars,position); |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | return shift;
|
||
134 | } |
||
135 | |||
136 | private int isExpression(char[] chars,int position){ |
||
137 | int desp = 0; |
||
138 | for (int i=position ;i<chars.length ; i++){ |
||
139 | if (chars[i] == ')'){ |
||
140 | break;
|
||
141 | } |
||
142 | desp++; |
||
143 | } |
||
144 | return desp;
|
||
145 | } |
||
146 | |||
147 | /**
|
||
148 | * Return true if the next array is a AND operator
|
||
149 | * @param chars
|
||
150 | * @param position
|
||
151 | * @return
|
||
152 | */
|
||
153 | private int isAndOperator(char[] chars,int position){ |
||
154 | if (chars[position] == 'A' && |
||
155 | chars[position+1] == 'N' && |
||
156 | chars[position+2] == 'D' && |
||
157 | chars[position+3] == ' '){ |
||
158 | return 3; |
||
159 | } |
||
160 | return 0; |
||
161 | } |
||
162 | |||
163 | /**
|
||
164 | * Return true if the next array is a OR operator
|
||
165 | * @param chars
|
||
166 | * @param position
|
||
167 | * @return
|
||
168 | */
|
||
169 | private int isOrOperator(char[] chars,int position){ |
||
170 | if (chars[position] == 'O' && |
||
171 | chars[position+1] == 'R' && |
||
172 | chars[position+2] == ' '){ |
||
173 | return 2; |
||
174 | } |
||
175 | return 0; |
||
176 | } |
||
177 | |||
178 | /**
|
||
179 | * Return true if the next array is a NOT operator
|
||
180 | * @param chars
|
||
181 | * @param position
|
||
182 | * @return
|
||
183 | */
|
||
184 | private int isNotOperator(char[] chars,int position){ |
||
185 | if (chars[position] == 'N' && |
||
186 | chars[position+1] == 'O' && |
||
187 | chars[position+2] == 'T' && |
||
188 | chars[position+3] == ' '){ |
||
189 | return 3; |
||
190 | } |
||
191 | return 0; |
||
192 | } |
||
193 | |||
194 | } |