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);
        }
Exemple #2
0
 protected BasePageCodeDomTreeGenerator(BasePageParser parser)
 {
     this.parser = parser;
 }
        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);
        }