Exemplo n.º 1
0
 private void btnValidate_Click(object sender, EventArgs e)
 {
     try
     {
         if (_mode == ExpressionEditorMode.Filter)
         {
             FdoFilter filter = FdoFilter.Parse(ExpressionText.Text);
             _validator.ValidateFilter(filter, _cls, _caps);
             MessageBox.Show(Strings.FilterIsValid);
         }
         else //Expression
         {
             FdoExpression expr = FdoExpression.Parse(ExpressionText.Text);
             _validator.ValidateExpression(expr, _cls, _caps);
             MessageBox.Show(Strings.ExprIsValid);
         }
     }
     catch (FdoExpressionValidationException ex)
     {
         new ExpressionParseErrorDialog(ex, this).ShowDialog();
     }
     catch (FdoMalformedExpressionException ex)
     {
         new MalformedExpressionDialog(ex, this).ShowDialog();
     }
 }
Exemplo n.º 2
0
        public void TestSqlInjectionProtection()
        {
            string suspectQuery = "RNAME = '{0}'";

            var tests = new[]
            {
                new { sql = "a'; DROP TABLE Parcels; SELECT * FROM Parcels WHERE '1' = '1", testTokens = new string[] { "DROP", ";" } },
                new { sql = "b'; DELETE FROM Parcels; SELECT * FROM Parcels WHERE '1' = '1", testTokens = new string[] { "DELETE", ";" } },
                new { sql = "c'; UPDATE Parcels SET RNAME = ''; SELECT * FROM Parcels WHERE '1' = '1", testTokens = new string[] { "UPDATE", "SET", ";" } }
            };

            int testNo = 1;

            foreach (var t in tests)
            {
                try
                {
                    string    origFilter = string.Format(suspectQuery, t.sql);
                    FdoFilter filter     = FdoFilter.Parse(origFilter);
                    string    filterStr  = filter.ToString();
                    Console.WriteLine("Test {0}: Drop table attempt\n   Original: {1}\n   Parsed: {2}", testNo, origFilter, filterStr);
                    Assert.False(t.testTokens.Any(tok => filterStr.ToUpper().Contains(tok.ToUpper())));
                }
                catch (ManagedFdoException ex)
                {
                    Console.WriteLine("Test {0}: Drop table attempt - {1}", testNo, ex.Message);
                }
                testNo++;
            }
        }
