directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r356223 - /directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java
Date Mon, 12 Dec 2005 09:11:19 GMT
Author: elecharny
Date: Mon Dec 12 01:11:14 2005
New Revision: 356223

URL: http://svn.apache.org/viewcvs?rev=356223&view=rev
Log:
Fixed a lot of method to deal correctly with user provided DN
Added some doco

Modified:
    directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java

Modified: directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java?rev=356223&r1=356222&r2=356223&view=diff
==============================================================================
--- directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java
(original)
+++ directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java
Mon Dec 12 01:11:14 2005
@@ -144,6 +144,18 @@
 import org.apache.ldap.common.LdapString;
 
 /**
+ * The LdapDN class contains a DN (Distinguished Name). Its specification can be found in
RFC 2253, 
+ * "UTF-8 String Representation of Distinguished Names".
+ * 
+ * We will store two representation of a DN :
+ *  - a user Provider represeentation, which is the parsed String given by a user
+ *  - an internal representation.
+ *  
+ * A DN is formed of RDNs, in a specific order :
+ *  RDN[n], RDN[n-1], ... RDN[1], RDN[0]
+ *  
+ * It represents a tree, in which the root is the last RDN (RDN[0]) and the leaf is the
+ * first RDN (RDN[n]).
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
@@ -194,12 +206,13 @@
      * @param bytes The byte buffer that contains the DN
      * @exception A InvalidNameException is thrown if the buffer does not contains a valid
DN.
      */
-    public LdapDN( String string ) throws InvalidNameException
+    public LdapDN( String upName ) throws InvalidNameException
     {
-        if ( StringUtils.isNotEmpty( string ) )
+        if ( StringUtils.isNotEmpty( upName ) )
         {
             try
             {
+            	// Here, we just check that the user provided DN is an UTF-8 encoded String
                 string.getBytes( "UTF-8" );
             }
             catch ( UnsupportedEncodingException uee )
@@ -207,15 +220,11 @@
                 throw new InvalidNameException( "The byte array is not an UTF-8 encoded Unicode
String : " + uee.getMessage() );
             }
 
-            DNParser.parse( string, rdns );
-        }
-        else
-        {
-            this.string = "";
+            DNParser.parse( upName, rdns );
         }
         
-        normalize();
-        upName = string;
+        // Stores the representations of a DN : internal (as a string and as a byte[]) and
external.
+        normalize( upName );
     }
     
     /**
@@ -235,9 +244,8 @@
         {
             upName = new String( bytes, "UTF-8" );
             DNParser.parse( upName, rdns );
-            string = toString();
-             
-            normalize();
+            this.string = toString();
+            this.bytes = bytes;
         }
         catch ( UnsupportedEncodingException uee )
         {
@@ -250,51 +258,51 @@
      * Normalize the DN by triming useless spaces and lowercasing names.
      * @return a normalized form of the DN
      */
-    private void normalize()
+    private void normalize( String upName )
     {
-        StringBuffer sb = new StringBuffer();
-        
-        if ( rdns == null )
+        string = toString();
+        bytes = StringUtils.getBytesUtf8( this.string );
+        this.upName = upName == null ? "" : upName ;
+    }
+    
+    /**
+     * Return the normalized DN as a String,
+     * @return A String representing the normalized DN
+     */
+    public String toString()
+    {
+        if ( ( rdns == null ) || ( rdns.size() == 0 ) )
         {
-            string = "";
+            return "";
         }
-        
-        Iterator elements = rdns.iterator();
-        boolean isFirst = true;
-        
-        while ( elements.hasNext() )
+        else
         {
-            LdapRDN rdn = (LdapRDN)elements.next();
+            StringBuffer sb = new StringBuffer();
+            boolean isFirst = true;
             
-            if ( isFirst )
-            {
-                isFirst = false;
-            }
-            else
+            for ( int i = 0; i < rdns.size(); i++ )
             {
-                sb.append(',');
+                if ( isFirst )
+                {
+                    isFirst = false;
+                }
+                else
+                {
+                    sb.append( ',' );
+                }
+                
+                sb.append( ( (LdapRDN)rdns.get( i ) ) );
             }
             
-            sb.append( rdn.toString() );
-        }
-
-        string = sb.toString();
-        
-        try
-        {
-            bytes = string.getBytes( "UTF-8" );
-        }
-        catch ( UnsupportedEncodingException uee )
-        {
-            // We can't reach this point
+            return sb.toString();
         }
     }
     
     /**
-     * Return the normalized DN as a String,
-     * @return A String representing the normalized DN
+     * Return the User Provided DN as a String,
+     * @return A String representing the User Provided DN
      */
-    public String toString()
+    public String toUpName()
     {
         if ( ( rdns == null ) || ( rdns.size() == 0 ) )
         {
@@ -316,12 +324,109 @@
                     sb.append( ',' );
                 }
                 
-                sb.append( ( (LdapRDN)rdns.get( i ) ) );
+                sb.append(( (LdapRDN)rdns.get( i ) ).getUpName() );
             }
             
             return sb.toString();
         }
     }
