From deafb3207ed05f8211a4b99e371a39e491ec5d2c Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Fri, 14 Sep 2007 22:45:02 +0000 Subject: [PATCH] escape all non-alpha/numeric characters with \ Thanks erik. See also: 575369 git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@575809 13f79535-47bb-0310-9956-ffa450edef68 --- .../solr/client/solrj/util/ClientUtils.java | 33 +++-------------- .../client/solrj/util/ClientUtilsTest.java | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java diff --git a/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java b/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java index 926fa4d2d2b..715b3893512 100644 --- a/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java +++ b/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java @@ -29,6 +29,8 @@ import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.TimeZone; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.httpclient.util.DateParseException; import org.apache.commons.httpclient.util.DateUtil; @@ -161,38 +163,15 @@ public class ClientUtils } } - + private static final Pattern escapePattern = Pattern.compile( "(\\W)" ); + /** * See: http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping Special Characters */ public static String escapeQueryChars( String input ) { - char buff[] = input.toCharArray(); - StringBuilder str = new StringBuilder( buff.length+5 ); - for( char c : buff ) { - switch( c ) { - case '+': - case '-': - case '&': - case '|': - case '(': - case ')': - case '{': - case '}': - case '[': - case ']': - case '^': - case '"': - case '*': - case ':': - case '~': - case '!': - case '\\': - str.append( '\\' ); - } - str.append( c ); - } - return str.toString(); + Matcher matcher = escapePattern.matcher( input ); + return matcher.replaceAll( "\\\\$1" ); } diff --git a/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java b/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java new file mode 100644 index 00000000000..77beeb35ce9 --- /dev/null +++ b/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.client.solrj.util; + +import junit.framework.TestCase; + +/** + * + * @version $Id$ + * @since solr 1.3 + */ +public class ClientUtilsTest extends TestCase { + + public void testEscapeQuery() + { + assertEquals( "nochange", ClientUtils.escapeQueryChars( "nochange" ) ); + assertEquals( "12345", ClientUtils.escapeQueryChars( "12345" ) ); + assertEquals( "with\\ space", ClientUtils.escapeQueryChars( "with space" ) ); + assertEquals( "h\\:ello\\!", ClientUtils.escapeQueryChars( "h:ello!" ) ); + assertEquals( "h\\~\\!", ClientUtils.escapeQueryChars( "h~!" ) ); + } +}