Java Code Examples for org.apache.directory.api.util.Strings#trim()

The following examples show how to use org.apache.directory.api.util.Strings#trim() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LdifReader.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Parse the value part.
 *
 * @param line The line which contains the value
 * @param pos The starting position in the line
 * @return A String or a byte[], depending of the kind of value we get
 */
protected static Object parseSimpleValue( String line, int pos )
{
    if ( line.length() > pos + 1 )
    {
        char c = line.charAt( pos + 1 );

        if ( c == ':' )
        {
            String value = Strings.trim( line.substring( pos + 2 ) );

            return Base64.decode( value.toCharArray() );
        }
        else
        {
            return Strings.trim( line.substring( pos + 1 ) );
        }
    }
    else
    {
        return null;
    }
}
 
Example 2
Source File: DefaultEntry.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Get the trimmed and lower cased entry ID
 *
 * @param upId The ID
 * @return The retrieved ID
 */
private String getId( String upId )
{
    String id = Strings.trim( Strings.toLowerCaseAscii( upId ) );

    // If empty, throw an error
    if ( Strings.isEmpty( id ) )
    {
        String message = I18n.err( I18n.ERR_13216_AT_ID_NULL );
        LOG.error( message );
        throw new IllegalArgumentException( message );
    }

    return id;
}
 
Example 3
Source File: DefaultEntry.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Get the UpId if it is null.
 *
 * @param upId The ID
 * @param attributeType The AttributeType to retrieve
 * @return the retrieved ID
 */
private String getUpId( String upId, AttributeType attributeType )
{
    String normUpId = Strings.trim( upId );

    if ( attributeType == null )
    {
        if ( Strings.isEmpty( normUpId ) )
        {
            String message = I18n.err( I18n.ERR_13226_CANNOT_ADD_ATTRIBUTE_NO_ID );
            LOG.error( message );
            throw new IllegalArgumentException( message );
        }

        return upId;
    }
    else if ( Strings.isEmpty( normUpId ) )
    {
        String id = attributeType.getName();

        if ( Strings.isEmpty( id ) )
        {
            id = attributeType.getOid();
        }

        return id;
    }
    else
    {
        return upId;
    }
}
 
Example 4
Source File: DefaultAttribute.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void setUpId( String upId, AttributeType attributeType )
{
    String trimmed = Strings.trim( upId );

    if ( Strings.isEmpty( trimmed ) && ( attributeType == null ) )
    {
        throw new IllegalArgumentException( I18n.err( I18n.ERR_13235_NULL_ID_WITH_NULL_AT_NOT_ALLOWED ) );
    }

    String newId = Strings.toLowerCaseAscii( trimmed );

    setUpIdInternal( upId, newId, attributeType );
}
 
Example 5
Source File: DefaultAttribute.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the User Provided ID as a byte[]
 * 
 * @param upId The User Provided ID
 * @param attributeType The asscoiated AttributeType
 */
public void setUpId( byte[] upId, AttributeType attributeType )
{
    byte[] trimmed = Strings.trim( upId );

    if ( Strings.isEmpty( trimmed ) && ( attributeType == null ) )
    {
        throw new IllegalArgumentException( I18n.err( I18n.ERR_13235_NULL_ID_WITH_NULL_AT_NOT_ALLOWED ) );
    }

    String newId = Strings.toLowerCase( trimmed );

    setUpIdInternal( Strings.utf8ToString( upId ), newId, attributeType );
}
 
Example 6
Source File: LdifReader.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Parse the changeType
 *
 * @param line The line which contains the changeType
 * @return The operation.
 */