Exemplo n.º 3
0
 private void viewParsedExpressionFilterToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         if (_mode == ExpressionEditorMode.Filter)
         {
             FdoFilter filter = FdoFilter.Parse(ExpressionText.Text);
             new ExpressionDisplayDialog(filter).ShowDialog();
         }
         else //Expression
         {
             FdoExpression expr = FdoExpression.Parse(ExpressionText.Text);
             new ExpressionDisplayDialog(expr).ShowDialog();
         }
     }
     catch (FdoParseException ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Exemplo n.º 4
0
        protected string TryTranslateFdoFilter(FdoFilter filter, string featureVar)
        {
            switch (filter)
            {
            case FdoComparisonCondition cmpCond:
            {
                var lhs = TryTranslateExpression(cmpCond.Left, featureVar);
                var rhs = TryTranslateExpression(cmpCond.Right, featureVar);
                var op  = TryTranslateComparsionOperator(cmpCond.Operator);
                if (lhs != null && rhs != null && op != null)
                {
                    return(lhs + op + rhs);
                }
                return(null);
            }

            default:
                return(null);
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="node"></param>
        internal FdoBinaryLogicalOperator(Irony.Parsing.ParseTreeNode node)
        {
            this.Left = FdoFilter.ParseNode(node.ChildNodes[0]);
            var opName = node.ChildNodes[1].ChildNodes[0].Token.ValueString;

            switch (opName.ToUpper())
            {
            case "AND":
                this.Operator = BinaryLogicalOperations.And;
                break;

            case "OR":
                this.Operator = BinaryLogicalOperations.Or;
                break;

            default:
                throw new FdoParseException("Unknown operator: " + opName);
            }
            this.Right = FdoFilter.ParseNode(node.ChildNodes[2]);
        }
Exemplo n.º 6
0
        // parse an FDO Filter that we except to return NULL because of error
        void ParseFilterToNull(string pwzFilter)
        {
            /*
             * Assert.Throws<ManagedFdoException>(() =>
             * {
             *  FdoFilter filter = FdoFilter.Parse(pwzFilter);
             * });
             */
            // get root node of expression parse tree
            FdoFilter pFilter = null;

            try
            {
                pFilter = FdoFilter.Parse(pwzFilter);
            }
            catch (ManagedFdoException)
            {
                return;
            }
            Assert.Null(pFilter); //, "FdoFilter::Parse() should have returned NULL!");
        }
Exemplo n.º 7
0
        // parse an FDO Filter
        void ParseFilter <T>(string pwzFilter, string pwzResult = null, Type expectedType = null) where T : FdoFilter
        {
            // get root node of expression parse tree
            FdoFilter pFilter = FdoFilter.Parse(pwzFilter);

            Assert.NotNull(pFilter);

            // output back to string if successful
            string pwzOut = pFilter.ToString();

            Assert.NotNull(pwzOut);

            if (pwzResult == null)
            {
                Assert.Equal(pwzOut, pwzFilter); //, "Parse/ToString do not match!\n\t<{0}> should be <{1}>\n", pwzOut, pwzFilter);
            }
            else
            {
                Assert.Equal(pwzOut, pwzResult); //, "Parse/ToString do not match!\n\t<{0}> should be <{1}>\n", pwzOut, pwzResult);
            }

            Assert.IsAssignableFrom <T>(pFilter);
        }
        private TreeNode LoadFilterTree(FdoFilter filter)
        {
            TreeNode node = new TreeNode();

            switch (filter.FilterType)
            {
            case FilterType.BinaryLogicalOperator:
            {
                var binOp = (FdoBinaryLogicalOperator)filter;
                node.Text = string.Format(Strings.FltrDispBinaryLogicalOperator, binOp.Operator);
                var left  = new TreeNode(Strings.FltrDispLeftNode);
                var right = new TreeNode(Strings.FltrDispRightNode);
                left.Nodes.Add(LoadFilterTree(binOp.Left));
                right.Nodes.Add(LoadFilterTree(binOp.Right));
                node.Nodes.Add(left);
                node.Nodes.Add(right);
            }
            break;

            case FilterType.ComparisonCondition:
            {
                var compCond = (FdoComparisonCondition)filter;
                node.Text = string.Format(Strings.FltrDispComparisonCondition, compCond.Operator);
                var left  = new TreeNode(Strings.FltrDispLeftNode);
                var right = new TreeNode(Strings.FltrDispRightNode);
                left.Nodes.Add(LoadExpressionTree(compCond.Left));
                right.Nodes.Add(LoadExpressionTree(compCond.Right));
                node.Nodes.Add(left);
                node.Nodes.Add(right);
            }
            break;

            case FilterType.DistanceCondition:
            {
                var distCond = (FdoDistanceCondition)filter;
                node.Text = string.Format(Strings.FdoDispDistanceCondition, distCond.Operator);
                var ident = new TreeNode(Strings.FdoDispIdentifierTitle);
                var expr  = new TreeNode(Strings.FdoDispExprTitle);
                var dist  = new TreeNode(Strings.FdoDispDistanceTitle);
                ident.Nodes.Add(LoadExpressionTree(distCond.Identifier));
                expr.Nodes.Add(LoadExpressionTree(distCond.Expression));
                dist.Nodes.Add(LoadExpressionTree(distCond.Distance));
                node.Nodes.Add(ident);
                node.Nodes.Add(expr);
                node.Nodes.Add(dist);
            }
            break;

            case FilterType.InCondition:
            {
                var inCond = (FdoInCondition)filter;
                node.Text = string.Format(Strings.FdoDispInCondition, inCond.Identifier.Name);
                foreach (var arg in inCond.ValueList)
                {
                    node.Nodes.Add(LoadExpressionTree(arg));
                }
            }
            break;

            case FilterType.NullCondition:
            {
                var nullCond = (FdoNullCondition)filter;
                node.Text = string.Format(Strings.FdoDispNullCondition, nullCond.Identifier.Name);
            }
            break;

            case FilterType.SpatialCondition:
            {
                var spCond = (FdoSpatialCondition)filter;
                node.Text = string.Format(Strings.FdoDispSpatialCondition, spCond.Operator);
                var ident    = new TreeNode(Strings.FdoDispIdentifierTitle);
                var exprNode = new TreeNode(Strings.FdoDispExprTitle);
                ident.Nodes.Add(LoadExpressionTree(spCond.Identifier));
                exprNode.Nodes.Add(LoadExpressionTree(spCond.Expression));
                node.Nodes.Add(ident);
                node.Nodes.Add(exprNode);
            }
            break;

            case FilterType.UnaryLogicalOperator:
            {
                var unCond = (FdoUnaryLogicalOperator)filter;
                node.Text = Strings.FdoDispUnaryOperator;
                node.Nodes.Add(LoadFilterTree(unCond.NegatedFilter));
            }
            break;
            }
            return(node);
        }
Exemplo n.º 9
0
        public async Task WritePointStyleFunctionAsync(StreamWriter sw, IVectorLayerDefinition vl, IPointVectorStyle pointStyle)
        {
            await sw.WriteLineAsync(INDENT + "var fillColor;");

            await sw.WriteLineAsync(INDENT + "var edgeColor;");

            await sw.WriteLineAsync(INDENT + "var edgeThickness;");

            await sw.WriteLineAsync(INDENT + "var pointRadius = 10;");

            var sb = new StringBuilder(512);
            var defaultRuleBlock = "";
            var themes           = new List <(string conditon, string filter, string block)>();

            await sw.WriteLineAsync(INDENT + "var pointStyle = OLPointCircle;");

            foreach (var rule in pointStyle.Rules)
            {
                if (rule.PointSymbolization2D != null)
                {
                    if (string.IsNullOrEmpty(rule.Filter)) //No filter = default rule effectively
                    {
                        if (string.IsNullOrEmpty(defaultRuleBlock))
                        {
                            BuildPointRuleAssignment(sb, rule.PointSymbolization2D.Symbol as IMarkSymbol);
                            defaultRuleBlock = sb.ToString();
                        }
                    }
                    else
                    {
                        //Parse this filter and see if it's translateable
                        var filter = FdoFilter.Parse(rule.Filter);
                        var check  = TryTranslateFdoFilter(filter, _featureVarName);
                        BuildPointRuleAssignment(sb, rule.PointSymbolization2D.Symbol as IMarkSymbol);
                        themes.Add((check, rule.Filter, sb.ToString()));
                    }
                }
            }

            //Now write out the theme
            if (themes.Count > 0)
            {
                bool bFirst = true;
                foreach (var th in themes)
                {
                    if (bFirst)
                    {
                        await sw.WriteLineAsync(INDENT + $"if ({th.conditon}) {{ // Translated from rule filter: {th.filter}");

                        await sw.WriteLineAsync(th.block);

                        await sw.WriteLineAsync(INDENT + "}");
                    }
                    else
                    {
                        await sw.WriteLineAsync(INDENT + $"else if ({th.conditon}) {{  // Translated from rule filter: {th.filter}");

                        await sw.WriteLineAsync(th.block);

                        await sw.WriteLineAsync(INDENT + "}");
                    }

                    bFirst = false;
                }

                if (!string.IsNullOrEmpty(defaultRuleBlock))
                {
                    await sw.WriteLineAsync(INDENT + $"else {{  // Default rule");

                    await sw.WriteLineAsync(defaultRuleBlock);

                    await sw.WriteLineAsync(INDENT + "}");
                }
            }
            else
            {
                await sw.WriteLineAsync("//Default rule");

                await sw.WriteLineAsync(defaultRuleBlock);
            }

            await sw.WriteLineAsync(INDENT + "var style = {");

            await sw.WriteLineAsync(INDENT + INDENT + "radius: pointRadius,");

            await sw.WriteLineAsync(INDENT + INDENT + $"fill: new ol.style.Fill({{ color: fillColor }}),");

            await sw.WriteLineAsync(INDENT + INDENT + $"stroke: new ol.style.Stroke({{ color: edgeColor, width: edgeThickness }})");

            await sw.WriteLineAsync(INDENT + "}");

            await sw.WriteLineAsync(INDENT + "return new ol.style.Style({ image: pointStyle(style) });");
        }
        /// <summary>
        /// Validates the given FDO filter
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="cls"></param>
        /// <param name="caps"></param>
        public void ValidateFilter(FdoFilter filter, ClassDefinition cls, IFdoProviderCapabilities caps)
        {
            switch (filter.FilterType)
            {
            case FilterType.BinaryLogicalOperator:
            {
                var binOp = (FdoBinaryLogicalOperator)filter;
                ValidateFilter(binOp.Left, cls, caps);
                ValidateFilter(binOp.Right, cls, caps);
            }
            break;

            case FilterType.ComparisonCondition:
            {
                var compCond = (FdoComparisonCondition)filter;
                if (compCond.Operator != ComparisonOperations.Like)
                {
                    if (!caps.Filter.ConditionTypes.Select(o => o.ToUpper()).Contains("COMPARISON"))         //NOXLATE
                    {
                        throw new FdoExpressionValidationException(string.Format(Strings.InvalidFilterUnsupportedConditionType, compCond.Operator), compCond.Operator.ToString());
                    }
                }
                else         //Like
                {
                    if (!caps.Filter.ConditionTypes.Select(o => o.ToUpper()).Any(o => o == compCond.Operator.ToString().ToUpper()))
                    {
                        throw new FdoExpressionValidationException(string.Format(Strings.InvalidFilterUnsupportedConditionType, compCond.Operator), compCond.Operator.ToString());
                    }
                }
                ValidateExpression(compCond.Left, cls, caps);
                ValidateExpression(compCond.Right, cls, caps);
            }
            break;

            case FilterType.DistanceCondition:
            {
                var distCond = (FdoDistanceCondition)filter;
                if (!caps.Filter.DistanceOperations.Select(o => o.ToUpper()).Any(o => o == distCond.Operator.ToString().ToUpper()))
                {
                    throw new FdoExpressionValidationException(string.Format(Strings.InvalidFilterUnsupportedDistanceOperator, distCond.Operator), distCond.Operator.ToString());
                }
                ValidateIdentifier(distCond.Identifier, cls);
                ValidateExpression(distCond.Expression, cls, caps);
            }
            break;

            case FilterType.InCondition:
            {
                if (!caps.Filter.ConditionTypes.Select(o => o.ToUpper()).Contains("IN"))                                                  //NOXLATE
                {
                    throw new FdoExpressionValidationException(string.Format(Strings.InvalidFilterUnsupportedConditionType, "IN"), "IN"); //NOXLATE
                }
                var inCond = (FdoInCondition)filter;
                ValidateIdentifier(inCond.Identifier, cls);
                foreach (var val in inCond.ValueList)
                {
                    ValidateExpression(val, cls, caps);
                }
            }
            break;

            case FilterType.NullCondition:
            {
                if (!caps.Filter.ConditionTypes.Select(o => o.ToUpper()).Contains("NULL"))                                                    //NOXLATE
                {
                    throw new FdoExpressionValidationException(string.Format(Strings.InvalidFilterUnsupportedConditionType, "NULL"), "NULL"); //NOXLATE
                }
                var nullCond = (FdoNullCondition)filter;
                ValidateIdentifier(nullCond.Identifier, cls);
            }
            break;

            case FilterType.SpatialCondition:
            {
                var spFilter = (FdoSpatialCondition)filter;
                if (!caps.Filter.ConditionTypes.Select(o => o.ToUpper()).Contains("SPATIAL"))                                                                          //NOXLATE
                {
                    throw new FdoExpressionValidationException(string.Format(Strings.InvalidFilterUnsupportedConditionType, "SPATIAL"), spFilter.Operator.ToString()); //NOXLATE
                }
                if (!caps.Filter.SpatialOperations.Select(o => o.ToUpper()).Any(o => o == spFilter.Operator.ToString().ToUpper()))
                {
                    throw new FdoExpressionValidationException(string.Format(Strings.InvalidFilterUnsupportedSpatialOperator, spFilter.Operator), spFilter.Operator.ToString());
                }

                ValidateIdentifier(spFilter.Identifier, cls);
                ValidateExpression(spFilter.Expression, cls, caps);
            }
            break;

            case FilterType.UnaryLogicalOperator:
            {
                var negatedFilter = ((FdoUnaryLogicalOperator)filter).NegatedFilter;
                ValidateFilter(negatedFilter, cls, caps);
            }
            break;
            }
        }
Exemplo n.º 11
0
        public void TestFilterParse()
        {
            FdoFilter filter = null;
            FdoFilter left   = null;
            FdoFilter right  = null;

            filter = FdoFilter.Parse("A = 'B'");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.EqualsTo, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("(A = 'B')");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.EqualsTo, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("A <> 'B'");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.NotEqualsTo, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("(A <> 'B')");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.NotEqualsTo, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("A > 2.3");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.GreaterThan, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("(A > 2.3)");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.GreaterThan, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("A >= 2.3");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.GreaterThanOrEqualTo, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("(A >= 2.3)");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.GreaterThanOrEqualTo, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("A < 2.3");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.LessThan, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("(A < 2.3)");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.LessThan, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("A <= 2.3");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.LessThanOrEqualTo, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("(A <= 2.3)");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.LessThanOrEqualTo, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("A LIKE 'B%'");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.Like, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("(A LIKE 'B%')");
            Assert.IsAssignableFrom <FdoComparisonCondition>(filter);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)filter).Left);
            Assert.Equal(ComparisonOperations.Like, ((FdoComparisonCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)filter).Right);

            filter = FdoFilter.Parse("NOT (A LIKE 'B%')");
            Assert.IsAssignableFrom <FdoUnaryLogicalOperator>(filter);
            var subExpr = ((FdoUnaryLogicalOperator)filter).NegatedFilter;

            Assert.IsAssignableFrom <FdoComparisonCondition>(subExpr);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)subExpr).Left);
            Assert.Equal(ComparisonOperations.Like, ((FdoComparisonCondition)subExpr).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)subExpr).Right);

            filter = FdoFilter.Parse("NAME NULL");
            Assert.IsAssignableFrom <FdoNullCondition>(filter);
            Assert.Equal("NAME", ((FdoNullCondition)filter).Identifier.Name);

            filter = FdoFilter.Parse("NOT NAME NULL");
            Assert.IsAssignableFrom <FdoUnaryLogicalOperator>(filter);
            subExpr = ((FdoUnaryLogicalOperator)filter).NegatedFilter;
            Assert.Equal("NAME", ((FdoNullCondition)subExpr).Identifier.Name);

            filter = FdoFilter.Parse("A = 'B' OR B = 'C'");
            left   = ((FdoBinaryLogicalOperator)filter).Left;
            right  = ((FdoBinaryLogicalOperator)filter).Right;
            Assert.IsAssignableFrom <FdoBinaryLogicalOperator>(filter);
            Assert.IsAssignableFrom <FdoComparisonCondition>(left);
            Assert.Equal(BinaryLogicalOperations.Or, ((FdoBinaryLogicalOperator)filter).Operator);
            Assert.IsAssignableFrom <FdoComparisonCondition>(right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)left).Left);
            Assert.Equal(ComparisonOperations.EqualsTo, ((FdoComparisonCondition)left).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)left).Right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)right).Left);
            Assert.Equal(ComparisonOperations.EqualsTo, ((FdoComparisonCondition)right).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)right).Right);

            filter = FdoFilter.Parse("A = 'B' AND B = 'C'");
            left   = ((FdoBinaryLogicalOperator)filter).Left;
            right  = ((FdoBinaryLogicalOperator)filter).Right;
            Assert.IsAssignableFrom <FdoBinaryLogicalOperator>(filter);
            Assert.IsAssignableFrom <FdoComparisonCondition>(left);
            Assert.Equal(BinaryLogicalOperations.And, ((FdoBinaryLogicalOperator)filter).Operator);
            Assert.IsAssignableFrom <FdoComparisonCondition>(right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)left).Left);
            Assert.Equal(ComparisonOperations.EqualsTo, ((FdoComparisonCondition)left).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)left).Right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)right).Left);
            Assert.Equal(ComparisonOperations.EqualsTo, ((FdoComparisonCondition)right).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)right).Right);

            filter = FdoFilter.Parse("(A = 'B') OR (B = 'C')");
            left   = ((FdoBinaryLogicalOperator)filter).Left;
            right  = ((FdoBinaryLogicalOperator)filter).Right;
            Assert.IsAssignableFrom <FdoBinaryLogicalOperator>(filter);
            Assert.IsAssignableFrom <FdoComparisonCondition>(left);
            Assert.Equal(BinaryLogicalOperations.Or, ((FdoBinaryLogicalOperator)filter).Operator);
            Assert.IsAssignableFrom <FdoComparisonCondition>(right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)left).Left);
            Assert.Equal(ComparisonOperations.EqualsTo, ((FdoComparisonCondition)left).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)left).Right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)right).Left);
            Assert.Equal(ComparisonOperations.EqualsTo, ((FdoComparisonCondition)right).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)right).Right);

            filter = FdoFilter.Parse("(A = 'B') AND (B = 'C')");
            left   = ((FdoBinaryLogicalOperator)filter).Left;
            right  = ((FdoBinaryLogicalOperator)filter).Right;
            Assert.IsAssignableFrom <FdoBinaryLogicalOperator>(filter);
            Assert.IsAssignableFrom <FdoComparisonCondition>(left);
            Assert.Equal(BinaryLogicalOperations.And, ((FdoBinaryLogicalOperator)filter).Operator);
            Assert.IsAssignableFrom <FdoComparisonCondition>(right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)left).Left);
            Assert.Equal(ComparisonOperations.EqualsTo, ((FdoComparisonCondition)left).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)left).Right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)right).Left);
            Assert.Equal(ComparisonOperations.EqualsTo, ((FdoComparisonCondition)right).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)right).Right);

            filter = FdoFilter.Parse("(A <> 'B') OR (B LIKE 'C%')");
            left   = ((FdoBinaryLogicalOperator)filter).Left;
            right  = ((FdoBinaryLogicalOperator)filter).Right;
            Assert.IsAssignableFrom <FdoBinaryLogicalOperator>(filter);
            Assert.IsAssignableFrom <FdoComparisonCondition>(left);
            Assert.Equal(BinaryLogicalOperations.Or, ((FdoBinaryLogicalOperator)filter).Operator);
            Assert.IsAssignableFrom <FdoComparisonCondition>(right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)left).Left);
            Assert.Equal(ComparisonOperations.NotEqualsTo, ((FdoComparisonCondition)left).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)left).Right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)right).Left);
            Assert.Equal(ComparisonOperations.Like, ((FdoComparisonCondition)right).Operator);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoComparisonCondition)right).Right);

            filter = FdoFilter.Parse("(A < 2.2) OR (B > 1.4)");
            left   = ((FdoBinaryLogicalOperator)filter).Left;
            right  = ((FdoBinaryLogicalOperator)filter).Right;
            Assert.IsAssignableFrom <FdoBinaryLogicalOperator>(filter);
            Assert.IsAssignableFrom <FdoComparisonCondition>(left);
            Assert.Equal(BinaryLogicalOperations.Or, ((FdoBinaryLogicalOperator)filter).Operator);
            Assert.IsAssignableFrom <FdoComparisonCondition>(right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)left).Left);
            Assert.Equal(ComparisonOperations.LessThan, ((FdoComparisonCondition)left).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)left).Right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)right).Left);
            Assert.Equal(ComparisonOperations.GreaterThan, ((FdoComparisonCondition)right).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)right).Right);

            filter = FdoFilter.Parse("(A < 2.2) AND (B > 1.4)");
            left   = ((FdoBinaryLogicalOperator)filter).Left;
            right  = ((FdoBinaryLogicalOperator)filter).Right;
            Assert.IsAssignableFrom <FdoBinaryLogicalOperator>(filter);
            Assert.IsAssignableFrom <FdoComparisonCondition>(left);
            Assert.Equal(BinaryLogicalOperations.And, ((FdoBinaryLogicalOperator)filter).Operator);
            Assert.IsAssignableFrom <FdoComparisonCondition>(right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)left).Left);
            Assert.Equal(ComparisonOperations.LessThan, ((FdoComparisonCondition)left).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)left).Right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)right).Left);
            Assert.Equal(ComparisonOperations.GreaterThan, ((FdoComparisonCondition)right).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)right).Right);

            filter = FdoFilter.Parse("(A <= 2.2) OR (B >= 1.4)");
            left   = ((FdoBinaryLogicalOperator)filter).Left;
            right  = ((FdoBinaryLogicalOperator)filter).Right;
            Assert.IsAssignableFrom <FdoBinaryLogicalOperator>(filter);
            Assert.IsAssignableFrom <FdoComparisonCondition>(left);
            Assert.Equal(BinaryLogicalOperations.Or, ((FdoBinaryLogicalOperator)filter).Operator);
            Assert.IsAssignableFrom <FdoComparisonCondition>(right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)left).Left);
            Assert.Equal(ComparisonOperations.LessThanOrEqualTo, ((FdoComparisonCondition)left).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)left).Right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)right).Left);
            Assert.Equal(ComparisonOperations.GreaterThanOrEqualTo, ((FdoComparisonCondition)right).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)right).Right);

            filter = FdoFilter.Parse("(A <= 2.2) AND (B >= 1.4)");
            left   = ((FdoBinaryLogicalOperator)filter).Left;
            right  = ((FdoBinaryLogicalOperator)filter).Right;
            Assert.IsAssignableFrom <FdoBinaryLogicalOperator>(filter);
            Assert.IsAssignableFrom <FdoComparisonCondition>(left);
            Assert.Equal(BinaryLogicalOperations.And, ((FdoBinaryLogicalOperator)filter).Operator);
            Assert.IsAssignableFrom <FdoComparisonCondition>(right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)left).Left);
            Assert.Equal(ComparisonOperations.LessThanOrEqualTo, ((FdoComparisonCondition)left).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)left).Right);
            Assert.IsAssignableFrom <FdoIdentifier>(((FdoComparisonCondition)right).Left);
            Assert.Equal(ComparisonOperations.GreaterThanOrEqualTo, ((FdoComparisonCondition)right).Operator);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoComparisonCondition)right).Right);

            filter = FdoFilter.Parse("A IN ('A', 'B', 'C')");
            Assert.IsAssignableFrom <FdoInCondition>(filter);
            Assert.Equal("A", ((FdoInCondition)filter).Identifier.Name);
            Assert.Equal(3, ((FdoInCondition)filter).ValueList.Count);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoInCondition)filter).ValueList[0]);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoInCondition)filter).ValueList[1]);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoInCondition)filter).ValueList[2]);

            filter = FdoFilter.Parse("A IN (1, 2.0, '3', TRUE, FALSE)");
            Assert.IsAssignableFrom <FdoInCondition>(filter);
            Assert.Equal("A", ((FdoInCondition)filter).Identifier.Name);
            Assert.Equal(5, ((FdoInCondition)filter).ValueList.Count);
            Assert.IsAssignableFrom <FdoInt32Value>(((FdoInCondition)filter).ValueList[0]);
            Assert.IsAssignableFrom <FdoDoubleValue>(((FdoInCondition)filter).ValueList[1]);
            Assert.IsAssignableFrom <FdoStringValue>(((FdoInCondition)filter).ValueList[2]);
            Assert.IsAssignableFrom <FdoBooleanValue>(((FdoInCondition)filter).ValueList[3]);
            Assert.IsAssignableFrom <FdoBooleanValue>(((FdoInCondition)filter).ValueList[4]);

            filter = FdoFilter.Parse("Geometry CONTAINS GeomFromText('POINT (1 1)')");
            Assert.IsAssignableFrom <FdoSpatialCondition>(filter);
            Assert.Equal("Geometry", ((FdoSpatialCondition)filter).Identifier.Name);
            Assert.Equal(SpatialOperations.Contains, ((FdoSpatialCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoSpatialCondition)filter).Expression);

            filter = FdoFilter.Parse("Geometry CROSSES GeomFromText('POINT (1 1)')");
            Assert.IsAssignableFrom <FdoSpatialCondition>(filter);
            Assert.Equal("Geometry", ((FdoSpatialCondition)filter).Identifier.Name);
            Assert.Equal(SpatialOperations.Crosses, ((FdoSpatialCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoSpatialCondition)filter).Expression);

            filter = FdoFilter.Parse("Geometry DISJOINT GeomFromText('POINT (1 1)')");
            Assert.IsAssignableFrom <FdoSpatialCondition>(filter);
            Assert.Equal("Geometry", ((FdoSpatialCondition)filter).Identifier.Name);
            Assert.Equal(SpatialOperations.Disjoint, ((FdoSpatialCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoSpatialCondition)filter).Expression);

            filter = FdoFilter.Parse("Geometry EQUALS GeomFromText('POINT (1 1)')");
            Assert.IsAssignableFrom <FdoSpatialCondition>(filter);
            Assert.Equal("Geometry", ((FdoSpatialCondition)filter).Identifier.Name);
            Assert.Equal(SpatialOperations.Equals, ((FdoSpatialCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoSpatialCondition)filter).Expression);

            filter = FdoFilter.Parse("Geometry INTERSECTS GeomFromText('POINT (1 1)')");
            Assert.IsAssignableFrom <FdoSpatialCondition>(filter);
            Assert.Equal("Geometry", ((FdoSpatialCondition)filter).Identifier.Name);
            Assert.Equal(SpatialOperations.Intersects, ((FdoSpatialCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoSpatialCondition)filter).Expression);

            filter = FdoFilter.Parse("Geometry OVERLAPS GeomFromText('POINT (1 1)')");
            Assert.IsAssignableFrom <FdoSpatialCondition>(filter);
            Assert.Equal("Geometry", ((FdoSpatialCondition)filter).Identifier.Name);
            Assert.Equal(SpatialOperations.Overlaps, ((FdoSpatialCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoSpatialCondition)filter).Expression);

            filter = FdoFilter.Parse("Geometry TOUCHES GeomFromText('POINT (1 1)')");
            Assert.IsAssignableFrom <FdoSpatialCondition>(filter);
            Assert.Equal("Geometry", ((FdoSpatialCondition)filter).Identifier.Name);
            Assert.Equal(SpatialOperations.Touches, ((FdoSpatialCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoSpatialCondition)filter).Expression);

            filter = FdoFilter.Parse("Geometry WITHIN GeomFromText('POINT (1 1)')");
            Assert.IsAssignableFrom <FdoSpatialCondition>(filter);
            Assert.Equal("Geometry", ((FdoSpatialCondition)filter).Identifier.Name);
            Assert.Equal(SpatialOperations.Within, ((FdoSpatialCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoSpatialCondition)filter).Expression);

            filter = FdoFilter.Parse("Geometry COVEREDBY GeomFromText('POINT (1 1)')");
            Assert.IsAssignableFrom <FdoSpatialCondition>(filter);
            Assert.Equal("Geometry", ((FdoSpatialCondition)filter).Identifier.Name);
            Assert.Equal(SpatialOperations.CoveredBy, ((FdoSpatialCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoSpatialCondition)filter).Expression);

            filter = FdoFilter.Parse("Geometry INSIDE GeomFromText('POINT (1 1)')");
            Assert.IsAssignableFrom <FdoSpatialCondition>(filter);
            Assert.Equal("Geometry", ((FdoSpatialCondition)filter).Identifier.Name);
            Assert.Equal(SpatialOperations.Inside, ((FdoSpatialCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoSpatialCondition)filter).Expression);

            filter = FdoFilter.Parse("Geometry BEYOND GeomFromText('POINT (1 1)') 2.0");
            Assert.IsAssignableFrom <FdoDistanceCondition>(filter);
            Assert.Equal("Geometry", ((FdoDistanceCondition)filter).Identifier.Name);
            Assert.Equal(DistanceOperations.Beyond, ((FdoDistanceCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoDistanceCondition)filter).Expression);
            Assert.Equal(DataType.Double, ((FdoDistanceCondition)filter).Distance.DataType);
            Assert.Equal(2.0, ((FdoDoubleValue)((FdoDistanceCondition)filter).Distance).Value);

            filter = FdoFilter.Parse("Geometry BEYOND GeomFromText('POINT (1 1)') 5");
            Assert.IsAssignableFrom <FdoDistanceCondition>(filter);
            Assert.Equal("Geometry", ((FdoDistanceCondition)filter).Identifier.Name);
            Assert.Equal(DistanceOperations.Beyond, ((FdoDistanceCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoDistanceCondition)filter).Expression);
            Assert.Equal(DataType.Int32, ((FdoDistanceCondition)filter).Distance.DataType);
            Assert.Equal(5, ((FdoInt32Value)((FdoDistanceCondition)filter).Distance).Value);

            filter = FdoFilter.Parse("Geometry WITHINDISTANCE GeomFromText('POINT (1 1)') 2.0");
            Assert.IsAssignableFrom <FdoDistanceCondition>(filter);
            Assert.Equal("Geometry", ((FdoDistanceCondition)filter).Identifier.Name);
            Assert.Equal(DistanceOperations.WithinDistance, ((FdoDistanceCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoDistanceCondition)filter).Expression);
            Assert.Equal(DataType.Double, ((FdoDistanceCondition)filter).Distance.DataType);
            Assert.Equal(2.0, ((FdoDoubleValue)((FdoDistanceCondition)filter).Distance).Value);

            filter = FdoFilter.Parse("Geometry WITHINDISTANCE GeomFromText('POINT (1 1)') 5");
            Assert.IsAssignableFrom <FdoDistanceCondition>(filter);
            Assert.Equal("Geometry", ((FdoDistanceCondition)filter).Identifier.Name);
            Assert.Equal(DistanceOperations.WithinDistance, ((FdoDistanceCondition)filter).Operator);
            Assert.IsAssignableFrom <FdoGeometryValue>(((FdoDistanceCondition)filter).Expression);
            Assert.Equal(DataType.Int32, ((FdoDistanceCondition)filter).Distance.DataType);
            Assert.Equal(5, ((FdoInt32Value)((FdoDistanceCondition)filter).Distance).Value);
        }
