public static Oid fromString()

in asn1/api/src/main/java/org/apache/directory/api/asn1/util/Oid.java [1274:1503]


    public static Oid fromString( String oidString ) throws DecoderException
    {
        if ( ( oidString == null ) || oidString.isEmpty() )
        {
            throw new DecoderException( I18n.err( I18n.ERR_00003_INVALID_OID, "empty" ) );
        }

        // Create a buffer that is wide enough to contain all the values
        byte[] buffer = new byte[oidString.length()];

        OidFSAState state = OidFSAState.START;
        
        // A counter of chars used for an arc. In 1.2.45345, this counter will be 5 for the '45345' arc.
        int arcNbChars = 0;
        
        // The position in the buffer where we accumulate the result. 
        int bufPos = 0;
        
        // The position in the OID string where we started to read an arc
        int startArc = 0;
        
        // The number of bytes in the resulting OID byte[]
        int nbBytes;
        
        for ( int i = 0; i < oidString.length(); i++ )
        {
            switch ( state )
            {
                case START :
                    // (Start) --['0'..'1']--> (A)
                    // (start) --['2']--> (F)
                    state = processStateStart( oidString, buffer, i );
                    break;
                    
                case STATE_A :
                    // (A) --['.']--> (B)
                    state = processStateA( oidString, i );

                    
                    break;
                    
                case STATE_B :
                    // (B) --['0']--> (D)
                    // (B) --['1'..'3']--> (C)
                    // (B) --['4'..'9']--> (E)
                    state = processStateB( oidString, buffer, i );
                    
                    break;
                    
                case STATE_C :
                    // (C) --['.']--> (K)
                    // (C) --['0'..'9']--> (E)
                    state = processStateC( oidString, buffer, i );

                    // the next arc will be store at position 1 in the buffer
                    bufPos = 1;

                    break;
                    
                case STATE_D :
                    // (D) --['.']--> (K)
                    // Fallthrough
                    
                case STATE_E :
                    // (E) --['.']--> (K)
                    state = processStateDE( oidString, buffer, i );
                    
                    // the next arc will be store at position 1 in teh buffer
                    bufPos = 1;

                    break;
                    
                case STATE_F :
                    // (F) --['.']--> (G)
                    state = processStateF( oidString, i );
                    
                    break;
                    
                case STATE_G :
                    // (G) --['0']--> (I)
                    // (G) --['1'..'9']--> (H)
                    state = processStateG( oidString, buffer, i );
                    arcNbChars = 1;
                    startArc = i;

                    break;

                case STATE_H :
                    // (H) --['.']--> (K)
                    // (H) --['0'..'9']--> (J)
                    state = processStateH( oidString, buffer, i );
                    
                    if ( state == OidFSAState.STATE_J )
                    {
                        // We have already two digits
                        arcNbChars = 2;
                        bufPos = 0;
                    }
                    
                    break;

                case STATE_I :
                    // (I) --['.']--> (K)
                    state = processStateI( oidString, buffer, i );
                    
                    // Set the arc position to buffer[1], we haven't yet accumulated digits.
                    bufPos = 1;
                    
                    break;

                case STATE_J :
                    // (J) --['.']--> (K)
                    // (J) --['0'..'9']--> (J)
                    state = processStateJ( oidString, buffer, arcNbChars + bufPos, i );
                    
                    if ( state == OidFSAState.STATE_J )
                    {
                        // We can increment the number of digit for this arc
                        arcNbChars++;
                    }
                    else
                    {
                        // We are done with the first arc : convert it
                        bufPos += convert( oidString, buffer, startArc, arcNbChars, bufPos, true );
                    }
                    
                    break;

                case STATE_K :
                    startArc = i;
                    state = processStateK( oidString, buffer, bufPos, i );
                    
                    if ( state == OidFSAState.STATE_M )
                    { 
                        bufPos++;
                    }
                    else
                    {
                        arcNbChars = 1;
                    }
                    
                    break;

                case STATE_L :
                    state = processStateL( oidString, buffer, arcNbChars + bufPos, i );
                    
                    if ( state == OidFSAState.STATE_L )
                    {
                        arcNbChars++;
                        break;
                    }
                    else
                    {
                        // We are done with the arc : convert it
                        bufPos += convert( oidString, buffer, startArc, arcNbChars, bufPos, false );
                    }

                    break;
                    
                case STATE_M :
                    state = processStateM( oidString, i );
                    break;
                    
                default :
                    // Exist to please checkstyle...
                    break;
            }
        }
        
        // End of the string : check that we are in a correct state for a completion
        // The only valid exit states are :
        // (C) --[]--> (End)
        // (D) --[]--> (End)
        // (E) --[]--> (End)
        // (H) --[]--> (End)
        // (I) --[]--> (End)
        // (J) --[]--> (End)
        // (L) --[]--> (End)
        // (M) --[]--> (End)
        switch ( state )
        {
            case STATE_C :
                // (C) --[]--> (End)
                // fallthrough
                
            case STATE_D :
                // (D) --[]--> (End)
                // fallthrough
                
            case STATE_E :
                // (E) --[]--> (End)
                // fallthrough

            case STATE_H :
                // (H) --[]--> (End)
                // fallthrough
                
            case STATE_I :
                // (I) --[]--> (End)
                byte[] bytes = new byte[1];
                bytes[0] = ( byte ) ( buffer[0] | buffer[1] );

                return new Oid( oidString, bytes );
                
            case STATE_J :
                // (J) --[]--> (End)
                nbBytes = convert( oidString, buffer, 2, arcNbChars, 0, true );
                bytes = new byte[nbBytes];
                System.arraycopy( buffer, 0, bytes, 0, nbBytes );
                
                return new Oid( oidString, bytes );

            case STATE_L :
                bufPos += convert( oidString, buffer, startArc, arcNbChars, bufPos, false );
                bytes = new byte[bufPos];
                System.arraycopy( buffer, 0, bytes, 0, bufPos );
                
                return new Oid( oidString, bytes );
                
            case STATE_M :
                bytes = new byte[bufPos];
                System.arraycopy( buffer, 0, bytes, 0, bufPos );
                
                return new Oid( oidString, bytes );
                
            default :
                // This should never happen...
                throw new DecoderException( I18n.err( I18n.ERR_00003_INVALID_OID, "Wrong OID" ) );
        }
    }