You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2010/01/06 18:52:31 UTC
svn commit: r896579 [3/14] - in /directory/shared/trunk: ./ ldap/
ldap/src/main/antlr/ ldap/src/main/java/org/apache/directory/shared/ldap/
ldap/src/main/java/org/apache/directory/shared/ldap/codec/
ldap/src/main/java/org/apache/directory/shared/ldap/c...
Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java?rev=896579&r1=896578&r2=896579&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java Wed Jan 6 17:52:15 2010
@@ -26,17 +26,18 @@
import java.io.ObjectOutput;
import java.util.Collection;
import java.util.Iterator;
-import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.naming.InvalidNameException;
+import javax.naming.NamingException;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.directory.shared.ldap.entry.Value;
import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
+import org.apache.directory.shared.ldap.schema.normalizers.OidNormalizer;
import org.apache.directory.shared.ldap.util.StringTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -107,7 +108,7 @@
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
-public class Rdn implements Cloneable, Comparable, Externalizable, Iterable<AttributeTypeAndValue>
+public class Rdn implements Cloneable, Comparable, Externalizable, Iterable<AVA>
{
/** The LoggerFactory used by this class */
protected static final Logger LOG = LoggerFactory.getLogger( Rdn.class );
@@ -140,7 +141,7 @@
* because we want the ATAVs to be sorted. An atav may contain more than one
* value. In this case, the values are String stored in a List.
*/
- private Set<AttributeTypeAndValue> atavs = null;
+ private Set<AVA> atavs = null;
/**
* We also keep a set of types, in order to use manipulations. A type is
@@ -148,8 +149,6 @@
*
* Note : there is no Generic available classes in commons-collection...
*/
- @SuppressWarnings(
- { "unchecked" })
private MultiMap atavTypes = new MultiValueMap();
/**
@@ -162,7 +161,7 @@
* case where we only have a single type=value. This will be 99.99% the
* case. This avoids the creation of a HashMap.
*/
- protected AttributeTypeAndValue atav = null;
+ protected AVA atav = null;
/**
* The number of atavs. We store this number here to avoid complex
@@ -251,6 +250,28 @@
/**
+ * A constructor that constructs a RDN from a type and a value. Constructs
+ * an Rdn from the given attribute type and value. The string attribute
+ * values are not interpreted as RFC 2253 formatted RDN strings. That is,
+ * the values are used literally (not parsed) and assumed to be un-escaped.
+ *
+ * @param upType The user provided type of the RDN
+ * @param upValue The user provided value of the RDN
+ * @throws InvalidNameException If the RDN is invalid
+ */
+ public Rdn( String upType, String upValue ) throws InvalidNameException
+ {
+ addAttributeTypeAndValue( upType, upType, new ClientStringValue( upValue ), new ClientStringValue( upValue ) );
+
+ upName = upType + '=' + upValue;
+ start = 0;
+ length = upName.length();
+ // create the internal normalized form
+ normalize();
+ }
+
+
+ /**
* A constructor that constructs a RDN from a type, a position and a length.
*
* @param start The starting point for this RDN in the user provided DN
@@ -274,8 +295,6 @@
* @param rdn
* The non-null Rdn to be copied.
*/
- @SuppressWarnings(
- { "unchecked" })
public Rdn( Rdn rdn )
{
nbAtavs = rdn.getNbAtavs();
@@ -290,17 +309,17 @@
return;
case 1:
- this.atav = ( AttributeTypeAndValue ) rdn.atav.clone();
+ this.atav = ( AVA ) rdn.atav.clone();
return;
default:
// We must duplicate the treeSet and the hashMap
- atavs = new TreeSet<AttributeTypeAndValue>();
+ atavs = new TreeSet<AVA>();
atavTypes = new MultiValueMap();
- for ( AttributeTypeAndValue currentAtav : rdn.atavs )
+ for ( AVA currentAtav : rdn.atavs )
{
- atavs.add( ( AttributeTypeAndValue ) currentAtav.clone() );
+ atavs.add( ( AVA ) currentAtav.clone() );
atavTypes.put( currentAtav.getNormType(), currentAtav );
}
@@ -311,10 +330,11 @@
/**
* Transform the external representation of the current RDN to an internal
- * normalized form where : - types are trimmed and lowercased - values are
- * trimmed and lowercased
+ * normalized form where :
+ * - types are trimmed and lower cased
+ * - values are trimmed and lower cased
*/
- // WARNING : The protection level is left unspecified intentionnaly.
+ // WARNING : The protection level is left unspecified on purpose.
// We need this method to be visible from the DnParser class, but not
// from outside this package.
/* Unspecified protection */void normalize()
@@ -331,7 +351,7 @@
// We will trim and lowercase type and value.
if ( !atav.getNormValue().isBinary() )
{
- normName = atav.getNormalizedValue();
+ normName = atav.getNormName();
}
else
{
@@ -346,7 +366,7 @@
boolean isFirst = true;
- for ( AttributeTypeAndValue ata : atavs )
+ for ( AVA ata : atavs )
{
if ( isFirst )
{
@@ -364,6 +384,28 @@
break;
}
}
+
+
+ /**
+ * Transform a RDN by changing the value to its OID counterpart and
+ * normalizing the value accordingly to its type.
+ *
+ * @param rdn The RDN to modify.
+ * @param oidsMap The map of all existing oids and normalizer.
+ * @throws InvalidNameException If the RDN is invalid.
+ * @throws NamingException If something went wrong.
+ */
+ public Rdn normalize( Map<String, OidNormalizer> oidsMap ) throws InvalidNameException, NamingException
+ {
+ String upName = getUpName();
+ LdapDN.rdnOidToName( this, oidsMap );
+ normalize();
+ this.upName = upName;
+
+
+ return this;
+ }
+
/**
@@ -379,8 +421,6 @@
// WARNING : The protection level is left unspecified intentionally.
// We need this method to be visible from the DnParser class, but not
// from outside this package.
- @SuppressWarnings(
- { "unchecked" })
/* Unspecified protection */void addAttributeTypeAndValue( String upType, String type, Value<?> upValue,
Value<?> value ) throws InvalidNameException
{
@@ -392,7 +432,7 @@
{
case 0:
// This is the first AttributeTypeAndValue. Just stores it.
- atav = new AttributeTypeAndValue( upType, type, upValue, normalizedValue );
+ atav = new AVA( upType, type, upValue, normalizedValue );
nbAtavs = 1;
atavType = normalizedType;
return;
@@ -401,7 +441,7 @@
// We already have an atav. We have to put it in the HashMap
// before adding a new one.
// First, create the HashMap,
- atavs = new TreeSet<AttributeTypeAndValue>();
+ atavs = new TreeSet<AVA>();
// and store the existing AttributeTypeAndValue into it.
atavs.add( atav );
@@ -415,7 +455,7 @@
default:
// add a new AttributeTypeAndValue
- AttributeTypeAndValue newAtav = new AttributeTypeAndValue( upType, type, upValue, normalizedValue );
+ AVA newAtav = new AVA( upType, type, upValue, normalizedValue );
atavs.add( newAtav );
atavTypes.put( normalizedType, newAtav );
@@ -434,9 +474,7 @@
// WARNING : The protection level is left unspecified intentionnaly.
// We need this method to be visible from the DnParser class, but not
// from outside this package.
- @SuppressWarnings(
- { "unchecked" })
- /* Unspecified protection */void addAttributeTypeAndValue( AttributeTypeAndValue value )
+ /* Unspecified protection */void addAttributeTypeAndValue( AVA value )
{
String normalizedType = value.getNormType();
@@ -444,7 +482,7 @@
{
case 0:
// This is the first AttributeTypeAndValue. Just stores it.
- this.atav = value;
+ atav = value;
nbAtavs = 1;
atavType = normalizedType;
return;
@@ -453,12 +491,12 @@
// We already have an atav. We have to put it in the HashMap
// before adding a new one.
// First, create the HashMap,
- atavs = new TreeSet<AttributeTypeAndValue>();
+ atavs = new TreeSet<AVA>();
// and store the existing AttributeTypeAndValue into it.
- atavs.add( this.atav );
+ atavs.add( atav );
atavTypes = new MultiValueMap();
- atavTypes.put( atavType, this.atav );
+ atavTypes.put( atavType, atav );
this.atav = null;
@@ -524,11 +562,11 @@
default:
if ( atavTypes.containsKey( normalizedType ) )
{
- Collection<AttributeTypeAndValue> atavList = ( Collection<AttributeTypeAndValue> ) atavTypes.get( normalizedType );
+ Collection<AVA> atavList = ( Collection<AVA> ) atavTypes.get( normalizedType );
StringBuffer sb = new StringBuffer();
boolean isFirst = true;
- for ( AttributeTypeAndValue elem : atavList )
+ for ( AVA elem : atavList )
{
if ( isFirst )
{
@@ -581,7 +619,7 @@
* The type of the NameArgument to be returned
* @return The AttributeTypeAndValue, of null if none is found.
*/
- public AttributeTypeAndValue getAttributeTypeAndValue( String type )
+ public AVA getAttributeTypeAndValue( String type )
{
// First, let's normalize the type
String normalizedType = StringTools.lowerCaseAscii( StringTools.trim( type ) );
@@ -602,7 +640,7 @@
default:
if ( atavTypes.containsKey( normalizedType ) )
{
- Collection<AttributeTypeAndValue> atavList = ( Collection<AttributeTypeAndValue> ) atavTypes.get( normalizedType );
+ Collection<AVA> atavList = ( Collection<AVA> ) atavTypes.get( normalizedType );
return atavList.iterator().next();
}
@@ -618,11 +656,11 @@
*
* @return an iterator of the components of this RDN, each an AttributeTypeAndValue
*/
- public Iterator<AttributeTypeAndValue> iterator()
+ public Iterator<AVA> iterator()
{
if ( nbAtavs == 1 || nbAtavs == 0 )
{
- return new Iterator<AttributeTypeAndValue>()
+ return new Iterator<AVA>()
{
private boolean hasMoreElement = nbAtavs == 1;
@@ -633,9 +671,9 @@
}
- public AttributeTypeAndValue next()
+ public AVA next()
{
- AttributeTypeAndValue obj = atav;
+ AVA obj = atav;
hasMoreElement = false;
return obj;
}
@@ -673,18 +711,18 @@
break;
case 1:
- rdn.atav = ( AttributeTypeAndValue ) this.atav.clone();
+ rdn.atav = ( AVA ) this.atav.clone();
rdn.atavTypes = atavTypes;
break;
default:
// We must duplicate the treeSet and the hashMap
rdn.atavTypes = new MultiValueMap();
- rdn.atavs = new TreeSet<AttributeTypeAndValue>();
+ rdn.atavs = new TreeSet<AVA>();
- for ( AttributeTypeAndValue currentAtav : this.atavs )
+ for ( AVA currentAtav : this.atavs )
{
- rdn.atavs.add( ( AttributeTypeAndValue ) currentAtav.clone() );
+ rdn.atavs.add( ( AVA ) currentAtav.clone() );
rdn.atavTypes.put( currentAtav.getNormType(), currentAtav );
}
@@ -746,8 +784,8 @@
// the types are already normalized and sorted in the atavs TreeSet
// so we could compare the 1st with the 1st, then the 2nd with the 2nd, etc.
- Iterator<AttributeTypeAndValue> localIterator = atavs.iterator();
- Iterator<AttributeTypeAndValue> paramIterator = rdn.atavs.iterator();
+ Iterator<AVA> localIterator = atavs.iterator();
+ Iterator<AVA> paramIterator = rdn.atavs.iterator();
while ( localIterator.hasNext() || paramIterator.hasNext() )
{
@@ -760,8 +798,8 @@
return INFERIOR;
}
- AttributeTypeAndValue localAtav = localIterator.next();
- AttributeTypeAndValue paramAtav = paramIterator.next();
+ AVA localAtav = localIterator.next();
+ AVA paramAtav = paramIterator.next();
int result = localAtav.compareTo( paramAtav );
if ( result != EQUAL )
{
@@ -831,7 +869,7 @@
*
* @return The first AttributeTypeAndValue of this RDN
*/
- public AttributeTypeAndValue getAtav()
+ public AVA getAtav()
{
switch ( nbAtavs )
{
@@ -842,7 +880,7 @@
return atav;
default:
- return ( ( TreeSet<AttributeTypeAndValue> ) atavs ).first();
+ return ( ( TreeSet<AVA> ) atavs ).first();
}
}
@@ -863,7 +901,7 @@
return atav.getUpType();
default:
- return ( ( TreeSet<AttributeTypeAndValue> ) atavs ).first().getUpType();
+ return ( ( TreeSet<AVA> ) atavs ).first().getUpType();
}
}
@@ -884,28 +922,7 @@
return atav.getNormType();
default:
- return ( ( TreeSet<AttributeTypeAndValue> ) atavs ).first().getNormType();
- }
- }
-
-
- /**
- * Return the value, or the first one of we have more than one (the lowest)
- *
- * @return The first value of this RDN
- */
- public Object getValue()
- {
- switch ( nbAtavs )
- {
- case 0:
- return null;
-
- case 1:
- return atav.getNormValue().get();
-
- default:
- return ( ( TreeSet<AttributeTypeAndValue> ) atavs ).first().getNormValue().get();
+ return ( ( TreeSet<AVA> ) atavs ).first().getNormType();
}
}
@@ -926,7 +943,7 @@
return atav.getUpValue().getString();
default:
- return ( ( TreeSet<AttributeTypeAndValue> ) atavs ).first().getUpValue().getString();
+ return ( ( TreeSet<AVA> ) atavs ).first().getUpValue().getString();
}
}
@@ -947,7 +964,7 @@
return atav.getNormValue().getString();
default:
- return ( ( TreeSet<AttributeTypeAndValue> ) atavs ).first().getNormalizedValue();
+ return ( ( TreeSet<AVA> ) atavs ).first().getNormValue().getString();
}
}
@@ -1319,7 +1336,7 @@
default:
// We have more than one AttributeTypeAndValue
- for ( AttributeTypeAndValue ata : atavs )
+ for ( AVA ata : atavs )
{
result = result * 17 + ata.hashCode();
}
@@ -1379,7 +1396,7 @@
break;
default:
- for ( AttributeTypeAndValue value : atavs )
+ for ( AVA value : atavs )
{
out.writeObject( value );
}
@@ -1426,19 +1443,19 @@
break;
case 1:
- atav = ( AttributeTypeAndValue ) in.readObject();
+ atav = ( AVA ) in.readObject();
atavType = atav.getNormType();
break;
default:
- atavs = new TreeSet<AttributeTypeAndValue>();
+ atavs = new TreeSet<AVA>();
atavTypes = new MultiValueMap();
for ( int i = 0; i < nbAtavs; i++ )
{
- AttributeTypeAndValue value = ( AttributeTypeAndValue ) in.readObject();
+ AVA value = ( AVA ) in.readObject();
atavs.add( value );
atavTypes.put( value.getNormType(), value );
}
Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnParser.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnParser.java?rev=896579&r1=896578&r2=896579&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnParser.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnParser.java Wed Jan 6 17:52:15 2010
@@ -22,12 +22,6 @@
import javax.naming.InvalidNameException;
-import org.apache.directory.shared.ldap.entry.client.ClientBinaryValue;
-import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
-import org.apache.directory.shared.ldap.util.DNUtils;
-import org.apache.directory.shared.ldap.util.Position;
-import org.apache.directory.shared.ldap.util.StringTools;
-
/**
* This class parse the name-component part or the following BNF grammar (as of
@@ -91,828 +85,6 @@
public class RdnParser
{
/**
- * Parse this rule : <br>
- * <p>
- * <oidValue> ::= [0-9] <digits> <oids>
- * </p>
- *
- * @param bytes The bytes array to parse
- * @param pos The current position in the byte buffer
- * @return The new position in the vyte array, or PARSING_ERROR if the rule
- * does not apply to the byte array
- */
- private static String parseOidValue( byte[] bytes, Position pos )
- {
- pos.start += pos.length;
- pos.end = pos.start;
-
- // <attributType> ::= [0-9] <digits> <oids>
- if ( !StringTools.isDigit( bytes, pos.start ) )
- {
- // Nope... An error
- return null;
- }
- else
- {
- // Let's process an oid
- pos.end++;
-
- while ( StringTools.isDigit( bytes, pos.end ) )
- {
- pos.end++;
- }
-
- // <oids> ::= '.' [0-9] <digits> <oids> | e
- if ( !StringTools.isCharASCII( bytes, pos.end, '.' ) )
- {
- return null;
- }
- else
- {
- do
- {
- pos.end++;
-
- if ( !StringTools.isDigit( bytes, pos.end ) )
- {
- return null;
- }
- else
- {
- pos.end++;
-
- while ( StringTools.isDigit( bytes, pos.end ) )
- {
- pos.end++;
- }
- }
-
- }
- while ( StringTools.isCharASCII( bytes, pos.end, '.' ) );
-
- return StringTools.utf8ToString( bytes, pos.start - pos.length, pos.end - pos.start + pos.length );
- }
- }
- }
-
-
- /**
- * Validate this rule : <br>
- * <p>
- * <oidValue> ::= [0-9] <digits> <oids>
- * </p>
- *
- * @param bytes The byte array to parse
- * @param pos The current position in the byte buffer
- * @return <code>true</code> if this is a valid OID
- */
- private static boolean isValidOidValue( byte[] bytes, Position pos )
- {
- pos.start += pos.length;
- pos.end = pos.start;
-
- // <attributType> ::= [0-9] <digits> <oids>
- if ( !StringTools.isDigit( bytes, pos.start ) )
- {
- // Nope... An error
- return false;
- }
- else
- {
- // Let's process an oid
- pos.end++;
-
- while ( StringTools.isDigit( bytes, pos.end ) )
- {
- pos.end++;
- }
-
- // <oids> ::= '.' [0-9] <digits> <oids> | e
- if ( !StringTools.isCharASCII( bytes, pos.end, '.' ) )
- {
- return false;
- }
- else
- {
- do
- {
- pos.end++;
-
- if ( !StringTools.isDigit( bytes, pos.end ) )
- {
- return false;
- }
- else
- {
- pos.end++;
-
- while ( StringTools.isDigit( bytes, pos.end ) )
- {
- pos.end++;
- }
- }
-
- }
- while ( StringTools.isCharASCII( bytes, pos.end, '.' ) );
-
- return true;
- }
- }
- }
-
-
- /**
- * Parse this rule : <br>
- * <p>
- * <oidPrefix> ::= 'OID.' | 'oid.' | e
- * </p>
- *
- * @param bytes The buffer to parse
- * @param pos The current position in the byte array
- * @return The new position in the byte array, or PARSING_ERROR if the rule
- * does not apply to the byte array
- */
- private static int parseOidPrefix( byte[] bytes, Position pos )
- {
- if ( StringTools.isICharASCII( bytes, pos.start, 'O' ) &&
- StringTools.isICharASCII( bytes, pos.start + 1, 'I' ) &&
- StringTools.isICharASCII( bytes, pos.start + 2, 'D' ) &&
- StringTools.isICharASCII( bytes, pos.start + 3, '.' ) )
- {
- pos.end += DNUtils.OID_LOWER.length();
- return DNUtils.PARSING_OK;
- }
- else
- {
- return DNUtils.PARSING_ERROR;
- }
- }
-
-
- /**
- * Parse this rule : <br>
- * <p>
- * <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix>
- * [0-9] <digits> <oids> | [0-9] <digits> <oids>
- * </p>
- * The string *MUST* be an ASCII string, not an unicode string.
- *
- * @param bytes The byte array to parse
- * @param pos The current position in the byte array
- * @return The new position in the byte array, or PARSING_ERROR if the rule
- * does not apply to the byte array
- */
- private static String parseAttributeType( byte[] bytes, Position pos )
- {
- // <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9] <digits>
- // <oids> | [0-9] <digits> <oids>
- if ( StringTools.isAlphaASCII( bytes, pos.start ) )
- {
- // <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9]
- // <digits> <oids>
-
- // We have got an Alpha char, it may be the begining of an OID ?
- if ( parseOidPrefix( bytes, pos ) != DNUtils.PARSING_ERROR )
- {
- pos.length = 4;
-
- return parseOidValue( bytes, pos );
- }
- else
- {
- // It's not an oid, it's a String (ASCII)
- // <attributType> ::= [a-zA-Z] <keychars>
- // <keychars> ::= [a-zA-Z] <keychar> | [0-9] <keychar> | '-'
- // <keychar> | e
- pos.end++;
-
- while ( StringTools.isAlphaDigitMinus( bytes, pos.end ) )
- {
- pos.end++;
- }
-
- return StringTools.utf8ToString( bytes, pos.start, pos.end - pos.start );
- }
- }
- else
- {
- // An oid
- // <attributType> ::= [0-9] <digits> <oids>
- return parseOidValue( bytes, pos );
- }
- }
-
-
- /**
- * Parse this rule : <br>
- * <p>
- * <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix>
- * [0-9] <digits> <oids> | [0-9] <digits> <oids>
- * </p>
- * The string *MUST* be an ASCII string, not an unicode string.
- *
- * @param bytes The byte array to parse
- * @param pos The current position in the byte array
- * @return The new position in the byte array, or PARSING_ERROR if the rule
- * does not apply to the byte array
- */
- private static boolean isValidAttributeType( byte[] bytes, Position pos )
- {
- // <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9] <digits>
- // <oids> | [0-9] <digits> <oids>
- if ( StringTools.isAlphaASCII( bytes, pos.start ) )
- {
- // <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9]
- // <digits> <oids>
-
- // We have got an Alpha char, it may be the begining of an OID ?
- if ( parseOidPrefix( bytes, pos ) != DNUtils.PARSING_ERROR )
- {
- pos.length = 4;
-
- return isValidOidValue( bytes, pos );
- }
- else
- {
- // It's not an oid, it's a String (ASCII)
- // <attributType> ::= [a-zA-Z] <keychars>
- // <keychars> ::= [a-zA-Z] <keychar> | [0-9] <keychar> | '-'
- // <keychar> | e
- pos.end++;
-
- while ( StringTools.isAlphaDigitMinus( bytes, pos.end ) )
- {
- pos.end++;
- }
-
- return true;
- }
- }
- else
- {
- // An oid
- // <attributType> ::= [0-9] <digits> <oids>
- return isValidOidValue( bytes, pos );
- }
- }
-
-
- /**
- * Parse this rule : <br>
- * <p>
- * <attributeValue> ::= <pairs-or-strings> | '#'
- * <hexstring> |'"' <quotechar-or-pairs> '"' <br>
- * <pairs-or-strings> ::= '\' <pairchar>
- * <pairs-or-strings> | <stringchar> <pairs-or-strings> | |
- * e <br>
- * <quotechar-or-pairs> ::= <quotechar>
- * <quotechar-or-pairs> | '\' <pairchar>
- * <quotechar-or-pairs> | e <br>
- * </p>
- *
- * @param bytes The byte array to parse
- * @param pos The current position in the byte array
- * @return The new position in the byte array, or PARSING_ERROR if the rule
- * does not apply to the byte array
- */
- private static Object parseAttributeValue( byte[] bytes, Position pos )
- {
- if ( pos.start == bytes.length )
- {
- // This is an empty value
- return "";
- }
-
- //StringBuffer sb = new StringBuffer();
- byte c = bytes[pos.start];
- byte[] buffer = new byte[bytes.length];
- int currPos = 0;
-
- if ( c == '#' )
- {
- pos.start++;
- int nbHex = 0;
- int currentPos = pos.start;
-
- // First, we will count the number of hexPairs
- while ( DNUtils.parseHexPair( bytes, currentPos ) >= 0 )
- {
- nbHex++;
- currentPos += DNUtils.TWO_CHARS;
- }
-
- byte[] hexValue = new byte[nbHex];
-
- // Now, convert the value
- // <attributeValue> ::= '#' <hexstring>
- if ( DNUtils.parseHexString( bytes, hexValue, pos ) == DNUtils.PARSING_ERROR )
- {
- return null;
- }
-
- pos.start--;
- StringTools.trimRight( bytes, pos );
- pos.length = pos.end - pos.start;
-
- return hexValue;
- }
- else if ( c == '"' )
- {
- pos.start++;
- pos.length = 0;
- pos.end = pos.start;
- int nbBytes = 0;
- int length = 0;
-
- // <attributeValue> ::= '"' <quotechar-or-pair> '"'
- // <quotechar-or-pairs> ::= <quotechar> <quotechar-or-pairs> | '\'
- // <pairchar> <quotechar-or-pairs> | e
- while ( true )
- {
- if ( StringTools.isCharASCII( bytes, pos.end, '\\' ) )
- {
- pos.end++;
- int nbChars = 0;
-
- if ( ( nbChars = DNUtils.countPairChar( bytes, pos.end ) ) == DNUtils.PARSING_ERROR )
- {
- return null;
- }
- else
- {
- if ( nbChars == 1 )
- {
- buffer[currPos++] = bytes[pos.end];
- }
- else
- {
- byte b = StringTools.getHexValue( bytes[pos.end], bytes[pos.end + 1] );
-
- buffer[currPos++] = b;
- }
-
- pos.end += nbChars;
- length += nbChars;
- }
- }
- else if ( ( nbBytes = DNUtils.isQuoteChar( bytes, pos.end ) ) != DNUtils.PARSING_ERROR )
- {
- for ( int i = 0; i < nbBytes; i++ )
- {
- buffer[currPos++] = bytes[pos.end + i];
- }
-
- pos.end += nbBytes;
- length += nbBytes;
- }
- else
- {
- //pos.length = pos.end - pos.start;
- break;
- }
- }
-
- if ( StringTools.isCharASCII( bytes, pos.end, '"' ) )
- {
- pos.end++;
- pos.length = length + 2;
- return StringTools.utf8ToString( buffer, length );
- }
- else
- {
- return null;
- }
- }
- else
- {
- int escapedSpace = -1;
-
- while ( true )
- {
- if ( StringTools.isCharASCII( bytes, pos.end, '\\' ) )
- {
- // '\' <pairchar> <pairs-or-strings>
- pos.end++;
-
- int nbChars = 0;
-
- if ( ( nbChars = DNUtils.countPairChar( bytes, pos.end ) ) == DNUtils.PARSING_ERROR )
- {
- return null;
- }
- else
- {
- if ( nbChars == 1 )
- {
- buffer[currPos++] = bytes[pos.end];
- }
- else
- {
- byte b = StringTools.getHexValue( bytes[pos.end], bytes[pos.end + 1] );
-
- buffer[currPos++] = b;
- }
-
- if ( bytes[pos.end] == ' ' )
- {
- escapedSpace = currPos;
- }
- else
- {
- escapedSpace = -1;
- }
-
- pos.end += nbChars;
- }
- }
- else
- {
- int nbChars = 0;
-
- // <stringchar> <pairs-or-strings>
- if ( ( nbChars = DNUtils.isStringChar( bytes, pos.end ) ) != DNUtils.PARSING_ERROR )
- {
- // If the char is not a space, we have to switch off the escapedSpace flag
- if ( ( escapedSpace != -1 ) && ! StringTools.isCharASCII( bytes, pos.end, ' ' ) )
- {
- escapedSpace = -1;
- }
-
- // A special case : if we have some spaces before the
- // '+' character,
- // we MUST skip them.
- if ( StringTools.isCharASCII( bytes, pos.end, '+' ) )
- {
- //StringTools.trimLeft( string, pos );
-
- if ( ( DNUtils.isStringChar( bytes, pos.end ) == DNUtils.PARSING_ERROR )
- && ( !StringTools.isCharASCII( bytes, pos.end, '\\' ) ) )
- {
- // Ok, we are done with the stringchar.
- String result = StringTools.trimRight(
- StringTools.utf8ToString( bytes, pos.start, pos.start + pos.length ) );
-
- return result;
- }
- else
- {
- for ( int i = 0; i < nbChars; i++ )
- {
- buffer[currPos++] = bytes[pos.end];
- pos.end ++;
- }
- }
- }
- else
- {
- for ( int i = 0; i < nbChars; i++ )
- {
- buffer[currPos++] = bytes[pos.end];
- pos.end ++;
- }
- }
- }
- else
- {
- pos.length = pos.end - pos.start;
-
- if ( escapedSpace == -1 )
- {
- String result = StringTools.trimRight( StringTools.utf8ToString( buffer, currPos ) );
- return result;
- }
-
- String result = StringTools.utf8ToString( buffer, escapedSpace );
-
- return result;
- }
- }
- }
- }
- }
-
-
- /**
- * Validate this rule : <br>
- * <p>
- * <attributeValue> ::= <pairs-or-strings> | '#'
- * <hexstring> |'"' <quotechar-or-pairs> '"' <br>
- * <pairs-or-strings> ::= '\' <pairchar>
- * <pairs-or-strings> | <stringchar> <pairs-or-strings> | |
- * e <br>
- * <quotechar-or-pairs> ::= <quotechar>
- * <quotechar-or-pairs> | '\' <pairchar>
- * <quotechar-or-pairs> | e <br>
- * </p>
- *
- * @param bytes The byte array to parse
- * @param pos The current position in the byte array
- * @return <code>true</code> if the rule is valid
- */
- private static boolean isValidAttributeValue( byte[] bytes, Position pos )
- {
- if( bytes.length <= pos.start )
- {
- // no attribute value
- return true;
- }
-
- byte c = bytes[pos.start];
-
- if ( c == '#' )
- {
- pos.start++;
- int nbHex = 0;
- int currentPos = pos.start;
-
- // First, we will count the number of hexPairs
- while ( DNUtils.parseHexPair( bytes, currentPos ) >= 0 )
- {
- nbHex++;
- currentPos += DNUtils.TWO_CHARS;
- }
-
- byte[] hexValue = new byte[nbHex];
-
- // Now, convert the value
- // <attributeValue> ::= '#' <hexstring>
- if ( DNUtils.parseHexString( bytes, hexValue, pos ) == DNUtils.PARSING_ERROR )
- {
- return false;
- }
-
- pos.start--;
- StringTools.trimRight( bytes, pos );
- pos.length = pos.end - pos.start;
-
- return true;
- }
- else if ( c == '"' )
- {
- pos.start++;
- pos.length = 0;
- pos.end = pos.start;
- int nbBytes = 0;
-
- // <attributeValue> ::= '"' <quotechar-or-pair> '"'
- // <quotechar-or-pairs> ::= <quotechar> <quotechar-or-pairs> | '\'
- // <pairchar> <quotechar-or-pairs> | e
- while ( true )
- {
- if ( StringTools.isCharASCII( bytes, pos.end, '\\' ) )
- {
- pos.end++;
- int nbChars = 0;
-
- if ( ( nbChars = DNUtils.countPairChar( bytes, pos.end ) ) != DNUtils.PARSING_ERROR )
- {
- pos.end += nbChars;
- }
- else
- {
- return false;
- }
- }
- else if ( ( nbBytes = DNUtils.isQuoteChar( bytes, pos.end ) ) != DNUtils.PARSING_ERROR )
- {
- pos.end += nbBytes;
- }
- else
- {
- pos.length = pos.end - pos.start;
- break;
- }
- }
-
- if ( StringTools.isCharASCII( bytes, pos.end, '"' ) )
- {
- pos.end++;
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- while ( true )
- {
- if ( StringTools.isCharASCII( bytes, pos.end, '\\' ) )
- {
- // '\' <pairchar> <pairs-or-strings>
- pos.end++;
-
- int nbChars = 0;
-
- if ( ( nbChars = DNUtils.countPairChar( bytes, pos.end ) ) == DNUtils.PARSING_ERROR )
- {
- return false;
- }
- else
- {
- pos.end += nbChars;
- }
- }
- else
- {
- int nbChars = 0;
-
- // <stringchar> <pairs-or-strings>
- if ( ( nbChars = DNUtils.isStringChar( bytes, pos.end ) ) != DNUtils.PARSING_ERROR )
- {
- // A special case : if we have some spaces before the
- // '+' character,
- // we MUST skip them.
- if ( StringTools.isCharASCII( bytes, pos.end, '+' ) )
- {
- //StringTools.trimLeft( string, pos );
-
- if ( ( DNUtils.isStringChar( bytes, pos.end ) == DNUtils.PARSING_ERROR )
- && ( !StringTools.isCharASCII( bytes, pos.end, '\\' ) ) )
- {
- // Ok, we are done with the stringchar.
- return true;
- }
- else
- {
- pos.end++;
- }
- }
- else
- {
- pos.end += nbChars;
- }
- }
- else
- {
- return true;
- }
- }
- }
- }
- }
-
-
- /**
- * Parse this rule : <br>
- * <p>
- * <nameComponents> ::= <spaces> '+' <spaces>
- * <attributeType> <spaces> '=' <spaces>
- * <attributeValue> <nameComponents> | e
- * </p>
- *
- * @param bytes The byte buffer to parse
- * @param pos The current position in the byte buffer
- * @param rdn The rdn to generate
- * @return The new position in the byte buffer, or PARSING_ERROR if the rule
- * does not apply to the byte buffer
- * @throws InvalidNameException If the NameComponent is invalid
- */
- private static int parseNameComponents( byte[] bytes, Position pos, Rdn rdn ) throws InvalidNameException
- {
- if ( rdn == null )
- {
- throw new InvalidNameException( "The RDN should not be null" );
- }
-
- int newStart = 0;
- String type = null;
- Object value = null;
-
- while ( true )
- {
- StringTools.trimLeft( bytes, pos );
-
- if ( StringTools.isCharASCII( bytes, pos.end, '+' ) )
- {
- pos.start++;
- pos.length = 0;
- }
- else
- {
- // <attributeTypeAndValues> ::= e
- rdn.normalize();
- return DNUtils.PARSING_OK;
- }
-
- StringTools.trimLeft( bytes, pos );
-
- if ( ( type = parseAttributeType( bytes, pos ) ) == null )
- {
- return DNUtils.PARSING_ERROR;
- }
-
- pos.start = pos.end;
-
- StringTools.trimLeft( bytes, pos );
-
- if ( StringTools.isCharASCII( bytes, pos.end, '=' ) )
- {
- pos.start++;
- }
- else
- {
- return DNUtils.PARSING_ERROR;
- }
-
- StringTools.trimLeft( bytes, pos );
-
- value = parseAttributeValue( bytes, pos );
-
- newStart = pos.end;
-
- if ( value != null )
- {
- if ( rdn != null )
- {
- if ( value instanceof String )
- {
- rdn.addAttributeTypeAndValue( type, type,
- new ClientStringValue( (String)value ),
- new ClientStringValue( (String)value ) );
- }
- else
- {
- rdn.addAttributeTypeAndValue( type, type,
- new ClientBinaryValue( (byte[])value ),
- new ClientBinaryValue( (byte[])value ) );
- }
- }
- }
-
- pos.start = newStart;
- pos.end = newStart;
- }
- }
-
-
- /**
- * Validate this rule : <br>
- * <p>
- * <nameComponents> ::= <spaces> '+' <spaces>
- * <attributeType> <spaces> '=' <spaces>
- * <attributeValue> <nameComponents> | e
- * </p>
- *
- * @param bytes The byte buffer to parse
- * @param pos The current position in the byte buffer
- * @param isFirstRdn A flag set if the RDN is the first one
- * @return <code>true</code> if the rule is valid
- */
- private static boolean isValidNameComponents( byte[] bytes, Position pos, boolean isFirstRdn )
- {
- int newStart = 0;
-
- while ( true )
- {
- StringTools.trimLeft( bytes, pos );
-
- if ( StringTools.isCharASCII( bytes, pos.end, '+' ) )
- {
- pos.start++;
- }
- else
- {
- // <attributeTypeAndValues> ::= e
- return true;
- }
-
- StringTools.trimLeft( bytes, pos );
-
- if ( !isValidAttributeType( bytes, pos ) )
- {
- return false;
- }
-
- pos.start = pos.end;
-
- StringTools.trimLeft( bytes, pos );
-
- if ( StringTools.isCharASCII( bytes, pos.end, '=' ) )
- {
- pos.start++;
- }
- else
- {
- return false;
- }
-
- StringTools.trimLeft( bytes, pos );
-
- if ( !isValidAttributeValue( bytes, pos ) )
- {
- return false;
- }
-
- newStart = pos.end;
- pos.start = newStart;
- pos.end = newStart;
- }
- }
-
-
- /**
* Parse a NameComponent : <br>
* <p>
* <name-component> ::= <attributeType> <spaces> '='
@@ -920,105 +92,21 @@
* </p>
*
* @param dn The String to parse
- * @param pos The current position in the buffer
- * @param rdn The constructed RDN
- * @return The new position in the char array, or PARSING_ERROR if the rule
- * does not apply to the char array
- * @throws InvalidNameException If the NameComponent is invalid
- */
- public static int parse( String dn, Position pos, Rdn rdn ) throws InvalidNameException
- {
- return parse( StringTools.getBytesUtf8( dn ), pos, rdn );
- }
-
-
- /**
- * Parse a NameComponent : <br>
- * <p>
- * <name-component> ::= <attributeType> <spaces> '='
- * <spaces> <attributeValue> <nameComponents>
- * </p>
- *
- * @param dn The byte array to parse
- * @param pos The current position in the buffer
- * @param rdn The constructed RDN
- * @return The new position in the byte array, or PARSING_ERROR if the rule
- * does not apply to the byte array
+ * @param rdn The RDN to fill. Beware that if the RDN is not empty, the new
+ * AttributeTypeAndValue will be added.
* @throws InvalidNameException If the NameComponent is invalid
*/
- public static int parse( byte[] dn, Position pos, Rdn rdn ) throws InvalidNameException
+ public static void parse( String dn, Rdn rdn ) throws InvalidNameException
{
- if ( rdn == null )
+ try
{
- throw new InvalidNameException( "Cannot feed a null RDN structure" );
+ FastLdapDnParser.INSTANCE.parseRdn( dn, rdn );
}
-
- String type = null;
- Object value = null;
- int start = pos.start;
-
- StringTools.trimLeft( dn, pos );
-
- pos.end = pos.start;
- pos.length = 0;
-
- if ( ( type = parseAttributeType( dn, pos ) ) == null )
+ catch ( TooComplexException e )
{
- return DNUtils.PARSING_ERROR;
+ rdn.clear();
+ new ComplexLdapDnParser().parseRdn( dn, rdn );
}
-
- pos.start = pos.end;
-
- StringTools.trimLeft( dn, pos );
-
- if ( !StringTools.isCharASCII( dn, pos.start, '=' ) )
- {
- return DNUtils.PARSING_ERROR;
- }
- else
- {
- pos.start++;
- }
-
- StringTools.trimLeft( dn, pos );
-
- pos.end = pos.start;
-
- int start2 = pos.start;
-
- if ( ( value = parseAttributeValue( dn, pos ) ) == null )
- {
- return DNUtils.PARSING_ERROR;
- }
-
- String upValue = StringTools.utf8ToString( dn, start2, pos.length );
-
- if ( value instanceof String )
- {
- rdn.addAttributeTypeAndValue( type, type,
- new ClientStringValue( upValue ),
- new ClientStringValue( (String)value ) );
- }
- else
- {
- rdn.addAttributeTypeAndValue( type, type,
- new ClientStringValue( upValue ),
- new ClientBinaryValue( (byte[])value ) );
- }
-
- rdn.normalize();
-
- pos.start = pos.end;
- pos.length = 0;
-
- if ( parseNameComponents( dn, pos, rdn ) == DNUtils.PARSING_ERROR )
- {
- return DNUtils.PARSING_ERROR;
- }
-
- rdn.setUpName( StringTools.utf8ToString( dn, start, pos.end - start ) );
- pos.start = pos.end;
- return DNUtils.PARSING_OK;
}
@@ -1029,90 +117,21 @@
* <spaces> <attributeValue> <nameComponents>
* </p>
*
- * @param dn The byte array to parse
- * @param pos The current position in the buffer
- * @param isFirstRdn a flag set if the RDN is the first for the current DN
+ * @param dn The string to parse
* @return <code>true</code> if the RDN is valid
*/
- public static boolean isValid( byte[] dn, Position pos, boolean isFirstRdn )
+ public static boolean isValid( String dn )
{
- StringTools.trimLeft( dn, pos );
-
- pos.end = pos.start;
- pos.length = 0;
-
- if ( !isValidAttributeType( dn, pos ) )
- {
- return false;
- }
-
- pos.start = pos.end;
-
- StringTools.trimLeft( dn, pos );
-
- if ( !StringTools.isCharASCII( dn, pos.start, '=' ) )
- {
- return false;
- }
- else
- {
- pos.start++;
- }
-
- StringTools.trimLeft( dn, pos );
-
- pos.end = pos.start;
-
- if ( !isValidAttributeValue( dn, pos ) )
+ Rdn rdn = new Rdn();
+ try
{
- return false;
+ parse( dn, rdn );
+ return true;
}
-
- pos.start = pos.end;
- pos.length = 0;
-
- if ( !isValidNameComponents( dn, pos, isFirstRdn ) )
+ catch ( InvalidNameException e )
{
return false;
}
-
- pos.start = pos.end;
- return true;
- }
-
-
- /**
- * Parse a NameComponent : <br>
- * <p>
- * <name-component> ::= <attributeType> <spaces> '='
- * <spaces> <attributeValue> <nameComponents>
- * </p>
- *
- * @param dn The String to parse
- * @param rdn The RDN to fill. Beware that if the RDN is not empty, the new
- * AttributeTypeAndValue will be added.
- * @throws InvalidNameException If the NameComponent is invalid
- */
- public static void parse( String dn, Rdn rdn ) throws InvalidNameException
- {
- if ( parse( StringTools.getBytesUtf8( dn ), new Position(), rdn ) == DNUtils.PARSING_ERROR )
- {
- throw new InvalidNameException( "The RDN can't be parsed." );
- }
}
- /**
- * Validate a NameComponent : <br>
- * <p>
- * <name-component> ::= <attributeType> <spaces> '='
- * <spaces> <attributeValue> <nameComponents>
- * </p>
- *
- * @param dn The string to parse
- * @return <code>true</code> if the RDN is valid
- */
- public static boolean isValid( String dn )
- {
- return isValid( StringTools.getBytesUtf8( dn ), new Position(), false );
- }
}
Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnSerializer.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnSerializer.java?rev=896579&r1=896578&r2=896579&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnSerializer.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnSerializer.java Wed Jan 6 17:52:15 2010
@@ -74,13 +74,13 @@
break;
case 1 :
- AtavSerializer.serialize( rdn.getAtav(), out );
+ AVASerializer.serialize( rdn.getAtav(), out );
break;
default :
- for ( AttributeTypeAndValue atav:rdn )
+ for ( AVA atav:rdn )
{
- AtavSerializer.serialize( atav, out );
+ AVASerializer.serialize( atav, out );
}
break;
@@ -129,7 +129,7 @@
return rdn;
case 1 :
- AttributeTypeAndValue atav = AtavSerializer.deserialize( in );
+ AVA atav = AVASerializer.deserialize( in );
rdn.addAttributeTypeAndValue( atav );
@@ -138,7 +138,7 @@
default :
for ( int i = 0; i < nbAtavs; i++ )
{
- atav = AtavSerializer.deserialize( in );
+ atav = AVASerializer.deserialize( in );
rdn.addAttributeTypeAndValue( atav );
}