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
This commit is contained in:
Ryan McKinley 2007-09-14 22:45:02 +00:00
parent 4d8f34f394
commit deafb3207e
2 changed files with 43 additions and 27 deletions

View File

@ -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" );
}

View File

@ -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~!" ) );
}
}