public class AttributeUtils extends Object
Constructor and Description |
---|
AttributeUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
applyModification(Entry entry,
Modification modification)
A method to apply a modification to an existing entry.
|
static Object |
cloneValue(Object value)
Clone the value.
|
static boolean |
containsValue(Attribute attr,
Value<?> compared,
AttributeType type)
Check if an attribute contains a specific value, using the associated matchingRule for that
|
static boolean |
containsValueCaseIgnore(Attribute attr,
Object value)
Check if an attribute contains a value.
|
static boolean |
equals(Object value1,
Object value2)
Compare two values and return true if they are equal.
|
static Attribute |
getAttribute(Attributes attrs,
AttributeType type)
Utility method to extract an attribute from Attributes object using
all combinationos of the name including aliases.
|
static Attribute |
getDifference(Attribute attr0,
Attribute attr1)
Creates a new attribute which contains the values representing the
difference of two attributes.
|
static Attribute |
getUnion(Attribute attr0,
Attribute attr1)
Creates a new attribute which contains the values representing the union
of two attributes.
|
static String |
parseAttribute(String str,
Position pos,
boolean withOption)
Parse an attribute.
|
static void |
parseOID(String str,
Position pos)
Parse an OID.
|
static Attribute |
removeAttribute(AttributeType type,
Attributes entry)
Correctly removes an attribute from an entry using it's attributeType information.
|
static Attribute |
toAttribute(EntryAttribute entryAttribute)
Converts an
EntryAttribute to an Attribute . |
static Attributes |
toAttributes(Entry entry)
Converts an
Entry to an Attributes . |
static Attribute |
toBasicAttribute(Attribute attribute)
Switch from a BasicAttribute to a AttributeImpl.
|
static Attributes |
toCaseInsensitive(Attributes attributes)
Check if the attributes is a BasicAttributes, and if so, switch
the case sensitivity to false to avoid tricky problems in the server.
|
static EntryAttribute |
toClientAttribute(Attribute attribute)
Convert a BasicAttribute or a AttributeImpl to a EntryAttribute
|
static Entry |
toClientEntry(Attributes attributes,
DN dn)
Check if an attribute contains a specific value and remove it using the associated
matchingRule for the attribute type supplied.
|
static String |
toString(Attribute attribute)
Return a string representing the attribute
|
static String |
toString(Attributes attributes)
Return a string representing the attributes
|
static String |
toString(String tabs,
Attribute attribute)
Return a string representing the attributes with tabs in front of the
string
|
static String |
toString(String tabs,
Attributes attributes)
Return a string representing the attributes with tabs in front of the
string
|
public static Attribute removeAttribute(AttributeType type, Attributes entry)
type
- the attributeType of the attribute to removeentry
- the entry to remove the attribute frompublic static final boolean equals(Object value1, Object value2)
value1
- The first valuevalue2
- The second valuepublic static Object cloneValue(Object value)
value
- The value to clonepublic static final Attribute toBasicAttribute(Attribute attribute)
attribute
- The attribute to transformpublic static final Attribute getAttribute(Attributes attrs, AttributeType type)
attrs
- the Attributes to get the Attribute object fromtype
- the attribute type specificationpublic static boolean containsValue(Attribute attr, Value<?> compared, AttributeType type) throws LdapException
attr
- The attribute we are searching incompared
- The object we are looking fortype
- The attribute typetrue
if the value exists in the attributeLdapException
- If something went wrong while accessing the datapublic static boolean containsValueCaseIgnore(Attribute attr, Object value)
attr
- The attribute to checkvalue
- The value to look forpublic static Attribute getDifference(Attribute attr0, Attribute attr1) throws NamingException
IllegalArgumentException
is
raised. Note that the order of arguments makes a difference.attr0
- the first attributeattr1
- the second attributeNamingException
- if there are problems accessing attribute valuespublic static Attribute getUnion(Attribute attr0, Attribute attr1) throws NamingException
IllegalArgumentException
is raised.attr0
- the first attributeattr1
- the second attributeNamingException
- if there are problems accessing attribute valuespublic static Attributes toCaseInsensitive(Attributes attributes)
attributes
- The Attributes to checkpublic static String toString(String tabs, Attribute attribute)
tabs
- Spaces to be added before the stringattribute
- The attribute to printpublic static String toString(Attribute attribute)
attribute
- The attribute to printpublic static String toString(String tabs, Attributes attributes)
tabs
- Spaces to be added before the stringattributes
- The attributes to printpublic static void parseOID(String str, Position pos) throws ParseException
str
- The OID to parsepos
- The current position in the stringParseException
- If we don't have a valid OIDpublic static String parseAttribute(String str, Position pos, boolean withOption) throws ParseException
str
- The parsed attribute,pos
- The position of the attribute in the current stringParseException
public static String toString(Attributes attributes)
attributes
- The attributes to printpublic static void applyModification(Entry entry, Modification modification) throws LdapException
entry
- The entry on which we want to apply a modificationmodification
- the Modification to be appliedLdapException
- if some operation fails.public static Entry toClientEntry(Attributes attributes, DN dn) throws LdapException
attr
- the attribute we are searching incompared
- the object we are looking fortype
- the attribute typeattributes
- the BasicAttributes or AttributesImpl instance to convertregistries
- The registries, needed ro build a ServerEntrydn
- The DN which is needed by the ServerEntryNamingException
- if something went wrong while removing the value
public static Object removeValue( Attribute attr, Object compared, AttributeType type ) throws NamingException
{
// quick bypass test
if ( attr.contains( compared ) )
{
return attr.remove( compared );
}
MatchingRule matchingRule = type.getEquality();
Normalizer normalizer;
if ( matchingRule != null )
{
normalizer = type.getEquality().getNormalizer();
}
else
{
normalizer = new NoOpNormalizer();
}
if ( type.getSyntax().isHumanReadable() )
{
String comparedStr = ( String ) normalizer.normalize( compared );
for ( NamingEnumeration> values = attr.getAll(); values.hasMoreElements(); )
{
String value = ( String ) values.nextElement();
if ( comparedStr.equals( normalizer.normalize( value ) ) )
{
return attr.remove( value );
}
}
}
else
{
byte[] comparedBytes = null;
if ( compared instanceof String )
{
if ( ( ( String ) compared ).length() < 3 )
{
return null;
}
// Tansform the String to a byte array
int state = 1;
comparedBytes = new byte[( ( String ) compared ).length() / 3];
int pos = 0;
for ( char c : ( ( String ) compared ).toCharArray() )
{
switch ( state )
{
case 1:
if ( c != '\\' )
{
return null;
}
state++;
break;
case 2:
int high = StringTools.getHexValue( c );
if ( high == -1 )
{
return null;
}
comparedBytes[pos] = ( byte ) ( high << 4 );
state++;
break;
case 3:
int low = StringTools.getHexValue( c );
if ( low == -1 )
{
return null;
}
comparedBytes[pos] += ( byte ) low;
pos++;
state = 1;
break;
}
}
}
else
{
comparedBytes = ( byte[] ) compared;
}
for ( NamingEnumeration> values = attr.getAll(); values.hasMoreElements(); )
{
Object value = values.nextElement();
if ( value instanceof byte[] )
{
if ( ArrayUtils.isEquals( comparedBytes, value ) )
{
return attr.remove( value );
}
}
}
}
return null;
}
/**
Convert a BasicAttributes or a AttributesImpl to a ServerEntryInvalidAttributeIdentifierException
- If we get an invalid attributeLdapException
public static Attributes toAttributes(Entry entry)
Entry
to an Attributes
.entry
- the Entry
to convertAttributes
public static Attribute toAttribute(EntryAttribute entryAttribute)
EntryAttribute
to an Attribute
.entryAttribute
- the EntryAttribute
to convertAttribute
public static EntryAttribute toClientAttribute(Attribute attribute)
attribute
- the BasicAttributes or AttributesImpl instance to convertattributeType
- InvalidAttributeIdentifierException
- If we had an incorrect attributeCopyright © 2003-2013 The Apache Software Foundation. All Rights Reserved.