private String internFormat()

in plugins/aciitemeditor/src/main/java/org/apache/directory/studio/aciitemeditor/sourceeditor/ACIFormattingStrategy.java [89:249]


    private String internFormat( String content )
    {
        StringBuilder sb = new StringBuilder();

        // flag to track if a new line was started
        boolean newLineStarted = true;

        // flag to track if we are within a quoted string
        boolean inQuotedString = false;

        // flag to track if the current expression is appended in one-line mode
        boolean oneLineMode = false;

        // the current indent
        int indent = 0;

        int contentLength = content.length();
        
        for ( int i = 0; i < contentLength; i++ )
        {
            char c = content.charAt( i );

            // track quotes
            if ( c == '"' )
            {
                inQuotedString ^= true;
            }

            if ( ( c == '{' ) && !inQuotedString )
            {
                // check one-line mode
                oneLineMode = checkInOneLine( i, content );

                if ( oneLineMode )
                {
                    // no new line in one-line mode
                    sb.append( c );
                    newLineStarted = false;
                }
                else
                {
                    // start a new line, but avoid blank lines if there are multiple opened curlies
                    if ( !newLineStarted )
                    {
                        sb.append( NEWLINE );
                        for ( int x = 0; x < indent; x++ )
                        {
                            sb.append( INDENT_STRING );
                        }
                    }

                    // append the curly
                    sb.append( c );

                    // start a new line and increment indent
                    sb.append( NEWLINE );
                    newLineStarted = true;
                    indent++;
                    
                    for ( int x = 0; x < indent; x++ )
                    {
                        sb.append( INDENT_STRING );
                    }
                }
            }
            else if ( ( c == '}' ) && !inQuotedString )
            {
                if ( oneLineMode )
                {
                    // no new line in one-line mode
                    sb.append( c );
                    newLineStarted = false;

                    // closed curly indicates end of one-line mode
                    oneLineMode = false;
                }
                else
                {
                    // decrement indent
                    indent--;

                    // start a new line, but avoid blank lines if there are multiple closed curlies
                    if ( newLineStarted )
                    {
                        // delete one indent 
                        sb.delete( sb.length() - INDENT_STRING.length(), sb.length() );
                    }
                    else
                    {
                        sb.append( NEWLINE );
                        
                        for ( int x = 0; x < indent; x++ )
                        {
                            sb.append( INDENT_STRING );
                        }
                    }

                    // append the curly
                    sb.append( c );

                    // start a new line 
                    sb.append( NEWLINE );
                    newLineStarted = true;
                    
                    for ( int x = 0; x < indent; x++ )
                    {
                        sb.append( INDENT_STRING );
                    }
                }
            }
            else if ( ( c == ',' ) && !inQuotedString )
            {
                // start new line on comma
                if ( oneLineMode )
                {
                    sb.append( c );
                    newLineStarted = false;
                }
                else
                {
                    sb.append( c );

                    sb.append( NEWLINE );
                    newLineStarted = true;

                    for ( int x = 0; x < indent; x++ )
                    {
                        sb.append( INDENT_STRING );
                    }
                }
            }
            else if ( Character.isWhitespace( c ) )
            {
                char c1 = 'A';
                
                if ( i + 1 < contentLength )
                {
                    c1 = content.charAt( i + 1 );
                }

                if ( ( !newLineStarted ) &&
                     // ignore space after starting a new line
                     ( c != '\n' ) && ( c != '\r' ) &&
                     // ignore new lines
                     !Character.isWhitespace( c1 ) && ( c1 != '\n' ) && ( c1 != '\r' ) )
                     // compress whitespaces
                {
                    sb.append( c );
                }
            }

            else
            {
                // default case: append the char
                sb.append( c );
                newLineStarted = false;
            }
        }

        return sb.toString();
    }