Exemplo n.º 12
0
        public async Task WritePolygonStyleFunctionAsync(StreamWriter sw, IVectorLayerDefinition vl, IAreaVectorStyle areaStyle)
        {
            await sw.WriteLineAsync(INDENT + "var fillColor;");

            await sw.WriteLineAsync(INDENT + "var edgeColor;");

            await sw.WriteLineAsync(INDENT + "var edgeThickness;");

            var sb = new StringBuilder(512);
            var defaultRuleBlock = "";
            var themes           = new List <(string conditon, string filter, string block)>();

            foreach (var rule in areaStyle.Rules)
            {
                if (rule.AreaSymbolization2D != null)
                {
                    if (string.IsNullOrEmpty(rule.Filter)) //No filter = default rule effectively
                    {
                        if (string.IsNullOrEmpty(defaultRuleBlock))
                        {
                            BuildPolygonRuleAssignment(sb, rule);
                            defaultRuleBlock = sb.ToString();
                        }
                    }
                    else
                    {
                        //Parse this filter and see if it's translateable
                        var filter = FdoFilter.Parse(rule.Filter);
                        var check  = TryTranslateFdoFilter(filter, _featureVarName);
                        BuildPolygonRuleAssignment(sb, rule);
                        themes.Add((check, rule.Filter, sb.ToString()));
                    }
                }
            }

            //Now write out the theme
            if (themes.Count > 0)
            {
                bool bFirst = true;
                foreach (var th in themes)
                {
                    if (bFirst)
                    {
                        await sw.WriteLineAsync(INDENT + $"if ({th.conditon}) {{ // Translated from rule filter: {th.filter}");

                        await sw.WriteLineAsync(th.block);

                        await sw.WriteLineAsync(INDENT + "}");
                    }
                    else
                    {
                        await sw.WriteLineAsync(INDENT + $"else if ({th.conditon}) {{  // Translated from rule filter: {th.filter}");

                        await sw.WriteLineAsync(th.block);

                        await sw.WriteLineAsync(INDENT + "}");
                    }

                    bFirst = false;
                }

                if (!string.IsNullOrEmpty(defaultRuleBlock))
                {
                    await sw.WriteLineAsync(INDENT + $"else {{  // Default rule");

                    await sw.WriteLineAsync(defaultRuleBlock);

                    await sw.WriteLineAsync(INDENT + "}");
                }
            }
            else
            {
                await sw.WriteLineAsync("//Default rule");

                await sw.WriteLineAsync(defaultRuleBlock);
            }

            await sw.WriteLineAsync(INDENT + "var style = {");

            await sw.WriteLineAsync(INDENT + INDENT + $"opacity: 1, fillOpacity: 1,");

            await sw.WriteLineAsync(INDENT + INDENT + $"fillColor: fillColor,");

            await sw.WriteLineAsync(INDENT + INDENT + $"color: edgeColor, weight: Math.max(edgeThickness, 1)");

            await sw.WriteLineAsync(INDENT + "}");

            await sw.WriteLineAsync(INDENT + "return style;");
        }