directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r412230 - /directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
Date Tue, 06 Jun 2006 22:35:28 GMT
Author: elecharny
Date: Tue Jun  6 15:35:28 2006
New Revision: 412230

URL: http://svn.apache.org/viewvc?rev=412230&view=rev
Log:
- Added a public normalize() method
- renamed the private normalize() method to normalizeInternal
- Added an ugly static OIDS_MAP : to be removed !!!

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

Modified: directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java?rev=412230&r1=412229&r2=412230&view=diff
==============================================================================
--- directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
(original)
+++ directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
Tue Jun  6 15:35:28 2006
@@ -31,7 +31,6 @@
 import javax.naming.NamingException;
 
 import org.apache.directory.shared.ldap.name.Rdn;
-import org.apache.directory.shared.ldap.schema.Normalizer;
 import org.apache.directory.shared.ldap.schema.OidNormalizer;
 import org.apache.directory.shared.ldap.util.StringTools;
 import org.slf4j.Logger;
@@ -92,6 +91,9 @@
 
     /** A null LdapDN */
     public static final LdapDN EMPTY_LDAPDN = new LdapDN();
+    
+    /** The OIDs map container */
+    private static Map OIDS_MAP;
 
 
     // ~ Methods
@@ -190,7 +192,7 @@
 
         // Stores the representations of a DN : internal (as a string and as a
         // byte[]) and external.
-        normalize();
+        normalizeInternal();
         this.upName = upName;
     }
 
@@ -232,7 +234,7 @@
      * 
      * @return a normalized form of the DN
      */
-    private void normalize()
+    private void normalizeInternal()
     {
         normName = toNormName();
     }
