private String recursiveExpand()

in expression-evaluator/src/main/java/org/apache/archiva/components/evaluator/DefaultExpressionEvaluator.java [56:120]


    private String recursiveExpand( String str, List<String> seenExpressions )
        throws EvaluatorException
    {
        if ( StringUtils.isEmpty( str ) )
        {
            // Empty string. Fail fast.
            return str;
        }

        if ( str.indexOf( "${" ) < 0 )
        {
            // Contains no potential expressions.  Fail fast.
            return str;
        }

        if ( this.expressionSources.isEmpty( ) )
        {
            throw new EvaluatorException( "Unable to expand expressions with empty ExpressionSource list." );
        }

        Pattern pat = Pattern.compile( "(?<=[^$]|^)(\\$\\{[^}]*\\})" );
        Matcher mat = pat.matcher( str );
        int offset = 0;
        String expression;
        String value;
        StringBuilder expanded = new StringBuilder( );

        while ( mat.find( offset ) )
        {
            expression = mat.group( 1 );

            if ( seenExpressions.contains( expression ) )
            {
                throw new EvaluatorException( "A recursive cycle has been detected with expression " + expression + "." );
            }

            seenExpressions.add( expression );

            expanded.append( str.substring( offset, mat.start( 1 ) ) );
            value = findValue( expression );
            if ( value != null )
            {
                String resolvedValue = recursiveExpand( value, seenExpressions );
                expanded.append( resolvedValue );
            }
            else
            {
                expanded.append( expression );
            }
            offset = mat.end( 1 );
        }

        expanded.append( str.substring( offset ) );

        if ( expanded.indexOf( "$$" ) >= 0 )
        {
            // Special case for escaped content.
            return expanded.toString( ).replaceAll( "\\$\\$", "\\$" );
        }
        else
        {
            // return expanded
            return expanded.toString( );
        }
    }