コード例 #1
0
        public override CodeExpression GetCodeExpression(BindingExpressionInfo exprInfo)
        {
            IDictionary <string, object> options = exprInfo.ParsedValues;
            Uri uri = (Uri)exprInfo.ParsedObject;

            string inputName = options.ContainsKey("name") ?
                               options["name"].ToString()
            : null;

            string path = uri.AbsolutePath;

            switch (path)
            {
            case bind.query:
                return(GetQueryExpression(inputName));

            case bind.form:
                return(GetFormExpression(inputName));

            case bind.cookie:
                return(GetCookieExpression(inputName, (bool)options["remove"]));

            case bind.header:
                return(GetHeaderExpression(inputName));

            case bind.http_method:
                return(GetHttpMethodExpression());

            default:
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "Invalid path '{0}'.", path), "exprInfo");
            }
        }
コード例 #2
0
        public static BindingExpressionInfo ParseExpr(string expression, BindingExpressionContext context)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            int colonIndex = expression.IndexOf(':');

            if (colonIndex == -1)
            {
                throw new ArgumentException("The expression must contain a colon.", "expression");
            }

            string prefix = expression.Substring(0, colonIndex);
            string ns;

            if (!context.InScopeNamespaces.TryGetValue(prefix, out ns))
            {
                throw new ArgumentException("The are no namespaces defined for prefix '{0}'.".FormatInvariant(prefix), "expression");
            }

            string value = expression.Substring(colonIndex + 1);

            BindingExpressionBuilder exprBuilder;

            ExpressionBuilderElement el = WebSection.Instance.Compilation.ExpressionBuilders.Get(ns);

            if (el == null)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "There are no expression builders registered for namespace '{0}'.", ns));
            }

            exprBuilder = (BindingExpressionBuilder)Activator.CreateInstance(el.TypeInternal);

            BindingExpressionInfo exprInfo = exprBuilder.ParseExpression(value, context);

            if (exprInfo == null)
            {
                exprInfo = new BindingExpressionInfo(expression);
            }

            exprInfo.ExpressionBuilder = exprBuilder;

            return(exprInfo);
        }
コード例 #3
0
        public override CodeExpression GetCodeExpression(BindingExpressionInfo exprInfo)
        {
            IDictionary<string, object> options = exprInfo.ParsedValues;
             Uri uri = (Uri)exprInfo.ParsedObject;

             string inputName = options.ContainsKey("name") ?
            options["name"].ToString()
            : null;

             string path = uri.AbsolutePath;

             switch (path) {
            case bind.it:
               return GetSessionExpression(inputName, (bool)options["remove"]);

            default:
               throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "Invalid path '{0}'.", path), "exprInfo");
             }
        }
コード例 #4
0
        public static BindingExpressionInfo ParseExpr(string expression, BindingExpressionContext context)
        {
            if (expression == null) throw new ArgumentNullException("expression");
             if (context == null) throw new ArgumentNullException("context");

             int colonIndex = expression.IndexOf(':');

             if (colonIndex == -1) {
              throw new ArgumentException("The expression must contain a colon.", "expression");
              }

             string prefix = expression.Substring(0, colonIndex);
             string ns;

             if (!context.InScopeNamespaces.TryGetValue(prefix, out ns)) {
            throw new ArgumentException("The are no namespaces defined for prefix '{0}'.".FormatInvariant(prefix), "expression");
             }

             string value = expression.Substring(colonIndex + 1);

             BindingExpressionBuilder exprBuilder;

             ExpressionBuilderElement el = WebSection.Instance.Compilation.ExpressionBuilders.Get(ns);

             if (el == null) {
            throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "There are no expression builders registered for namespace '{0}'.", ns));
             }

             exprBuilder = (BindingExpressionBuilder)Activator.CreateInstance(el.TypeInternal);

             BindingExpressionInfo exprInfo = exprBuilder.ParseExpression(value, context);

             if (exprInfo == null) {
            exprInfo = new BindingExpressionInfo(expression);
             }

             exprInfo.ExpressionBuilder = exprBuilder;

             return exprInfo;
        }
コード例 #5
0
        CodeMemberMethod GetSetBoundParametersMethod()
        {
            var setParams = new CodeMemberMethod {
                Name             = "SetBoundParameters",
                Attributes       = MemberAttributes.Family | MemberAttributes.Override,
                CustomAttributes =
                {
                    new CodeAttributeDeclaration(this.DebuggerNonUserCodeTypeReference)
                },
                Parameters =
                {
                    new CodeParameterDeclarationExpression {
                        Name = "parameters",
                        Type = new CodeTypeReference(typeof(IDictionary <XmlQualifiedName, object>))
                    }
                }
            };

            var @this      = new CodeThisReferenceExpression();
            var parameters = new CodeVariableReferenceExpression(setParams.Parameters[0].Name);

            int pindex = 0;

            foreach (PageParameterInfo param in this.parser.Parameters)
            {
                if (param.Binding == null)
                {
                    continue;
                }

                pindex++;

                BindingExpressionInfo bind = param.Binding;
                string paramName           = param.Name;
                bool   atomicValueSequence = (param.AtomicTypeName != null && !param.AtomicTypeName.IsEmpty);

                CodeExpression valueExpr = bind.GetCodeExpression();

                valueExpr = new CodeMethodInvokeExpression {
                    Method = new CodeMethodReferenceExpression {
                        MethodName   = "CheckParamLength",
                        TargetObject = @this
                    },
                    Parameters =
                    {
                        new CodePrimitiveExpression(param.Name),
                        valueExpr,
                        new CodePrimitiveExpression(param.MinLength),
                        new CodePrimitiveExpression(param.MaxLength)
                    }
                };

                if (bind.ParsedValues.ContainsKey("accept"))
                {
                    valueExpr = new CodeMethodInvokeExpression {
                        Method = new CodeMethodReferenceExpression {
                            MethodName   = "CheckParamValues",
                            TargetObject = new CodeThisReferenceExpression()
                        },
                        Parameters =
                        {
                            new CodePrimitiveExpression(param.Name),
                            valueExpr,
                            new CodeArrayCreateExpression(typeof(string), ((string[])bind.ParsedValues["accept"]).Select(s => new CodePrimitiveExpression(s)).ToArray())
                        }
                    };
                }

                if (atomicValueSequence)
                {
                    valueExpr = new CodeMethodInvokeExpression {
                        Method = new CodeMethodReferenceExpression {
                            MethodName   = "CreateAtomicValueSequence",
                            TargetObject = new CodePropertyReferenceExpression {
                                PropertyName = "ItemFactory",
                                TargetObject = new CodePropertyReferenceExpression {
                                    PropertyName = "Processor",
                                    TargetObject = new CodePropertyReferenceExpression {
                                        PropertyName = "Executable",
                                        TargetObject = @this
                                    }
                                }
                            }
                        },
                        Parameters =
                        {
                            valueExpr,
                            new CodeObjectCreateExpression {
                                CreateType = new CodeTypeReference(typeof(XmlQualifiedName)),
                                Parameters =
                                {
                                    new CodePrimitiveExpression(param.AtomicTypeName.Name),
                                    new CodePrimitiveExpression(param.AtomicTypeName.Namespace)
                                }
                            }
                        }
                    };
                }

                CodeVariableDeclarationStatement pvarStatement =
                    new CodeVariableDeclarationStatement {
                    Type           = new CodeTypeReference((atomicValueSequence)? typeof(IEnumerable <XPathItem>) : typeof(object)),
                    Name           = "p" + pindex,
                    InitExpression = valueExpr
                };

                if (bind.LineNumber > 0)
                {
                    pvarStatement.LinePragma = new CodeLinePragma(this.parser.PhysicalPath.LocalPath, bind.LineNumber);
                }

                var pvarReference = new CodeVariableReferenceExpression {
                    VariableName = pvarStatement.Name
                };

                CodeBinaryOperatorExpression pvarCheckCondition =
                    new CodeBinaryOperatorExpression {
                    Left     = pvarReference,
                    Operator = CodeBinaryOperatorType.IdentityInequality,
                    Right    = new CodePrimitiveExpression(null)
                };

                if (atomicValueSequence)
                {
                    pvarCheckCondition.Left = new CodeMethodInvokeExpression {
                        Method = new CodeMethodReferenceExpression {
                            MethodName   = "Count",
                            TargetObject = new CodeTypeReferenceExpression(typeof(Enumerable))
                        },
                        Parameters =
                        {
                            pvarCheckCondition.Left
                        }
                    };

                    pvarCheckCondition.Operator = CodeBinaryOperatorType.GreaterThan;
                    pvarCheckCondition.Right    = new CodePrimitiveExpression(0);
                }

                CodeConditionStatement pvarCheckStatement =
                    new CodeConditionStatement {
                    Condition      = pvarCheckCondition,
                    TrueStatements =
                    {
                        new CodeAssignStatement                    {
                            Left = new CodeIndexerExpression       {
                                Indices =
                                {
                                    new CodeObjectCreateExpression {
                                        CreateType = new CodeTypeReference(typeof(XmlQualifiedName)),
                                        Parameters =
                                        {
                                            new CodePrimitiveExpression(paramName)
                                        }
                                    }
                                },
                                TargetObject = parameters
                            },
                            Right = pvarReference
                        }
                    }
                };

                setParams.Statements.Add(pvarStatement);
                setParams.Statements.Add(pvarCheckStatement);
            }

            return(setParams);
        }
コード例 #6
0
        public override BindingExpressionInfo ParseExpression(string expression, BindingExpressionContext context)
        {
            // TODO: Fix error messages not to refer to web:bind but expression builder prefix/ns instead

            var uri = new Uri(expression, UriKind.RelativeOrAbsolute);

            if (!uri.IsAbsoluteUri)
            {
                uri = new Uri(String.Concat(SessionModule.Prefix, ":", uri.OriginalString), UriKind.Absolute);
            }

            var validValues = new List <string>()
            {
                bind.it
            };

            string path     = uri.AbsolutePath;
            string nodeName = context.NodeName ?? context.BoundNode.Name;

            if (!validValues.Contains(path))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The value of the '{0}' attribute must be one of these values: {1}.", nodeName, String.Join(", ", validValues.ToArray())));
            }

            if (context.AffectsXsltInitiation)
            {
                throw new ArgumentException("Cannot bind to session when the parameter affects XSLT initiation.");
            }

            BasePageParser pageParser = context.Parser as BasePageParser;

            if (path == bind.it && pageParser != null && pageParser.EnableSessionState == PagesEnableSessionState.False)
            {
                throw new ArgumentException(
                          String.Format(CultureInfo.InvariantCulture, "Cannot bind to {0} because session state is disabled for this page. Try setting enable-session-state=\"true\" on the page processing instruction.", bind.it)
                          );
            }

            NameValueCollection query = (uri.Query.Length > 1) ?
                                        HttpUtility.ParseQueryString(uri.Query.Replace(';', '&')) :
                                        new NameValueCollection();

            var exprInfo = new BindingExpressionInfo(expression)
            {
                ParsedObject = uri
            };

            if (query["name"] != null)
            {
                exprInfo.ParsedValues["name"] = query["name"];
                query.Remove("name");
            }
            else
            {
                XPathNavigator nav = context.BoundNode.Clone();
                nav.MoveToParent();

                if (nav.NodeType == XPathNodeType.Element &&
                    nav.LocalName == "param" &&
                    nav.NamespaceURI == WellKnownNamespaces.XSLT)
                {
                    exprInfo.ParsedValues["name"] = nav.GetAttribute("name", "");
                }
            }

            switch (path)
            {
            case bind.it:
                exprInfo.ParsedValues["remove"] = GetBooleanOrDefault(query["remove"]);
                query.Remove("remove");
                break;

            default:
                if (query["remove"] != null)
                {
                    throw new ArgumentException(
                              String.Format(CultureInfo.InvariantCulture, "The remove option is not valid for {0}.", path)
                              );
                }
                break;
            }

            foreach (string key in query.AllKeys)
            {
                exprInfo.ParsedValues.Add(key, query[key]);
            }

            return(exprInfo);
        }
コード例 #7
0
 public override CodeExpression GetCodeExpression(BindingExpressionInfo exprInfo)
 {
     return(new CodeSnippetExpression(exprInfo.Expression));
 }