+    
+    /**
+     * Return the User Provided prefix representation of the DN starting at the posn position.
+     * 
+     * If posn = 0, return an empty string.
+     * 
+     * for DN : sn=smith, dc=apache, dc=org
+     * 
+     * getUpname(0) -> ""
+     * getUpName(1) -> "dc=org"
+     * getUpname(3) -> "sn=smith, dc=apache, dc=org"
+     * getUpName(4) -> ArrayOutOfBoundException
+     * 
+     * Warning ! The returned String is not exactly the user provided DN, as spaces
+     * before and after each RDNs have been trimmed.
+     * 
+     * @param posn The starting position
+     * @return The truncated DN
+     */
+    private String getUpNamePrefix( int posn )
+    {
+    	if ( posn == 0 )
+    	{
+    		return "";
+    	}
+    	
+    	if ( posn > rdns.size() )
+    	{
+    		throw new ArrayIndexOutOfBoundsException( "Impossible to get the position " + posn
+ ", the DN only has " + rdns.size() + " RDNs" );
+    	}
+    
+    	int start = rdns.size() - posn;
+    	StringBuffer sb = new StringBuffer();
+    	boolean isFirst = true;
+    	
+    	for ( int i = start; i < rdns.size(); i++ )
+    	{
+    		if ( isFirst )
+    		{
+    			isFirst = false;
+    		}
+    		else
+    		{
+    			sb.append( ',' );
+    		}
+    		
+    		sb.append( ( (LdapRDN)rdns.get( i ) ).getUpName() );
+    	}
+    	
+    	return sb.toString();
+    }
+
+    /**
+     * Return the User Provided suffix representation of the DN starting at the posn position.
+     * 
+     * If posn = 0, return an empty string.
+     * 
+     * for DN : sn=smith, dc=apache, dc=org
+     * 
+     * getUpname(0) -> "sn=smith, dc=apache, dc=org"
+     * getUpName(1) -> "sn=smith, dc=apache"
+     * getUpname(3) -> "sn=smith"
+     * getUpName(4) -> ""
+     * 
+     * Warning ! The returned String is not exactly the user provided DN, as spaces
+     * before and after each RDNs have been trimmed.
+     * 
+     * @param posn The starting position
+     * @return The truncated DN
+     */
+    private String getUpNameSuffix( int posn )
+    {
+    	if ( posn > rdns.size() )
+    	{
+    		return "";
+    	}
+    	
+    	int end = rdns.size() - posn;
+    	StringBuffer sb = new StringBuffer();
+    	boolean isFirst = true;
+    	
+    	for ( int i = 0; i < end; i++ )
+    	{
+    		if ( isFirst )
+    		{
+    			isFirst = false;
+    		}
+    		else
+    		{
+    			sb.append( ',' );
+    		}
+    		
+    		sb.append( ( (LdapRDN)rdns.get( i ) ).getUpName() );
+    	}
+    	
+    	return sb.toString();
+    }
 
     /**
      * Get the initial DN (without normalization) 
@@ -557,9 +662,9 @@
             newLdapDN.rdns.add( ( (LdapRDN)rdns.get( i ) ).clone() );
         }
 
-        newLdapDN.normalize();
         newLdapDN.string = newLdapDN.toString();
-        newLdapDN.upName = newLdapDN.string;
+        newLdapDN.bytes = StringUtils.getBytesUtf8( newLdapDN.string );
+        newLdapDN.upName = getUpNamePrefix( posn );
         
         return newLdapDN;
     }
@@ -598,12 +703,13 @@
             newLdapDN.rdns.add( ( (LdapRDN)rdns.get( i ) ).clone() );
         }
 
-        newLdapDN.normalize();
         newLdapDN.string = newLdapDN.toString();
-        newLdapDN.upName = newLdapDN.string;
+        newLdapDN.bytes = StringUtils.getBytesUtf8( newLdapDN.string );
+        newLdapDN.upName = getUpNameSuffix( posn );
 
         return newLdapDN;
     }
+    
     /**
      * Adds the components of a name -- in order -- to the end of this name.
      *
@@ -655,9 +761,7 @@
             rdns.addAll( size() - posn, ((LdapDN)name).rdns );
 
             // Regenerate the normalized name and the original string
-            normalize();
-            
-            upName = toString();
+            normalize( toUpName() );
             
             return this;
         }
@@ -683,9 +787,7 @@
         LdapRDN newRdn = new LdapRDN( comp );
         
         rdns.add( 0, newRdn );
-        normalize();
-
-        upName = toString();
+        normalize( toUpName() );
 
         return this;
     }
@@ -720,9 +822,8 @@
         
         int realPos = size() - posn;
         rdns.add( realPos, newRdn );
-        normalize();
         
-        upName = toString();
+        normalize( toUpName() );
         
         return this;
     }
@@ -757,9 +858,8 @@
         
         int realPos = size() - posn - 1;
         LdapRDN rdn = (LdapRDN)rdns.remove( realPos );
-        normalize();
         
-        upName = toString();
+        normalize( toUpName() );
         
         return rdn;
     }



Mime
View raw message