You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2015/01/28 19:02:19 UTC

svn commit: r1655420 - in /directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search: Filter.java FilterBuilder.java SubstringFilter.java

Author: elecharny
Date: Wed Jan 28 18:02:19 2015
New Revision: 1655420

URL: http://svn.apache.org/r1655420
Log:
o added the /*no qualifier*/ comment in front of the classes
o Implemented the contains/startsWith/endsWith/substring methods in the FilterBuilder
o Fixed the SubstringFilter to use the previously added methods

Modified:
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/Filter.java
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/FilterBuilder.java
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/SubstringFilter.java

Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/Filter.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/Filter.java?rev=1655420&r1=1655419&r2=1655420&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/Filter.java (original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/Filter.java Wed Jan 28 18:02:19 2015
@@ -26,7 +26,7 @@ package org.apache.directory.ldap.client
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-interface Filter
+/* No qualifier*/interface Filter
 {
     /**
      * Constructs a String representation of a Filter

Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/FilterBuilder.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/FilterBuilder.java?rev=1655420&r1=1655419&r2=1655420&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/FilterBuilder.java (original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/FilterBuilder.java Wed Jan 28 18:02:19 2015
@@ -140,8 +140,8 @@ public class FilterBuilder
     {
         return new FilterBuilder( AttributeValueAssertionFilter.equal( attribute, value ) );
     }
-    
-    
+
+
     /**
      * An extension point for building filters.  To use this feature, create a
      * new class that extends {@link Filter} and supply it to this method.
@@ -180,7 +180,8 @@ public class FilterBuilder
      * @param filter The filter for an extension
      * @return A new FilterBuilder
      */
-    public static FilterBuilder extended( Filter filter ) {
+    public static FilterBuilder extended( Filter filter )
+    {
         return new FilterBuilder( filter );
     }
 
@@ -312,28 +313,124 @@ public class FilterBuilder
 
 
     /**
-     * Returns a new FilterBuilder that will construct a SubString filter.  
+     * Returns a new FilterBuilder that will construct a SubString filter, with an <em>initial</em part, 
+     * and zero to N <em>any</em> part, but no <em>final</em> part.  
      * 
-     * For example:
+     * For instance:
+     * 
+     * <pre>
+     * startswith( "sn", "Th", "Soft", "Foun" )).toString()
+     * </pre>
+     * would result in the string:
+     * <pre>
+     * (sn=Th*Soft*Foun*)
+     * </pre>
+     * 
+     * Which would match any entry with the <code>sn</code> starting with <code>'Th'</code>, and 
+     * having a <code>Soft</code> and <code>Foun</code> strings in the middle, like 
+     * 'The Apache Software Foundation'.
+     *
+     * @param builders The filters to or together
+     * @param parts The sub elements to use in the filter
+     * @return A new FilterBuilder
+     */
+    public static FilterBuilder startsWith( String attribute, String... parts )
+    {
+        return new FilterBuilder( SubstringFilter.startsWith( attribute, parts ) );
+    }
+
+
+    /**
+     * Returns a new FilterBuilder that will construct a SubString filter, with an <em>initial</em part, 
+     * and zero to N <em>any</em> parts, but no <em>final</em> part.  
+     * 
+     * For instance:
+     * 
+     * <pre>
+     * startswith( "sn", "Th", "Soft", "Foun" )).toString()
+     * </pre>
+     * would result in the string:
+     * <pre>
+     * (sn=Th*Soft*Foun*)
+     * </pre>
+     * 
+     * Which would match any entry with the <code>sn</code> starting with <code>'Th'</code>, and 
+     * having a <code>Soft</code> and <code>Foun</code> strings in the middle, like 
+     * 'The Apache Software Foundation'.
+     *
+     * @param builders The filters to or together
+     * @param parts The sub elements to use in the filter
+     * @return A new FilterBuilder
+     */
+    public static FilterBuilder endsWith( String attribute, String... parts )
+    {
+        return new FilterBuilder( SubstringFilter.endsWith( attribute, parts ) );
+    }
+
+
+    /**
+     * Returns a new FilterBuilder that will construct a SubString filter, with zero to N <em>any</em> parts, 
+     * but no <em>initial</em> or <em>final</em> parts.  
+     * 
+     * For instance:
      * 
      * <pre>
-     * substring( "sn", "Th", "tion", "Software" )).toString()
+     * contains( "sn", "Soft", "Foun" )).toString()
      * </pre>
      * would result in the string:
      * <pre>
-     * (sn=Th*Software*tion)
+     * (sn=*Soft*Foun*)
      * </pre>
      * 
-     * Which would match any entry with the <code>sn</code> starting with <code>'Th'</code>, ending
-     * with <code>tion</code> and having a <code>Software</code> in the middle, like 
+     * Which would match any entry with the <code>sn</code> having a <code>Soft</code> 
+     * and <code>Foun</code> strings in the middle, like 
      * 'The Apache Software Foundation'.
      *
      * @param builders The filters to or together
+     * @param parts The sub elements to use in the filter
+     * @return A new FilterBuilder
+     */
+    public static FilterBuilder contains( String attribute, String... parts )
+    {
+        return new FilterBuilder( SubstringFilter.contains( attribute, parts ) );
+    }
+
+
+    /**
+     * Returns a new FilterBuilder that will construct a SubString filter, with a <em>initial</em> part, 
+     * zero to N <em>any</em> parts, and a <em>final</em> part.
+     * 
+     * For instance:
+     * 
+     * <pre>
+     * substring( "sn", "The", "Soft", "Foun", "ion" )).toString()
+     * </pre>
+     * would result in the string:
+     * <pre>
+     * (sn=The*Soft*Foun*ion)
+     * </pre>
+     * 
+     * Which would match any entry with the <code>sn</code> having a <code>Soft</code> 
+     * and <code>Foun</code> strings in the middle, starts with <code>The</code> and ends with <code>ion</code> like 
+     * 'The Apache Software Foundation'.
+     * <p>
+     * Note that if we have only two strings in the parts, they will be the <em>initial</em> and <em>final</em> ones :
+     * 
+     * <pre>
+     * substring( "sn", "The", "ion" )).toString()
+     * </pre>
+     * would result in the string:
+     * <pre>
+     * (sn=The*ion)
+     * </pre>
+     * 
+     * @param builders The filters to or together
+     * @param parts The sub elements to use in the filter
      * @return A new FilterBuilder
      */
-    public static FilterBuilder substring( String attribute, String initial, String end, String... any )
+    public static FilterBuilder substring( String attribute, String... parts )
     {
-        return new FilterBuilder( SubstringFilter.substring( attribute, initial, any, end ) );
+        return new FilterBuilder( SubstringFilter.substring( attribute, parts ) );
     }
 
 

Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/SubstringFilter.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/SubstringFilter.java?rev=1655420&r1=1655419&r2=1655420&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/SubstringFilter.java (original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/search/SubstringFilter.java Wed Jan 28 18:02:19 2015
@@ -23,6 +23,7 @@ package org.apache.directory.ldap.client
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.directory.api.ldap.model.filter.FilterEncoder;
 import org.apache.directory.api.util.Strings;
 
 
@@ -80,17 +81,140 @@ class SubstringFilter extends AbstractFi
 
     /**
      * Create a SubstringFilter based on the filter elements. Such a filter
-     * has a form like Attribute=[initial]*([any]*)*[final].
+     * has a form like <b>Attribute=initial*([any]*)*</b>. We don't expect any
+     * <em>final</em> String.
      *
      * @param attribute The AttributeType for this filter
-     * @param initial The first part of the substring
-     * @param any The inner strings
-     * @param end The final part of the substring.
+     * @param parts The parts that are the initial string and zero to N any strings
      * @return An instance of a SubstringFilter
      */
-    public static SubstringFilter substring( String attribute, String initial, String[] any, String end )
+    public static SubstringFilter startsWith( String attribute, String... parts )
     {
-        return new SubstringFilter( attribute, initial, any, end );
+        if ( ( parts != null ) && ( parts.length > 0 ) )
+        {
+            if ( parts.length > 1 )
+            {
+                String[] any = new String[parts.length - 1];
+                System.arraycopy( parts, 1, any, 0, any.length );
+
+                return new SubstringFilter( attribute, parts[0], any, null );
+            }
+            else
+            {
+                return new SubstringFilter( attribute, parts[0], null, null );
+            }
+        }
+        else
+        {
+            // This is a presence filter, kind of
+            return new SubstringFilter( attribute, null, null, null );
+        }
+    }
+
+
+    /**
+     * Create a SubstringFilter based on the filter elements. Such a filter
+     * has a form like <b>Attribute=*([any]*)*final</b>. We don't expect any
+     * <em>initial</em> String.
+     *
+     * @param attribute The AttributeType for this filter
+     * @param parts The parts that are zero to N any strings followed by a final string
+     * @return An instance of a SubstringFilter
+     */
+    public static SubstringFilter endsWith( String attribute, String... parts )
+    {
+        if ( ( parts != null ) && ( parts.length > 0 ) )
+        {
+            if ( parts.length > 1 )
+            {
+                String[] any = new String[parts.length - 1];
+                System.arraycopy( parts, 0, any, 0, any.length );
+
+                return new SubstringFilter( attribute, null, any, parts[parts.length - 1] );
+            }
+            else
+            {
+                return new SubstringFilter( attribute, null, null, parts[0] );
+            }
+        }
+        else
+        {
+            // This is a presence filter, kind of
+            return new SubstringFilter( attribute, null, null, null );
+        }
+    }
+
+
+    /**
+     * Create a SubstringFilter based on the filter elements. Such a filter
+     * has a form like <b>Attribute=*([any]*)*</b>. We don't expect any
+     * <em>initial</em>or <em>final</em> Strings.
+     *
+     * @param attribute The AttributeType for this filter
+     * @param parts The parts that are zero to N any strings with no initial nor final Strings
+     * @return An instance of a SubstringFilter
+     */
+    public static SubstringFilter contains( String attribute, String... parts )
+    {
+        if ( ( parts != null ) && ( parts.length > 0 ) )
+        {
+            if ( parts.length > 1 )
+            {
+                String[] any = new String[parts.length];
+                System.arraycopy( parts, 0, any, 0, any.length );
+
+                return new SubstringFilter( attribute, null, any, null );
+            }
+            else
+            {
+                return new SubstringFilter( attribute, null, parts, null );
+            }
+        }
+        else
+        {
+            // This is a presence filter, kind of
+            return new SubstringFilter( attribute, null, null, null );
+        }
+    }
+
+
+    /**
+     * Create a SubstringFilter based on the filter elements. Such a filter
+     * has a form like <b>Attribute=initial*([any]*)*final</b>.
+     *
+     * @param attribute The AttributeType for this filter
+     * @param parts The parts that are zero to N any strings starting with an initial String and 
+     * followed by a final string
+     * @return An instance of a SubstringFilter
+     */
+    public static SubstringFilter substring( String attribute, String... parts )
+    {
+        if ( ( parts != null ) && ( parts.length > 0 ) )
+        {
+            if ( parts.length > 2 )
+            {
+                // We have initial, any and final
+                String[] any = new String[parts.length - 2];
+                System.arraycopy( parts, 1, any, 0, any.length );
+
+                return new SubstringFilter( attribute, parts[0], any, parts[parts.length - 1] );
+            }
+            else if ( parts.length > 1 )
+            {
+                // we only have initial and final
+                return new SubstringFilter( attribute, parts[0], null, parts[1] );
+            }
+            else
+            {
+                // We don't have any or final
+                return new SubstringFilter( attribute, parts[0], null, null );
+            }
+        }
+        else
+        {
+            // This is a presence filter, kind of
+            return new SubstringFilter( attribute, null, null, null );
+        }
     }
 
 
@@ -104,14 +228,14 @@ class SubstringFilter extends AbstractFi
 
         if ( !Strings.isEmpty( initial ) )
         {
-            builder.append( initial );
+            builder.append( FilterEncoder.encodeFilterValue( initial ) );
         }
 
         if ( any != null )
         {
             for ( String string : any )
             {
-                builder.append( '*' ).append( string );
+                builder.append( '*' ).append( FilterEncoder.encodeFilterValue( string ) );
             }
         }
 
@@ -119,7 +243,7 @@ class SubstringFilter extends AbstractFi
 
         if ( !Strings.isEmpty( end ) )
         {
-            builder.append( end );
+            builder.append( FilterEncoder.encodeFilterValue( end ) );
         }
 
         builder.append( ")" );