All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.btmatthews.maven.plugins.ldap.dsml.DSMLFormatWriter Maven / Gradle / Ivy

Go to download

Handler for importing and exporting directory entries in from and to Directory Services Markup Language (DSML) files

The newest version!
/*
 * Copyright 2013-2016 Brian Thomas Matthews
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.btmatthews.maven.plugins.ldap.dsml;

import com.btmatthews.maven.plugins.ldap.FormatWriter;
import com.unboundid.ldap.sdk.Attribute;
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.util.ByteStringBuffer;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * A {@link FormatWriter} that outputs LDAP directory entries to DSML formatted files.
 *
 * @author Brian Matthews
 * @since 1.2.0
 */
public final class DSMLFormatWriter implements FormatWriter {

    /**
     * The underlying output stream.
     */
    private final OutputStream ldifOutputStream;
    /**
     * The end of line marker.
     */
    private final String eol;

    /**
     * Create {@link PrintWriter} that will be used to write the directory entries to the output stream and write the
     * opening <dsml:dsml;> and <dsml:directory-entry;> tags.
     *
     * @param outputStream The file output stream.
     * @throws IOException If there was a problem writing the openng tags.
     */
    public DSMLFormatWriter(final OutputStream outputStream) throws IOException {
        ldifOutputStream = outputStream;
        eol = System.getProperty("line.separator", "\n");
        final ByteStringBuffer buffer = new ByteStringBuffer();
        buffer.append("");
        buffer.append(eol);
        buffer.append("");
        buffer.append(eol);
        buffer.append("\t");
        buffer.append(eol);
        ldifOutputStream.write(buffer.toByteArray());
    }

    /**
     * Writes an individual LDAP directory entry to the DSML file.
     *
     * @param entry The directory entry.
     * @throws IOException If there was a problem writing to the underlying output stream.
     */
    public void printEntry(final Entry entry) throws IOException {
        final ByteStringBuffer buffer = new ByteStringBuffer();
        buffer.append("\t\t");
        buffer.append(eol);
        final String[] values = entry.getAttributeValues("objectclass");
        if (values != null) {
            buffer.append("\t\t\t");
            buffer.append(eol);
            for (final String value : values) {
                buffer.append("\t\t\t\t");
                buffer.append(value);
                buffer.append("");
                buffer.append(eol);
            }
            buffer.append("\t\t\t");
            buffer.append(eol);
        }
        for (final Attribute attribute : entry.getAttributes()) {
            final String name = attribute.getName();
            if (!name.equals("objectclass")) {
                buffer.append("\t\t\t");
                buffer.append(eol);
                for (final String value : attribute.getValues()) {
                    buffer.append("\t\t\t\t");
                    buffer.append(value);
                    buffer.append("");
                    buffer.append(eol);
                }
                buffer.append("\t\t\t");
                buffer.append(eol);
            }
        }
        buffer.append("\t\t");
        buffer.append(eol);
        ldifOutputStream.write(buffer.toByteArray());
    }

    /**
     * Close the {@link FormatWriter} writing the closing tags.
     *
     * @throws IOException If there was a problem writing to the underlying output stream.
     */
    public void close() throws IOException {
        final ByteStringBuffer buffer = new ByteStringBuffer();
        buffer.append("\t\n");
        buffer.append(eol);
        buffer.append("");
        buffer.append(eol);
        ldifOutputStream.write(buffer.toByteArray());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy