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

📄 dxattribute.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    log.log(Level.INFO, "can't find syntax for attribute " + getID(), e);
                }
            }
        }
        else
        {
            log.fine("no schema available");
        }
    }

    /**
     * Whether the attribute contains isNonString data; ideally found by checking Syntax, but
     * often set by inspection of the attribute value (whether it is a Byte array).
     *
     * @deprecated use isString instead
     */
    public boolean isBinary()
    {
        return !isString();
    }


    /**
     * Whether the attribute contains isNonString data; ideally found by checking Syntax, but
     * often set by inspection of the attribute value (whether it is a Byte array).
     */
    public boolean isString()
    {
        return isString;
    }

    /**
     * Sets the isNonString status of the attribute.  Shouldn't be required any more;
     * should be set by syntax checking.
     *
     * @deprecated use setString() instead
     */
    public void setBinary(boolean bin)
    {
        setString(!bin);
    }

    /**
     * Sets the isNonString status of the attribute.  Shouldn't be required any more;
     * should be set by syntax checking.
     */
    public void setString(boolean stringStatus)
    {
        knownAttributeTypes.put(getID(), (stringStatus)?STRING:BYTE_ARRAY);
        isString = stringStatus;
    }

    /**
     * Utility Function: takes a schema Entry class such as 'AttributeDefinition/cn',     *
     * and returns the result of looking up a particular attribute within that
     * (e.g. 'DESC').
     *
     * @param schemaEntry     the full schema entry name to lookup, e.g. 'ClassDefinition/alias'
     * @param schemaAttribute the attribute to look up, e.g. 'MUST'
     * @return the value of the schema entry attribute (e.g. '2.5.4.1')
     */

    public String schemaLookup(String schemaEntry, String schemaAttribute)
    {
        if (schema == null)
            return null;
        else
            return schema.schemaLookup(schemaEntry, schemaAttribute);
    }

    /**
     * returns whether this attribute has a description in the schema
     * @return
     */
    public boolean hasOptions()
    {
        if (description == null)  // try to load a description if it exists
            getDescription();

        if (description == null || description == "" || description.indexOf("LIST:") < 0)
            return false;

        return true;
    }

    /**
     * IF a description field has been set in the schema, and IF that
     * description field LISTs a set of values, read 'em and return them
     * as a string array.
     */

    public String[] getOptions()
    {
        if (description == null)
            getDescription();
        if (description == null || description == "")
            return new String[0];

        // can't be fagged working out java generic parse stuff - do own quickie...
        int len = description.length();
        int pos = description.indexOf("LIST:");
        if (pos < 0) return new String[0];
        pos += 5;
        int next = pos;

        Vector resVect = new Vector();
        resVect.add("");
//        String option;
        while (pos < len && pos > 0)
        {
            next = description.indexOf(',', next + 1);
            if (next < 0)
            {
                resVect.add(description.substring(pos));
                pos = 0;
            }
            else if (description.charAt(next - 1) != '\\')
            {
                resVect.add(description.substring(pos, next));
                pos = next + 1;
            }
            else
            {
                next++;  // move past the escaped comma
            }
        }

        // dump vector into string array, unescaping as we go.
        String[] result = new String[resVect.size()];
        for (int i = 0; i < resVect.size(); i++)
            result[i] = unEscape((String) resVect.elementAt(i));

        return result;
    }

    public String unEscape(String escapeMe)
    {
        int slashpos = escapeMe.indexOf('\\');
        while (slashpos >= 0)
        {
            escapeMe = escapeMe.substring(0, slashpos) + escapeMe.substring(slashpos + 1);
            slashpos = escapeMe.indexOf('\\');
        }
        return escapeMe;
    }

    /**
     * returns the OID of the schema entry corresponding to this particular entry.  Whoo hoo... something like
     * '1.3.6.1.4.1.1466.115.121.1.27'.
     * @return
     */

    public String getSyntaxOID()
    {
        if (syntaxOID == null)
            setAttributeTypeFromSchema();  // sets syntaxOID

        if (syntaxOID == null)
            return "<unknown>";
        else
            return syntaxOID;
    }


    /**
     * Returns (and caches) the syntax description.
     */
    public String getSyntaxDesc()
    {
        if (syntaxDesc == null)
            syntaxDesc = schemaLookup("SyntaxDefinition/" + getSyntaxOID(), "DESC");
        return syntaxDesc;
    }


    public String getSyntaxName()
    {
        if (syntaxOID == null)
            setAttributeTypeFromSchema();  // sets syntaxOID

        if (syntaxOID == null)
            return "<unknown>";
        else
            return schema.translateOID(syntaxOID);
    }


    /**
     * Usefull escape to allow renaming of attributes.  Use with caution,
     * since an arbitrary name may not correspond to a valid schema name.
     */
    public void setName(String newName)
    {
        name = newName;
    }

    public String getName()
    {
        if (name == null)
            name = schemaLookup("AttributeDefinition/" + getID(), "NAME");
        if (name == null)
            name = getID();

        return name;
    }

    /**
     * Returns the attribute's 'DESC' field from the attribute schema
     * definition, if such exists (it's an extension).  Not to be
     * confused with the Syntax Description, which is something like "isNonString".
     */

    public String getDescription()
    {
        if (description == null)
        {
            description = schemaLookup("AttributeDefinition/" + getID(), "DESC");
            if (description == null)    // if description is still null, set it
                description = "";       // to an empty string, to avoid repeatedly looking it up.
        }
        return description;
    }

    /**
     * <p>A synonym for getID().  Use 'toDebugString()' for the complete printout.</p>
     *
     * @return
     */
    public String toString()
    {
        return getID();
    }

    /**
     * General descriptive string: used mainly for debugging...
     */

    public String toDebugString()
    {
        int count = 1;
        try
        {
            StringBuffer result = new StringBuffer().append("att: ").append(getID()).append(" (size=").append(size()).append(") ");

            NamingEnumeration vals = getAll();
            if (!isString)
            {
                result.append(" (Byte Array) ");
                while (vals.hasMore())
                {
                    try
                    {
                        byte[] b = (byte[]) vals.next();
                        result.append("\n    ").append((count++)).append(":").append(((b == null) ? "null" : CBBase64.binaryToString(b)));
                    }
                    catch (ClassCastException cce)
                    {
                        result.append("\n    ").append((count++)).append(": <error - not a byte array>");
                    }
                }
            }
            else
            {
                while (vals.hasMore())
                {
                    Object o = vals.next();
                    result.append("\n    ").append((count++)).append(":").append(((o == null) ? "null" : o.toString()));
                }
            }

            return result.append("\n").toString();
        }
        catch (NamingException e)
        {
            log.log(Level.WARNING, "error listing values for " + getID(), e);
            return (getID() + " (error listing values)");
        }


    }

    // ugly, ugly hack to add ';binary' when writting data to dir, but
    // not otherwise.
    public static void setVerboseBinary(boolean status)
    {
        appendBinaryOption = status;
        log.fine("setting isNonString attribute status to " + status);
    }

    /**
     * This returns the name of the attribute.
     * @return the attribute name; e.g. 'commonName'
     */

    //TODO:  should this use the OID instead?  Would solve problems with loonies who
    //TODO:  use different names for the same attribute in different place

    public String getID()
    {
        String id = super.getID();
        if (appendBinaryOption && !(id.endsWith(";binary")))
        {
            //System.out.println("binaryness = " + isNonString);
            // TODO: this should only happen for ASN.1. syntaxes.
            if (isASN1)
            {
                id = id + ";binary";
                log.info("appending ;binary to attribute name "+id);
            }

        }
        return id;
    }

    /**
     * A last resort hack to guess whether an otherwise unknown attribute is
     * an ASN1 structure.  This should only be used if the schema is unavailable.
     * @param id
     * @return
     */

    public boolean isKnownASN1Attribute(String id)
    {
        String search = id.toLowerCase();
        if (search.indexOf("certificate") >= 0)
            return true;
        else if (search.indexOf("revocation") >= 0)
            return true;
        else if (search.indexOf("supportedalgorithms") >= 0)
            return true;
        else if (search.indexOf("userpkcs12") >= 0)
            return true;

        return false;
    }


    /**
     * This method gets all the values of an attribute.  Useful only
     * for multivalued attributes (use get() otherwise).
     *
     * @return all the values of this attribute in a String Array.
     *         XXX - assumes all values are strings??
     */

    public Object[] getValues()
    {
        Object[] values = new String[size()];

        try
        {
            for (int i = 0; i < size(); i++)
                values[i] = get(i);
            return values;
        }
        catch (NamingException e)
        {
            return new String[]{};
        }
    }


    /**
     * Gets rid of null values and empty strings from the value set.
     */
    public void trim()
    {
        for (int i = size() - 1; i > 0; i--)
        {
            Object o = null;
            try
            {
                o = get(i);
                if (o == null || "".equals(o))
                {
                    remove(i);
                }
            }
            catch (NamingException e)  // shouldn't happen...
            {
                log.log(Level.WARNING, "Bad Attribute value in DXAttribute - removing ", e);
                remove(i);             // .. but remove offending entry if it does.

            }
        }
    }

    public void setOrdered(boolean state)
    {
        ordered = state;
    }


    /**
     * Returns true if this attribute is a SINGLE-VALUE attribute.
     *
     * @return true if this attribute is a SINGLE-VALUE attribute,
     *         false otherwise.
     */

    public boolean isSingleValued()
    {/* TE */
        return schema.isAttributeSingleValued(getName());
    }

    /**
     * <p>The collation key is usually set by the constructor
     * based on getID(), but
     * may be over-ridden here if required.  Not sure that this
     * will ever be necessary; maybe if doing something insanely
     * clever across multiple platforms with different languages?</p>
     *
     * @param key
     */
    public void setCollationKey(CollationKey key)
    {
        collationKey = key;
    }

    /**
     * <p>This returns the collation key used for language sensitive
     * sorting.</p>
     *
     * @return
     */
    public CollationKey getCollationKey()
    {
        return collationKey;
    }

    /**
     * <p>This is intended to compare two DXAttribute objects, and will
     * throw a class cast exception for anything else.  It sorts on
     * their internal collationkeys.</p>
     *
     * @param o
     * @return
     */
    public int compareTo(Object o)
    {
        return collationKey.compareTo(((DXAttribute) o).getCollationKey());
    }
}

⌨️ 快捷键说明

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