コード例 #8
0
        protected void AddInitializeRuntimeOptionsStatements(CodeStatementCollection statements, CodeVariableReferenceExpression optionsVar)
        {
            // Initial template
            //------------------------------------------------

            BindingExpressionInfo bind         = parser.InitialTemplateBinding;
            XmlQualifiedName      initialTempl = parser.InitialTemplate;

            if (bind != null || initialTempl != null)
            {
                CodeAssignStatement assignFromPrimitive = null;

                if (initialTempl != null)
                {
                    assignFromPrimitive = new CodeAssignStatement {
                        Left = new CodePropertyReferenceExpression {
                            PropertyName = "InitialTemplate",
                            TargetObject = optionsVar
                        },
                        Right = new CodeObjectCreateExpression(
                            typeof(XmlQualifiedName),
                            new CodePrimitiveExpression(initialTempl.Name),
                            new CodePrimitiveExpression(initialTempl.Namespace)
                            )
                    };
                }

                if (bind != null)
                {
                    bool bindRequired = initialTempl == null;

                    var paramName = new CodePrimitiveExpression(bind.ParsedValues.ContainsKey("name") ? bind.ParsedValues["name"] : bind.Expression);

                    var initialTemplVar = new CodeVariableDeclarationStatement {
                        Name           = "initialTempl",
                        Type           = new CodeTypeReference(typeof(String)),
                        InitExpression = new CodeMethodInvokeExpression {
                            Method = new CodeMethodReferenceExpression {
                                MethodName   = "CheckParamLength",
                                TargetObject = new CodeThisReferenceExpression()
                            },
                            Parameters =
                            {
                                paramName,
                                bind.GetCodeExpression(),
                                new CodePrimitiveExpression(bindRequired ? 1 : 0),
                                new CodePrimitiveExpression(1)
                            }
                        }
                    };

                    if (bind.LineNumber > 0)
                    {
                        initialTemplVar.LinePragma = new CodeLinePragma(this.parser.PhysicalPath.LocalPath, bind.LineNumber);
                    }

                    if (bind.ParsedValues.ContainsKey("accept"))
                    {
                        initialTemplVar.InitExpression = new CodeMethodInvokeExpression {
                            Method = new CodeMethodReferenceExpression {
                                MethodName   = "CheckParamValues",
                                TargetObject = new CodeThisReferenceExpression()
                            },
                            Parameters =
                            {
                                paramName,
                                initialTemplVar.InitExpression,
                                new CodeArrayCreateExpression(typeof(string), ((string[])bind.ParsedValues["accept"]).Select(s => new CodePrimitiveExpression(s)).ToArray())
                            }
                        };
                    }

                    initialTemplVar.InitExpression = new CodeMethodInvokeExpression {
                        Method = new CodeMethodReferenceExpression {
                            MethodName   = "AsString",
                            TargetObject = new CodeThisReferenceExpression()
                        },
                        Parameters =
                        {
                            initialTemplVar.InitExpression
                        }
                    };

                    statements.Add(initialTemplVar);

                    var assignFromVar = new CodeAssignStatement {
                        Left = new CodePropertyReferenceExpression {
                            PropertyName = "InitialTemplate",
                            TargetObject = optionsVar
                        },
                        Right = new CodeObjectCreateExpression(
                            typeof(XmlQualifiedName),
                            new CodeMethodInvokeExpression {
                            Method = new CodeMethodReferenceExpression {
                                MethodName   = "ToString",
                                TargetObject = new CodeVariableReferenceExpression(initialTemplVar.Name)
                            }
                        }
                            )
                    };

                    if (bindRequired)
                    {
                        statements.Add(assignFromVar);
                    }
                    else
                    {
                        var ifStatement = new CodeConditionStatement {
                            Condition = new CodeBinaryOperatorExpression {
                                Left     = new CodeVariableReferenceExpression(initialTemplVar.Name),
                                Operator = CodeBinaryOperatorType.IdentityInequality,
                                Right    = new CodePrimitiveExpression(null)
                            },
                            TrueStatements  = { assignFromVar },
                            FalseStatements = { assignFromPrimitive }
                        };

                        statements.Add(ifStatement);
                    }
                }
                else
                {
                    statements.Add(assignFromPrimitive);
                }
            }

            // Initial context node
            //------------------------------------------------

            if (initialContextNodeField != null || this.parser.PageType == XsltPageType.SimplifiedStylesheet)
            {
                if (initialContextNodeField != null)
                {
                    statements.Add(
                        new CodeAssignStatement {
                        Left = new CodePropertyReferenceExpression {
                            PropertyName = "InitialContextNode",
                            TargetObject = optionsVar
                        },
                        Right = new CodeFieldReferenceExpression {
                            FieldName    = initialContextNodeField.Name,
                            TargetObject = PageTypeReferenceExpression
                        }
                    }
                        );
                }
                else
                {
                    statements.Add(
                        new CodeAssignStatement {
                        Left = new CodePropertyReferenceExpression {
                            PropertyName = "InitialContextNode",
                            TargetObject = optionsVar
                        },
                        Right = new CodeMethodInvokeExpression {
                            Method = new CodeMethodReferenceExpression {
                                MethodName   = "CreateNodeReadOnly",
                                TargetObject = new CodePropertyReferenceExpression {
                                    PropertyName = "ItemFactory",
                                    TargetObject = new CodePropertyReferenceExpression {
                                        PropertyName = "Processor",
                                        TargetObject = new CodePropertyReferenceExpression {
                                            PropertyName = "Executable",
                                            TargetObject = new CodeThisReferenceExpression()
                                        }
                                    }
                                }
                            },
                        }
                    }
                        );
                }
            }
        }
