Example #1
0
        ///<summary>
        ///Appends the standard naming context for evaluating an OGNL expression
        ///into the context given so that cached maps can be used as a context.
        ///</summary>
        ///<param name="root"> the root of the object graph</param>
        ///<param name="context"> the context to which OGNL context will be added.</param>
        ///<returns>
        ///a new IDictionary with the keys <c>root</c> and <c>context</c>
        ///set appropriately
        ///</returns>
        public static IDictionary addDefaultContext(object root, ClassResolver classResolver, TypeConverter converter, MemberAccess memberAccess, IDictionary context)
        {
            OgnlContext result;

            if (!(context is OgnlContext))
            {
                result = new OgnlContext();
                result.setValues(context);
            }
            else
            {
                result = (OgnlContext)context;
            }
            if (classResolver != null)
            {
                result.setClassResolver(classResolver);
            }
            if (converter != null)
            {
                result.setTypeConverter(converter);
            }
            if (memberAccess != null)
            {
                result.setMemberAccess(memberAccess);
            }
            result.setRoot(root);
            return(result);
        }
Example #2
0
        ///
        ///Evaluates the given OGNL expression tree to insert a value into the object graph
        ///rooted at the given root object.  The default context is set for the given
        ///context and root via <CODE>addDefaultContext()</CODE>.
        ///
        ///@param tree the OGNL expression tree to evaluate, as returned by parseExpression()
        ///@param context the naming context for the evaluation
        ///@param root the root object for the OGNL expression
        ///@param value the value to insert into the object graph
        ///@// throws MethodFailedException if the expression called a method which failed
        ///@// throws NoSuchPropertyException if the expression referred to a nonexistent property
        ///@// throws InappropriateExpressionException if the expression can't be used in this context
        ///@// throws OgnlException if there is a pathological environmental problem
        ///
        public static void setValue(object tree, IDictionary context, object root, object value)          // throws OgnlException
        {
            OgnlContext ognlContext = (OgnlContext)addDefaultContext(root, context);
            Node        n           = (Node)tree;

            n.setValue(ognlContext, root, value);
        }
Example #3
0
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            IDictionary answer;

            if (className == null)
            {
                try {
                    answer = (IDictionary)DEFAULT_MAP_CLASS.GetConstructor(new Type[0]).Invoke(new object[0]);
                } catch (Exception ex) {
                    /* This should never happen */
                    throw new OgnlException("Default IDictionary class '" + DEFAULT_MAP_CLASS.Name + "' instantiation error", ex);
                }
            }
            else
            {
                try {
                    answer = (IDictionary)OgnlRuntime.classForName(context, className).GetConstructor(new Type[0]).Invoke(new object[0]);
                } catch (Exception ex) {
                    throw new OgnlException("IDictionary implementor '" + className + "' not found", ex);
                }
            }

            for (int i = 0; i < jjtGetNumChildren(); ++i)
            {
                ASTKeyValue kv = (ASTKeyValue)children[i];
                Node        k  = kv.getKey(),
                            v  = kv.getValue();

                answer.Add(k.getValue(context, source), (v == null) ? null : v.getValue(context, source));
            }
            return(answer);
        }
Example #4
0
 public object[] getValues(OgnlContext context, object source)
 {
     object [] result = new object[children.Length];
     for (int i = 0; i < children.Length; ++i)
     {
         result [i] = children[i].getValue(context, source);
     }
     return(result);
 }
Example #5
0
        public override bool isNodeConstant(OgnlContext context) // throws OgnlException
        {
            bool      result = false;
            Exception reason = null;

            try {
                Type c = OgnlRuntime.classForName(context, className);

                /*
                 *  Check for virtual static field "class"; this cannot interfere with
                 *  normal static fields because it is a reserved word.  It is considered
                 *  constant.
                 */
                if (fieldName.Equals("class"))
                {
                    result = true;
                }
                else
                {
                    FieldInfo f = c.GetField(fieldName);
                    if (f == null)
                    {
                        // try to load Property
                        PropertyInfo p = c.GetProperty(fieldName);
                        if (p == null)
                        {
                            throw new MissingFieldException("Field or Property " + fieldName + " of class " + className + " is not found.");
                        }
                        else
                        if (!p.GetAccessors() [0].IsStatic)
                        {
                            throw new MissingFieldException("Property " + fieldName + " of class " + className + " is not static.");
                        }
                        // Property can't be constant.
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    if (!f.IsStatic)
                    {
                        throw new OgnlException("Field " + fieldName + " of class " + className + " is not static");
                    }

                    result = f.IsLiteral;
                }
            }   catch (TypeLoadException e)    { reason = e; }
            catch (MissingFieldException e)      { reason = e; }
            catch (SecurityException e)         { reason = e; }

            if (reason != null)
            {
                throw new OgnlException("Could not get static field " + fieldName + " from class " + className, reason);
            }
            return(result);
        }
Example #6
0
        protected override void setValueBody(OgnlContext context, object target, object value) // throws OgnlException
        {
            int last = children.Length - 1;

            for (int i = 0; i < last; ++i)
            {
                children[i].getValue(context, target);
            }
            children[last].setValue(context, target, value);
        }
Example #7
0
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            object result = null;

            for (int i = 0; i < children.Length; ++i)
            {
                result = children[i].getValue(context, source);
            }
            return(result); // The result is just the last one we saw.
        }
