You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2006/08/18 16:15:19 UTC

svn commit: r432597 - in /directory/trunks: apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/ shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/ shared/ldap/src/main/java/org/apache/directory/shared/l...

Author: akarasulu
Date: Fri Aug 18 07:15:18 2006
New Revision: 432597

URL: http://svn.apache.org/viewvc?rev=432597&view=rev
Log:
applying second set of patches for DIRSERVER-612

Modified:
    directory/trunks/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/DumpCommandExecutor.java
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/message/LockableAttributesImpl.java

Modified: directory/trunks/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/DumpCommandExecutor.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/DumpCommandExecutor.java?rev=432597&r1=432596&r2=432597&view=diff
==============================================================================
--- directory/trunks/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/DumpCommandExecutor.java (original)
+++ directory/trunks/apacheds/server-tools/src/main/java/org/apache/directory/server/tools/commands/dumpcmd/DumpCommandExecutor.java Fri Aug 18 07:15:18 2006
@@ -321,49 +321,18 @@
 
             filterAttributes( dn, entry );
 
-            if ( !( entry instanceof LockableAttributesImpl ) )
+            buf.append( "# Entry: " ).append( id ).append( "\n#---------------------\n\n" );
+            if ( !LdifUtils.isLDIFSafe( dn ) )
             {
-                Attributes tmp = entry;
-                entry = new LockableAttributesImpl();
-                NamingEnumeration attrs = tmp.getAll();
-                while ( attrs.hasMore() )
-                {
-                    Attribute attr = ( Attribute ) attrs.next();
-                    LockableAttributeImpl myattr = new LockableAttributeImpl( attr.getID() );
-                    entry.put( myattr );
-                    for ( int ii = 0; ii < attr.size(); ii++ )
-                    {
-                        Object value = attr.get(ii);
-                        
-                        // Checking if the value is binary
-                        if ( value instanceof byte[] )
-                        {
-                        	// It is binary, so we have to encode it using Base64 before adding it
-                        	char[] encoded = Base64.encode( ( byte[] ) value );
-                        	
-                        	myattr.add( new String( encoded ) );                        	
-                        }
-                        else if ( value instanceof String )
-                        {
-                        	// It's a String but, we have to check if encoding isn't required
-                        	String str = (String) value;
-                        	if ( !LdifUtils.isLDIFSafe( str ) )
-                        	{
-                        		char[] encoded = Base64.encode( ( ( String ) value ).getBytes() );
-                        		
-                        		myattr.add( new String( encoded ) );
-                        	}
-                        	else
-                        	{
-                        		myattr.add( value );
-                        	}
-                        }
-                    }
-                }
-            }
+            	// If the DN isn't LdifSafe, it needs to be Base64 encoded.
 
-            buf.append( "# Entry: " ).append( id ).append( "\n#---------------------\n\n" );
-            buf.append( "dn: " ).append( dn ).append( "\n" ).append( entry );
+                buf.append( "dn:: " ).append( new String( Base64.encode( dn.getBytes() ) ) );
+            }
+            else
+            {
+                buf.append( "dn: " ).append( dn );
+            }
+            buf.append( "\n" ).append( LdifUtils.convertToLdif( entry ) );
             if ( list.hasMore() )
             {
                 buf.append( "\n\n#---------------------\n" );

Modified: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java?rev=432597&r1=432596&r2=432597&view=diff
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java (original)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java Fri Aug 18 07:15:18 2006
@@ -16,6 +16,13 @@
  */
 package org.apache.directory.shared.ldap.ldif;
 
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+
+import org.apache.directory.shared.ldap.util.Base64;
+
 
 /**
  * Some LDIF useful methods
@@ -101,5 +108,119 @@
     	// The String cannot end with a space
     	return ( currentChar != ' ' );
     }
+    
+    /**
+     * Convert an Attributes as LDIF
+     * @param attrs the Attributes to convert
+     * @return the corresponding LDIF code as a String
+     * @throws NamingException If a naming exception is encountered.
+     */
+    public static String convertToLdif( Attributes attrs ) throws NamingException
+    {
+		StringBuffer sb = new StringBuffer();
+		
+		NamingEnumeration ne = attrs.getAll();
+		
+		while ( ne.hasMore() )
+		{
+			Object attribute = ne.next();
+			if (attribute instanceof Attribute) {
+				sb.append( convertToLdif( (Attribute) attribute ) );
+			}			
+		}
+		
+		return sb.toString();
+	}
+    
+    /**
+     * Converts an Attribute as LDIF
+     * @param attr the Attribute to convert
+     * @return the corresponding LDIF code as a String
+     * @throws NamingException If a naming exception is encountered.
+     */
+	private static String convertToLdif(Attribute attr) throws NamingException
+	{
+		StringBuffer sb = new StringBuffer();
+		
+		// iterating on the attribute's values
+		for ( int i = 0; i < attr.size(); i++ )
+        {
+			StringBuffer lineBuffer = new StringBuffer();
+			
+			lineBuffer.append( attr.getID() );
+			
+			Object value = attr.get(i);
+            
+            // Checking if the value is binary
+            if ( value instanceof byte[] )
+            {
+            	// It is binary, so we have to encode it using Base64 before adding it
+            	char[] encoded = Base64.encode( ( byte[] ) value );
+            	
+            	lineBuffer.append( ":: " + new String( encoded ) );                        	
+            }
+            else if ( value instanceof String )
+            {
+            	// It's a String but, we have to check if encoding isn't required
+            	String str = (String) value;
+            	if ( !LdifUtils.isLDIFSafe( str ) )
+            	{
+            		char[] encoded = Base64.encode( ( ( String ) value ).getBytes() );
+            		
+            		lineBuffer.append( ":: " + new String( encoded ) );
+            	}
+            	else
+            	{
+            		lineBuffer.append( ": " + value );
+            	}
+            }
+            
+            lineBuffer.append( "\n" );
+            sb.append( stripLineToNChars(lineBuffer.toString(), 80));
+        }
+		
+		return sb.toString();
+	}
+	
+	/**
+	 * Strips the String every n specified characters
+	 * @param str the string to strip
+	 * @param nbChars the number of characters
+	 * @return the stripped String
+	 */
+	private static String stripLineToNChars( String str, int nbChars)
+	{
+		if ( str.length() <= nbChars )
+		{
+			return str;
+		}
+		
+		StringBuffer sb = new StringBuffer();
+		String substr;
+		int i = 0;
+		boolean firstPass = true;
+		
+		while ( i < (str.length() - nbChars) ) {
+			if ( firstPass )
+			{
+				substr = str.substring( i, i + nbChars);
+				firstPass = false;
+				// Since we add a space at the beginning of the next line,
+				// we need to update nbChars
+				nbChars--;
+			}
+			else
+			{
+				substr = str.substring( i, i + nbChars );
+			}
+			
+			sb.append( substr + "\n " );
+			i = i + nbChars;
+		}
+		// Adding the last characters
+		sb.append( str.substring(i, str.length() ) );
+		
+		return sb.toString();
+	}
 }
 

Modified: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/message/LockableAttributesImpl.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/message/LockableAttributesImpl.java?rev=432597&r1=432596&r2=432597&view=diff
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/message/LockableAttributesImpl.java (original)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/message/LockableAttributesImpl.java Fri Aug 18 07:15:18 2006
@@ -471,7 +471,7 @@
 
 
     /**
-     * Prints out the attributes as an LDIF.
+     * Returns a string representation of the object.
      * 
      * @see java.lang.Object#toString()
      */