Exemplo n.º 1
0
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            object result = source;

            for (int i = 0, ilast = children.Length - 1; i <= ilast; ++i)
            {
                bool handled = false;

                if (i < ilast)
                {
                    if (children[i] is ASTProperty)
                    {
                        ASTProperty propertyNode = (ASTProperty)children[i];
                        int         indexType    = propertyNode.getIndexedPropertyType(context, result);

                        if ((indexType != OgnlRuntime.INDEXED_PROPERTY_NONE) && (children[i + 1] is ASTProperty))
                        {
                            ASTProperty indexNode = (ASTProperty)children[i + 1];

                            if (indexNode.isIndexedAccess())
                            {
                                object index = indexNode.getProperty(context, result);

                                if (index is DynamicSubscript)
                                {
                                    if (indexType == OgnlRuntime.INDEXED_PROPERTY_INT)
                                    {
                                        object array = propertyNode.getValue(context, result);
                                        int    len   = ((Array)array).Length;

                                        switch (((DynamicSubscript)index).getFlag())
                                        {
                                        case DynamicSubscript.ALL:
                                            result = Array.CreateInstance(array.GetType().GetElementType(), len);
                                            Array.Copy((Array)array, 0, (Array)result, 0, len);
                                            handled = true;
                                            i++;
                                            break;

                                        case DynamicSubscript.FIRST:
                                            index = ((len > 0) ? 0 : -1);
                                            break;

                                        case DynamicSubscript.MID:
                                            index = ((len > 0) ? (len / 2) : -1);
                                            break;

                                        case DynamicSubscript.LAST:
                                            index = ((len > 0) ? (len - 1) : -1);
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        if (indexType == OgnlRuntime.INDEXED_PROPERTY_OBJECT)
                                        {
                                            throw new OgnlException("DynamicSubscript '" + indexNode + "' not allowed for object indexed property '" + propertyNode + "'");
                                        }
                                    }
                                }
                                if (!handled)
                                {
                                    result  = OgnlRuntime.getIndexedProperty(context, result, propertyNode.getProperty(context, result).ToString(), index);
                                    handled = true;
                                    i++;
                                }
                            }
                        }
                    }
                }
                if (!handled)
                {
                    result = children[i].getValue(context, result);
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        protected override void setValueBody(OgnlContext context, object target, object value) // throws OgnlException
        {
            bool handled = false;

            for (int i = 0, ilast = children.Length - 2; i <= ilast; ++i)
            {
                if (i == ilast)
                {
                    if (children[i] is ASTProperty)
                    {
                        ASTProperty propertyNode = (ASTProperty)children[i];
                        int         indexType    = propertyNode.getIndexedPropertyType(context, target);

                        if ((indexType != OgnlRuntime.INDEXED_PROPERTY_NONE) && (children[i + 1] is ASTProperty))
                        {
                            ASTProperty indexNode = (ASTProperty)children[i + 1];

                            if (indexNode.isIndexedAccess())
                            {
                                object index = indexNode.getProperty(context, target);

                                if (index is DynamicSubscript)
                                {
                                    if (indexType == OgnlRuntime.INDEXED_PROPERTY_INT)
                                    {
                                        object array = propertyNode.getValue(context, target);
                                        int    len   = ((Array)array).Length;

                                        switch (((DynamicSubscript)index).getFlag())
                                        {
                                        case DynamicSubscript.ALL:
                                            Array.Copy((Array)target, 0, (Array)value, 0, len);
                                            handled = true;
                                            i++;
                                            break;

                                        case DynamicSubscript.FIRST:
                                            index = ((len > 0) ? 0 : -1);
                                            break;

                                        case DynamicSubscript.MID:
                                            index = ((len > 0) ? (len / 2) : -1);
                                            break;

                                        case DynamicSubscript.LAST:
                                            index = ((len > 0) ? (len - 1) : -1);
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        if (indexType == OgnlRuntime.INDEXED_PROPERTY_OBJECT)
                                        {
                                            throw new OgnlException("DynamicSubscript '" + indexNode + "' not allowed for object indexed property '" + propertyNode + "'");
                                        }
                                    }
                                }
                                if (!handled)
                                {
                                    OgnlRuntime.setIndexedProperty(context, target, propertyNode.getProperty(context, target).ToString(), index, value);
                                    handled = true;
                                    i++;
                                }
                            }
                        }
                    }
                }
                if (!handled)
                {
                    target = children[i].getValue(context, target);
                }
            }
            if (!handled)
            {
                children[children.Length - 1].setValue(context, target, value);
            }
        }