Example #8
0
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            IList answer = new ArrayList(jjtGetNumChildren());

            for (int i = 0; i < jjtGetNumChildren(); ++i)
            {
                answer.Add(children[i].getValue(context, source));
            }
            return(answer);
        }
Example #9
0
 public bool hasGetProperty(OgnlContext context, object target, object oname)          // throws OgnlException
 {
     try
     {
         return(OgnlRuntime.hasGetProperty(context, target, oname));
     }
     catch (/*Introspection*/ Exception ex)
     {
         throw new OgnlException("checking if " + target + " has gettable property " + oname, ex);
     }
 }
Example #10
0
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            Node              expr             = children[0];
            IList             answer           = new ArrayList();
            IElementsAccessor elementsAccessor = OgnlRuntime.getElementsAccessor(OgnlRuntime.getTargetClass(source));

            for (IEnumerator e = elementsAccessor.getElements(source); e.MoveNext();)
            {
                answer.Add(expr.getValue(context, e.Current));
            }
            return(answer);
        }
Example #11
0
        ///<summary>
        ///Evaluates the given OGNL expression tree to extract a value from the given root
        ///object. The default context is set for the given context and root via
        ///<c>addDefaultContext()</c>.
        ///</summary>
        ///<param name="tree"> the OGNL expression tree to evaluate, as returned by parseExpression()</param>
        ///<param name="context"> the naming context for the evaluation</param>
        ///<param name="root"> the root object for the OGNL expression</param>
        ///<param name="resultType"></param>
        ///<returns>the result of evaluating the expression</returns>
        ///<exception cref="MethodFailedException"> if the expression called a method which failed</exception>
        ///<exception cref="NoSuchPropertyException"> if the expression referred to a nonexistent property</exception>
        ///<exception cref="InappropriateExpressionException"> if the expression can't be used in this context</exception>
        ///<exception cref="OgnlException"> if there is a pathological environmental problem</
        ///
        public static object getValue(object tree, IDictionary context, object root, Type resultType)          // throws OgnlException
        {
            object      result;
            OgnlContext ognlContext = (OgnlContext)addDefaultContext(root, context);

            result = ((Node)tree).getValue(ognlContext, root);
            if (resultType != null)
            {
                result = getTypeConverter(context).convertValue(context, root, null, null, result, resultType);
            }
            return(result);
        }
Example #12
0
        /**
         *  Returns true if this property is described by an IndexedPropertyDescriptor
         *  and that if followed by an index specifier it will call the index get/set
         *  methods rather than go through property accessors.
         */
        public int getIndexedPropertyType(OgnlContext context, object source) // throws OgnlException
        {
            if (!isIndexedAccess())
            {
                object property = getProperty(context, source);

                if (property is string)
                {
                    return(OgnlRuntime.getIndexedPropertyType(context, (source == null) ? null : source.GetType(), (string)property));
                }
            }
            return(OgnlRuntime.INDEXED_PROPERTY_NONE);
        }
Example #13
0
        protected override void setValueBody(OgnlContext context, object target, object value) // throws OgnlException
        {
            if (indexedAccess && children [0] is ASTSequence)
            {
                // As property [index1, index2]...
                // Use Indexer.
                object [] indexParameters = ((ASTSequence)children [0]).getValues(context, context.getRoot());

                /*IndexerAccessor.setIndexerValue (target, value ,indexParameters) ;*/
                OgnlRuntime.setIndxerValue(context, target, "Indexer", value, indexParameters);
                return;
            }

            OgnlRuntime.setProperty(context, target, getProperty(context, target), value);
        }
Example #14
0
        protected object evaluateGetValueBody(OgnlContext context, object source)
        {
            object result;

            context.setCurrentObject(source);
            context.setCurrentNode(this);
            if (!constantValueCalculated)
            {
                constantValueCalculated = true;
                hasConstantValue        = isConstant(context);
                if (hasConstantValue)
                {
                    constantValue = getValueBody(context, source);
                }
            }
            return(hasConstantValue ? constantValue : getValueBody(context, source));
        }
Example #15
0
        public override bool isConstant(OgnlContext context)
        {
            bool result = isNodeConstant(context);

            if ((children != null) && (children.Length > 0))
            {
                result = true;
                for (int i = 0; result && (i < children.Length); ++i)
                {
                    if (children[i] is SimpleNode)
                    {
                        result = ((SimpleNode)children[i]).isConstant(context);
                    }
                    else
                    {
                        result = false;
                    }
                }
            }
            return(result);
        }
