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 2007/10/12 14:45:35 UTC

svn commit: r584151 - /directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java

Author: elecharny
Date: Fri Oct 12 05:45:34 2007
New Revision: 584151

URL: http://svn.apache.org/viewvc?rev=584151&view=rev
Log:
Added util methods to convert from Attribute(s) to ServerEntry and ServerAttribute

Modified:
    directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java

Modified: directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java?rev=584151&r1=584150&r2=584151&view=diff
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java (original)
+++ directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java Fri Oct 12 05:45:34 2007
@@ -20,9 +20,14 @@
 package org.apache.directory.shared.ldap.util;
 
 
+import org.apache.directory.shared.ldap.common.ServerAttribute;
+import org.apache.directory.shared.ldap.common.ServerAttributeImpl;
+import org.apache.directory.shared.ldap.common.ServerEntry;
+import org.apache.directory.shared.ldap.common.ServerEntryImpl;
 import org.apache.directory.shared.ldap.message.AttributeImpl;
 import org.apache.directory.shared.ldap.message.AttributesImpl;
 import org.apache.directory.shared.ldap.message.ModificationItemImpl;
+import org.apache.directory.shared.ldap.name.LdapDN;
 import org.apache.directory.shared.ldap.schema.AttributeType;
 import org.apache.directory.shared.ldap.schema.MatchingRule;
 import org.apache.directory.shared.ldap.schema.NoOpNormalizer;
@@ -1109,5 +1114,66 @@
     public static String toString( Attributes attributes )
     {
         return toString( "", attributes );
+    }
+    
+    
+    /**
+     * Convert a BasicAttribute or an AttributeImpl instance to a
+     * ServerAttribute instance
+     *
+     * @param attribute The attribute to convert
+     * @return A ServerAttributeImpl instance 
+     */
+    public static ServerAttribute convertAttribute( Attribute attribute ) throws NamingException
+    {
+        assert( attribute != null );
+        
+        ServerAttribute serverAttribute = new ServerAttributeImpl( attribute.getID() );
+        NamingEnumeration<?> values = attribute.getAll();
+        
+        while ( values.hasMoreElements() )
+        {
+            Object value = values.nextElement();
+            
+            if ( value instanceof String )
+            {
+                serverAttribute.add( (String)value );
+            }
+            else if ( value instanceof byte[] )
+            {
+                serverAttribute.add( (byte[])value );
+            }
+            else
+            {
+                serverAttribute.add( (String)null );
+            }
+        }
+        
+        return serverAttribute;
+    }
+    
+    
+    /**
+     * Convert an instance of Attributes to an instance of ServerEntry
+     *
+     * @param attributes The instance to convert
+     * @return An instance of ServerEntryImpl
+     */
+    public static ServerEntry convertEntry( LdapDN dn, Attributes attributes ) throws NamingException
+    {
+        assert( dn != null );
+        assert( attributes != null );
+        
+        ServerEntry serverEntry = new ServerEntryImpl( dn );
+        
+        NamingEnumeration<? extends Attribute> attrs = attributes.getAll();
+        
+        while ( attrs.hasMoreElements() )
+        {
+            Attribute attribute = attrs.nextElement();
+            serverEntry.put( convertAttribute( attribute ) );
+        }
+        
+        return serverEntry;
     }
 }