@@ -930,7 +932,7 @@
     public Name addAll( Name suffix ) throws InvalidNameException
     {
         addAll( rdns.size(), suffix );
-        normalize();
+        normalizeInternal();
         toUpName();
 
         return this;
@@ -968,7 +970,7 @@
             rdns.addAll( size() - posn, ( ( LdapDN ) name ).rdns );
 
             // Regenerate the normalized name and the original string
-            normalize();
+            normalizeInternal();
             toUpName();
 
             return this;
@@ -997,7 +999,7 @@
         Rdn newRdn = new Rdn( comp );
 
         rdns.add( 0, newRdn );
-        normalize();
+        normalizeInternal();
         toUpName();
 
         return this;
@@ -1016,7 +1018,7 @@
     public Name add( Rdn newRdn ) throws InvalidNameException
     {
         rdns.add( rdns.size() - 1, newRdn );
-        normalize();
+        normalizeInternal();
         toUpName();
 
         return this;
@@ -1054,7 +1056,7 @@
         int realPos = size() - posn;
         rdns.add( realPos, newRdn );
 
-        normalize();
+        normalizeInternal();
         toUpName();
 
         return this;
@@ -1093,7 +1095,7 @@
         int realPos = size() - posn - 1;
         Rdn rdn = ( Rdn ) rdns.remove( realPos );
 
-        normalize();
+        normalizeInternal();
         toUpName();
 
         return rdn;
@@ -1218,7 +1220,7 @@
     }
 
 
-    private static AttributeTypeAndValue atavOidToName( AttributeTypeAndValue atav, Map oids
)
+    private static AttributeTypeAndValue atavOidToName( AttributeTypeAndValue atav )
         throws InvalidNameException, NamingException
     {
         String type = StringTools.trim( atav.getType() );
@@ -1230,18 +1232,25 @@
 
         if ( StringTools.isNotEmpty( StringTools.lowerCase( type ) ) )
         {
-            OidNormalizer oidNormalizer = ( OidNormalizer ) oids.get( type );
-
-            if ( oidNormalizer != null )
+            if ( OIDS_MAP == null )
             {
-                return new AttributeTypeAndValue( oidNormalizer.getAttributeTypeOid(), (
String ) oidNormalizer.getNormalizer()
-                    .normalize( atav.getValue() ) );
-
+                return atav;
             }
             else
             {
-                // We don't have a normalizer for this OID : just do nothing.
-                return atav;
+                OidNormalizer oidNormalizer = ( OidNormalizer ) OIDS_MAP.get( type );
+    
+                if ( oidNormalizer != null )
+                {
+                    return new AttributeTypeAndValue( oidNormalizer.getAttributeTypeOid(),
( String ) oidNormalizer.getNormalizer()
+                        .normalize( atav.getValue() ) );
+    
+                }
+                else
+                {
+                    // We don't have a normalizer for this OID : just do nothing.
+                    return atav;
+                }
             }
         }
         else
@@ -1266,7 +1275,7 @@
      *             If
      * @throws NamingException
      */
-    private static void rdnOidToName( Rdn rdn, Map oids ) throws InvalidNameException, NamingException
+    private static void rdnOidToName( Rdn rdn ) throws InvalidNameException, NamingException
     {
         if ( rdn.getNbAtavs() > 1 )
         {
@@ -1280,7 +1289,7 @@
             while ( atavs.hasNext() )
             {
                 Object val = atavs.next();
-                AttributeTypeAndValue newAtav = atavOidToName( ( AttributeTypeAndValue )
val, oids );
+                AttributeTypeAndValue newAtav = atavOidToName( ( AttributeTypeAndValue )
val );
                 rdn.addAttributeTypeAndValue( newAtav.getType(), newAtav.getValue() );
             }
 
@@ -1296,22 +1305,29 @@
 
             if ( StringTools.isNotEmpty( StringTools.lowerCase( type ) ) )
             {
-                OidNormalizer oidNormalizer = ( OidNormalizer ) oids.get( type );
-
-                if ( oidNormalizer != null )
+                if ( OIDS_MAP == null )
                 {
-                    Rdn rdnCopy = ( Rdn ) rdn.clone();
-                    rdn.clear();
-                    
-                    rdn.addAttributeTypeAndValue( oidNormalizer.getAttributeTypeOid(), (
String ) oidNormalizer.getNormalizer()
-                        .normalize( rdnCopy.getValue() ) );
-
+                    return;
                 }
                 else
                 {
-                    // We don't have a normalizer for this OID : just do
-                    // nothing.
-                    return;
+                    OidNormalizer oidNormalizer = ( OidNormalizer ) OIDS_MAP.get( type );
+    
+                    if ( oidNormalizer != null )
+                    {
+                        Rdn rdnCopy = ( Rdn ) rdn.clone();
+                        rdn.clear();
+                        
+                        rdn.addAttributeTypeAndValue( oidNormalizer.getAttributeTypeOid(),
( String ) oidNormalizer.getNormalizer()
+                            .normalize( rdnCopy.getValue() ) );
+    
+                    }
+                    else
+                    {
+                        // We don't have a normalizer for this OID : just do
+                        // nothing.
+                        return;
+                    }
                 }
             }
             else
@@ -1342,9 +1358,9 @@
      * @throws InvalidNameException
      *             If the DN is invalid
      */
-    public static LdapDN normalize( LdapDN dn, Map oids ) throws InvalidNameException, NamingException
+    public static LdapDN normalize( LdapDN dn ) throws InvalidNameException, NamingException
     {
-        if ( ( dn == null ) || ( dn.size() == 0 ) || ( oids == null ) || ( oids.size() ==
0 ) )
+        if ( ( dn == null ) || ( dn.size() == 0 ) || ( OIDS_MAP == null ) || ( OIDS_MAP.size()
== 0 ) )
         {
             return dn;
         }
@@ -1358,13 +1374,63 @@
         {
             Rdn rdn = ( Rdn ) rdns.nextElement();
             String upName = rdn.getUpName();
-            rdnOidToName( rdn, oids );
+            rdnOidToName( rdn );
             rdn.normalizeString();
             rdn.setUpName( upName );
         }
 
-        newDn.normalize();
+        newDn.normalizeInternal();
 
         return newDn;
+    }
+    
+    /**
+     * Change the internal DN, using the first alias instead of oids or other
+     * aliases. As we still have the UP name of each RDN, we will be able to
+     * provide both representation of the DN. example : dn: 2.5.4.3=People,
+     * dc=example, domainComponent=com will be transformed to : cn=People,
+     * dc=example, dc=com because 2.5.4.3 is the OID for cn and dc is the first
+     * alias of the couple of aliases (dc, domaincomponent). This is really
+     * important do have such a representation, as 'cn' and 'commonname' share
+     * the same OID.
+     * 
+     * @param dn
+     *            The DN to transform
+     * @param oids
+     *            The mapping between names and oids.
+     * @return A normalized form of the DN
+     * @throws InvalidNameException
+     *             If the DN is invalid
+     */
+    public void normalize() throws InvalidNameException, NamingException
+    {
+        if ( ( OIDS_MAP == null ) || ( OIDS_MAP.size() == 0 ) )
+        {
+            return;
+        }
+
+        Enumeration rdns = getAllRdn();
+
+        // Loop on all RDNs
+        while ( rdns.hasMoreElements() )
+        {
+            Rdn rdn = ( Rdn ) rdns.nextElement();
+            String upName = rdn.getUpName();
+            rdnOidToName( rdn );
+            rdn.normalizeString();
+            rdn.setUpName( upName );
+        }
+
+        normalizeInternal();
+    }
+    
+    /**
+     * Set the static OIDs map with the values.
+     * 
+     * @param oidsMap The HashMap which contains <oid, normalizer> pairs
+     */
+    public static synchronized void setOidsMap( Map oidsMap )
+    {
+        OIDS_MAP = oidsMap;
     }
 }



Mime
View raw message