Java Code Examples for org.apache.directory.api.ldap.model.schema.AttributeType#setSyntax()

The following examples show how to use org.apache.directory.api.ldap.model.schema.AttributeType#setSyntax() . 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: EntryUtils.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
static AttributeType getBytesAttributeType()
{
    AttributeType attributeType = new AttributeType( "1.2" );
    LdapSyntax syntax = new LdapSyntax( "1.2.1", "", false );

    syntax.setSyntaxChecker( new SyntaxChecker( "1.2.1" )
    {
        public static final long serialVersionUID = 1L;


        public boolean isValidSyntax( Object value )
        {
            return ( value == null ) || ( ( ( byte[] ) value ).length < 5 );
        }
    } );

    MatchingRule matchingRule = new MatchingRule( "1.2.2" );
    matchingRule.setSyntax( syntax );

    matchingRule.setLdapComparator( new ByteArrayComparator( "1.2.2" ) );

    matchingRule.setNormalizer( new NoOpNormalizer( "1.1.1" ) );

    attributeType.setEquality( matchingRule );
    attributeType.setSyntax( syntax );

    return attributeType;
}
 
Example 2
Source File: TestEntryUtils.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
static AttributeType getIA5StringAttributeType()
{
    AttributeType attributeType = new AttributeType( "1.1" );
    attributeType.addName( "1.1" );
    LdapSyntax syntax = new LdapSyntax( "1.1.1", "", true );

    syntax.setSyntaxChecker( new SyntaxChecker( "1.1.2" )
    {
        /** The mandatory serialVersionUID field */
        public static final long serialVersionUID = 1L;


        public boolean isValidSyntax( Object value )
        {
            String trimmedValue = Strings.deepTrim( ( String )value );

            return ( trimmedValue == null ) || ( trimmedValue.length() < 7 );
        }
    } );

    MatchingRule matchingRule = new MatchingRule( "1.1.2" );
    matchingRule.setSyntax( syntax );

    matchingRule.setLdapComparator( new LdapComparator<String>( matchingRule.getOid() )
    {
        /** The mandatory serialVersionUID field */
        public static final long serialVersionUID = 1L;


        public int compare( String o1, String o2 )
        {
            return ( ( o1 == null ) ? ( o2 == null ? 0 : -1 ) : ( o2 == null ? 1 : o1.compareTo( o2 ) ) );
        }
    } );

    matchingRule.setNormalizer( new DeepTrimToLowerNormalizer( matchingRule.getOid() ) );

    attributeType.setEquality( matchingRule );
    attributeType.setSyntax( syntax );

    return attributeType;
}
 
Example 3
Source File: LdifReader.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Parse an AttributeType/AttributeValue
 *
 * @param entry The entry where to store the value
 * @param line The line to parse
 * @param lowerLine The same line, lowercased
 * @throws LdapException If anything goes wrong
 */
public void parseAttributeValue( LdifEntry entry, String line, String lowerLine ) throws LdapException
{
    int colonIndex = line.indexOf( ':' );

    String attributeType = lowerLine.substring( 0, colonIndex );

    // We should *not* have a Dn twice
    if ( "dn".equals( attributeType ) )
    {
        LOG.error( I18n.err( I18n.ERR_13400_ENTRY_WITH_TWO_DNS, lineNumber ) );
        throw new LdapLdifException( I18n.err( I18n.ERR_13439_LDIF_ENTRY_WITH_TWO_DNS ) );
    }

    Object attributeValue = parseValue( attributeType, line, colonIndex );

    if ( schemaManager != null )
    {
        AttributeType at = schemaManager.getAttributeType( attributeType );

        if ( at != null )
        {
            if ( at.getSyntax().isHumanReadable() )
            {
                if ( attributeValue == null )
                {
                    attributeValue = "";
                }
                else if ( attributeValue instanceof byte[] )
                {
                    attributeValue = Strings.utf8ToString( ( byte[] ) attributeValue );
                }
            }
            else
            {
                if ( attributeValue instanceof String )
                {
                    attributeValue = Strings.getBytesUtf8( ( String ) attributeValue );
                }
            }
        }
    }

    // Update the entry
    try
    {
        entry.addAttribute( attributeType, attributeValue );
    }
    catch ( Exception e )
    {
        // The attribute does not exist already, create a fake one 
        if ( ( schemaManager != null ) && schemaManager.isRelaxed() )
        {
            AttributeType newAttributeType = new AttributeType( "1.3.6.1.4.1.18060.0.9999." + oidCounter++ );
            newAttributeType.setNames( attributeType );
            newAttributeType.setSyntax( schemaManager.getLdapSyntaxRegistry().get( SchemaConstants.DIRECTORY_STRING_SYNTAX ) );
            schemaManager.add( newAttributeType );
            entry.addAttribute( attributeType, attributeValue );
        }
    }
}
 
Example 4
Source File: EntryUtils.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
static AttributeType getIA5StringAttributeType()
{
    AttributeType attributeType = new AttributeType( "1.1" );
    attributeType.addName( "1.1" );
    LdapSyntax syntax = new LdapSyntax( "1.1.1", "", true );

    syntax.setSyntaxChecker( new SyntaxChecker( "1.1.2" )
    {
        public static final long serialVersionUID = 1L;


        public boolean isValidSyntax( Object value )
        {
            String strValue = Strings.deepTrim( ( String ) value );
            return ( strValue == null ) || ( strValue.length() < 7 );
        }
    } );

    MatchingRule matchingRule = new MatchingRule( "1.1.2" );
    matchingRule.setSyntax( syntax );

    matchingRule.setLdapComparator( new LdapComparator<String>( matchingRule.getOid() )
    {
        public static final long serialVersionUID = 1L;


        public int compare( String o1, String o2 )
        {
            return ( ( o1 == null ) ?
                ( o2 == null ? 0 : -1 ) :
                ( o2 == null ? 1 : o1.compareTo( o2 ) ) );
        }
    } );

    matchingRule.setNormalizer( new DeepTrimToLowerNormalizer( matchingRule.getOid() ) );

    attributeType.setEquality( matchingRule );
    attributeType.setSyntax( syntax );

    return attributeType;
}