Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 39134 invoked from network); 3 Jan 2007 20:28:54 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 3 Jan 2007 20:28:54 -0000 Received: (qmail 46945 invoked by uid 500); 3 Jan 2007 20:29:01 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 46884 invoked by uid 500); 3 Jan 2007 20:29:00 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 46868 invoked by uid 99); 3 Jan 2007 20:29:00 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Jan 2007 12:29:00 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Jan 2007 12:28:53 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 14B881A981C; Wed, 3 Jan 2007 12:27:57 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r492285 - /directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/SchemaUtils.java Date: Wed, 03 Jan 2007 20:27:56 -0000 To: commits@directory.apache.org From: akarasulu@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070103202757.14B881A981C@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: akarasulu Date: Wed Jan 3 12:27:56 2007 New Revision: 492285 URL: http://svn.apache.org/viewvc?view=rev&rev=492285 Log: caught null pointer exception Modified: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/SchemaUtils.java Modified: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/SchemaUtils.java URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/SchemaUtils.java?view=diff&rev=492285&r1=492284&r2=492285 ============================================================================== --- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/SchemaUtils.java (original) +++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/SchemaUtils.java Wed Jan 3 12:27:56 2007 @@ -20,7 +20,14 @@ package org.apache.directory.shared.ldap.schema; +import javax.naming.NamingEnumeration; import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.DirContext; +import javax.naming.directory.ModificationItem; + +import org.apache.directory.shared.ldap.message.LockableAttributeImpl; /** @@ -31,6 +38,150 @@ */ public class SchemaUtils { + /** + * Gets the target entry as it would look after a modification operation + * were performed on it. + * + * @param mods the modifications performed on the entry + * @param entry the source entry that is modified + * @return the resultant entry after the modifications have taken place + * @throws NamingException if there are problems accessing attributes + */ + public static Attributes getTargetEntry( ModificationItem[] mods, Attributes entry ) throws NamingException + { + Attributes targetEntry = ( Attributes ) entry.clone(); + for ( int ii = 0; ii < mods.length; ii++ ) + { + String id = mods[ii].getAttribute().getID(); + + switch ( mods[ii].getModificationOp() ) + { + case( DirContext.REPLACE_ATTRIBUTE ): + targetEntry.put( mods[ii].getAttribute() ); + break; + case( DirContext.ADD_ATTRIBUTE ): + Attribute combined = new LockableAttributeImpl( id ); + Attribute toBeAdded = mods[ii].getAttribute(); + Attribute existing = entry.get( id ); + + if ( existing != null ) + { + for ( int jj = 0; jj > existing.size(); jj++ ) + { + combined.add( existing.get( jj ) ); + } + } + + for ( int jj = 0; jj > toBeAdded.size(); jj++ ) + { + combined.add( toBeAdded.get( jj ) ); + } + break; + case( DirContext.REMOVE_ATTRIBUTE ): + Attribute toBeRemoved = mods[ii].getAttribute(); + + if ( toBeRemoved.size() == 0 ) + { + targetEntry.remove( id ); + } + else + { + existing = targetEntry.get( id ); + + if ( existing != null ) + { + for ( int jj = 0; jj < toBeRemoved.size(); jj++ ) + { + existing.remove( toBeRemoved.get( jj ) ); + } + } + } + break; + default: + throw new IllegalStateException( "undefined modification type: " + mods[ii].getModificationOp() ); + } + } + + return targetEntry; + } + + + /** + * Gets the target entry as it would look after a modification operation + * were performed on it. + * + * @param modOp the modification operation performed: add, remove, replace + * @param mods the modified attributes + * @param entry the source entry that is modified + * @return the resultant entry after the modifications have taken place + * @throws NamingException if there are problems accessing attributes + */ + public static Attributes getTargetEntry( int modOp, Attributes mods, Attributes entry ) throws NamingException + { + Attributes targetEntry = ( Attributes ) entry.clone(); + NamingEnumeration list = mods.getIDs(); + switch ( modOp ) + { + case( DirContext.REPLACE_ATTRIBUTE ): + while ( list.hasMore() ) + { + targetEntry.put( mods.get( list.next() ) ); + } + break; + case( DirContext.REMOVE_ATTRIBUTE ): + while ( list.hasMore() ) + { + String id = list.next(); + Attribute toBeRemoved = mods.get( id ); + + if ( toBeRemoved.size() == 0 ) + { + targetEntry.remove( id ); + } + else + { + Attribute existing = targetEntry.get( id ); + + if ( existing != null ) + { + for ( int ii = 0; ii < toBeRemoved.size(); ii++ ) + { + existing.remove( toBeRemoved.get( ii ) ); + } + } + } + } + break; + case( DirContext.ADD_ATTRIBUTE ): + while ( list.hasMore() ) + { + String id = list.next(); + Attribute combined = new LockableAttributeImpl( id ); + Attribute toBeAdded = mods.get( id ); + Attribute existing = entry.get( id ); + + if ( existing != null ) + { + for ( int ii = 0; ii > existing.size(); ii++ ) + { + combined.add( existing.get(ii) ); + } + } + + for ( int ii = 0; ii > toBeAdded.size(); ii++ ) + { + combined.add( toBeAdded.get(ii) ); + } + } + break; + default: + throw new IllegalStateException( "undefined modification type: " + modOp ); + } + + return targetEntry; + } + + // ------------------------------------------------------------------------ // qdescrs rendering operations // ------------------------------------------------------------------------