📄 dn.java
字号:
{
if (o == null)
return false;
if (o instanceof DN)
return equals((DN)o);
else if (o instanceof Name)
return (compareTo((Name)o) == 0);
else
return false; // cannot be equal in any sense if not a name
}
/**
* Checks whether the testDN is a subset of the current DN,
* starting from the root. Currently case insensitive.
*
* @param testDN the subset DN to test against
*/
public boolean startsWith(DN testDN)
{
return startsWith((Name)testDN);
}
/**
* Test if the DNs are identical except for the
* lowest RDN.
* In other words, test if they are leaves on the same branch.
*
* @param testDN the putatitive sibling DN
*/
public boolean sharesParent(DN testDN)
{
if (testDN.size()!= size()) return false;
for (int i=0; i<size()-1; i++)
{
if ((testDN.getRDN(i).equals(getRDN(i)))==false)
return false;
}
return true;
}
/**
* Return the full DN of this DN's immediate parent.
* In other words, return this DN after removing the lowest RDN.
*
* @return the parent of this DN, or an empty DN if this is the top level DN.
*/
public DN parentDN()
{
// XXX what to do if this is already an empty DN? The same?
if (size()<=1) return new DN(); // return empty DN for top level DNs
DN newDN = new DN(this);
newDN.RDNs.removeElementAt(size()-1);
return newDN;
}
/**
* reverse the order of elements in a DN...
*/
public void reverse()
{
Vector rev = new Vector();
for (int i=RDNs.size()-1; i>=0; i--)
rev.add(RDNs.elementAt(i));
RDNs = rev;
}
/**
* Empties the DN of all RDNs.
*/
public void clear()
{
RDNs.clear();
errorString = null;
}
/**
* Overload this method for app specific error handling.
*/
public void setError(String msg, NamingException e)
{
errorString = msg;
rootException = e;
System.out.println(e);
}
/**
* Whether there was an error using this DN (i.e. when creating it).
*/
public boolean error()
{
return (errorString == null);
}
/**
* Gets the error message (if any) associated with this DN.
*/
public String getError()
{
return errorString;
}
/**
* Gets the root exception (if any) associated with this DN
*/
public NamingException getNamingException()
{
return rootException;
}
/**
* Prepare a dn for jndi transmission
*/
/*
public void escape()
{
for (int i=0; i<size(); i++)
{
getRDN(i).escape();
}
}
*/
/**
* Unescape a dn that has been *normally* escaped using ldap v3 (i.e. by the
* preceeding ftn.).
*/
/*
public void unescape()
throws InvalidNameException
{
for (int i=0; i<size(); i++)
{
getRDN(i).unescape();
}
}
*/
/** (Obsolete)
* Unescape a dn that has been returned by jndi, that may contain either
* ldap v2 escaping, or the multiple-slash wierdness bug.
*/
/*
public void unescapeJndiReturn()
throws InvalidNameException // shouldn't happen...
{
for (int i=0; i<size(); i++)
{
getRDN(i).unescapeJndiReturn();
}
}
*/
/**
* Add an RDN to the end of the DN.
*/
public Name add(RDN rdn)
{
//RDNs.insertElementAt(rdn,size());
add(size(), rdn);
return this;
}
/**
* The core method for adding RDN objects to the name.
* Called by all add methods.
* @param posn the position in the DN to add the RDN at (0 = root)
* @param rdn the RDN to add (may be multi-valued).
*/
public Name add(int posn, RDN rdn)
{
RDNs.insertElementAt(rdn,posn);
return this;
}
// NN N A MM MM EEEEEE
// NNN N A A M MMM M EE
// N NN N A A M M M EEEE (Interface Def.)
// N NNN AAAAAAA M M M EE
// N NN AA AA M M M EEEEEE
/*
* Adds a single component at a specified position within this name.
*/
// These two ftns should be used by all code to add rdns... the RDN array
// should not be accessed directly.
public Name add(int posn, String rdn)
throws InvalidNameException
{
RDN r = new RDN(rdn); // may throw invalidName Exception
add(posn, r);
return this;
}
/*
* Adds a single component to the end of this name.
*/
public Name add(String rdn)
throws InvalidNameException
{
RDN r = new RDN(rdn); // may throw invalidName Exception
add(size(), r);
return this;
}
/*
* Adds the components of a name -- in order -- at a specified position within this name.
*/
public Name addAll(int posn, Name n)
throws InvalidNameException
{
Enumeration e = n.getAll();
while (e.hasMoreElements())
add(posn++, e.nextElement().toString());
return this;
}
/*
* Adds the components of a name -- in order -- to the end of this name.
*/
public Name addAll(Name suffix)
throws InvalidNameException
{
Enumeration e = suffix.getAll();
while (e.hasMoreElements())
add(e.nextElement().toString());
return this;
}
/*
* Generates a new copy of this name.
*/
public Object clone()
{
return new DN(this);
}
/*
* Compares this name with another name for order.
* ... for the time being, ordering is alphabetical by rdns ordered
* right to left. Damn but the ldap rdn ordering system is screwed.
*/
public int compareTo(Object obj)
{
int val = 0;
int pos = 1;
if (obj instanceof Name)
{
Name compareMe = (Name)obj;
int size = size();
int compSize = compareMe.size();
while (val == 0)
{
String RDN = get(size-pos);
String compRDN = compareMe.get(compSize-pos);
int rdnOrder = RDN.compareTo(compRDN);
if (rdnOrder != 0)
return rdnOrder; // return alphabetic order of rdn.
pos++;
if (pos>size || pos>compSize)
{
if (size==compSize)
return 0; // names are equal
if (pos>size)
return -1; // shorter dns first
else
return 1;
}
}
}
else
throw new ClassCastException("non Name object in DN.compareTo - object was " + obj.getClass());
return 0; // never reached.
}
/*
* Determines whether this name ends with a specified suffix.
*/
public boolean endsWith(Name n)
{
return false;
}
/*
* Retrieves a component of this name. Returns zero length string for
* an empty DN's first element - otherwise throws a ArrayIndexException.
* (is this correct behaviour?).
*/
public String get(int posn)
{
if (posn==0 && isEmpty()) return ""; // return empty string for empty DN
return RDNs.elementAt(posn).toString();
}
/*
* Retrieves the components of this name as an enumeration of strings.
*/
public java.util.Enumeration getAll()
{
DXNamingEnumeration ret = new DXNamingEnumeration();
for (int i=0; i<size(); i++)
ret.add(get(i));
return ret;
}
/*
* Creates a name whose components consist of a prefix of the components of this name.
*/
public Name getPrefix(int posn)
{
DN returnMe = new DN();
try
{
for (int i=0; i<posn; i++)
returnMe.add(get(i));
return returnMe;
}
catch (InvalidNameException e)
{
System.err.println("unexpected error in DN:\n " + e);
return new DN();
}
}
/*
* Creates a name whose components consist of a suffix of the components in this name.
*/
public Name getSuffix(int posn)
{
DN returnMe = new DN();
for (int i=posn; i<size(); i++)
{
returnMe.add(new RDN(getRDN(i)));
}
return returnMe;
}
/**
* returns true if this is an 'empty', or root DN (i.e. \"\")
* @return empty status
*/
public boolean isEmpty()
{
return (size()==0);
}
/*
* Removes a component from this name.
*/
public Object remove(int posn)
{
return RDNs.remove(posn);
}
/*
* Returns the number of components in this name,
* and hence the level (the number
* of nodes from root) of the DN.
*/
public int size()
{
return RDNs.size();
}
/*
* Returns the number of components in this name,
* and hence the level (the number
* of nodes from root) of the DN.
* (synonym of 'size()'. Why java didn't standardise on just one...)
*/
/*
public int length()
{
return RDNs.size();
}
*/
/*
* Determines whether this name starts with a specified prefix.
*/
public boolean startsWith(Name n)
{
int pos = 0;
Enumeration e = n.getAll();
while (e.hasMoreElements())
if (e.nextElement().toString().equalsIgnoreCase(get(pos++).toString())==false)
return false;
return true; // falls through - all tested components must be equal!
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -