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/01/10 17:28:45 UTC

svn commit: r494877 - /directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java

Author: elecharny
Date: Wed Jan 10 08:28:44 2007
New Revision: 494877

URL: http://svn.apache.org/viewvc?view=rev&rev=494877
Log:
- Added a flag 'normalized' and a method isNormalized() which tells if the dn has been normalized. It allows some core methods to avoid doing a costly normalization
- Applied generic to List and Map
- Added a static factory (nromalize( String dn, Map)) to create a normalized DN in one call. 

Modified:
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java

Modified: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java?view=diff&rev=494877&r1=494876&r2=494877
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java (original)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java Wed Jan 10 08:28:44 2007
@@ -77,6 +77,9 @@
 
    /** Value returned by the compareTo method if values are equals */
    public final static int EQUALS = 0;
+   
+   /** A flag used to tell if the DN has been normalized */
+   private boolean normalized;
 
    // ~ Static fields/initializers
    // -----------------------------------------------------------------
@@ -106,6 +109,7 @@
        super();
        upName = "";
        normName = "";
+       normalized = true;
    }
 
 
@@ -124,6 +128,9 @@
                add( nameComponent );
            }
        }
+
+       normalized = false;
+       
    }
 
 
@@ -133,18 +140,18 @@
     *
     * @param list of String name components.
     */
-   LdapDN( List list ) throws InvalidNameException
+   LdapDN( List<String> list ) throws InvalidNameException
    {
        if ( ( list != null ) && ( list.size() != 0 ) )
        {
-           Iterator nameComponents = list.iterator();
-
-           while ( nameComponents.hasNext() )
+           for ( String nameComponent:list )
            {
-               String nameComponent = ( String ) nameComponents.next();
                add( 0, nameComponent );
            }
        }
+
+       normalized = false;
+       
    }
 
 
@@ -164,6 +171,8 @@
                add( 0, nameComponent );
            }
        }
+       
+       normalized = false;
    }
 
 
@@ -181,20 +190,63 @@
     * @exception InvalidNameException is thrown if the buffer does not
     *                contains a valid DN.
     */
-   public LdapDN( String upName ) throws InvalidNameException
-   {
-       if ( upName != null )
-       {
-           LdapDnParser.parseInternal( upName, rdns );
-       }
-
-       // Stores the representations of a DN : internal (as a string and as a
-       // byte[]) and external.
-       normalizeInternal();
-       this.upName = upName;
-   }
-
+    public LdapDN( String upName ) throws InvalidNameException
+    {
+        if ( upName != null )
+        {
+            LdapDnParser.parseInternal( upName, rdns );
+        }
+
+        // Stores the representations of a DN : internal (as a string and as a
+        // byte[]) and external.
+        normalizeInternal();
+        normalized = false;
+        
+        this.upName = upName;
+    }
 
+    /**
+     * Static factory which creates a normalized DN from a String and a Map of OIDs.
+     *
+     * @param name The DN as a String
+     * @param oidsMap The OID mapping
+     * @return A valid DN
+     * @throws InvalidNameException If the DN is invalid
+     */
+    public static Name normalize( String name, Map<String, OidNormalizer> oidsMap ) throws InvalidNameException, NamingException
+    {
+        if ( ( name == null ) || ( name.length() == 0 ) || ( oidsMap == null ) || ( oidsMap.size() == 0 ) )
+        {
+            return LdapDN.EMPTY_LDAPDN;
+        }
+
+        try
+        {
+            LdapDN newDn = new LdapDN( name );
+        
+            Enumeration<Rdn> rdns = newDn.getAllRdn();
+        
+            // Loop on all RDNs
+            while ( rdns.hasMoreElements() )
+            {
+                Rdn rdn = rdns.nextElement();
+                String upName = rdn.getUpName();
+                rdnOidToName( rdn, oidsMap );
+                rdn.normalize();
+                rdn.setUpName( upName );
+            }
+        
+            newDn.normalizeInternal();
+            newDn.normalized = true;
+            
+            return newDn;
+        } 
+        catch ( NamingException ne )
+        {
+            throw new InvalidNameException( ne.getMessage() );
+        }
+    }
+    
    /**
     * Parse a buffer and checks that it is a valid DN <br>
     * <p>
@@ -217,6 +269,7 @@
            upName = new String( bytes, "UTF-8" );
            LdapDnParser.parseInternal( upName, rdns );
            this.normName = toNormName();
+           normalized = false;
        }
        catch ( UnsupportedEncodingException uee )
        {
@@ -1238,7 +1291,7 @@
    }
 
 
-   private static AttributeTypeAndValue atavOidToName( AttributeTypeAndValue atav, Map oidsMap )
+   private static AttributeTypeAndValue atavOidToName( AttributeTypeAndValue atav, Map<String, OidNormalizer> oidsMap )
        throws InvalidNameException, NamingException
    {
        String type = StringTools.trim( atav.getType() );
@@ -1256,7 +1309,7 @@
            }
            else
            {
-               OidNormalizer oidNormalizer = ( OidNormalizer ) oidsMap.get( type );
+               OidNormalizer oidNormalizer = oidsMap.get( type );
 
                if ( oidNormalizer != null )
                {
@@ -1293,7 +1346,7 @@
     *             If
     * @throws NamingException
     */
-   private static void rdnOidToName( Rdn rdn, Map oidsMap ) throws InvalidNameException, NamingException
+   private static void rdnOidToName( Rdn rdn, Map<String, OidNormalizer> oidsMap ) throws InvalidNameException, NamingException
    {
        if ( rdn.getNbAtavs() > 1 )
        {
@@ -1329,7 +1382,7 @@
                }
                else
                {
-                   OidNormalizer oidNormalizer = ( OidNormalizer ) oidsMap.get( type );
+                   OidNormalizer oidNormalizer = oidsMap.get( type );
 
                    if ( oidNormalizer != null )
                    {
@@ -1380,7 +1433,7 @@
     * @throws InvalidNameException
     *             If the DN is invalid
     */
-   public static LdapDN normalize( LdapDN dn, Map oidsMap ) throws InvalidNameException, NamingException
+   public static LdapDN normalize( LdapDN dn, Map<String, OidNormalizer> oidsMap ) throws InvalidNameException, NamingException
    {
        if ( ( dn == null ) || ( dn.size() == 0 ) || ( oidsMap == null ) || ( oidsMap.size() == 0 ) )
        {
@@ -1403,6 +1456,7 @@
 
        newDn.normalizeInternal();
 
+       newDn.normalized = true;
        return newDn;
    }
 
@@ -1421,7 +1475,7 @@
     * @throws InvalidNameException
     *             If the DN is invalid
     */
-   public void normalize( Map oidsMap ) throws InvalidNameException, NamingException
+   public void normalize( Map<String, OidNormalizer> oidsMap ) throws InvalidNameException, NamingException
    {
        if ( ( oidsMap == null ) || ( oidsMap.size() == 0 ) )
        {
@@ -1446,6 +1500,7 @@
        }
 
        normalizeInternal();
+       normalized = true;
    }
    
    /**
@@ -1459,4 +1514,10 @@
    {
        return LdapDnParser.validateInternal( dn );
    }
+
+
+public boolean isNormalized()
+{
+    return normalized;
+}
 }