001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.filter.visitor;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Stack;
022:
023: import org.geotools.filter.AttributeExpression;
024: import org.geotools.filter.BetweenFilter;
025: import org.geotools.filter.CompareFilter;
026: import org.geotools.filter.Expression;
027: import org.geotools.filter.FidFilter;
028: import org.geotools.filter.Filter;
029: import org.geotools.filter.FilterFactory;
030: import org.geotools.filter.FilterVisitor2;
031: import org.geotools.filter.Filters;
032: import org.geotools.filter.FunctionExpression;
033: import org.geotools.filter.GeometryFilter;
034: import org.geotools.filter.IllegalFilterException;
035: import org.geotools.filter.LikeFilter;
036: import org.geotools.filter.LiteralExpression;
037: import org.geotools.filter.LogicFilter;
038: import org.geotools.filter.MathExpression;
039: import org.geotools.filter.NullFilter;
040: import org.opengis.filter.ExcludeFilter;
041: import org.opengis.filter.IncludeFilter;
042:
043: /**
044: * Used to duplicate a Filter & or Expression
045: *
046: * @author Jody Garnett, Refractions Research Inc.
047: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/filter/visitor/DuplicatorFilterVisitor.java $
048: * @deprecated use {@link DuplicatingFilterVisitor}
049: */
050: public class DuplicatorFilterVisitor extends AbstractFilterVisitor
051: implements FilterVisitor2 {
052: protected Stack pages = new Stack(); // need a Stack as Filter structure is recursive
053: protected FilterFactory ff;
054: private boolean strict;
055:
056: public Stack getPages() {
057: return pages;
058: }
059:
060: /**
061: * New instance
062: * @param factory factory to use for creating the filters.
063: */
064: public DuplicatorFilterVisitor(FilterFactory factory) {
065: this (factory, true);
066: }
067:
068: /**
069: * New instance
070: * @param factory factory to use for creating the filters.
071: * @param strict if false then unkown filters will not be copied. The same instance will be returned as the copy.
072: * Otherwise an exception will be thrown when encountering an unkown filter.
073: */
074: public DuplicatorFilterVisitor(FilterFactory factory, boolean strict) {
075: ff = factory;
076: this .strict = strict;
077: }
078:
079: public void setFilterFactory(FilterFactory factory) {
080: ff = factory;
081: }
082:
083: public void visit(IncludeFilter filter) {
084: pages.push(Filter.INCLUDE);
085: }
086:
087: public void visit(ExcludeFilter filter) {
088: pages.push(Filter.EXCLUDE);
089: }
090:
091: public void visit(Filter filter) {
092: if (filter == org.geotools.filter.Filter.NONE) {
093: pages.push(org.geotools.filter.Filter.NONE);
094: return;
095: } else if (filter == org.geotools.filter.Filter.ALL) {
096: pages.push(org.geotools.filter.Filter.ALL);
097: return;
098: }
099: if (strict)
100: // Should not happen?
101: throw new RuntimeException("visit(Filter) unsupported");
102: else
103: pages.push(filter);
104: }
105:
106: public void visit(BetweenFilter filter) {
107: BetweenFilter copy = null;
108:
109: try {
110: Expression leftCopy = null;
111:
112: if (filter.getLeftValue() != null) {
113: filter.getLeftValue().accept(this );
114: leftCopy = (Expression) pages.pop();
115: }
116:
117: Expression middleCopy = null;
118:
119: if (filter.getMiddleValue() != null) {
120: filter.getMiddleValue().accept(this );
121: middleCopy = (Expression) pages.pop();
122: }
123:
124: Expression rightCopy = null;
125:
126: if (filter.getRightValue() != null) {
127: filter.getRightValue().accept(this );
128: rightCopy = (Expression) pages.pop();
129: }
130:
131: copy = ff.createBetweenFilter();
132: copy.addLeftValue(leftCopy);
133: copy.addMiddleValue(middleCopy);
134: copy.addRightValue(rightCopy);
135: } catch (IllegalFilterException erp) {
136: throw new RuntimeException(erp);
137: }
138:
139: pages.push(copy);
140: }
141:
142: public void visit(CompareFilter filter) {
143: CompareFilter copy = null;
144:
145: try {
146: Expression leftCopy = null;
147:
148: if (filter.getLeftValue() != null) {
149: filter.getLeftValue().accept(this );
150: leftCopy = (Expression) pages.pop();
151: }
152:
153: Expression rightCopy = null;
154:
155: if (filter.getRightValue() != null) {
156: filter.getRightValue().accept(this );
157: rightCopy = (Expression) pages.pop();
158: }
159:
160: copy = ff.createCompareFilter(filter.getFilterType());
161: copy.addLeftValue(leftCopy);
162: copy.addRightValue(rightCopy);
163: } catch (IllegalFilterException erp) {
164: throw new RuntimeException(erp);
165: }
166:
167: pages.push(copy);
168: }
169:
170: public void visit(GeometryFilter filter) {
171: GeometryFilter copy = null;
172:
173: try {
174: Expression leftCopy = null;
175:
176: if (filter.getLeftGeometry() != null) {
177: filter.getLeftGeometry().accept(this );
178: leftCopy = (Expression) pages.pop();
179: }
180:
181: Expression rightCopy = null;
182:
183: if (filter.getRightGeometry() != null) {
184: filter.getRightGeometry().accept(this );
185: rightCopy = (Expression) pages.pop();
186: }
187:
188: copy = ff.createGeometryFilter(filter.getFilterType());
189: copy.addLeftGeometry(leftCopy);
190: copy.addRightGeometry(rightCopy);
191: } catch (IllegalFilterException erp) {
192: throw new RuntimeException(erp);
193: }
194:
195: pages.push(copy);
196: }
197:
198: public void visit(LikeFilter filter) {
199: LikeFilter copy = null;
200:
201: try {
202: Expression valueCopy = null;
203:
204: if (filter.getValue() != null) {
205: filter.getValue().accept(this );
206: valueCopy = (Expression) pages.pop();
207: }
208:
209: copy = ff.createLikeFilter();
210: copy.setValue(valueCopy);
211: copy.setPattern(filter.getPattern(), filter
212: .getWildcardMulti(), filter.getWildcardSingle(),
213: filter.getEscape());
214: } catch (IllegalFilterException erp) {
215: throw new RuntimeException(erp);
216: }
217:
218: pages.push(copy);
219: }
220:
221: public void visit(LogicFilter filter) {
222: LogicFilter copy = null;
223:
224: Iterator iterator = filter.getFilterIterator();
225:
226: List subFilters = new ArrayList();
227: while (iterator.hasNext()) {
228: org.opengis.filter.Filter subFilter = (org.opengis.filter.Filter) iterator
229: .next();
230: Filters.accept(subFilter, this );
231: subFilters.add(pages.pop());
232: }
233:
234: try {
235: copy = ff.createLogicFilter(filter.getFilterType());
236: Iterator copyIterator = subFilters.iterator();
237: while (copyIterator.hasNext()) {
238: copy.addFilter((org.opengis.filter.Filter) copyIterator
239: .next());
240: }
241: } catch (IllegalFilterException erp) {
242: throw new RuntimeException(erp);
243: }
244:
245: pages.push(copy);
246: }
247:
248: public void visit(NullFilter filter) {
249: NullFilter copy = null;
250:
251: try {
252: Expression valueCopy = null;
253:
254: if (filter.getNullCheckValue() != null) {
255: filter.getNullCheckValue().accept(this );
256: valueCopy = (Expression) pages.pop();
257: }
258:
259: copy = ff.createNullFilter();
260: copy.nullCheckValue(valueCopy);
261: } catch (IllegalFilterException erp) {
262: throw new RuntimeException(erp);
263: }
264:
265: pages.push(copy);
266: }
267:
268: public void visit(FidFilter filter) {
269: FidFilter copy = ff.createFidFilter();
270:
271: String[] fids = filter.getFids();
272: for (int i = 0; i < fids.length; i++) {
273: copy.addFid(fids[i]);
274: }
275:
276: pages.push(copy);
277: }
278:
279: public void visit(AttributeExpression expression) {
280: AttributeExpression copy = null;
281: copy = ff.createAttributeExpression(expression
282: .getAttributePath()); //not a true copy, but what the heck...
283:
284: pages.push(copy);
285: }
286:
287: public void visit(Expression expression) {
288: // Should not happen?
289: throw new RuntimeException("visit(Expression) unsupported");
290: }
291:
292: public void visit(LiteralExpression expression) {
293: LiteralExpression copy = null;
294:
295: try {
296: copy = ff.createLiteralExpression(expression.getLiteral());
297: } catch (IllegalFilterException erp) {
298: throw new RuntimeException(erp);
299: }
300:
301: pages.push(copy);
302: }
303:
304: public void visit(MathExpression expression) {
305: MathExpression copy = null;
306:
307: try {
308: copy = ff.createMathExpression(expression.getType());
309:
310: if (expression.getLeftValue() != null) {
311: expression.getLeftValue().accept(this );
312: copy.addLeftValue((Expression) pages.pop());
313: }
314: if (expression.getRightValue() != null) {
315: expression.getRightValue().accept(this );
316: copy.addRightValue((Expression) pages.pop());
317: }
318: } catch (IllegalFilterException erp) {
319: throw new RuntimeException(erp);
320: }
321:
322: pages.push(copy);
323: }
324:
325: public void visit(FunctionExpression expression) {
326: FunctionExpression copy = null;
327:
328: Expression[] args = expression.getArgs();
329: Expression[] copyArgs = new Expression[args.length];
330: for (int i = 0; i < args.length; i++) {
331: args[i].accept(this );
332: copyArgs[i] = (Expression) pages.pop();
333: }
334:
335: copy = ff.createFunctionExpression(expression.getName());
336: copy.setArgs(copyArgs);
337:
338: pages.push(copy);
339: }
340:
341: public Object getCopy() {
342: if (pages.isEmpty())
343: return null;
344: return pages.firstElement();
345: }
346: }
|