Customer Portal

LDIF file format

Comments 2

  • Avatar
    dpavlis
    0
    Comment actions Permalink
    Unfortunately, CloverETL as of 2.7 version does not have an LDIF parser. You can read LDAP data (once they are loaded into LDAP server) through LDAPreader component.

    If you decide to build your own, then you may have a look at org.jetel.data.parser.Parser interface and as an inspiration see org.jetel.data.parser.DelimitedDataParser (it is one of the simpler ones).

    If you succeed in creating it and decide to donate it into project, we will be more than happy :) Also, if you encounter any problems during developing it, don't hesitate to contact us (on info@cloveretl.org)
  • Avatar
    felix
    0
    Comment actions Permalink
    Thanks for the information.

    I will try to implement this new reader if I found some time.

    In the meantime I have done this simple program which transform LDIF into CSV . Depends on ldapjdk.jar library.

    import java.util.*;
    import netscape.ldap.util.*;
    import netscape.ldap.*;
    import java.io.*;

    /**
    * LDIF2CSV
    * Created on 9 avr. 2009, 16:30:26
    *
    * @author amielp
    *
    *
    *
    */
    public class LDIF2CSV {

    final static String SEPARATOR = ",";

    public LDIF2CSV(String file) throws IOException {
    super();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    if (args.length < 1) {
    System.out.println("Usage: java LDIF2CSV <FILENAME> [List of attribute to fetch. Multiple attrs can be concatenated in one value using pipe separator]");
    System.out.println("ex: java LDIF2CSV /tmp/file.ldif cn description attr1|attr2");
    System.out.println("Remarks or comments ? mail amielp@gmail.com");
    System.exit(1);
    }
    LDIF ldif = null;
    try {
    ldif = new LDIF(args[0]);
    } catch (Exception e) {
    System.err.println("Failed to read LDIF file " + args[0] +
    ", " + e.toString());
    System.exit(1);
    }
    try {
    // Pass param to lowercase
    for (int j = 1; j < args.length; j++) {
    args[j] = args[j].toLowerCase();
    }
    // Print header
    System.out.print(args[1]);
    for (int j = 2; j < args.length; j++) {
    System.out.print(SEPARATOR + args[j]);
    }
    System.out.println();

    for (LDIFRecord rec = ldif.nextRecord(); rec != null; rec = ldif.nextRecord()) {
    LDIFAttributeContent content = (LDIFAttributeContent) rec.getContent();

    try {
    LDAPAttribute[] m_attrs = content.getAttributes();
    LDAPAttribute attr;
    String[] out = new String[args.length - 1];

    for (int i = 0; i < m_attrs.length; i++) {
    attr = (LDAPAttribute) m_attrs[i];
    for (int j = 1; j < args.length; j++) {
    if (attr.getName().equalsIgnoreCase(args[j]) || args[j].indexOf(attr.getName().toLowerCase() + '|') != -1 || args[j].indexOf('|' + attr.getName().toLowerCase()) != -1) {
    // Optimized version but sensible to LDAP API case
    // if (attr.getName().equals(args[j]) || args[j].indexOf(attr.getName() + '|') != -1 || args[j].indexOf('|' + attr.getName()) != -1) {
    if (attr.getStringValueArray().length == 1) {
    out[j - 1] = attr.getStringValueArray()[0];
    } else {
    StringBuffer v = new StringBuffer("\"");
    Enumeration e = attr.getStringValues();
    if (e.hasMoreElements()) {
    v.append((String) e.nextElement());
    }
    while (e.hasMoreElements()) {
    v.append(SEPARATOR);
    v.append((String) e.nextElement());
    }
    v.append("\"");
    out[j - 1] = v.toString();
    }
    break;
    }
    }

    }

    // print record
    System.out.print(out[0]);
    for (int j = 1; j < out.length; j++) {
    System.out.print(SEPARATOR + out[j]);
    }
    System.out.println();
    } catch (NullPointerException e) {
    e.printStackTrace();
    }
    }
    } catch (IOException ex) {
    System.out.println(ex);
    System.exit(1);
    }

    System.exit(0);
    }
    }

Please sign in to leave a comment.