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 2006/05/21 14:21:33 UTC
svn commit: r408160 -
/directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/Entry.java
Author: elecharny
Date: Sun May 21 05:21:32 2006
New Revision: 408160
URL: http://svn.apache.org/viewvc?rev=408160&view=rev
Log:
- Added a toString() method
- Deleted the version member
- Added a control to avoid multiple modifications of a single attribute
Modified:
directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/Entry.java
Modified: directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/Entry.java
URL: http://svn.apache.org/viewvc/directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/Entry.java?rev=408160&r1=408159&r2=408160&view=diff
==============================================================================
--- directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/Entry.java (original)
+++ directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/Entry.java Sun May 21 05:21:32 2006
@@ -30,9 +30,12 @@
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.ldap.Control;
+import org.apache.directory.shared.ldap.util.StringTools;
+
/**
* A entry to be populated by an ldif parser.
*
@@ -43,9 +46,6 @@
*/
public class Entry implements Cloneable
{
- /** the version of the ldif */
- private int version;
-
/** the change type */
private int changeType;
@@ -93,22 +93,10 @@
modificationItems = new HashMap();
dn = null;
attributeList = new BasicAttributes( true );
- version = 1; // default version in ldif
control = null;
}
/**
- * Sets the version of this ldif
- *
- * @param version
- * The version of this ldif
- */
- public void setVersion( int version )
- {
- this.version = version;
- }
-
- /**
* Set the Distinguished Name
*
* @param dn
@@ -223,7 +211,7 @@
* @param value
* The attribute's value
*/
- public void addModificationItem( int modOp, String id, Object value )
+ public void addModificationItem( int modOp, String id, Object value ) throws NamingException
{
if ( changeType == MODIFY )
{
@@ -232,6 +220,15 @@
if ( modificationItems.containsKey( id ) )
{
ModificationItem item = (ModificationItem) modificationItems.get( id );
+
+ if ( item.getModificationOp() != modOp )
+ {
+ // This is an error : we can't have two different
+ // modifications of the same attribute for the same entry
+
+ throw new NamingException( "Bad modification" );
+ }
+
Attribute attribute = item.getAttribute();
attribute.add( value );
@@ -332,14 +329,6 @@
}
/**
- * @return The ldif file version
- */
- public int getVersion()
- {
- return this.version;
- }
-
- /**
* @return The number of entry modifications
*/
public int size()
@@ -571,5 +560,151 @@
}
return clone;
+ }
+
+ /**
+ * Dumps the attributes
+ */
+ private String dumpAttributes()
+ {
+ StringBuffer sb = new StringBuffer();
+
+ try
+ {
+ for ( NamingEnumeration attrs = attributeList.getAll(); attrs.hasMoreElements(); )
+ {
+ Attribute attribute = (Attribute) attrs.nextElement();
+
+ sb.append( " ").append( attribute.getID() ).append( ":\n" );
+
+ for ( NamingEnumeration values = attribute.getAll(); values.hasMoreElements(); )
+ {
+ Object value = values.nextElement();
+
+ if ( value instanceof String )
+ {
+ sb.append( " " ).append( (String)value ).append('\n' );
+ }
+ else
+ {
+ sb.append( " " ).append( StringTools.dumpBytes( (byte[]) value ) ).append('\n' );
+ }
+ }
+ }
+ }
+ catch ( NamingException ne )
+ {
+ return "";
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Dumps the modifications
+ */
+ private String dumpModificationItems()
+ {
+ StringBuffer sb = new StringBuffer();
+
+ for ( Iterator iter = modificationList.iterator(); iter.hasNext(); )
+ {
+ ModificationItem modif = (ModificationItem) ( iter.next() );
+
+ sb.append( " Operation: " );
+
+ switch ( modif.getModificationOp() )
+ {
+ case DirContext.ADD_ATTRIBUTE :
+ sb.append( "ADD\n" );
+ break;
+
+ case DirContext.REMOVE_ATTRIBUTE :
+ sb.append( "REMOVE\n" );
+ break;
+
+ case DirContext.REPLACE_ATTRIBUTE :
+ sb.append( "REPLACE \n" );
+ break;
+ }
+
+ Attribute attribute = modif.getAttribute();
+
+ sb.append( " Attribute: " ).append( attribute.getID() ).append( '\n' );
+
+ if ( attribute.size() != 0 )
+ {
+ try
+ {
+ for ( NamingEnumeration values = attribute.getAll(); values.hasMoreElements(); )
+ {
+ Object value = values.nextElement();
+
+ if ( value instanceof String )
+ {
+ sb.append( " " ).append( (String)value ).append('\n' );
+ }
+ else
+ {
+ sb.append( " " ).append( StringTools.dumpBytes( (byte[]) value ) ).append('\n' );
+ }
+ }
+ }
+ catch ( NamingException ne )
+ {
+ return "";
+ }
+ }
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Return a String representing the Entry
+ */
+ public String toString()
+ {
+ StringBuffer sb = new StringBuffer();
+ sb.append( "Entry : " ).append( dn ).append( '\n' );
+
+ if ( control != null )
+ {
+ sb.append( " Control : " ).append( control ).append( '\n' );
+ }
+
+ switch ( changeType )
+ {
+ case ADD :
+ sb.append( " Change type is ADD\n" );
+ sb.append( " Attributes : \n" );
+ sb.append( dumpAttributes() );
+ break;
+
+ case MODIFY :
+ sb.append( " Change type is MODIFY\n" );
+ sb.append( " Modifications : \n" );
+ sb.append( dumpModificationItems() );
+ break;
+
+ case DELETE :
+ sb.append( " Change type is DELETE\n" );
+ break;
+
+ case MODDN :
+ case MODRDN :
+ sb.append( " Change type is ").append( changeType == MODDN ? "MODDN\n" : "MODRDN\n" );
+ sb.append( " Delete old RDN : " ).append( deleteOldRdn ? "true\n" : "false\n" );
+ sb.append( " New RDN : " ).append( newRdn ).append( '\n' );
+
+ if ( StringTools.isEmpty( newSuperior ) == false )
+ {
+ sb.append( " New superior : " ).append( newSuperior ).append( '\n' );
+ }
+
+ break;
+ }
+
+ return sb.toString();
}
}