directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r359006 - /directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/LdapDN.java
Date Sun, 25 Dec 2005 22:09:04 GMT
Author: elecharny
Date: Sun Dec 25 14:08:59 2005
New Revision: 359006

URL: http://svn.apache.org/viewcvs?rev=359006&view=rev
Log:
- Changed LdapRDN to Rdn
- Added some error log
- Finished the toOid() method

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=359006&r1=359005&r2=359006&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
Sun Dec 25 14:08:59 2005
@@ -153,6 +153,7 @@
             }
             catch ( UnsupportedEncodingException uee )
             {
+            	log.error( "The byte array is not an UTF-8 encoded Unicode String : " + uee.getMessage()
);
                 throw new InvalidNameException( "The byte array is not an UTF-8 encoded Unicode
String : " + uee.getMessage() );
             }
 
@@ -185,6 +186,7 @@
         }
         catch ( UnsupportedEncodingException uee )
         {
+        	log.error( "The byte array is not an UTF-8 encoded Unicode String : " + uee.getMessage()
);
             throw new InvalidNameException( "The byte array is not an UTF-8 encoded Unicode
String : " + uee.getMessage() );
         }
     }
@@ -227,7 +229,7 @@
                     sb.append( ',' );
                 }
                 
-                sb.append( ( (LdapRDN)rdns.get( i ) ) );
+                sb.append( ( (Rdn)rdns.get( i ) ) );
             }
             
             return sb.toString();
@@ -260,7 +262,7 @@
                     sb.append( ',' );
                 }
                 
-                sb.append(( (LdapRDN)rdns.get( i ) ).getUpName() );
+                sb.append(( (Rdn)rdns.get( i ) ).getUpName() );
             }
             
             return sb.toString();
@@ -294,7 +296,9 @@
     	
     	if ( posn > rdns.size() )
     	{
-    		throw new ArrayIndexOutOfBoundsException( "Impossible to get the position " + posn
+ ", the DN only has " + rdns.size() + " RDNs" );
+    		String message = "Impossible to get the position " + posn + ", the DN only has " +
rdns.size() + " RDNs";
+    		log.error( message );
+    		throw new ArrayIndexOutOfBoundsException( message );
     	}
     
     	int start = rdns.size() - posn;
@@ -312,7 +316,7 @@
     			sb.append( ',' );
     		}
     		
-    		sb.append( ( (LdapRDN)rdns.get( i ) ).getUpName() );
+    		sb.append( ( (Rdn)rdns.get( i ) ).getUpName() );
     	}
     	
     	return sb.toString();
@@ -358,7 +362,7 @@
     			sb.append( ',' );
     		}
     		
-    		sb.append( ( (LdapRDN)rdns.get( i ) ).getUpName() );
+    		sb.append( ( (Rdn)rdns.get( i ) ).getUpName() );
     	}
     	
     	return sb.toString();
@@ -430,8 +434,8 @@
             
             for ( int i = nameDN.size() - 1; i >= 0; i-- )
             {
-                LdapRDN nameRdn = (LdapRDN)(nameDN.rdns.get( nameDN.rdns.size() - i - 1 ));
-                LdapRDN ldapRdn = (LdapRDN)rdns.get( rdns.size() - i - 1 );
+            	Rdn nameRdn = (Rdn)(nameDN.rdns.get( nameDN.rdns.size() - i - 1 ));
+            	Rdn ldapRdn = (Rdn)rdns.get( rdns.size() - i - 1 );
                 
                 if ( nameRdn.compareTo(ldapRdn) != 0 )
                 {
@@ -483,8 +487,8 @@
             // Ok, iterate through all the RDN of the name
             for ( int i = 0; i < nameDN.size(); i++ )
             {
-                LdapRDN nameRdn = (LdapRDN)(nameDN.rdns.get( i ));
-                LdapRDN ldapRdn = (LdapRDN)rdns.get( i );
+            	Rdn nameRdn = (Rdn)(nameDN.rdns.get( i ));
+            	Rdn ldapRdn = (Rdn)rdns.get( i );
                 
                 if ( nameRdn.compareTo(ldapRdn) != 0 )
                 {
@@ -530,7 +534,7 @@
         }
         else
         {
-            LdapRDN rdn = (LdapRDN)rdns.get( rdns.size() - posn - 1 );
+        	Rdn rdn = (Rdn)rdns.get( rdns.size() - posn - 1 );
         
             return rdn.toString();
         }
@@ -565,6 +569,7 @@
             {
                 if ( pos >= rdns.size() ) 
                 {
+                	log.error( "Exceeded number of elements in the current object" );
                     throw new NoSuchElementException() ;
                 }
 
@@ -597,7 +602,9 @@
         
         if ( ( posn < 0 ) || ( posn > rdns.size() ) )
         {
-            throw new ArrayIndexOutOfBoundsException("The posn(" + posn + ") should be in
the range [0, " + rdns.size() + "]");
+        	String message = "The posn(" + posn + ") should be in the range [0, " + rdns.size()
+ "]";
+        	log.error( message );
+            throw new ArrayIndexOutOfBoundsException( message );
         }
         
         LdapDN newLdapDN = new LdapDN();
@@ -605,7 +612,7 @@
         for (int i = rdns.size() - posn; i < rdns.size(); i++ )
         {
             // Don't forget to clone the rdns !
-            newLdapDN.rdns.add( ( (LdapRDN)rdns.get( i ) ).clone() );
+            newLdapDN.rdns.add( ( (Rdn)rdns.get( i ) ).clone() );
         }
 
         newLdapDN.string = newLdapDN.toString();
@@ -638,7 +645,9 @@
         
         if ( ( posn < 0 ) || ( posn > rdns.size() ) )
         {
-            throw new ArrayIndexOutOfBoundsException("The posn(" + posn + ") should be in
the range [0, " + rdns.size() + "]");
+            String message = "The posn(" + posn + ") should be in the range [0, " + rdns.size()
+ "]";
+            log.error( message );
+            throw new ArrayIndexOutOfBoundsException( message );
         }
         
         LdapDN newLdapDN = new LdapDN();
@@ -646,7 +655,7 @@
         for (int i = 0; i < size() - posn; i++ )
         {
             // Don't forget to clone the rdns !
-            newLdapDN.rdns.add( ( (LdapRDN)rdns.get( i ) ).clone() );
+            newLdapDN.rdns.add( ( (Rdn)rdns.get( i ) ).clone() );
         }
 
         newLdapDN.string = newLdapDN.toString();
@@ -713,6 +722,7 @@
         }
         else
         {
+        	log.error( "Not a valid LdapDN suffix : " + name );
             throw new InvalidNameException( "The suffix is not a LdapDN" );
         }
     }
@@ -730,7 +740,7 @@
     public Name add(String comp) throws InvalidNameException
     {
         // We have to parse the nameComponent which is given as an argument
-        LdapRDN newRdn = new LdapRDN( comp );
+    	Rdn newRdn = new Rdn( comp );
         
         rdns.add( 0, newRdn );
         normalize( toUpName() );
@@ -760,11 +770,13 @@
     {
         if ( ( posn < 0 ) || ( posn > size() ) )
         {
-            throw new ArrayIndexOutOfBoundsException("The posn(" + posn + ") should be in
the range [0, " + rdns.size() + "]"); 
+        	String message = "The posn(" + posn + ") should be in the range [0, " + rdns.size()
+ "]";
+        	log.error( message );
+            throw new ArrayIndexOutOfBoundsException( message ); 
         }
 
         // We have to parse the nameComponent which is given as an argument
-        LdapRDN newRdn = new LdapRDN( comp );
+        Rdn newRdn = new Rdn( comp );
         
         int realPos = size() - posn;
         rdns.add( realPos, newRdn );
@@ -799,11 +811,13 @@
         
         if ( ( posn < 0 ) || ( posn >= rdns.size() ) )
         {
-            throw new ArrayIndexOutOfBoundsException("The posn(" + posn + ") should be in
the range [0, " + rdns.size() + "]");
+        	String message = "The posn(" + posn + ") should be in the range [0, " + rdns.size()
+ "]";
+        	log.error ( message );
+            throw new ArrayIndexOutOfBoundsException( message );
         }
         
         int realPos = size() - posn - 1;
-        LdapRDN rdn = (LdapRDN)rdns.remove( realPos );
+        Rdn rdn = (Rdn)rdns.remove( realPos );
         
         normalize( toUpName() );
         
@@ -828,14 +842,15 @@
             
             for ( int i = 0; i < rdns.size(); i++ )
             {
-                dn.rdns.add( i, ((LdapRDN)rdns.get( i ) ).clone() );
+                dn.rdns.add( i, ((Rdn)rdns.get( i ) ).clone() );
             }
             
             return dn;
         }
         catch ( CloneNotSupportedException cnse )
         {
-            throw new Error( "Assertion failure" );
+        	log.error( "The clone operation has failed" );
+            throw new Error( "Assertion failure : cannot clone the object" );
         }
     }
 
@@ -859,7 +874,7 @@
 
             for ( int i = 0; i < size(); i++ ) 
             {
-                if ( ( (LdapRDN)name.rdns.get( i ) ).compareTo( rdns.get( i ) ) != LdapRDN.EQUALS)
+                if ( ( (Rdn)name.rdns.get( i ) ).compareTo( rdns.get( i ) ) != 0)
                 {
                     return false;
                 }
@@ -908,8 +923,8 @@
 
             for ( int i = rdns.size(); i > 0; i-- )
             {
-            	LdapRDN rdn1 = (LdapRDN) rdns.get( i - 1 );
-            	LdapRDN rdn2 = (LdapRDN) ldapDN.rdns.get( i - 1 );
+            	Rdn rdn1 = (Rdn) rdns.get( i - 1 );
+            	Rdn rdn2 = (Rdn) ldapDN.rdns.get( i - 1 );
                 int res = rdn1.compareTo( rdn2 );
                 
                 if ( res != 0 )
@@ -925,6 +940,93 @@
             return 1;
         }
     }
+    
+    private static AttributeTypeAndValue atavToOid( AttributeTypeAndValue atav, Map oids
) throws InvalidNameException, NamingException
+    {
+		 String type = StringUtils.trim( atav.getType() );
+	    	
+		 if ( StringUtils.isNotEmpty( StringUtils.lowerCase( type ) ) )
+		 {
+			OidNormalizer oidNormalizer = (OidNormalizer)oids.get( type );
+				 
+			 if ( oidNormalizer != null )
+			 {
+				 return new AttributeTypeAndValue( oidNormalizer.getOid(), 
+						 (String)oidNormalizer.getNormalizer().normalize( atav.getValue() ) );
+				 
+			 }
+			 else
+			 {
+				 // We don't have a normalizer for this OID : just do nothing.
+				 return atav;
+			 }
+		 }
+		 else
+		 {
+			 // The type is empty : this is not possible... 
+			 log.error( "Empty type not allowed in a DN" );
+			 throw new InvalidNameException( "Empty type not allowed in a DN" );
+		 }
+    	
+    }
+    
+    /**
+     * Transform a RDN by changing the value to its OID counterpart and normalizing
+     * the value accordingly to its type.
+     * @param rdn The RDN to modify
+     * @param oids The map of all existing oids and normalizer 
+     * @throws InvalidNameException If 
+     * @throws NamingException
+     */
+    private static void rdnToOid( Rdn rdn, Map oids ) throws InvalidNameException, NamingException
+    {
+		 if ( rdn.getNbAtavs() > 1 )
+		 {
+			 // We have more than one ATAV for this RDN. We will loop on all ATAVs
+			 Rdn rdnCopy = (Rdn)rdn.clone();
+			 rdn.clear();
+			 
+			 Iterator atavs = rdnCopy.iterator();
+			 
+			 while ( atavs.hasNext() )
+			 {
+				 AttributeTypeAndValue newAtav = atavToOid( (AttributeTypeAndValue)atavs.next(), oids
);
+				 rdn.addAttributeTypeAndValue( newAtav.getType(), newAtav.getValue() );
+			 }
+			 
+			 
+		 }
+		 else
+		 {
+			 String type = StringUtils.trim( rdn.getType() );
+		    	
+			 if ( StringUtils.isNotEmpty( StringUtils.lowerCase( type ) ) )
+			 {
+				OidNormalizer oidNormalizer = (OidNormalizer)oids.get( type );
+					 
+				 if ( oidNormalizer != null )
+				 {
+					 Rdn rdnCopy = (Rdn)rdn.clone();
+					 rdn.clear();
+					 
+					 rdn.addAttributeTypeAndValue( oidNormalizer.getOid(), 
+							 (String)oidNormalizer.getNormalizer().normalize( rdnCopy.getValue() ) );
+					 
+				 }
+				 else
+				 {
+					 // We don't have a normalizer for this OID : just do nothing.
+					 return;
+				 }
+			 }
+			 else
+			 {
+				 // The type is empty : this is not possible... 
+				 log.error( "We should not have an empty DN" );
+				 throw new InvalidNameException( "Empty type not allowed in a DN" );
+			 }
+		 }
+    }
 
     /**
      * Change the internal DN, using OID instead of names. As we still have the UP name
@@ -960,42 +1062,11 @@
     	// Loop on all RDNs
     	while ( rdns.hasMoreElements() )
     	{
-    		 LdapRDN rdn = (LdapRDN)rdns.nextElement();
-    		 
-    		 if ( rdn.getNbAtavs() > 1 )
-    		 {
-    			 // We have more than one ATAV for this RDN. We will loop on all ATAVs
-    			 
-    		 }
-    		 else
-    		 {
-    			 // We have only one ATAV
-    			 AttributeTypeAndValue atav = rdn.getAtav();
-    			 String type = StringUtils.trim( atav.getType() );
-    				 
-    			 if ( StringUtils.isNotEmpty( StringUtils.lowerCase( type ) ) )
-    			 {
-    				OidNormalizer oidNormalizer = (OidNormalizer)oids.get( type );
-    					 
-					 if ( oidNormalizer != null )
-					 {
-						 rdn.removeAttributeTypeAndValue( atav.getType() );
-						 rdn.addAttributeTypeAndValue( oidNormalizer.getOid(), 
-								 (String)oidNormalizer.getNormalizer().normalize( atav.getValue() ) );
-					 }
-					 else
-					 {
-						 // We don't have a normalizer for this OID : just do nothing.
-						 continue;
-					 }
-				 }
-				 else
-				 {
-					 // The type is empty : this is not possible... 
-					 log.error( "There is an empty type fo the DN '" + newDn.upName +"'. This is not allowed"
);
-					 throw new InvalidNameException( "Empty type not allowed in a DN" );
-				 }
-    		 }
+    		Rdn rdn = (Rdn)rdns.nextElement();
+    		String upName = rdn.getUpName();
+    		rdnToOid( rdn, oids );
+    		rdn.normalizeString();
+    		rdn.setUpName( upName );
     	}
     	
     	return newDn;



Mime
View raw message