org.opends.server.util
Class LDIFWriter

java.lang.Object
  extended by org.opends.server.util.LDIFWriter

@PublicAPI(stability=UNCOMMITTED,
           mayInstantiate=true,
           mayExtend=false,
           mayInvoke=true)
public final class LDIFWriter
extends java.lang.Object

This class provides a mechanism for writing entries in LDIF form to a file or an output stream.


Constructor Summary
LDIFWriter(LDIFExportConfig exportConfig)
          Creates a new LDIF writer with the provided configuration.
 
Method Summary
static void appendLDIFSeparatorAndValue(java.lang.StringBuilder buffer, byte[] valueBytes)
          Appends an LDIF separator and properly-encoded form of the given value to the provided buffer.
 void close()
          Closes the LDIF writer and the underlying output stream or file.
 void flush()
          Flushes the data written to the output stream or underlying file.
 void writeAddChangeRecord(Entry entry)
          Writes an add change record for the provided entry.
 void writeChangeRecord(ChangeRecordEntry changeRecord)
          Writes a change record entry for the provided change record.
 void writeComment(Message comment, int wrapColumn)
          Writes the provided comment to the LDIF file, optionally wrapping near the specified column.
 void writeDeleteChangeRecord(Entry entry, boolean commentEntry)
          Writes a delete change record for the provided entry, optionally including a comment with the full entry contents.
 boolean writeEntries(java.util.Collection<Entry> entries)
          Iterates over each entry contained in the map and writes out the entry in LDIF format.
 boolean writeEntry(Entry entry)
          Writes the provided entry to LDIF.
static void writeLDIFLine(java.lang.StringBuilder line, java.io.BufferedWriter writer, boolean wrapLines, int wrapColumn)
          Writes the provided line to LDIF using the provided information.
 void writeModifyChangeRecord(DN dn, java.util.List<Modification> modifications)
          Writes a modify change record with the provided information.
 void writeModifyDNChangeRecord(DN dn, RDN newRDN, boolean deleteOldRDN, DN newSuperior)
          Writes a modify DN change record with the provided information.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LDIFWriter

public LDIFWriter(LDIFExportConfig exportConfig)
           throws java.io.IOException
Creates a new LDIF writer with the provided configuration.

Parameters:
exportConfig - The configuration to use for the export. It must not be null.
Throws:
java.io.IOException - If a problem occurs while opening the writer.
Method Detail

writeComment

public void writeComment(Message comment,
                         int wrapColumn)
                  throws java.io.IOException
