Ejemplo n.º 1
0
        /// <summary>
        /// Evaluates a single SIF_Condition against an object and returns whether it matches or not
        /// </summary>
        /// <param name="cond"></param>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        /// <exception cref="OpenADK.Library.AdkSchemaException">If the condition contains references to invalid elements</exception>
        private bool EvaluateCondition(SifXPathContext context,
                                        Condition cond,
                                        CultureInfo culture)
        {
            // TODO: Add support for comparison using the SIF Data Types
            Element def = context.GetElementOrAttribute(cond.GetXPath());
            String conditionValue = cond.Value;

            String elementValue = null;
            if (def != null)
            {
                SifSimpleType value = def.SifValue;
                if (value != null)
                {
                    // Format the value to string, based on the query version
                    elementValue = value.ToString(EffectiveVersion);
                }
                else
                {
                    // TODO: Not sure if this would ever return a value if the above does not
                    elementValue = def.TextValue;
                }
            }

            if (elementValue == null || conditionValue == null)
            {
                // Don't use standard comparision because it will fail. If
                // one or the other value is null, it cannot be compared, except for
                // if the operator is EQ or NOT
                bool bothAreNull = (elementValue == null && conditionValue == null);
                switch (cond.Operators)
                {
                    case ComparisonOperators.EQ:
                    case ComparisonOperators.GE:
                    case ComparisonOperators.LE:
                        return bothAreNull;
                    case ComparisonOperators.NE:
                        return !bothAreNull;
                    default:
                        // For any other operator, the results are indeterminate with
                        // null values. Return false in this case.
                        return false;
                }
            }

            int compareLevel = String.Compare(elementValue, conditionValue, false, culture);

            switch (cond.Operators)
            {
                case ComparisonOperators.EQ:
                    return compareLevel == 0;
                case ComparisonOperators.NE:
                    return compareLevel != 0;
                case ComparisonOperators.GT:
                    return compareLevel > 0;
                case ComparisonOperators.LT:
                    return compareLevel < 0;
                case ComparisonOperators.GE:
                    return compareLevel >= 0;
                case ComparisonOperators.LE:
                    return compareLevel <= 0;
            }
            return false;
        }