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 2010/07/17 16:48:16 UTC

svn commit: r965090 - in /directory/shared/trunk: ldap-ldif/src/main/java/org/apache/directory/shared/ldap/ldif/ ldap/src/main/java/org/apache/directory/shared/ldap/entry/

Author: elecharny
Date: Sat Jul 17 14:48:15 2010
New Revision: 965090

URL: http://svn.apache.org/viewvc?rev=965090&view=rev
Log:
o Fixed the DefaultEntry.toString() method which was generating the ObjectClass AT twice
o Added a LdifUtils.createEntry() method to create an entry from an Ldif

Modified:
    directory/shared/trunk/ldap-ldif/src/main/java/org/apache/directory/shared/ldap/ldif/LdifAttributesReader.java
    directory/shared/trunk/ldap-ldif/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultEntry.java

Modified: directory/shared/trunk/ldap-ldif/src/main/java/org/apache/directory/shared/ldap/ldif/LdifAttributesReader.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap-ldif/src/main/java/org/apache/directory/shared/ldap/ldif/LdifAttributesReader.java?rev=965090&r1=965089&r2=965090&view=diff
==============================================================================
--- directory/shared/trunk/ldap-ldif/src/main/java/org/apache/directory/shared/ldap/ldif/LdifAttributesReader.java (original)
+++ directory/shared/trunk/ldap-ldif/src/main/java/org/apache/directory/shared/ldap/ldif/LdifAttributesReader.java Sat Jul 17 14:48:15 2010
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.StringReader;
 import java.util.ArrayList;
 
+import javax.naming.NamingException;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttributes;
@@ -429,10 +430,10 @@ public class LdifAttributesReader extend
     
     
     /**
-     * A method which parses a ldif string and returns a list of Entry.
+     * A method which parses a ldif string and returns an Entry.
      * 
      * @param ldif The ldif string
-     * @return A list of Entry, or an empty List
+     * @return An entry
      * @throws LdapLdifException If something went wrong
      */
     // This will suppress PMD.EmptyCatchBlock warnings in this method

Modified: directory/shared/trunk/ldap-ldif/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap-ldif/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java?rev=965090&r1=965089&r2=965090&view=diff
==============================================================================
--- directory/shared/trunk/ldap-ldif/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java (original)
+++ directory/shared/trunk/ldap-ldif/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java Sat Jul 17 14:48:15 2010
@@ -705,5 +705,83 @@ public class LdifUtils
 
         return attributes;
     }
-}
 
+
+    /**
+     * Build a new Attributes instance from a LDIF list of lines. The values can be 
+     * either a complete AVA, or a couple of AttributeType ID and a value (a String or 
+     * a byte[]). The following sample shows the three cases :
+     * 
+     * <pre>
+     * Attribute attr = AttributeUtils.createAttributes(
+     *     "objectclass: top",
+     *     "cn", "My name",
+     *     "jpegPhoto", new byte[]{0x01, 0x02} );
+     * </pre>
+     * 
+     * @param avas The AttributeType and Values, using a ldif format, or a couple of 
+     * Attribute ID/Value
+     * @return An Attributes instance
+     * @throws LdapException If the data are invalid
+     * @throws LdapLdifException 
+     */
+    public static Entry createEntry( DN dn, Object... avas ) throws LdapException, LdapLdifException
+    {
+        StringBuilder sb = new StringBuilder();
+        int pos = 0;
+        boolean valueExpected = false;
+        
+        for ( Object ava : avas)
+        {
+            if ( !valueExpected )
+            {
+                if ( !(ava instanceof String) )
+                {
+                    throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err( I18n.ERR_12085, (pos+1) ) );
+                }
+                
+                String attribute = (String)ava;
+                sb.append( attribute );
+                
+                if ( attribute.indexOf( ':' ) != -1 )
+                {
+                    sb.append( '\n' );
+                }
+                else
+                {
+                    valueExpected = true;
+                }
+            }
+            else
+            {
+                if ( ava instanceof String )
+                {
+                    sb.append( ": " ).append( (String)ava ).append( '\n' );
+                }
+                else if ( ava instanceof byte[] )
+                {
+                    sb.append( ":: " );
+                    sb.append( new String( Base64.encode( (byte[] )ava ) ) );
+                    sb.append( '\n' );
+                }
+                else
+                {
+                    throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err( I18n.ERR_12086, (pos+1) ) );
+                }
+                
+                valueExpected = false;
+            }
+        }
+        
+        if ( valueExpected )
+        {
+            throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err( I18n.ERR_12087 ) );
+        }
+        
+        LdifAttributesReader reader = new LdifAttributesReader();
+        Entry entry = reader.parseEntry( sb.toString() );
+        entry.setDn( dn );
+
+        return entry;
+    }
+}

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultEntry.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultEntry.java?rev=965090&r1=965089&r2=965090&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultEntry.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultEntry.java Sat Jul 17 14:48:15 2010
@@ -2952,36 +2952,25 @@ public class DefaultEntry implements Ent
         {
             for ( EntryAttribute attribute : attributes.values() )
             {
-                if ( attribute.getAttributeType() != OBJECT_CLASS_AT )
-                {
-                    sb.append( attribute );
-                    continue;
-                }
-
                 String id = attribute.getId();
 
                 if ( schemaManager != null )
                 {
-                    try
-                    {
-                        AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( id );
+                    AttributeType attributeType = schemaManager.getAttributeType( id );
 
-                        if ( attributeType != OBJECT_CLASS_AT )
-                        {
-                            sb.append( attribute );
-                        }
-                    }
-                    catch ( LdapException le )
+                    if ( attributeType != OBJECT_CLASS_AT )
                     {
-                        // Not found...
+                        sb.append( attribute );
+                        continue;
                     }
                 }
                 else
                 {
-                    if ( !SchemaConstants.OBJECT_CLASS_AT.equalsIgnoreCase( id )
-                        && !SchemaConstants.OBJECT_CLASS_AT_OID.equalsIgnoreCase( id ) )
+                    if ( !id.equalsIgnoreCase( SchemaConstants.OBJECT_CLASS_AT ) &&
+                         !id.equals( SchemaConstants.OBJECT_CLASS_AT_OID ) )
                     {
                         sb.append( attribute );
+                        continue;
                     }
                 }
             }