Пример #1
0
        public override System.Object eval(FormInstance model, EvaluationContext evalContext)
        {
            System.Object aval   = a.eval(model, evalContext);
            System.Object bval   = b.eval(model, evalContext);
            bool          result = false;

            //xpath spec says comparisons only defined for numbers (not defined for strings)
            aval = XPathFuncExpr.toNumeric(aval);
            bval = XPathFuncExpr.toNumeric(bval);

            double fa = ((System.Double)aval);
            double fb = ((System.Double)bval);

            switch (op)
            {
            case LT:  result = fa < fb; break;

            case GT:  result = fa > fb; break;

            case LTE:  result = fa <= fb; break;

            case GTE:  result = fa >= fb; break;
            }

            return(result);
        }
Пример #2
0
        public override System.Object eval(FormInstance model, EvaluationContext evalContext)
        {
            System.Object aval = XPathFuncExpr.unpack(a.eval(model, evalContext));
            System.Object bval = XPathFuncExpr.unpack(b.eval(model, evalContext));
            bool          eq   = false;

            if (aval is System.Boolean || bval is System.Boolean)
            {
                if (!(aval is System.Boolean))
                {
                    aval = XPathFuncExpr.toBoolean(aval);
                }
                else if (!(bval is System.Boolean))
                {
                    bval = XPathFuncExpr.toBoolean(bval);
                }

                bool ba = ((System.Boolean)aval);
                bool bb = ((System.Boolean)bval);
                eq = (ba == bb);
            }
            else if (aval is System.Double || bval is System.Double)
            {
                if (!(aval is System.Double))
                {
                    aval = XPathFuncExpr.toNumeric(aval);
                }
                else if (!(bval is System.Double))
                {
                    bval = XPathFuncExpr.toNumeric(bval);
                }

                double fa = ((System.Double)aval);
                double fb = ((System.Double)bval);
                eq = System.Math.Abs(fa - fb) < 1.0e-12;
            }
            else
            {
                aval = XPathFuncExpr.toString(aval);
                bval = XPathFuncExpr.toString(bval);
                eq   = (aval.Equals(bval));
            }

            return(equal?eq:!eq);
        }
Пример #3
0
        public override Object eval(FormInstance model, EvaluationContext evalContext)
        {
            Object  aval = XPathFuncExpr.unpack(a.eval(model, evalContext));
            Object  bval = XPathFuncExpr.unpack(b.eval(model, evalContext));
            Boolean eq   = false;

            if (aval is Boolean || bval is Boolean)
            {
                if (!(aval is Boolean))
                {
                    aval = XPathFuncExpr.toBoolean(aval);
                }
                else if (!(bval is Boolean))
                {
                    bval = XPathFuncExpr.toBoolean(bval);
                }

                Boolean ba = ((Boolean)aval);
                Boolean bb = ((Boolean)bval);
                eq = (ba == bb);
            }
            else if (aval is Double || bval is Double)
            {
                if (!(aval is Double))
                {
                    aval = XPathFuncExpr.toNumeric(aval);
                }
                else if (!(bval is Double))
                {
                    bval = XPathFuncExpr.toNumeric(bval);
                }

                double fa = ((Double)aval);
                double fb = ((Double)bval);
                eq = Math.Abs(fa - fb) < 1.0e-12;
            }
            else
            {
                aval = XPathFuncExpr.ToString(aval);
                bval = XPathFuncExpr.ToString(bval);
                eq   = (aval.Equals(bval));
            }

            return((Boolean)(equal ? eq : !eq));
        }
Пример #4
0
        public Boolean Equals(Object o)
        {
            if (o is XPathFuncExpr)
            {
                XPathFuncExpr x = (XPathFuncExpr)o;

                //Shortcuts for very easily comprable values
                if (!id.Equals(x.id) || args.Length != x.args.Length)
                {
                    return(false);
                }

                return(ExtUtil.arrayEquals(args, x.args));
            }
            else
            {
                return(false);
            }
        }
Пример #5
0
        public Object eval(FormInstance model, EvaluationContext evalContext)
        {
            double aval = XPathFuncExpr.toNumeric(a.eval(model, evalContext));
            double bval = XPathFuncExpr.toNumeric(b.eval(model, evalContext));

            double result = 0;

            switch (op)
            {
            case ADD: result = aval + bval; break;

            case SUBTRACT: result = aval - bval; break;

            case MULTIPLY: result = aval * bval; break;

            case DIVIDE: result = aval / bval; break;

            case MODULO: result = aval % bval; break;
            }
            return(result);
        }
Пример #6
0
        public override System.Object eval(FormInstance model, EvaluationContext evalContext)
        {
            bool aval = XPathFuncExpr.toBoolean(a.eval(model, evalContext));

            //short-circuiting
            if ((!aval && op == AND) || (aval && op == OR))
            {
                return(aval);
            }

            bool bval = XPathFuncExpr.toBoolean(b.eval(model, evalContext));

            bool result = false;

            switch (op)
            {
            case AND:  result = aval && bval; break;

            case OR:  result = aval || bval; break;
            }
            return(result);
        }
Пример #7
0
        /// <summary> translate an xpath path reference into a TreeReference
        /// TreeReferences only support a subset of true xpath paths; restrictions are:
        /// simple child name tests 'child::name', '.', and '..' allowed only
        /// no predicates
        /// all '..' steps must come before anything else
        /// </summary>
        public virtual TreeReference getReference(bool allowPredicates)
        {
            TreeReference ref_Renamed = new TreeReference();
            bool          parentsAllowed;

            switch (init_context)
            {
            case XPathPathExpr.INIT_CONTEXT_ROOT:
                ref_Renamed.RefLevel = TreeReference.REF_ABSOLUTE;
                parentsAllowed       = false;
                break;

            case XPathPathExpr.INIT_CONTEXT_RELATIVE:
                ref_Renamed.RefLevel = 0;
                parentsAllowed       = true;
                break;

            case XPathPathExpr.INIT_CONTEXT_EXPR:
                if (this.filtExpr.x != null && this.filtExpr.x is XPathFuncExpr)
                {
                    XPathFuncExpr func = (XPathFuncExpr)(this.filtExpr.x);
                    if (func.id.ToString().Equals("instance"))
                    {
                        ref_Renamed.RefLevel = TreeReference.REF_ABSOLUTE;                                 //i assume when refering the non main instance you have to be absolute
                        parentsAllowed       = false;
                        if (func.args.Length != 1)
                        {
                            throw new XPathUnsupportedException("instance() function used with " + func.args.Length + " arguements. Expecting 1 arguement");
                        }
                        if (!(func.args[0] is XPathStringLiteral))
                        {
                            throw new XPathUnsupportedException("instance() function expecting 1 string literal arguement arguement");
                        }
                        XPathStringLiteral strLit = (XPathStringLiteral)(func.args[0]);
                        //we've got a non-standard instance in play, watch out
                        if (strLit.s == null)
                        {
                            // absolute reference to the main instance
                            ref_Renamed.Context      = TreeReference.CONTEXT_ABSOLUTE;
                            ref_Renamed.InstanceName = null;
                        }
                        else
                        {
                            ref_Renamed.Context      = TreeReference.CONTEXT_INSTANCE;
                            ref_Renamed.InstanceName = strLit.s;
                        }
                    }
                    else
                    {
                        if (func.id.ToString().Equals("current"))
                        {
                            parentsAllowed      = true;
                            ref_Renamed.Context = TreeReference.CONTEXT_ORIGINAL;
                        }
                        else
                        {
                            //We only support expression root contexts for instance refs, everything else is an illegal filter
                            throw new XPathUnsupportedException("filter expression");
                        }
                    }
                }
                else
                {
                    //We only support expression root contexts for instance refs, everything else is an illegal filter
                    throw new XPathUnsupportedException("filter expression");
                }

                break;

            default:  throw new XPathUnsupportedException("filter expression");
            }
            for (int i = 0; i < steps.Length; i++)
            {
                XPathStep step = steps[i];
                if (step.axis == XPathStep.AXIS_SELF)
                {
                    if (step.test != XPathStep.TEST_TYPE_NODE)
                    {
                        throw new XPathUnsupportedException("step other than 'child::name', '.', '..'");
                    }
                }
                else if (step.axis == XPathStep.AXIS_PARENT)
                {
                    if (!parentsAllowed || step.test != XPathStep.TEST_TYPE_NODE)
                    {
                        throw new XPathUnsupportedException("step other than 'child::name', '.', '..'");
                    }
                    else
                    {
                        ref_Renamed.incrementRefLevel();
                    }
                }
                else if (step.axis == XPathStep.AXIS_ATTRIBUTE)
                {
                    if (step.test == XPathStep.TEST_NAME)
                    {
                        ref_Renamed.add(step.name.ToString(), TreeReference.INDEX_ATTRIBUTE);
                        parentsAllowed = false;
                        //TODO: Can you step back from an attribute, or should this always be
                        //the last step?
                    }
                    else
                    {
                        throw new XPathUnsupportedException("attribute step other than 'attribute::name");
                    }
                }
                else if (step.axis == XPathStep.AXIS_CHILD)
                {
                    if (step.test == XPathStep.TEST_NAME)
                    {
                        ref_Renamed.add(step.name.ToString(), TreeReference.INDEX_UNBOUND);
                        parentsAllowed = true;
                    }
                    else if (step.test == XPathStep.TEST_NAME_WILDCARD)
                    {
                        ref_Renamed.add(TreeReference.NAME_WILDCARD, TreeReference.INDEX_UNBOUND);
                        parentsAllowed = true;
                    }
                    else
                    {
                        throw new XPathUnsupportedException("step other than 'child::name', '.', '..'");
                    }
                }
                else
                {
                    throw new XPathUnsupportedException("step other than 'child::name', '.', '..'");
                }

                if (step.predicates.Length > 0)
                {
                    int refLevel = ref_Renamed.RefLevel;

                    List <XPathExpression> v = new List <XPathExpression>();
                    for (int j = 0; j < step.predicates.Length; j++)
                    {
                        v.addElement(step.predicates[j]);
                    }
                    ref_Renamed.addPredicate(i, v);
                }
            }
            return(ref_Renamed);
        }
Пример #8
0
        public override Object eval(FormInstance model, EvaluationContext evalContext)
        {
            double aval = XPathFuncExpr.toNumeric(a.eval(model, evalContext));

            return(-aval);
        }
Пример #9
0
        public void print(Object o)
        {
            indent += 1;

            if (o is XPathStringLiteral)
            {
                XPathStringLiteral x = (XPathStringLiteral)o;
                printStr("strlit {" + x.s + "}");
            }
            else if (o is XPathNumericLiteral)
            {
                XPathNumericLiteral x = (XPathNumericLiteral)o;
                printStr("numlit {" + x.d + "}");
            }
            else if (o is XPathVariableReference)
            {
                XPathVariableReference x = (XPathVariableReference)o;
                printStr("var {" + x.id.ToString() + "}");
            }
            else if (o is XPathArithExpr)
            {
                XPathArithExpr x  = (XPathArithExpr)o;
                String         op = null;
                switch (x.op)
                {
                case XPathArithExpr.ADD: op = "add"; break;

                case XPathArithExpr.SUBTRACT: op = "subtr"; break;

                case XPathArithExpr.MULTIPLY: op = "mult"; break;

                case XPathArithExpr.DIVIDE: op = "div"; break;

                case XPathArithExpr.MODULO: op = "mod"; break;
                }
                printStr(op + " {{");
                print(x.a);
                printStr(" } {");
                print(x.b);
                printStr("}}");
            }
            else if (o is XPathBoolExpr)
            {
                XPathBoolExpr x  = (XPathBoolExpr)o;
                String        op = null;
                switch (x.op)
                {
                case XPathBoolExpr.AND: op = "and"; break;

                case XPathBoolExpr.OR: op = "or"; break;
                }
                printStr(op + " {{");
                print(x.a);
                printStr(" } {");
                print(x.b);
                printStr("}}");
            }
            else if (o is XPathCmpExpr)
            {
                XPathCmpExpr x  = (XPathCmpExpr)o;
                String       op = null;
                switch (x.op)
                {
                case XPathCmpExpr.LT: op = "lt"; break;

                case XPathCmpExpr.LTE: op = "lte"; break;

                case XPathCmpExpr.GT: op = "gt"; break;

                case XPathCmpExpr.GTE: op = "gte"; break;
                }
                printStr(op + " {{");
                print(x.a);
                printStr(" } {");
                print(x.b);
                printStr("}}");
            }
            else if (o is XPathEqExpr)
            {
                XPathEqExpr x  = (XPathEqExpr)o;
                String      op = x.equal ? "eq" : "neq";
                printStr(op + " {{");
                print(x.a);
                printStr(" } {");
                print(x.b);
                printStr("}}");
            }
            else if (o is XPathUnionExpr)
            {
                XPathUnionExpr x = (XPathUnionExpr)o;
                printStr("union {{");
                print(x.a);
                printStr(" } {");
                print(x.b);
                printStr("}}");
            }
            else if (o is XPathNumNegExpr)
            {
                XPathNumNegExpr x = (XPathNumNegExpr)o;
                printStr("neg {");
                print(x.a);
                printStr("}");
            }
            else if (o is XPathFuncExpr)
            {
                XPathFuncExpr x = (XPathFuncExpr)o;
                if (x.args.Length == 0)
                {
                    printStr("func {" + x.id.ToString() + ", args {none}}");
                }
                else
                {
                    printStr("func {" + x.id.ToString() + ", args {{");
                    for (int i = 0; i < x.args.Length; i++)
                    {
                        print(x.args[i]);
                        if (i < x.args.Length - 1)
                        {
                            printStr(" } {");
                        }
                    }
                    printStr("}}}");
                }
            }
            else if (o is XPathPathExpr)
            {
                XPathPathExpr x    = (XPathPathExpr)o;
                String        init = null;

                switch (x.init_context)
                {
                case XPathPathExpr.INIT_CONTEXT_ROOT: init = "root"; break;

                case XPathPathExpr.INIT_CONTEXT_RELATIVE: init = "relative"; break;

                case XPathPathExpr.INIT_CONTEXT_EXPR: init = "expr"; break;
                }

                printStr("path {init-context:" + init + ",");

                if (x.init_context == XPathPathExpr.INIT_CONTEXT_EXPR)
                {
                    printStr(" init-expr:{");
                    print(x.filtExpr);
                    printStr(" }");
                }

                if (x.steps.Length == 0)
                {
                    printStr(" steps {none}");
                    printStr("}");
                }
                else
                {
                    printStr(" steps {{");
                    for (int i = 0; i < x.steps.Length; i++)
                    {
                        print(x.steps[i]);
                        if (i < x.steps.Length - 1)
                        {
                            printStr(" } {");
                        }
                    }
                    printStr("}}}");
                }
            }
            else if (o is XPathFilterExpr)
            {
                XPathFilterExpr x = (XPathFilterExpr)o;

                printStr("filter-expr:{{");
                print(x.x);

                if (x.predicates.Length == 0)
                {
                    printStr(" } predicates {none}}");
                }
                else
                {
                    printStr(" } predicates {{");
                    for (int i = 0; i < x.predicates.Length; i++)
                    {
                        print(x.predicates[i]);
                        if (i < x.predicates.Length - 1)
                        {
                            printStr(" } {");
                        }
                    }
                    printStr(" }}}");
                }
            }
            else if (o is XPathStep)
            {
                XPathStep x    = (XPathStep)o;
                String    axis = null;
                String    test = null;

                axis = XPathStep.axisStr(x.axis);
                test = x.testStr();

                if (x.predicates.Length == 0)
                {
                    printStr("step {axis:" + axis + " test:" + test + " predicates {none}}");
                }
                else
                {
                    printStr("step {axis:" + axis + " test:" + test + " predicates {{");
                    for (int i = 0; i < x.predicates.Length; i++)
                    {
                        print(x.predicates[i]);
                        if (i < x.predicates.Length - 1)
                        {
                            printStr(" } {");
                        }
                    }
                    printStr("}}}");
                }
            }

            indent -= 1;
        }