コード例 #9
0
        public override BindingExpressionInfo ParseExpression(string expression, BindingExpressionContext context)
        {
            // TODO: Fix error messages not to refer to web:bind but expression builder prefix/ns instead

             var uri = new Uri(expression, UriKind.RelativeOrAbsolute);

             if (!uri.IsAbsoluteUri) {
            uri = new Uri(String.Concat(SessionModule.Prefix, ":", uri.OriginalString), UriKind.Absolute);
             }

             var validValues = new List<string>() { bind.it };

             string path = uri.AbsolutePath;
             string nodeName = context.NodeName ?? context.BoundNode.Name;

             if (!validValues.Contains(path))
            throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The value of the '{0}' attribute must be one of these values: {1}.", nodeName, String.Join(", ", validValues.ToArray())));

             if (context.AffectsXsltInitiation) {
            throw new ArgumentException("Cannot bind to session when the parameter affects XSLT initiation.");
             }

             BasePageParser pageParser = context.Parser as BasePageParser;

             if (path == bind.it && pageParser != null && pageParser.EnableSessionState == PagesEnableSessionState.False)
            throw new ArgumentException(
               String.Format(CultureInfo.InvariantCulture, "Cannot bind to {0} because session state is disabled for this page. Try setting enable-session-state=\"true\" on the page processing instruction.", bind.it)
            );

             NameValueCollection query = (uri.Query.Length > 1) ?
            HttpUtility.ParseQueryString(uri.Query.Replace(';', '&')) :
            new NameValueCollection();

             var exprInfo = new BindingExpressionInfo(expression) {
            ParsedObject = uri
             };

             if (query["name"] != null) {

            exprInfo.ParsedValues["name"] = query["name"];
            query.Remove("name");

             } else {

            XPathNavigator nav = context.BoundNode.Clone();
            nav.MoveToParent();

            if (nav.NodeType == XPathNodeType.Element
               && nav.LocalName == "param"
               && nav.NamespaceURI == WellKnownNamespaces.XSLT) {

               exprInfo.ParsedValues["name"] = nav.GetAttribute("name", "");
            }
             }

             switch (path) {
            case bind.it:
               exprInfo.ParsedValues["remove"] = GetBooleanOrDefault(query["remove"]);
               query.Remove("remove");
               break;

            default:
               if (query["remove"] != null) {
                  throw new ArgumentException(
                     String.Format(CultureInfo.InvariantCulture, "The remove option is not valid for {0}.", path)
                  );
               }
               break;
             }

             foreach (string key in query.AllKeys) {
            exprInfo.ParsedValues.Add(key, query[key]);
             }

             return exprInfo;
        }
