mirror of https://github.com/apache/lucene.git
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:
parent
4d8f34f394
commit
deafb3207e
|
@ -29,6 +29,8 @@ import java.util.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.TimeZone;
|
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.DateParseException;
|
||||||
import org.apache.commons.httpclient.util.DateUtil;
|
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
|
* See: http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping Special Characters
|
||||||
*/
|
*/
|
||||||
public static String escapeQueryChars( String input )
|
public static String escapeQueryChars( String input )
|
||||||
{
|
{
|
||||||
char buff[] = input.toCharArray();
|
Matcher matcher = escapePattern.matcher( input );
|
||||||
StringBuilder str = new StringBuilder( buff.length+5 );
|
return matcher.replaceAll( "\\\\$1" );
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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~!" ) );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue