Example usage for javax.naming.ldap LdapName getPrefix

List of usage examples for javax.naming.ldap LdapName getPrefix

Introduction

In this page you can find the example usage for javax.naming.ldap LdapName getPrefix.

Prototype

public Name getPrefix(int posn) 

Source Link

Document

Creates a name whose components consist of a prefix of the components of this LDAP name.

Usage

From source file:LdapNameSuffixPrefix.java

public static void main(String args[]) {
    String name = "cn=Mango, ou=Fruits, o=Food";
    try {//  www.j  a  v  a  2 s.c o  m
        LdapName dn = new LdapName(name);
        Name suffix = dn.getSuffix(1); // 1 <= index < cn.size()
        Name prefix = dn.getPrefix(1); // 0 <= index < 1
        System.out.println(suffix);
        System.out.println(prefix);
    } catch (InvalidNameException e) {
        System.out.println("Cannot parse name: " + name);
    }
}

From source file:org.lsc.utils.output.LdifLayout.java

public static String format(LscModifications lm) {
    StringBuilder msgBuffer = new StringBuilder();

    printHeader(msgBuffer);//from   w ww .  j  av  a  2  s.  co  m

    String dn = "";
    if (lm.getMainIdentifier() != null && lm.getMainIdentifier().length() > 0) {
        dn = lm.getMainIdentifier();
    }

    // print dn and base64 encode if it's not a SAFE-STRING
    msgBuffer.append("dn");
    if (isLdifSafeString(dn)) {
        msgBuffer.append(": ").append(dn);
    } else {
        msgBuffer.append(":: ").append(toBase64(dn));
    }
    msgBuffer.append("\n");

    switch (lm.getOperation()) {
    case CREATE_OBJECT:
        msgBuffer.append("changetype: add\n");
        msgBuffer.append(listToLdif(lm.getLscAttributeModifications(), true));
        break;
    case CHANGE_ID:
        LdapName ln;
        try {
            ln = new LdapName(lm.getNewMainIdentifier());
            msgBuffer.append("changetype: modrdn\nnewrdn: ");
            msgBuffer.append(ln.get(ln.size() - 1));
            msgBuffer.append("\ndeleteoldrdn: 1\nnewsuperior: ");
            if (ln.size() > 1) {
                msgBuffer.append(ln.getPrefix(ln.size() - 1));
            }
            msgBuffer.append("\n");
        } catch (InvalidNameException e) {
            msgBuffer.append("changetype: modrdn\nnewrdn: ");
            msgBuffer.append(lm.getNewMainIdentifier());
            msgBuffer.append("\ndeleteoldrdn: 1\nnewsuperior: ");
            msgBuffer.append(lm.getNewMainIdentifier());
            msgBuffer.append("\n");
        }
        break;
    case UPDATE_OBJECT:
        msgBuffer.append("changetype: modify\n");
        msgBuffer.append(listToLdif(lm.getLscAttributeModifications(), false));
        break;
    case DELETE_OBJECT:
        msgBuffer.append("changetype: delete\n");
        break;
    default:
    }
    msgBuffer.append("\n");
    return msgBuffer.toString();
}