Writes the provided comment to the LDIF file, optionally wrapping near the specified column. Each line will be prefixed by the octothorpe (#) character followed by a space. If the comment should be wrapped at a specified column, then it will attempt to do so at the first whitespace character at or before that column (so it will try not wrap in the middle of a word).

This comment will be ignored by the Directory Server's LDIF reader, as well as any other compliant LDIF parsing software.

Parameters:
comment - The comment to be written. Any line breaks that it contains will be honored, and potentially new line breaks may be introduced by the wrapping process. It must not be null.
wrapColumn - The column at which long lines should be wrapped, or -1 to indicate that no additional wrapping should be added. This will override the wrap column setting specified in the LDIF export configuration.
Throws:
java.io.IOException - If a problem occurs while attempting to write the comment to the LDIF file.

writeEntries

public boolean writeEntries(java.util.Collection<Entry> entries)
                     throws java.io.IOException,
                            LDIFException
Iterates over each entry contained in the map and writes out the entry in LDIF format. The main benefit of this method is that the entries can be sorted by DN and output in sorted order.

Parameters:
entries - The Map containing the entries keyed by DN.
Returns:
trueof all of the entries were written out, falseif it was not because of the export configuration.
Throws:
java.io.IOException - If a problem occurs while writing the entry to LDIF.
LDIFException - If a problem occurs while trying to determine whether to include the entry in the export.

writeEntry

public boolean writeEntry(Entry entry)
                   throws java.io.IOException,
                          LDIFException
Writes the provided entry to LDIF.

Parameters:
entry - The entry to be written. It must not be null.
Returns:
true if the entry was actually written, or false if it was not because of the export configuration.
Throws:
java.io.IOException - If a problem occurs while writing the entry to LDIF.
LDIFException - If a problem occurs while trying to determine whether to include the entry in the export.

writeChangeRecord

public void writeChangeRecord(ChangeRecordEntry changeRecord)
                       throws java.io.IOException
Writes a change record entry for the provided change record.

Parameters:
changeRecord - The change record entry to be written.
Throws:
java.io.IOException - If a problem occurs while writing the change record.

writeAddChangeRecord

public void writeAddChangeRecord(Entry entry)
                          throws java.io.IOException
Writes an add change record for the provided entry. No filtering will be performed for this entry, nor will any export plugins be invoked. Further, only the user attributes will be included.

Parameters:
entry - The entry to include in the add change record. It must not be null.
Throws:
java.io.IOException - If a problem occurs while writing the add record.

writeDeleteChangeRecord

public void writeDeleteChangeRecord(Entry entry,
                                    boolean commentEntry)
                             throws java.io.IOException
Writes a delete change record for the provided entry, optionally including a comment with the full entry contents. No filtering will be performed for this entry, nor will any export plugins be invoked. Further, only the user attributes will be included.

Parameters:
entry - The entry to include in the delete change record. It must not be null.
commentEntry - Indicates whether to include a comment with the contents of the entry.
Throws:
java.io.IOException - If a problem occurs while writing the delete record.

writeModifyChangeRecord

public void writeModifyChangeRecord(DN dn,
                                    java.util.List<Modification> modifications)
                             throws java.io.IOException
Writes a modify change record with the provided information. No filtering will be performed, nor will any export plugins be invoked.

Parameters:
dn - The DN of the entry being modified. It must not be null.
modifications - The set of modifications to include in the change record. It must not be null.
Throws:
java.io.IOException - If a problem occurs while writing the modify record.

writeModifyDNChangeRecord

public void writeModifyDNChangeRecord(DN dn,
                                      RDN newRDN,
                                      boolean deleteOldRDN,
                                      DN newSuperior)
                               throws java.io.IOException
Writes a modify DN change record with the provided information. No filtering will be performed, nor will any export plugins be invoked.

Parameters:
dn - The DN of the entry before the rename. It must not be null.
newRDN - The new RDN for the entry. It must not be null.
deleteOldRDN - Indicates whether the old RDN value should be removed from the entry.
newSuperior - The new superior DN for the entry, or null if the entry will stay below the same parent.
Throws:
java.io.IOException - If a problem occurs while writing the modify record.

flush

public void flush()
           throws java.io.IOException
Flushes the data written to the output stream or underlying file.

Throws:
java.io.IOException - If a problem occurs while flushing the output.

close

public void close()
           throws java.io.IOException
Closes the LDIF writer and the underlying output stream or file.

Throws:
java.io.IOException - If a problem occurs while closing the writer.

appendLDIFSeparatorAndValue

public static void appendLDIFSeparatorAndValue(java.lang.StringBuilder buffer,
                                               byte[] valueBytes)
Appends an LDIF separator and properly-encoded form of the given value to the provided buffer. If the value is safe to include as-is, then a single colon, a single space, space, and the provided value will be appended. Otherwise, two colons, a single space, and a base64-encoded form of the value will be appended.

Parameters:
buffer - The buffer to which the information should be appended. It must not be null.
valueBytes - The value to append to the buffer. It must not be null.

writeLDIFLine

public static void writeLDIFLine(java.lang.StringBuilder line,
                                 java.io.BufferedWriter writer,
                                 boolean wrapLines,
                                 int wrapColumn)
                          throws java.io.IOException
Writes the provided line to LDIF using the provided information.

Parameters:
line - The line of information to write. It must not be null.
writer - The writer to which the data should be written. It must not be null.
wrapLines - Indicates whether to wrap long lines.
wrapColumn - The column at which long lines should be wrapped.
Throws:
java.io.IOException - If a problem occurs while writing the information.