directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1696953 - in /directory/shared/trunk/ldap/model/src: main/java/org/apache/directory/api/ldap/model/ldif/ test/java/org/apache/directory/api/ldap/model/ldif/
Date Fri, 21 Aug 2015 12:17:09 GMT
Author: elecharny
Date: Fri Aug 21 12:17:09 2015
New Revision: 1696953

URL: http://svn.apache.org/r1696953
Log:
Fix for LDAPAPI-246 : when a Modify operation that replace an attribute with no value is presentign
a LDIF file, we don't create a empty value :

replace: url
-

is generated instead of :

replace: url
url:
-

The second form is used to inject null value into an attribute, when it's allowed.

For the same reason, the LdifEntry.toString() method now correctly generates the same thing.

Modified:
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifEntry.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifUtils.java
    directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifEntry.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifEntry.java?rev=1696953&r1=1696952&r2=1696953&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifEntry.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifEntry.java
Fri Aug 21 12:17:09 2015
@@ -419,6 +419,31 @@ public class LdifEntry implements Clonea
 
 
     /**
+     * Add a modification with no value
+     * 
+     * @param modOp The modification operation value. One of : 
+     * <ul>
+     * <li>ModificationOperation.ADD_ATTRIBUTE</li>
+     * <li>ModificationOperation.REMOVE_ATTRIBUTE</li>
+     * <li>ModificationOperation.REPLACE_ATTRIBUTE</li>
+     * </ul>
+     * 
+     * @param id The attribute's ID
+     */
+    public void addModification( ModificationOperation modOp, String id )
+    {
+        if ( changeType == ChangeType.Modify )
+        {
+            Attribute attr = new DefaultAttribute( id );
+
+            Modification item = new DefaultModification( modOp, attr );
+            modificationList.add( item );
+            modifications.put( id, item );
+        }
+    }
+
+
+    /**
      * Add a modification
      * 
      * @param modOp The modification operation value. One of : 

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java?rev=1696953&r1=1696952&r2=1696953&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifReader.java
Fri Aug 21 12:17:09 2015
@@ -1164,8 +1164,15 @@ public class LdifReader implements Itera
                 {
                     if ( isEmptyValue )
                     {
-                        // Update the entry
-                        entry.addModification( modificationType, modified, null );
+                        if ( state == ATTRVAL_SPEC_OR_SEP )
+                        {
+                            entry.addModification( modificationType, modified );
+                        }
+                        else
+                        {
+                            // Update the entry with a null value
+                            entry.addModification( modificationType, modified, null );
+                        }
                     }
                     else
                     {

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifUtils.java?rev=1696953&r1=1696952&r2=1696953&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifUtils.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifUtils.java
Fri Aug 21 12:17:09 2015
@@ -485,8 +485,20 @@ public final class LdifUtils
                 break;
 
             case Modify:
+                boolean isFirst = true;
+                
                 for ( Modification modification : entry.getModifications() )
                 {
+                    
+                    if ( isFirst )
+                    {
+                        isFirst = false;
+                    }
+                    else
+                    {
+                        sb.append( "-\n" );
+                    }
+
                     switch ( modification.getOperation() )
                     {
                         case ADD_ATTRIBUTE:
@@ -510,9 +522,9 @@ public final class LdifUtils
                     sb.append( '\n' );
 
                     sb.append( convertToLdif( modification.getAttribute(), length ) );
-                    sb.append( "-\n" );
                 }
 
+                sb.append( '-' );
                 break;
 
             default:
@@ -562,6 +574,12 @@ public final class LdifUtils
     public static String convertToLdif( Attribute attr, int length ) throws LdapException
     {
         StringBuilder sb = new StringBuilder();
+        
+        if ( attr.size() == 0 )
+        {
+            // Special case : we don't have any value
+            return "";
+        }
 
         for ( Value<?> value : attr )
         {

Modified: directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java?rev=1696953&r1=1696952&r2=1696953&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java
(original)
+++ directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/ldif/LdifReaderTest.java
Fri Aug 21 12:17:09 2015
@@ -2485,4 +2485,31 @@ public class LdifReaderTest
 
     }
 
+
+
+    @Test
+    public void testLdifParserWithReplaceEmptyValue() throws Exception, Exception
+    {
+        String ldif =
+            "dn: cn=Steven Nguyen,ou=SAP,dc=sap,dc=local\n" +
+            "changetype: modify\n" +
+            "replace: objectClass\n" +
+            "objectClass: top\n" +
+            "objectClass: user\n" +
+            "objectClass: person\n" +
+            "objectClass: organizationalPerson\n" +
+            "-\n" +
+            "replace: sn\n" +
+            "sn: Nguyen Linh\n" +
+            "-\n" +
+            "replace: url\n" +
+            "-\n";
+
+        LdifReader reader = new LdifReader();
+        List<LdifEntry> entries = reader.parseLdif( ldif );
+        LdifEntry entry = entries.get( 0 );
+
+        assertEquals( ldif, entry.toString() );
+        reader.close();
+    }
 }



Mime
View raw message