in ldap/model/src/main/java/org/apache/directory/api/ldap/model/ldif/LdifUtils.java [366:530]
public static String convertToLdif( LdifEntry entry, int length ) throws LdapException
{
StringBuilder sb = new StringBuilder();
// First, dump the Dn
if ( isLDIFSafe( entry.getDn().getName() ) )
{
sb.append( stripLineToNChars( "dn: " + entry.getDn(), length ) );
}
else
{
sb.append( stripLineToNChars( "dn:: " + encodeBase64( entry.getDn().getName() ), length ) );
}
sb.append( '\n' );
// Dump the ChangeType
String changeType = Strings.toLowerCaseAscii( entry.getChangeType().toString() );
if ( entry.getChangeType() != ChangeType.None )
{
// First dump the controls if any
if ( entry.hasControls() )
{
for ( LdifControl control : entry.getControls().values() )
{
StringBuilder controlStr = new StringBuilder();
controlStr.append( "control: " ).append( control.getOid() );
controlStr.append( " " ).append( control.isCritical() );
if ( control.hasValue() )
{
controlStr.append( "::" ).append( Base64.getEncoder().encode( control.getValue() ) );
}
sb.append( stripLineToNChars( controlStr.toString(), length ) );
sb.append( '\n' );
}
}
sb.append( stripLineToNChars( "changetype: " + changeType, length ) );
sb.append( '\n' );
}
switch ( entry.getChangeType() )
{
case None:
if ( entry.hasControls() )
{
sb.append( stripLineToNChars( "changetype: " + ChangeType.Add, length ) );
}
// Fallthrough
case Add:
if ( entry.getEntry() == null )
{
throw new LdapException( I18n.err( I18n.ERR_13472_ENTRY_WITH_NO_ATTRIBUTE ) );
}
// Now, iterate through all the attributes
for ( Attribute attribute : entry.getEntry() )
{
sb.append( convertToLdif( attribute, length ) );
}
break;
case Delete:
if ( entry.getEntry() != null )
{
throw new LdapException( I18n.err( I18n.ERR_13471_DELETED_ENTRY_WITH_ATTRIBUTES ) );
}
break;
case ModDn:
case ModRdn:
if ( entry.getEntry() != null )
{
throw new LdapException( I18n.err( I18n.ERR_13473_MODDN_WITH_ATTRIBUTES ) );
}
// Stores the new Rdn
Attribute newRdn = new DefaultAttribute( "newrdn", entry.getNewRdn() );
sb.append( convertToLdif( newRdn, length ) );
// Stores the deleteoldrdn flag
sb.append( "deleteoldrdn: " );
if ( entry.isDeleteOldRdn() )
{
sb.append( "1" );
}
else
{
sb.append( "0" );
}
sb.append( '\n' );
// Stores the optional newSuperior
if ( !Strings.isEmpty( entry.getNewSuperior() ) )
{
Attribute newSuperior = new DefaultAttribute( "newsuperior", entry.getNewSuperior() );
sb.append( convertToLdif( newSuperior, length ) );
}
break;
case Modify:
boolean isFirst = true;
for ( Modification modification : entry.getModifications() )
{
if ( isFirst )
{
isFirst = false;
}
else
{
sb.append( "-\n" );
}
switch ( modification.getOperation() )
{
case ADD_ATTRIBUTE:
sb.append( "add: " );
break;
case REMOVE_ATTRIBUTE:
sb.append( "delete: " );
break;
case REPLACE_ATTRIBUTE:
sb.append( "replace: " );
break;
case INCREMENT_ATTRIBUTE:
sb.append( "increment: " );
break;
default:
throw new IllegalArgumentException( I18n.err( I18n.ERR_13434_UNEXPECTED_MOD_OPERATION, modification.getOperation() ) );
}
sb.append( modification.getAttribute().getUpId() );
sb.append( '\n' );
sb.append( convertToLdif( modification.getAttribute(), length ) );
}
sb.append( '-' );
break;
default:
throw new IllegalArgumentException( I18n.err( I18n.ERR_13431_UNEXPECTED_CHANGETYPE, entry.getChangeType() ) );
}
sb.append( '\n' );
return sb.toString();
}