mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
22.26.2016 91d2c2f00090718004ea0e04419678e2b4da0c5e
OPENDJ-2877: add methods for generating LDIF from entries and change records

Added the following methods:

* Entries.toLDIF(Entry)
* LDIF.toLDIF(Entry)
* LDIF.toLDIF(ChangeRecord)

Changed AbstractEntry.toString() to output LDIF as it is for more debug
friendly.
3 files modified
66 ■■■■ changed files
opendj-core/src/main/java/org/forgerock/opendj/ldap/AbstractEntry.java 15 ●●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java 12 ●●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java 39 ●●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/AbstractEntry.java
@@ -237,20 +237,7 @@
    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        builder.append('"');
        builder.append(getName());
        builder.append("\":{");
        boolean firstValue = true;
        for (final Attribute attribute : getAllAttributes()) {
            if (!firstValue) {
                builder.append(',');
            }
            builder.append(attribute);
            firstValue = false;
        }
        builder.append('}');
        return builder.toString();
        return Entries.toLDIF(this);
    }
    private boolean isAssignable(final AttributeDescription from, final AttributeDescription to) {
opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java
@@ -933,6 +933,18 @@
    }
    /**
     * Returns the LDIF representation of {@code entry}. All attributes will be included and no wrapping will be
     * performed. This method can be useful when debugging applications.
     *
     * @param entry
     *         The entry to be converted to LDIF.
     * @return The LDIF representation of {@code entry}.
     */
    public static String toLDIF(final Entry entry) {
        return LDIF.toLDIF(entry);
    }
    /**
     * Returns a read-only view of {@code entry} and its attributes. Query
     * operations on the returned entry and its attributes "read-through" to the
     * underlying entry or attribute, and attempts to modify the returned entry
opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java
@@ -18,6 +18,7 @@
import static com.forgerock.opendj.ldap.CoreMessages.*;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -788,6 +789,44 @@
        };
    }
    /**
     * Returns the LDIF representation of {@code entry}. All attributes will be included and no wrapping will be
     * performed. This method can be useful when debugging applications.
     *
     * @param entry
     *         The entry to be converted to LDIF.
     * @return The LDIF representation of {@code entry}.
     */
    public static String toLDIF(final Entry entry) {
        try (final StringWriter writer = new StringWriter();
             final LDIFEntryWriter ldifWriter = new LDIFEntryWriter(writer)) {
            ldifWriter.writeEntry(entry);
            ldifWriter.flush();
            return writer.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * Returns the LDIF representation of {@code change}. No wrapping will be performed. This method can be useful when
     * debugging applications.
     *
     * @param change
     *         The change record to be converted to LDIF.
     * @return The LDIF representation of {@code change}.
     */
    public static String toLDIF(final ChangeRecord change) {
        try (final StringWriter writer = new StringWriter();
             final LDIFChangeRecordWriter ldifWriter = new LDIFChangeRecordWriter(writer)) {
            ldifWriter.writeChangeRecord(change).toString();
            ldifWriter.flush();
            return writer.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static List<byte[][]> readEntriesAsList(final EntryReader reader) throws IOException {
        final List<byte[][]> entries = new ArrayList<>();