⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rdn.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ca.commons.naming;

//import java.util.Vector;

//import com.ca.commons.cbutil.*;

import javax.naming.InvalidNameException;

/**
 *    An RDN element.  May be multi-valued (but most aren't)<p>
 *
 *    Some quick definitions: <ul>
 *    <li>'raw' means an rdn with unescaped special
 *    characters in it, suitable for display to a user - cn=fred+nurk.
 *    <li>'escaped' means with special characters escaped in a form suitable
 *    for jndi transmission (leading '\' characters, and quad slashes '\\\\' for
 *    an escaped slash.
 *    <li>'jndireturn' means an incorrectly escaped string from a jndi query
 *    which requires special handling due to bugs (?) in jndi (as of java 1.3)
 *    </ul>
 *    Values are entered and stored in the RDN <i>escaped</i>, in an internal
 *    string ldapEscapedRDN.  Utility ftns
 *    are provided to translate rdns between these different forms, and functions
 *    creating/changing rdns should make sure always to pass the final (no escape
 *    character) form when using RDNs.<p>
 *
 *    While parts of an rdn (particular attributes and values) may be manipulated
 *    as raw, unescaped strings, entire rdns are always escaped when represented
 *    as strings (e.g. by the 'toString()' method).<p>
 *
 *    An added complication is unicode.  While strings may be entered as escaped
 *    utf8, they are always converted to unicode asap, and never returned as utf8.
 *    (they are automatically translated to utf8 by jndi when transmitted to the
 *    server, or manually by JXplorer when saving ldif files as a final step).
 *
 *    <p>The class is optimised for single valued RDNs, as this represents the
 *    majority of examples seen by the author...</p>
 *
 *    <p>This class uses delayed evaluation of RDN strings.  Hence invalid RDNs
 *    can be instantiated, and will only throw exceptions when used.</p>
 *
 *    @author Chris Betts
 */

public class RDN
{
    /**
     *    <p>This gives the positions of seperators ('+') or '1 past the end of string'
     *    for att-value sub elements in a multi-valued rdn.   The first element is
     *    always -1, the last is the length of the string.
     *    Usually length 2, since most RDNs are
     *    not multivalued.</p>
     *
     *    <pre>
     *     cn=Sue\,Grabbit\+Run+sn=Law         (27 characters)
     *    ^                    ^      ^
     *    |                    |      |
     *    -1                   20     27
     *    [0]                  [1]    [2]
     *    </pre>
     *
     *    Any rdn sub string (i) is thus element[i]+1 to element[i+1]
     */

    private int[] elements = null;

    /**
     *    The escaped ldap RDN (e.g. cn=Sue\,Grabbit\+Run+sn=Law)
     */

    private String ldapEscapedRDN;


    /**
     *    status constants of the RDN - whether it has been tested,
     *    and if it has, whether it is single valued or multi valued.
     */

    private int UNTESTED = 0, SINGLEVALUED = 1, MULTIVALUED = 2;

    /**
     *   the status of the RDN (one of UNTESTED|SINGLEVALUED|MULTIVALUED)
     */

    private int status = UNTESTED;

    /**
     *  Default number of allowable elements (before a manual array resize
     *  must be done).
     */

    private static int MAXELEMENTS = 16;  // maximum number of multi-valued attributes...

    /**
     *    Empty constructor - creates an RDN with no values.
     */

    public RDN() { ldapEscapedRDN = "";}


    private boolean debug = false;

    /**
     *    Standard constructor - creates an RDN using an ldap escaped utf8
     *    rdn string, which may be multi-valued.
     *    @param rdn the string rdn to be parsed
     */



    public RDN(String rdn)
    {
        if (rdn == null)
        {
            rdn = "";
        }
        else
        {
            // trim any unnecessary white space off the end...
            int len = rdn.length();

            if ((rdn.indexOf('\\') > -1) && (len >=2 && rdn.charAt(len-2) == '\\'  && rdn.charAt(len-1) == ' '))
            {
                    rdn = specialSpaceHandling(rdn); // pathalogical case
            }
            else
                rdn = rdn.trim();
        }

        ldapEscapedRDN = rdn;

        if (debug) System.out.println(" % NEW RDN: " + rdn);
    }

    /**
     *   clones an RDN.
     *   @param copyMe the RDN to copy.
     */

    public RDN(RDN copyMe)
    {
        this(copyMe.ldapEscapedRDN);
    }

    /**
     *  This RDN may have a special escaped space on the end...
     *  this method handles this (rare) case and cleans up the rdn
     *  in a process that takes a bit longer than normal.
     */

    // XXX this is kinda messy, and only works for single valued RDNs..  Can we come up with a nicer algorithm?

    private String specialSpaceHandling(String rdn)
    {
        // count the slashes...

        int finalPos = rdn.length() - 2;
        int pos = finalPos;

        // work backwards from the second last position, deleting slashes

        while (rdn.charAt(pos) == '\\')  // remember '\\' is a *single* slash!
        {
            pos--;
        }

        int numSlashesDeleted = finalPos - pos;

        int valuePos = rdn.indexOf('=')+1;
        String att = rdn.substring(0, valuePos);
        String val = rdn.substring(valuePos);

        if (numSlashesDeleted%2 == 0) // o.k. - we can trim that pesky space
        {
            val = val.trim();
        }                      // (otherwise leave it alone, it's
        else                       // escaped and meant to be there) - so
        {                       // just get rid of leading spaces...
            val = val.trim() + " ";
        }

        rdn = att + val;

        return rdn;
    }

    /**
     *    Whether the rdn is empty (i.e. is an empty string)
     */

    public boolean isEmpty()
    {
        return ("".equals(ldapEscapedRDN));
    }

    /**
     *    adds an ldap escaped utf8 Name element (i.e. the portion of an rdn separated by a '+' sign)
     *    @param rdnfragment an attribute = value pair.
     */

    public void addEscaped(String rdnfragment)
                 throws InvalidNameException
    {
        validate();  // throws InvalidNameException

        int equalpos = NameUtility.next(rdnfragment, 0, '=');
        // check rdn has at least one non null attribute and one non null value

        if (equalpos <= 0 || equalpos == rdnfragment.length()-1)
            throw new InvalidNameException("RDN.add(): invalid rdn fragment '" + ((rdnfragment==null)?"<null>":rdnfragment) + "' (can't find equal sign)");

        if (ldapEscapedRDN.length()>0)
            ldapEscapedRDN += "+" + rdnfragment;
        else
            ldapEscapedRDN = rdnfragment;

    }

    /**
     *    adds an unescaped unicode Name element (i.e. one of the parts seperated by a '+' sign).
     *    This will fail on multi-part elements (e.g. cn="fred" will work, cn="fred"+sn="erick" won't).
     *    XXX - this escapes the equals sign?
     *    @param rdnfragment an attribute = value pair.
     */

    public void addRaw(String rdnfragment)
                 throws InvalidNameException
    {
        int equalpos = NameUtility.next(rdnfragment, 0, '=');
        // check rdn has at least one non null attribute and one non null value

        if (equalpos <= 0 || equalpos == rdnfragment.length()-1)
            throw new InvalidNameException("RDN.addRaw(): invalid rdn fragment '" + ((rdnfragment==null)?"<null>":rdnfragment) + "' (can't find equal sign)");

        String attribute = rdnfragment.substring(0, equalpos);
        String value = rdnfragment.substring(equalpos+1);

        addEscaped(attribute + "=" + NameUtility.escape(value));
    }


    /**
     *    Returns the RDN as an ldap escaped ldap utf8 string.
     *    (This is a very inexpensive operation - it simply
     *    returns the pre-existing string.)
     *    @return the internal representation of the RDN as an ldap escaped string.
     */

    public String toString()
    {
        return ldapEscapedRDN;
    }

    /**
     *    Debug prints the raw, unescaped form of the elements.
     */

    public void dump()
    {
        if (status == UNTESTED)
            checkForMultiValued();

        System.out.println("DEBUG DUMP - RDN: " + ldapEscapedRDN + ((status==MULTIVALUED)?" MULTI VALUED":" SINGLE VALUED"));

        if (status == MULTIVALUED)
        {
            for (int i=0; i<(elements.length - 1); i++)
            {
                System.out.println("element-m (" + (elements[i]+1) + ") -> (" + elements[i+1] + ") " + i + ": " + getElement(i));
            }
        }
        else
        {
            System.out.println("element-s 0: " + ldapEscapedRDN);
        }

        Thread.currentThread().dumpStack();

    }

    /**
     *    Returns the Ith att-val pair in escaped ldap form.
     *    @param i the element index to get (counting from 0)
     *    @return the attribute value pair.
     */

    public String getElement(int i)
    {
        if (status == UNTESTED)
            checkForMultiValued();

        if (status == SINGLEVALUED && i==0)
            return ldapEscapedRDN;

        if (i<0 || elements == null || elements.length <= i+1)
            return "error VII";

        return ldapEscapedRDN.substring(elements[i]+1, elements[i+1]);
    }

    /**
     *    Returns all elements as a string array, in escaped ldap form.
     */

    public String[] getElements()
    {
        if (status == UNTESTED)
            checkForMultiValued();

        if (status == SINGLEVALUED)
            return new String[] {ldapEscapedRDN};

        if (elements == null)
            return new String[] {"error VIIB"};

        String[] elementArray = new String[elements.length-1];

        for (int i=0; i<(elements.length-1); i++)
            elementArray[i] = ldapEscapedRDN.substring(elements[i]+1, elements[i+1]);

        return elementArray;
    }

    /**
     *    Sets the Ith att-val pair in escaped ldap form.
     *    @param i the element index to get (counting from 0)
     *    @param ldapEscapedElement the element to replace (if the
     *           rdn is single valued, this would be the whole rdn.)
     */

    public void setElement(int i, String ldapEscapedElement)
        throws InvalidNameException
    {
        validate();

        if (status == SINGLEVALUED)
        {
            if (i==0)
                ldapEscapedRDN = ldapEscapedElement;
            else
                throw new InvalidNameException("cannot set non zero element of single valued rdn.");
        }
        else
        {
            if (i < 0 || i >= size())
                throw new InvalidNameException("attempt to set element " + i + " of rdn: '" + ldapEscapedRDN + "' (size = " + size() + ")");

            ldapEscapedRDN = ldapEscapedRDN.substring(0, elements[i]+1) +
                             ldapEscapedElement +
                             ldapEscapedRDN.substring(elements[i+1]);

            parseMultiValued();

        }
    }



    /**
     *    Gets the first attribute name.
     */

    public String getAtt()
    {
        return getAtt(0);
    }

    /**
     *    gets the attribute name from a particular indexed rdn element.
     */

    public String getAtt(int i)
    {
        if (status == UNTESTED)
            checkForMultiValued();

        if (status == SINGLEVALUED && i!=0)
            return "rdn error VIII";

        String element = getElement(i);

        int pos = element.indexOf('='); // no need for escape check, since att must be unescaped always.

        if (pos == -1) return "rdn error IX";

if (debug)
{
    System.out.println("Debug = " + debug);
    Thread.currentThread().dumpStack();
    System.out.println(" % RDN -> found attribute as '" + element.substring(0,pos) + "'");
}

        return element.substring(0, pos);
    }

    /**
     *    gets the attribute type names as a String array.
     *    @return an array of attribute types as a string; e.g. {'cn', 'uid'}
     */

    public String[] getAtts()
    {
        if (status == UNTESTED)
            checkForMultiValued();

        String[] atts = getElements();

        for (int i=0; i<atts.length; i++)
        {
            int pos = atts[i].indexOf('='); // no need for escape check, since att must be unescaped always.

            if (pos == -1) return new String[] {"rdn error IXB"};

            atts[i] = atts[i].substring(0, pos);
        }

        return atts;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -