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

📄 dxattributes.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

package com.ca.commons.naming;

import com.ca.commons.cbutil.*;
import com.ca.commons.jndi.SchemaOps;

import javax.naming.*;
import javax.naming.directory.*;

import java.util.*;
import java.util.logging.Logger;
import java.util.logging.Level;

/**
 *    This class is a container for a collection of
 *    Attribute objects.  It is schema aware, and will
 *    search schema to find a complete set of attributes
 *    for its component objectClass(s).<p>
 *
 *    The class is built around a hashtable of id/attribute
 *    pairs.
 */

// nb this class must work with both oid's (2.3.1.2.3.1.2.41.23.1.pi.2.e.34.phi)
// and ldap names ('c').  Ideally it should get the long form as well
// (i.e. 'country') but the server doesn't always oblige...
// I try to document the difference by referring to ids as either oids or ldap ids.
// if the distinction isn't obvious, it's probably an ldap id (i.e. a string name)

// XXX a lot of effort is gone to to trim out ';binary' from the names of
// XXX ldap attributes.  THis is a grubby hack, and handling should be improved
// XXX somehow.

public class DXAttributes implements Attributes
{

    static Hashtable attOIDs = new Hashtable(100);  // a global hashset of all attribute oid-s and corresponding ldap descr names known to the program...

    static int ID = 0;

    int id; // unique debug id.

    Hashtable atts;                 // a hashtable of attribute id/ attribute object values, keyed by lowercase id.

    HashSet must;                   // a hashset of attribute ldap ids that *must* be entered
                                    // - set in checkSchema()

    boolean ignoreCase = true;      // whether attribute IDs are case sensitive

    boolean schemaChecked = false;  // indicates a schema search has been made,
                                    // and a full list of objectclass(s) attribute obtained
    Attribute allObjectClasses;     // a list of allObjectClasses, including parents.

    //String baseObjectClass = null;  // the deepest ObjectClass.

    String objectClassName = null;  // the current name of 'objectclass' or 'objectClass'

    Vector orderedSOCs = new Vector(); // structural object classes, in deepest-first order


    static SchemaOps schema;              // the schema context

    static Hashtable knownParents = new Hashtable(30);      // hash of DXAttributes containing the known object class parents for a particular object class subset (e.g. 'inetorgperson' -> 'orgperson','person','top')
    static Hashtable knownSubSets = new Hashtable(30);      // hash of known object class subsets
    static Hashtable objectClassDepths = new Hashtable(30); // hash of known object class 'depths' in object class inheritance tree

    private final static Logger log = Logger.getLogger(DXAttributes.class.getName());


    static
    {
        objectClassDepths.put("top", new Integer(0));

        // pre set 'synthetic' schema attributes
        objectClassDepths.put("schema", new Integer(1));
        objectClassDepths.put("AttributeDefinition", new Integer(2));
        objectClassDepths.put("ClassDefinition", new Integer(2));
        objectClassDepths.put("SyntaxDefinition", new Integer(2));

    }

    // common code run by all the more basic constructors.
    void basicInit()
    {
        id = ID++;

        atts = new Hashtable();
        must = new HashSet();

//        schema = null;
    }

    /**
     *    Initialises an empty set of attributes with no schema
     */
    public DXAttributes()
    {
        basicInit();
    }

    /**
     *    Initialises a set of attributes with a single attribute
     */

    public DXAttributes(Attribute a)
    {
        basicInit();
        put(a);
    }

    /**
     *    Copies an existing Attributes object into a DXAttributes
     *    object.
     */

    public DXAttributes(Attributes a)
    {

        if (a==null)
        {
            atts = new Hashtable();
            must = new HashSet();
        }
        else
        {
            atts = new Hashtable(a.size()+10);
            must = new HashSet(a.size());   // overkill, but what the heck...

            Enumeration e = a.getAll();
            while (e.hasMoreElements())
            {
                DXAttribute newAtt = new DXAttribute((Attribute)e.nextElement());
                put (newAtt);
            }
        }
    }

    /**
     *    Initialises a set of DXattributes using
     *    a Hashtable of existing id/attribute pairs...
     *    @param newAtts hashtable of id/attribute pairs to
     *           initialise the new DXAttributes object
     */

    public DXAttributes(Hashtable newAtts)
    {
        atts = (Hashtable) newAtts.clone();
    }


    /**
     *    Initialises a set of DXattributes using
     *    a NamingEnumeration of existing attribute(s).
     *    @param newAtts namingenumeration of attributes to
     *           initialise the new DXAttributes object
     */

    public DXAttributes(NamingEnumeration newAtts)
    {
        atts = new Hashtable();
        while (newAtts.hasMoreElements())
        {
            Attribute current = (Attribute) newAtts.nextElement();
            atts.put(current.getID().toLowerCase(), current);
        }
    }


    /**
     *    <p>This sets the standard schema to use while this connection is open.
     *    (It may be possible in future releases to set schema on a per-Attribute
     *    basis - it is not clear yet whether this would be useful.)</p>
     *
     *    <p>Note: currently this also sets the default schema for the DXAttribute
     *    Class.</p>
     */
    public static void setDefaultSchema(SchemaOps defaultSchema)
    {
        schema = defaultSchema;

        DXAttribute.setDefaultSchema(defaultSchema);
    }


    public int getID() { return id; }

    /**
     *    creates a new DXAttributes, copying each Attribute object.
     *    @return a new DXAttributes object
     */

    public Object clone()
    {
        return new DXAttributes(atts);
    }

    /**
     *    gets an Attribute specified by its ID
     *    @param attrID the ID of the attribute to retrieve
     *    @return the specified attribute (actually a DXAttribute)
     *            - null if it can't be found.
     */

    public Attribute get(java.lang.String attrID)
    {
        Attribute ret = (Attribute) atts.get(attrID.toLowerCase());
        if (ret==null)
        {
            ret = (Attribute) atts.get(attrID.toLowerCase() + ";binary");
        }

        return ret;
    }

    /**
     *    returns all the Attribute(s) in this DXAttributes object.
     *    - the NamingEnumeration is (evily) pre-sorted alphabetically...
     *    @return enumeration of all stored Attribute objects
     */
    public NamingEnumeration getAll()
    {
        return (new DXNamingEnumeration (atts.elements())).sort();
    }

    /**
     *    For convenience and display, DXAttributes objects have a complete
     *    list of all attribute(s) that the objectclass(s) represented might
     *    possibly contain.  However sometimes we need an <b>Attributes</b>
     *    object that has no null-valued attributes (i.e. when adding it to
     *    the directory).
     *    @return an Attributes object containing no null valued attribute(s).
     */
    public Attributes getAsNonNullAttributes()
    {
        return new DXAttributes(getAllNonNull());
    }

    /**
     *    returns all the Attribute(s) that have non-null values
     *    in this DXAttributes object.
     *    - the NamingEnumeration is (evily) pre-sorted alphabetically...<p>
     *
     *    Warning: whether an attribute is added is undefined if the
     *    attribute is multi-valued and contains one or more null values in
     *    addition to other non-null values.
     *
     *    @return enumeration of all stored Attribute objects with non-null values
     */

    public NamingEnumeration getAllNonNull()
    {
        DXNamingEnumeration returnEnumeration = new DXNamingEnumeration ();
        Enumeration allatts = getAll();

        while (allatts.hasMoreElements())
        {
            Attribute fnord = (Attribute) allatts.nextElement();
            if (fnord != null) // should never happen...
            {
                try
                {
                    if (fnord.get() != null)            // if there is at least one non-null value...
                        returnEnumeration.add(fnord);   // add it to the list
                }
                catch (NoSuchElementException e)        // 'expected' exception (love that jndi)
                {

                }
                catch (NamingException e2)
                {
                    log.log(Level.WARNING, "whoops: Naming Exception reading " + fnord.getID(), e2);
                }
            }
        }

        returnEnumeration.sort();

        return returnEnumeration;
    }

    /**
     *    returns all the mandatory 'MUST' have Attribute(s) in this DXAttributes object.
     *    - the NamingEnumeration is (evily) pre-sorted alphabetically...
     *    @return enumeration of all stored Attribute objects
     */
    public NamingEnumeration getMandatory()
    {
        DXNamingEnumeration returnEnumeration = new DXNamingEnumeration ();

        if (must==null) return returnEnumeration;  // return empty enumeration if not initialised...

        Iterator musts = must.iterator();
        while (musts.hasNext())
        {
            String s = (String)musts.next();
            returnEnumeration.add(get(s));
        }
        returnEnumeration.sort();

        return returnEnumeration;
    }



   /**
    *   Returns the list of mandatory attribute IDs (HashSet:must).
    *   @return the list of mandatory attribute IDs (e.g. objectClass, cn, sn).
    */

    public HashSet getMandatoryIDs()
    {
        return must;
    }



    /**
     *    returns all the optional 'MAY' Attribute(s) in this DXAttributes object.
     *    - the NamingEnumeration is (evily) pre-sorted alphabetically...
     *    @return enumeration of all stored Attribute objects
     */
    public NamingEnumeration getOptional()
    {
        DXNamingEnumeration returnEnumeration = new DXNamingEnumeration ();
        Enumeration allIDs = atts.keys();
        while (allIDs.hasMoreElements())
        {
            String id = (String) allIDs.nextElement();
            if (must.contains(id)==false)    // if it's *not* mandatory
                returnEnumeration.add(get(id)); // add it to the optional list
        }
        returnEnumeration.sort();

        return returnEnumeration;
    }



    /**
     *    returns all the attribute IDs held in this DXAttributes object.
     *    @return all attribute IDs
     */

    public NamingEnumeration getIDs()
    {
        // cannot simply return hash keys, as they are standardised to lower case.
        DXNamingEnumeration ret = new DXNamingEnumeration();
        NamingEnumeration allAtts = getAll();
        while (allAtts.hasMoreElements())
            ret.add(((Attribute)allAtts.nextElement()).getID());

        return ret;

        //return (NamingEnumeration)(new DXNamingEnumeration (atts.keys()));
    }

    /**
     *    returns whether attribute IDs are stored in a case
     *    sensitive manner; i.e. whether 'person' is different
     *    from 'Person'.  The default is <i>false</i>, implying
     *    case sensitivity.
     *    @return whether case is ignored for attribute IDs
     */

    public boolean isCaseIgnored()
    {
        return ignoreCase;
    }

    /**

⌨️ 快捷键说明

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