Example #16
0
        public override bool isSimpleNavigationChain(OgnlContext context) // throws OgnlException
        {
            bool result = false;

            if ((children != null) && (children.Length > 0))
            {
                result = true;
                for (int i = 0; result && (i < children.Length); i++)
                {
                    if (children[i] is SimpleNode)
                    {
                        result = ((SimpleNode)children[i]).isSimpleProperty(context);
                    }
                    else
                    {
                        result = false;
                    }
                }
            }
            return(result);
        }
Example #17
0
        ///<summary>
        /// Returns OgnlRuntime.NotFound if the property does not exist.
        ///</summary>
        public object setPossibleProperty(IDictionary context, object target, string name, object value)          // throws OgnlException
        {
            object      result      = null;
            OgnlContext ognlContext = (OgnlContext)context;

            try
            {
                if (!OgnlRuntime.setMethodValue(ognlContext, target, name, value, true))
                {
                    result = OgnlRuntime.setFieldValue(ognlContext, target, name, value) ? null : OgnlRuntime.NotFound;
                }
            }
            catch (OgnlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new OgnlException(name, ex);
            }
            return(result);
        }
Example #18
0
        public object getValue(OgnlContext context, object source)
        {
            if (context.getTraceEvaluations())
            {
                EvaluationPool pool          = OgnlRuntime.getEvaluationPool();
                object         result        = null;
                Exception      evalException = null;
                Evaluation     evaluation    = pool.create(this, source);

                context.pushEvaluation(evaluation);
                try {
                    result = evaluateGetValueBody(context, source);
                } catch (OgnlException ex) {
                    evalException = ex;
                    throw ex;
                } catch (Exception ex) {
                    evalException = ex;
                    throw ex;
                } finally {
                    Evaluation eval = context.popEvaluation();

                    eval.setResult(result);
                    if (evalException != null)
                    {
                        eval.setException(evalException);
                    }
                    if ((evalException == null) && (context.getRootEvaluation() == null) && !context.getKeepLastEvaluation())
                    {
                        pool.recycleAll(eval);
                    }
                }
                return(result);
            }
            else
            {
                return(evaluateGetValueBody(context, source));
            }
        }
Example #19
0
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            if (indexedAccess && children [0] is ASTSequence)
            {
                // As property [index1, index2]...
                // Use Indexer.
                object [] indexParameters = ((ASTSequence)children [0]).getValues(context, context.getRoot());

                /* return IndexerAccessor.getIndexerValue (source, indexParameters) ; */
                return(OgnlRuntime.getIndxerValue(context, source, "Indexer", indexParameters));
            }
            object result;

            object property = getProperty(context, source);
            Node   indexSibling;

            result = OgnlRuntime.getProperty(context, source, property);
            if (result == null)
            {
                result = OgnlRuntime.getNullHandler(OgnlRuntime.getTargetClass(source)).nullPropertyValue(context, source, property);
            }
            return(result);
        }
Example #20
0
        public void setValue(OgnlContext context, object target, object value)
        {
            if (context.getTraceEvaluations())
            {
                EvaluationPool pool          = OgnlRuntime.getEvaluationPool();
                Exception      evalException = null;
                Evaluation     evaluation    = pool.create(this, target, true);

                context.pushEvaluation(evaluation);
                try {
                    evaluateSetValueBody(context, target, value);
                } catch (OgnlException ex) {
                    evalException = ex;
                    ex.setEvaluation(evaluation);
                    throw ex;
                } catch (Exception ex) {
                    evalException = ex;
                    throw ex;
                } finally {
                    Evaluation eval = context.popEvaluation();

                    if (evalException != null)
                    {
                        eval.setException(evalException);
                    }
                    if ((evalException == null) && (context.getRootEvaluation() == null) && !context.getKeepLastEvaluation())
                    {
                        pool.recycleAll(eval);
                    }
                }
            }
            else
            {
                evaluateSetValueBody(context, target, value);
            }
        }
Example #21
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);
        }
Example #22
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);
            }
        }
Example #23
0
 public virtual bool isSimpleProperty(OgnlContext context)
 {
     return(isNodeSimpleProperty(context));
 }
Example #24
0
 public object getProperty(OgnlContext context, object source)   // throws OgnlException
 {
     return(children[0].getValue(context, context.getRoot()));
 }
Example #25
0
 public override bool isNodeSimpleProperty(OgnlContext context) // throws OgnlException
 {
     return((children != null) && (children.Length == 1) && ((SimpleNode)children[0]).isConstant(context));
 }
Example #26
0
 /**
  *  Returns true iff this node is constant without respect to the children.
  */
 public virtual bool isNodeConstant(OgnlContext context)
 {
     return(false);
 }
Example #27
0
 public virtual bool isConstant(OgnlContext context)
 {
     return(isNodeConstant(context));
 }
Example #28
0
 protected override object getValueBody(OgnlContext context, object source)
 {
     return(this.value);
 }
Example #29
0
 public override bool isNodeConstant(OgnlContext context) // throws OgnlException
 {
     return(true);
 }
Example #30
0
 public virtual bool isSimpleNavigationChain(OgnlContext context)
 {
     return(isSimpleProperty(context));
 }