Patch looks good Gabriel.
Gabriel Roldán wrote:
> Hi all,
>
> I'm being asked by Andrea to back port the CQL module from trunk to 2.3.x so
> we can use it on GeoServer 1.5.x
>
> So I went ahead and did it on my box. Now, I had to fill a couple holes in
> Expr.java in order to actually create functions, and had to add boolean and
> date literal types for LiteralExpression.
>
> Attached are the patches that make the trick. Applying them on my box and
> running mvn clean install from the root pom works just fine.
>
> Please review as I'm waiting for the go ahead or not to commit this changes
> and upload the cql module on trunk.
>
> Cheers,
>
> Gabriel
>
>
> ------------------------------------------------------------------------
>
> Index:
> /home/gabriel/workspaces/svn/geotools/2.3.x/module/api/src/org/geotools/filter/ExpressionType.java
> ===================================================================
> ---
> /home/gabriel/workspaces/svn/geotools/2.3.x/module/api/src/org/geotools/filter/ExpressionType.java
> (revision 24044)
> +++
> /home/gabriel/workspaces/svn/geotools/2.3.x/module/api/src/org/geotools/filter/ExpressionType.java
> (working copy)
> @@ -44,6 +44,12 @@
> /** Defines a literal expression with a declared long type. */
> public static final short LITERAL_LONG = 100;
>
> + /** Defines a literal expression with a declared Boolean type. */
> + public static final short LITERAL_BOOLEAN = 115;
> +
> + /** Defines a literal expression with a declared java.util.Date type. */
> + public static final short LITERAL_DATE = 116;
> +
> /* Types implemented by ExpressionMath. */
> /** Defines a math expression for adding. */
> public static final short MATH_ADD = 105;
>
>
> ------------------------------------------------------------------------
>
> Index:
> /home/gabriel/workspaces/svn/geotools/2.3.x/module/main/src/org/geotools/filter/Expr.java
> ===================================================================
> ---
> /home/gabriel/workspaces/svn/geotools/2.3.x/module/main/src/org/geotools/filter/Expr.java
> (revision 24044)
> +++
> /home/gabriel/workspaces/svn/geotools/2.3.x/module/main/src/org/geotools/filter/Expr.java
> (working copy)
> @@ -17,10 +17,13 @@
>
> import java.util.ArrayList;
> import java.util.Arrays;
> +import java.util.HashMap;
> +import java.util.Iterator;
> import java.util.List;
> +import java.util.Map;
> import java.util.Set;
>
> -import org.geotools.filter.FilterFactory;
> +import org.geotools.factory.Hints;
> import org.geotools.filter.expression.AddImpl;
> import org.geotools.filter.expression.DivideImpl;
> import org.geotools.filter.expression.MultiplyImpl;
> @@ -114,6 +117,8 @@
> */
> public abstract class Expr implements FilterFactory {
>
> + private FunctionFinder functionFinder = new FunctionFinder(null);
> +
> public And and(Filter f, Filter g ) {
> List/*<Filter>*/ list = new ArrayList/*<Filter>*/( 2 );
> list.add( f );
> @@ -392,18 +397,20 @@
> }
>
> public Function function(String name, Expression[] args) {
> - //TODO: Auto-generated method stub
> - return null;
> + Function function = functionFinder.findFunction( name,
> Arrays.asList(args) );
> + return function;
> }
>
> +
> public Function function(String name, Expression arg1) {
> - // TODO Auto-generated method stub
> - return null;
> - }
> + Function function = functionFinder.findFunction( name,
> Arrays.asList( new Expression[]{ arg1 } ) );
> + return function;
> + }
>
> public Function function(String name, Expression arg1, Expression arg2)
> {
> - // TODO Auto-generated method stub
> - return null;
> + Function function =
> + functionFinder.findFunction( name, Arrays.asList( new
> Expression[]{ arg1, arg2 }) );
> + return function;
> }
>
> public Function function(String name, Expression arg1, Expression arg2,
> @@ -408,8 +415,10 @@
>
> public Function function(String name, Expression arg1, Expression arg2,
> Expression arg3) {
> - // TODO Auto-generated method stub
> - return null;
> + Function function =
> + functionFinder.findFunction( name, Arrays.asList( new
> Expression[]{ arg1, arg2, arg3 }) );
> +
> + return function;
> }
>
> public Literal literal(Object obj) {
> @@ -417,10 +426,10 @@
> return new LiteralExpressionImpl(obj);
> }
> catch (IllegalFilterException e) {
> - new IllegalArgumentException().initCause(e);
> + throw (RuntimeException)new
> IllegalArgumentException().initCause(e);
> }
>
> - return null;
> + //return null;
> }
>
> public Literal literal(byte b) {
> @@ -455,4 +464,87 @@
> throw new UnsupportedOperationException("Filter api does not
> support boolean literals");
> }
>
> +
> + /**
> + * Isolate function lookup code from Factory implementation(s).
> + * <p>
> + * This is done to look for two things:
> + * <ul>
> + * <li>org.geotools.filter.Function
> + * <li>org.opengis.filter.expression.Function
> + * </ul>
> + * This is done as a proper utility class that accepts Hints.
> + *
> + * @author Jody Garnett
> + */
> + private static class FunctionFinder {
> + private Map functionExpressionCache;
> +
> + private Map functionImplCache;
> +
> + public FunctionFinder(Hints hints) {
> + // currently hints are not used, need help :-P
> + }
> +
> + public Function findFunction(String name) {
> + return findFunction(name, null);
> + }
> +
> + public Function findFunction(String name, List/* <Expression>
> */parameters) {
> + name = functionName(name);
> +
> + try {
> + // load the caches at first access
> + if (functionExpressionCache == null) {
> + functionExpressionCache = new HashMap();
> + functionImplCache = new HashMap();
> +
> + for (Iterator it = org.geotools.factory.FactoryFinder
> + .factories(FunctionExpression.class);
> it.hasNext();) {
> + FunctionExpression function = (FunctionExpression)
> it.next();
> +
> functionExpressionCache.put(function.getName().toLowerCase(),
> function.getClass());
> + }
> + for (Iterator i =
> org.geotools.factory.FactoryFinder.factories(FunctionExpression.class);
> + i.hasNext();) {
> + FunctionExpression function =
> (FunctionExpression) i.next();
> +
> functionImplCache.put(function.getName().toLowerCase(), function.getClass());
> + }
> + }
> +
> + // cache lookup
> + Class clazz = (Class)
> functionExpressionCache.get(name.toLowerCase());
> + if(clazz != null) {
> + FunctionExpression function = (FunctionExpression)
> clazz.newInstance();
> + if(parameters != null)
> + function.setArgs((org.geotools.filter.Expression[])
> parameters.toArray(new org.geotools.filter.Expression[]{}));
> + return function;
> + }
> + clazz = (Class) functionImplCache.get(name.toLowerCase());
> + if(clazz != null) {
> + FunctionExpression function = (FunctionExpression)
> clazz.newInstance();
> + if(parameters != null)
> + function.setArgs((org.geotools.filter.Expression[])
> parameters.toArray(new org.geotools.filter.Expression[]{}));
> + return function;
> + }
> +
> + } catch (Exception e) {
> + throw new RuntimeException("Unable to create class " + name
> + "Function", e);
> + }
> + throw new RuntimeException("Unable to find function " + name);
> + }
> +
> + private String functionName(String name) {
> + int index = -1;
> +
> + if ((index = name.indexOf("Function")) != -1) {
> + name = name.substring(0, index);
> + }
> +
> + name = name.toLowerCase().trim();
> + char c = name.charAt(0);
> + name = name.replaceFirst("" + c, "" + Character.toUpperCase(c));
> +
> + return name;
> + }
> + }
> }
> Index:
> /home/gabriel/workspaces/svn/geotools/2.3.x/module/main/src/org/geotools/filter/LiteralExpressionImpl.java
> ===================================================================
> ---
> /home/gabriel/workspaces/svn/geotools/2.3.x/module/main/src/org/geotools/filter/LiteralExpressionImpl.java
> (revision 24044)
> +++
> /home/gabriel/workspaces/svn/geotools/2.3.x/module/main/src/org/geotools/filter/LiteralExpressionImpl.java
> (working copy)
> @@ -15,6 +15,8 @@
> */
> package org.geotools.filter;
>
> +import java.util.Date;
> +
> import org.geotools.feature.Feature;
> import org.opengis.filter.expression.ExpressionVisitor;
>
> @@ -181,6 +183,10 @@
> expressionType = LITERAL_STRING;
> } else if (literal instanceof Geometry) {
> expressionType = LITERAL_GEOMETRY;
> + } else if (literal instanceof Boolean) {
> + expressionType = LITERAL_BOOLEAN;
> + } else if (literal instanceof Date) {
> + expressionType = LITERAL_DATE;
> } else {
>
>
> @@ -255,6 +261,12 @@
> } else if (expressionType == LITERAL_DOUBLE) {
> return ((Double) this.literal).equals((Double) expLit
> .getLiteral());
> + } else if (expressionType == LITERAL_BOOLEAN) {
> + return ((Boolean) this.literal).equals((Boolean) expLit
> + .getLiteral());
> + }else if (expressionType == LITERAL_DATE) {
> + return ((Date) this.literal).equals((Date) expLit
> + .getLiteral());
> } else {
> return true;
> }
>
>
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share your
> opinions on IT & business topics through brief surveys - and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
>
> !DSPAM:1004,45b92455119221775926497!
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Geotools-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-devel
>
>
> !DSPAM:1004,45b92455119221775926497!
--
Justin Deoliveira
[EMAIL PROTECTED]
The Open Planning Project
http://topp.openplans.org
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel