directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seelm...@apache.org
Subject svn commit: r771342 - in /directory/shared/trunk/ldap/src: main/antlr/distinguishedName.g main/java/org/apache/directory/shared/ldap/name/FastLdapDnParser.java test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java
Date Mon, 04 May 2009 16:03:04 GMT
Author: seelmann
Date: Mon May  4 16:03:03 2009
New Revision: 771342

URL: http://svn.apache.org/viewvc?rev=771342&view=rev
Log:
Fixed handling of trailing spaces of a value: trailing spaces are not part of the value

Modified:
    directory/shared/trunk/ldap/src/main/antlr/distinguishedName.g
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/FastLdapDnParser.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java

Modified: directory/shared/trunk/ldap/src/main/antlr/distinguishedName.g
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/antlr/distinguishedName.g?rev=771342&r1=771341&r2=771342&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/antlr/distinguishedName.g (original)
+++ directory/shared/trunk/ldap/src/main/antlr/distinguishedName.g Mon May  4 16:03:03 2009
@@ -146,6 +146,7 @@
     {
         String upValue = "";
         Object normValue = "";
+        String trailingSpaces = "";
     }
 }
 
@@ -295,7 +296,7 @@
             try
             {
                 rdn.addAttributeTypeAndValue( type, type, value.upValue, value.normValue
);
-                { upName += value.upValue; }
+                { upName += value.upValue + value.trailingSpaces; }
             }
             catch ( InvalidNameException e )
             {
@@ -385,11 +386,17 @@
     }
     :
     (
-        quotestring [value]
+        (
+            quotestring [value]
+            ( SPACE { value.trailingSpaces += " "; } )*
+        )
         |
         string [value]
         |
-        hexstring [value]
+        (
+            hexstring [value]
+            ( SPACE { value.trailingSpaces += " "; } )*
+        )
     )?
     ;
 
@@ -408,7 +415,7 @@
     }
     :
     (
-        DQUOTE
+        dq1:DQUOTE { value.upValue += dq1.getText(); }
         (
             (
                 s:~(DQUOTE|ESC|ESCESC|HEXPAIR) 
@@ -420,7 +427,7 @@
             |
             bytes = pair[value] { bb.append( bytes ); }
         )*
-        DQUOTE
+        dq2:DQUOTE { value.upValue += dq2.getText(); }
     )
     {
         // TODO: pair / s
@@ -517,6 +524,9 @@
         {
             string = string.substring( 0, length - 1 );
             length = string.length();
+            
+            value.upValue = value.upValue.substring( 0, value.upValue.length() - 1 );
+            value.trailingSpaces += " ";
         }
         string = string.replace("\\ ", " ");
         

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/FastLdapDnParser.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/FastLdapDnParser.java?rev=771342&r1=771341&r2=771342&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/FastLdapDnParser.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/FastLdapDnParser.java
Mon May  4 16:03:03 2009
@@ -171,6 +171,9 @@
         String value = StringTools.trimRight( upValue );
         // TODO: trim, normalize, etc
 
+        // SPACE*
+        matchSpaces( name, pos );
+
         rdn.addAttributeTypeAndValue( type, type, upValue, value );
 
         rdn.setUpName( name.substring( rdnStart, pos.start ) );
@@ -188,12 +191,15 @@
      */
     private void matchSpaces( String name, Position pos ) throws InvalidNameException
     {
-        char c = ' ';
-        while ( c == ' ' && hasMoreChars( pos ) )
+        while ( hasMoreChars( pos ) )
         {
-            c = nextChar( name, pos, true );
+            char c = nextChar( name, pos, true );
+            if ( c != ' ' )
+            {
+                pos.start--;
+                break;
+            }
         }
-        pos.start--;
     }
 
 
@@ -526,11 +532,13 @@
     private String matchValue( String name, Position pos ) throws InvalidNameException
     {
         StringBuilder value = new StringBuilder();
+        int numTrailingSpaces = 0;
         while ( true )
         {
             if ( !hasMoreChars( pos ) )
             {
-                return value.toString();
+                pos.start -= numTrailingSpaces;
+                return value.substring( 0, value.length() - numTrailingSpaces );
             }
             char c = nextChar( name, pos, true );
             switch ( c )
@@ -543,8 +551,14 @@
                 case ',':
                 case ';':
                     pos.start--;
-                    return value.toString();
+                    pos.start -= numTrailingSpaces;
+                    return value.substring( 0, value.length() - numTrailingSpaces );
+                case ' ':
+                    numTrailingSpaces++;
+                    value.append( c );
+                    break;
                 default:
+                    numTrailingSpaces = 0;
                     value.append( c );
             }
         }

Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java?rev=771342&r1=771341&r2=771342&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java
(original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java
Mon May  4 16:03:03 2009
@@ -26,6 +26,7 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Enumeration;
@@ -3222,4 +3223,49 @@
         
         assertEquals( "1.2.3.4.5=0+1.2.3.4.6=0+1.2.3.4.7=omnischmomni,2.5.4.3=subtree,0.9.2342.19200300.100.1.25=example,0.9.2342.19200300.100.1.25=com",
dn.toString() );
     }
+
+    /**
+     * Tests that AttributeTypeAndValues are correctly trimmed.
+     */
+    @Test
+    public void testTrimAtavs() throws InvalidNameException
+    {
+        // antlr parser: string value with trailing spaces
+        LdapDN dn1 = new LdapDN( " cn = Amos\\,Tori , ou=system " );
+        assertEquals( " cn = Amos\\,Tori ", dn1.getRdn().getUpName() );
+        AttributeTypeAndValue atav1 = dn1.getRdn().getAtav();
+        assertEquals( "cn", atav1.getUpType() );
+        assertEquals( "Amos\\,Tori", ( String ) atav1.getUpValue() );
+
+        // antlr parser: hexstring with trailing spaces
+        LdapDN dn3 = new LdapDN( " cn = #414243 , ou=system " );
+        assertEquals( " cn = #414243 ", dn3.getRdn().getUpName() );
+        AttributeTypeAndValue atav3 = dn3.getRdn().getAtav();
+        assertEquals( "cn", atav3.getUpType() );
+        assertEquals( "#414243", ( String ) atav3.getUpValue() );
+        assertTrue( Arrays.equals( StringTools.getBytesUtf8( "ABC" ), ( byte[] ) atav3.getNormValue()
) );
+
+        // antlr parser: 
+        LdapDN dn4 = new LdapDN( " cn = \\41\\42\\43 , ou=system " );
+        assertEquals( " cn = \\41\\42\\43 ", dn4.getRdn().getUpName() );
+        AttributeTypeAndValue atav4 = dn4.getRdn().getAtav();
+        assertEquals( "cn", atav4.getUpType() );
+        assertEquals( "\\41\\42\\43", ( String ) atav4.getUpValue() );
+        assertEquals( "ABC", ( String ) atav4.getNormValue() );
+
+        // antlr parser: quotestring with trailing spaces
+        LdapDN dn5 = new LdapDN( " cn = \"ABC\" , ou=system " );
+        assertEquals( " cn = \"ABC\" ", dn5.getRdn().getUpName() );
+        AttributeTypeAndValue atav5 = dn5.getRdn().getAtav();
+        assertEquals( "cn", atav5.getUpType() );
+        assertEquals( "\"ABC\"", ( String ) atav5.getUpValue() );
+        assertEquals( "ABC", ( String ) atav5.getNormValue() );
+
+        // fast parser: string value with trailing spaces 
+        LdapDN dn2 = new LdapDN( " cn = Amos Tori , ou=system " );
+        assertEquals( " cn = Amos Tori ", dn2.getRdn().getUpName() );
+        AttributeTypeAndValue atav2 = dn2.getRdn().getAtav();
+        assertEquals( "cn", atav2.getUpType() );
+        assertEquals( "Amos Tori", ( String ) atav2.getUpValue() );
+    }
 }



Mime
View raw message