public boolean isValidSyntax()

in ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/TeletexTerminalIdentifierSyntaxChecker.java [102:301]


    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();
        }

        if ( strValue.length() == 0 )
        {
            if ( LOG.isDebugEnabled() )
            {
                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
            }
            
            return false;
        }

        // Search for the first '$' separator
        int dollar = strValue.indexOf( '$' );

        String terminalIdentifier = ( dollar == -1 ) ? strValue : strValue.substring( 0, dollar );

        if ( terminalIdentifier.length() == 0 )
        {
            // It should not be null
            if ( LOG.isDebugEnabled() )
            {
                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
            }
            
            return false;
        }

        if ( !Strings.isPrintableString( terminalIdentifier ) )
        {
            // It's not a valid PrintableString 
            if ( LOG.isDebugEnabled() )
            {
                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
            }
            
            return false;
        }

        if ( dollar == -1 )
        {
            // No ttx-param : let's get out
            if ( LOG.isDebugEnabled() )
            {
                LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
            }
            
            return true;
        }

        // Ok, now let's deal with optional ttx-params
        String[] ttxParams = strValue.substring( dollar + 1 ).split( "\\$" );

        if ( ttxParams.length == 0 )
        {
            if ( LOG.isDebugEnabled() )
            {
                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
            }
            
            return false;
        }

        for ( String ttxParam : ttxParams )
        {
            int colon = ttxParam.indexOf( ':' );

            if ( colon == -1 )
            {
                // we must have a ':' separator
                if ( LOG.isDebugEnabled() )
                {
                    LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
                }
                
                return false;
            }

            String key = ttxParam.substring( 0, colon );

            if ( key.startsWith( "graphic" )
                || key.startsWith( "control" )
                || key.startsWith( "misc" )
                || key.startsWith( "page" )
                || key.startsWith( "private" ) )
            {
                if ( colon + 1 == ttxParam.length() )
                {
                    if ( LOG.isDebugEnabled() )
                    {
                        LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
                    }
                    
                    return false;
                }

                boolean hasEsc = false;

                for ( byte b : Strings.getBytesUtf8( ttxParam ) )
                {
                    switch ( b )
                    {
                        case 0x24:
                            // '$' is not accepted
                            if ( LOG.isDebugEnabled() )
                            {
                                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
                            }
                            
                            return false;

                        case 0x5c:
                            if ( hasEsc )
                            {
                                // two following \ are not accepted
                                if ( LOG.isDebugEnabled() )
                                {
                                    LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
                                }
                                
                                return false;
                            }
                            else
                            {
                                hasEsc = true;
                            }

                            continue;

                        case '2':
                            continue;

                        case '4':
                            // We have found a "\24"
                            hasEsc = false;
                            continue;

                        case '5':
                            continue;

                        case 'c':
                        case 'C':
                            // We have found a "\5c" or a "\5C"
                            hasEsc = false;
                            continue;

                        default:
                            if ( hasEsc )
                            {
                                // A \ should be followed by "24" or "5c" or "5C"
                                return false;
                            }

                            continue;
                    }
                }
            }
            else
            {
                if ( LOG.isDebugEnabled() )
                {
                    LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
                }
                
                return false;
            }
        }

        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
        }
        
        return true;
    }