public String toSetSourceString()

in src/main/java/org/apache/commons/ognl/ASTChain.java [382:482]


    public String toSetSourceString( OgnlContext context, Object target )
    {
        String prevChain = (String) context.get( "_currentChain" );
        String prevChild = (String) context.get( "_lastChild" );

        if ( prevChain != null )
        {
            throw new UnsupportedCompilationException( "Can't compile nested chain expressions." );
        }

        if ( target != null )
        {
            context.setCurrentObject( target );
            context.setCurrentType( target.getClass() );
        }

        String result = "";
        NodeType lastType = null;
        boolean constructor = false;
        try
        {
            if ( ( children != null ) && ( children.length > 0 ) )
            {
                if (children[0] instanceof ASTConst)
                {
                    throw new UnsupportedCompilationException( "Can't modify constant values." );
                }

                for ( int i = 0; i < children.length; i++ )
                {
                    // System.out.println("astchain setsource child[" + i + "] : " + _children[i].getClass().getName());

                    if ( i == ( children.length - 1 ) )
                    {
                        context.put( "_lastChild", "true" );
                    }

                    String value = children[i].toSetSourceString( context, context.getCurrentObject() );
                    // if (value == null || value.trim().isEmpty())
                    // return "";

                    // System.out.println("astchain setter child returned >>  " + value + "  <<");

                    if (children[i] instanceof ASTCtor)
                    {
                        constructor = true;
                    }

                    if ( children[i] instanceof NodeType
                        && ( (NodeType) children[i] ).getGetterClass() != null )
                    {
                        lastType = (NodeType) children[i];
                    }

                    if ( !(children[i] instanceof ASTVarRef)
                        && !constructor
                        && !( children[i] instanceof OrderedReturn
                        && ( (OrderedReturn) children[i] ).getLastExpression() != null )
                        && ( parent == null || !(parent instanceof ASTSequence)) )
                    {
                        value = OgnlRuntime.getCompiler( context ).castExpression( context, children[i], value );
                    }

                    // System.out.println("astchain setter after cast value is: " + value);

                    /*
                     * if (!constructor && !OrderedReturn.class.isInstance(_children[i]) && (_parent == null ||
                     * !ASTSequence.class.isInstance(_parent))) { value =
                     * OgnlRuntime.getCompiler().castExpression(context, _children[i], value); }
                     */

                    if ( children[i] instanceof ASTOr || children[i] instanceof ASTAnd
                        || children[i] instanceof ASTCtor || children[i] instanceof ASTStaticField)
                    {
                        context.put( "_noRoot", "true" );
                        result = value;
                    }
                    else
                    {
                        result += value;
                    }

                    context.put( "_currentChain", result );
                }
            }
        }
        catch ( Throwable t )
        {
            throw OgnlOps.castToRuntime( t );
        }

        context.put( "_lastChild", prevChild );
        context.put( "_currentChain", prevChain );

        if ( lastType != null )
        {
            setterClass = lastType.getSetterClass();
        }

        return result;
    }