mirror of
https://github.com/apache/lucene.git
synced 2025-02-20 17:07:09 +00:00
SOLR-794: added escape() method to ClientUtils.
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@702314 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9b4a62d0a4
commit
41ea2b308d
@ -18,6 +18,8 @@ Detailed Change List
|
||||
New Features
|
||||
----------------------
|
||||
|
||||
1. SOLR-794: added escape() method to ClientUtils. (koji)
|
||||
|
||||
Optimizations
|
||||
----------------------
|
||||
|
||||
|
@ -191,15 +191,32 @@ public class ClientUtils
|
||||
private static final Pattern escapePattern = Pattern.compile( "(\\W)" );
|
||||
|
||||
/**
|
||||
* See: http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping Special Characters
|
||||
* Non-word characters are escaped by a preceding <code>\</code>.
|
||||
*/
|
||||
public static String escapeQueryChars( String input )
|
||||
{
|
||||
Matcher matcher = escapePattern.matcher( input );
|
||||
return matcher.replaceAll( "\\\\$1" );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* See: http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping Special Characters
|
||||
*/
|
||||
public static String escape(String s) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
// These characters are part of the query syntax and must be escaped
|
||||
if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':'
|
||||
|| c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
|
||||
|| c == '*' || c == '?' || c == '|' || c == '&') {
|
||||
sb.append('\\');
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String toQueryString( SolrParams params, boolean xml ) {
|
||||
StringBuilder sb = new StringBuilder(128);
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user