protected ChangeType parseChangeType( String line )
{
    ChangeType operation = ChangeType.Add;

    String modOp = Strings.trim( line.substring( "changetype:".length() ) );

    if ( "add".equalsIgnoreCase( modOp ) )
    {
        operation = ChangeType.Add;
    }
    else if ( "delete".equalsIgnoreCase( modOp ) )
    {
        operation = ChangeType.Delete;
    }
    else if ( "modify".equalsIgnoreCase( modOp ) )
    {
        operation = ChangeType.Modify;
    }
    else if ( "moddn".equalsIgnoreCase( modOp ) )
    {
        operation = ChangeType.ModDn;
    }
    else if ( "modrdn".equalsIgnoreCase( modOp ) )
    {
        operation = ChangeType.ModRdn;
    }

    return operation;
}
 
Example 7
Source File: SearchScopeSyntaxChecker.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isValidSyntax( Object value )
{
    String strValue;

    if ( value == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
        }
        
        return false;
    }

    if ( value instanceof String )
    {
        strValue = ( String ) value;
    }
    else if ( value instanceof byte[] )
    {
        strValue = Strings.utf8ToString( ( byte[] ) value );
    }
    else
    {
        strValue = value.toString();
    }

    strValue = Strings.trim( Strings.toLowerCaseAscii( strValue ) );

    return "base".equals( strValue ) || "one".equals( strValue ) || "sub".equals( strValue );
}
 
Example 8
Source File: DerefAliasSyntaxChecker.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isValidSyntax( Object value )
{
    String strValue;

    if ( value == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
        }
        
        return false;
    }

    if ( value instanceof String )
    {
        strValue = ( String ) value;
    }
    else if ( value instanceof byte[] )
    {
        strValue = Strings.utf8ToString( ( byte[] ) value );
    }
    else
    {
        strValue = value.toString();
    }

    strValue = Strings.trim( Strings.toLowerCaseAscii( strValue ) );

    return "never".equals( strValue ) || "finding".equals( strValue ) || "searching".equals( strValue ) || "always"
        .equals( strValue );
}
 
Example 9
Source File: DefaultEntry.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Get the trimmed and lower cased entry ID
 */
private String getId( String upId )
{
    String id = Strings.trim( Strings.toLowerCase( upId ) );

    // If empty, throw an error
    if ( Strings.isEmpty( id ) )
    {
        String message = I18n.err( I18n.ERR_04133 );
        LOG.error( message );
        throw new IllegalArgumentException( message );
    }

    return id;
}
 
Example 10
Source File: DefaultEntry.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Get the UpId if it is null.
 * 
 * @param upId The ID
 */
private String getUpId( String upId, AttributeType attributeType )
{
    String normUpId = Strings.trim( upId );

    if ( ( attributeType == null ) )
    {
        if ( Strings.isEmpty( normUpId ) )
        {
            String message = I18n.err( I18n.ERR_04458 );
            LOG.error( message );
            throw new IllegalArgumentException( message );
        }

        return upId;
    }
    else if ( Strings.isEmpty( normUpId ) )
    {
        String id = attributeType.getName();

        if ( Strings.isEmpty( id ) )
        {
            id = attributeType.getOid();
        }

        return id;
    }
    else
    {
        return upId;
    }
}
 
Example 11
Source File: DefaultEntry.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Get the trimmed and lower cased entry ID
 */
private String getId( String upId )
{
    String id = Strings.trim( Strings.toLowerCase( upId ) );

    // If empty, throw an error
    if ( Strings.isEmpty( id ) )
    {
        String message = I18n.err( I18n.ERR_04133 );
        LOG.error( message );
        throw new IllegalArgumentException( message );
    }

    return id;
}
 
Example 12
Source File: DefaultEntry.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Get the UpId if it is null.
 * 
 * @param upId The ID
 */
private String getUpId( String upId, AttributeType attributeType )
{
    String normUpId = Strings.trim( upId );

    if ( ( attributeType == null ) )
    {
        if ( Strings.isEmpty( normUpId ) )
        {
            String message = I18n.err( I18n.ERR_04458 );
            LOG.error( message );
            throw new IllegalArgumentException( message );
        }

        return upId;
    }
    else if ( Strings.isEmpty( normUpId ) )
    {
        String id = attributeType.getName();

        if ( Strings.isEmpty( id ) )
        {
            id = attributeType.getOid();
        }

        return id;
    }
    else
    {
        return upId;
    }
}
 
Example 13
Source File: Ava.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Construct an Ava. The type and value are normalized :
 * <ul>
 *   <li> the type is trimmed and lowercased </li>
 *   <li> the value is trimmed </li>
 * </ul>
 * <p>
 * Note that the upValue should <b>not</b> be null or empty, or resolved
 * to an empty string after having trimmed it.
 *
 * @param attributeType The AttributeType for this value
 * @param upType The User Provided type
 * @param normType The normalized type
 * @param value The value
 * @param upName The User Provided name (may be escaped)
 * 
 * @throws LdapInvalidDnException If the given type or value are invalid
 */
// 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.
/* Unspecified protection */Ava( AttributeType attributeType, String upType, String normType, Value value, String upName )
    throws LdapInvalidDnException
{
    this.attributeType = attributeType;
    String upTypeTrimmed = Strings.trim( upType );
    String normTypeTrimmed = Strings.trim( normType );

    if ( Strings.isEmpty( upTypeTrimmed ) )
    {
        if ( Strings.isEmpty( normTypeTrimmed ) )
        {
            String message = I18n.err( I18n.ERR_13600_TYPE_IS_NULL_OR_EMPTY );
            LOG.error( message );
            throw new LdapInvalidDnException( ResultCodeEnum.INVALID_DN_SYNTAX, message );
        }
        else
        {
            // In this case, we will use the normType instead
            this.normType = Strings.lowerCaseAscii( normTypeTrimmed );
            this.upType = normType;
        }
    }
    else if ( Strings.isEmpty( normTypeTrimmed ) )
    {
        // In this case, we will use the upType instead
        this.normType = Strings.lowerCaseAscii( upTypeTrimmed );
        this.upType = upType;
    }
    else
    {
        this.normType = Strings.lowerCaseAscii( normTypeTrimmed );
        this.upType = upType;
    }

    this.value = value;
    this.upName = upName;
    hashCode();
}
 
Example 14
Source File: Ava.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Construct an Ava. The type and value are normalized :
 * <ul>
 *   <li> the type is trimmed and lowercased </li>
 *   <li> the value is trimmed </li>
 * </ul>
 * <p>
 * Note that the upValue should <b>not</b> be null or empty, or resolved
 * to an empty string after having trimmed it.
 *
 * @param upType The User Provided type
 * @param upValue The User Provided value
 * 
 * @throws LdapInvalidDnException If the given type or value are invalid
 */
private void createAva( String upType, Value upValue ) throws LdapInvalidDnException
{
    String upTypeTrimmed = Strings.trim( upType );
    String normTypeTrimmed = Strings.trim( normType );

    if ( Strings.isEmpty( upTypeTrimmed ) )
    {
        if ( Strings.isEmpty( normTypeTrimmed ) )
        {
            String message = I18n.err( I18n.ERR_13600_TYPE_IS_NULL_OR_EMPTY );
            // Do NOT log the message here. The error may be handled and therefore the log message may polute the log files.
            // Let the caller log the exception if needed.
            throw new LdapInvalidDnException( ResultCodeEnum.INVALID_DN_SYNTAX, message );
        }
        else
        {
            // In this case, we will use the normType instead
            this.normType = Strings.lowerCaseAscii( normTypeTrimmed );
            this.upType = normType;
        }
    }
    else if ( Strings.isEmpty( normTypeTrimmed ) )
    {
        // In this case, we will use the upType instead
        this.normType = Strings.lowerCaseAscii( upTypeTrimmed );
        this.upType = upType;
    }
    else
    {
        this.normType = Strings.lowerCaseAscii( normTypeTrimmed );
        this.upType = upType;

    }

    value = upValue;

    upName = getEscaped();
    
    hashCode();
}