コード例 #10
0
        public override BindingExpressionInfo ParseExpression(string expression, BindingExpressionContext context)
        {
            // TODO: Fix error messages not to refer to web:bind but expression builder prefix/ns instead

            var uri = new Uri(expression, UriKind.RelativeOrAbsolute);

            if (!uri.IsAbsoluteUri)
            {
                uri = new Uri(String.Concat(RequestModule.Prefix, ":", uri.OriginalString), UriKind.Absolute);
            }

            var validValues = new List <string>()
            {
                bind.query, bind.cookie, bind.form,
                bind.header, bind.http_method
            };

            string path     = uri.AbsolutePath;
            string nodeName = context.NodeName ?? context.BoundNode.Name;

            if (!validValues.Contains(path))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The value of the '{0}' attribute must be one of these values: {1}.", nodeName, String.Join(", ", validValues.ToArray())));
            }

            BasePageParser pageParser = context.Parser as BasePageParser;

            NameValueCollection query = (uri.Query.Length > 1) ?
                                        HttpUtility.ParseQueryString(uri.Query.Replace(';', '&')) :
                                        new NameValueCollection();

            var exprInfo = new BindingExpressionInfo(expression)
            {
                ParsedObject = uri
            };

            if (query["name"] != null)
            {
                exprInfo.ParsedValues["name"] = query["name"];
                query.Remove("name");
            }
            else
            {
                XPathNavigator nav = context.BoundNode.Clone();
                nav.MoveToParent();

                if (nav.NodeType == XPathNodeType.Element &&
                    nav.LocalName == "param" &&
                    nav.NamespaceURI == WellKnownNamespaces.XSLT)
                {
                    exprInfo.ParsedValues["name"] = nav.GetAttribute("name", "");
                }
            }

            if (query["accept"] != null)
            {
                switch (path)
                {
                case bind.http_method:
                    throw new ArgumentException(
                              String.Format(CultureInfo.InvariantCulture, "When '{0}' is set to '{1}' use the '{2}' attribute instead of the 'accept' option.", XsltPageParser.page.bind_initial_template, bind.http_method, XsltPageParser.page.accept_verbs)
                              );
                }

                exprInfo.ParsedValues["accept"] = query["accept"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                query.Remove("accept");
            }
            else
            {
                if (context.AffectsXsltInitiation)
                {
                    if (path != bind.http_method)
                    {
                        throw new ArgumentException(
                                  String.Format(CultureInfo.InvariantCulture, "The 'accept' option is required for '{0}'. Try this: {1}?accept=[comma-separated template names]", XsltPageParser.page.bind_initial_template, path)
                                  );
                    }
                    else if (pageParser != null && pageParser.AcceptVerbs.Count == 0)
                    {
                        throw new ArgumentException(
                                  String.Format(CultureInfo.InvariantCulture, "The '{0}' attribute is required when '{1}' is set to '{2}'.", XsltPageParser.page.accept_verbs, XsltPageParser.page.bind_initial_template, path)
                                  );
                    }
                }
            }

            switch (path)
            {
            case bind.cookie:
                exprInfo.ParsedValues["remove"] = GetBooleanOrDefault(query["remove"]);
                query.Remove("remove");
                break;

            default:
                if (query["remove"] != null)
                {
                    throw new ArgumentException(
                              String.Format(CultureInfo.InvariantCulture, "The remove option is not valid for {0}.", path)
                              );
                }
                break;
            }

            foreach (string key in query.AllKeys)
            {
                exprInfo.ParsedValues.Add(key, query[key]);
            }

            return(exprInfo);
        }
コード例 #11
0
 public abstract CodeExpression GetCodeExpression(BindingExpressionInfo exprInfo);
コード例 #12
0
 public abstract CodeExpression GetCodeExpression(BindingExpressionInfo exprInfo);
コード例 #13
0
 public override CodeExpression GetCodeExpression(BindingExpressionInfo exprInfo)
 {
     return new CodeSnippetExpression(exprInfo.Expression);
 }