解决GeoTools中CQL解析中文字段名的问题

GeoTools中CQL无法解析中文字段名的过滤条件,会报异常错误,经过一个下午的努力,终于通过简单有效的方式解决啦

String filterCondition = "temp='" + gbCodeString + "'"; //使用临时英文字段名temp作为字段名,若为中文字段名则无法正确创建Filter

  Filter filter =ECQL.toFilter(filterCondition); //创建Filter
  //解决字段名为中文的情况
  FilterUtil.decodeFilter(filter,fieldName); //用真正的中文字段名fieldName来替换临时字段名temp
  SimpleFeatureCollection features = source.getFeatures(filter);

 

public class FilterUtil {
 @SuppressWarnings("unchecked")
 public static void decodeFilter(Filter filter,String fieldName)
   throws UnsupportedEncodingException {
   if (filter instanceof IsEqualsToImpl) {
   IsEqualsToImpl impl = (IsEqualsToImpl) filter;
   decodeExpression(impl.getExpression1(),fieldName);
   }
 }

 private static void decodeExpression(Expression exp,String fieldName)
   throws UnsupportedEncodingException {
 
  if (exp instanceof AttributeExpressionImpl)
  {
   AttributeExpressionImpl impl = (AttributeExpressionImpl) exp;
   impl.setPropertyName(fieldName);
  }
 }

}

 

解决问题过程中参考了http://blog.csdn.net/warrenwyf/article/details/5864667中的FilterUtil类,但该类无法直接使用,存在一些问题,且对于一般应用过于繁琐。

原始类代码转载如下:

 

原始FilterUtil类
package com.gi.engine.util.gt;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Iterator;

import org.geotools.filter.AndImpl;
import org.geotools.filter.IsEqualsToImpl;
import org.geotools.filter.IsNotEqualToImpl;
import org.geotools.filter.LikeFilterImpl;
import org.geotools.filter.LiteralExpressionImpl;
import org.geotools.filter.NotImpl;
import org.geotools.filter.OrImpl;
import org.geotools.filter.text.ecql.ECQL;
import org.opengis.filter.Filter;
import org.opengis.filter.expression.Expression;

public  class FilterUtil {

     private  static  char startMark = 0x02;
     private  static  char endMark = 0x03;

     public  static Filter whereclauseToFilter(String where) {
        Filter filter =  null;
         try {
            StringBuilder sb =  new StringBuilder();
             for ( int i = 0, count = where.length(); i < count; i++) {
                 char c = where.charAt(i);
                 if (c < 256) {
                    sb.append(c);
                }  else {
                    String enc = URLEncoder.encode(String.valueOf(c), "UTF-8");
                    enc = enc.replaceAll("\\%", "");
                    sb.append(startMark + enc + endMark);
                }
            }
            String encode = sb.toString();
            Filter f = ECQL.toFilter(encode);
            decodeFilter(f);
            filter = f;
        }  catch (Exception ex) {
            ex.printStackTrace();
        }

         return filter;
    }

    @SuppressWarnings("unchecked")
     private  static  void decodeFilter(Filter filter)
             throws UnsupportedEncodingException {
         if (filter  instanceof OrImpl) {
            OrImpl impl = (OrImpl) filter;
             for (Iterator<Filter> itr = impl.getFilterIterator(); itr.hasNext();) {
                Filter f = itr.next();
                decodeFilter(f);
            }
        }  else  if (filter  instanceof AndImpl) {
            AndImpl impl = (AndImpl) filter;
             for (Iterator<Filter> itr = impl.getFilterIterator(); itr.hasNext();) {
                Filter f = itr.next();
                decodeFilter(f);
            }
        }  else  if (filter  instanceof NotImpl) {
            NotImpl impl = (NotImpl) filter;
            Filter f = impl.getFilter();
            decodeFilter(f);
        }  else  if (filter  instanceof LikeFilterImpl) {
            LikeFilterImpl impl = (LikeFilterImpl) filter;
            String encode = impl.getLiteral();
            impl.setLiteral(decodeString(encode));
        }  else  if (filter  instanceof IsEqualsToImpl) {
            IsEqualsToImpl impl = (IsEqualsToImpl) filter;
            decodeExpression(impl.getExpression1());
            decodeExpression(impl.getExpression2());
        }  else  if (filter  instanceof IsNotEqualToImpl) {
            IsNotEqualToImpl impl = (IsNotEqualToImpl) filter;
            decodeExpression(impl.getExpression1());
            decodeExpression(impl.getExpression2());
        }
    }

     private  static  void decodeExpression(Expression exp)
             throws UnsupportedEncodingException {
         if (exp  instanceof LiteralExpressionImpl) {
            LiteralExpressionImpl impl = (LiteralExpressionImpl) exp;
            String encode = String.valueOf(impl.getValue());
            impl.setValue(decodeString(encode));
        }
    }

     private  static String decodeString(String encode)
             throws UnsupportedEncodingException {
        StringBuilder sb =  new StringBuilder();
         int i = 0, count = encode.length();
         while (i < count) {
             int start = encode.indexOf(startMark, i);
             if (start >= 0) {
                sb.append(encode.substring(i, start));
            }  else {
                sb.append(encode.substring(i));
                 return sb.toString();
            }

             int end = encode.indexOf(endMark, i);
             if (end > 0) {
                i = end + 1;

                String enc = encode.substring(start + 1, end);
                StringBuilder sbEnc =  new StringBuilder();
                 for ( int j = 0, l = enc.length(); j < l; j += 2) {
                    sbEnc.append("%" + enc.charAt(j) + enc.charAt(j + 1));
                }
                String dec = URLDecoder.decode(sbEnc.toString(), "UTF-8");
                sb.append(dec);
            }  else {
                sb.append(encode.substring(i + 1));
                i = count;
            }
        }
         return sb.toString();
    }

}

 

 本博客声明:本人的技术探索过程中,得到了国信司南公司支持。今后,本人博客里的所有技术探索成果将归“无痕客”、“国信司南”和“博客园”三方共同所有,原创作品如需转载,请注明本博客声明

 

转载于:https://www.cnblogs.com/wuhenke/archive/2011/12/07/2278681.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值