You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by ry...@apache.org on 2007/09/15 00:46:01 UTC

svn commit: r575809 - in /lucene/solr/trunk/client/java/solrj: src/org/apache/solr/client/solrj/util/ClientUtils.java test/org/apache/solr/client/solrj/util/ test/org/apache/solr/client/solrj/util/ClientUtilsTest.java

Author: ryan
Date: Fri Sep 14 15:45:02 2007
New Revision: 575809

URL: http://svn.apache.org/viewvc?rev=575809&view=rev
Log:
escape all non-alpha/numeric characters with \

Thanks erik.  See also: 575369 

Added:
    lucene/solr/trunk/client/java/solrj/test/org/apache/solr/client/solrj/util/
    lucene/solr/trunk/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java   (with props)
Modified:
    lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java

Modified: lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java?rev=575809&r1=575808&r2=575809&view=diff
==============================================================================
--- lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java (original)
+++ lucene/solr/trunk/client/java/solrj/src/org/apache/solr/client/solrj/util/ClientUtils.java Fri Sep 14 15:45:02 2007
@@ -29,6 +29,8 @@
 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 @@
     }
   }
   
-
+  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" );
   }
   
 

Added: lucene/solr/trunk/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java?rev=575809&view=auto
==============================================================================
--- lucene/solr/trunk/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java (added)
+++ lucene/solr/trunk/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java Fri Sep 14 15:45:02 2007
@@ -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~!" ) );
+  }
+}

Propchange: lucene/solr/trunk/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/solr/trunk/client/java/solrj/test/org/apache/solr/client/solrj/util/ClientUtilsTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL