public static BindingExpressionInfo ParseExpr(string expression, BindingExpressionContext context) { if (expression == null) throw new ArgumentNullException("expression"); if (context == null) throw new ArgumentNullException("context"); string ns = context.Namespace ?? context.BoundNode.NamespaceURI; BindingExpressionBuilder exprBuilder; ExpressionBuilderElement el = LibraryConfigSection.Instance.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(expression, context); if (exprInfo == null) exprInfo = new BindingExpressionInfo(expression); exprInfo.ExpressionBuilder = exprBuilder; return exprInfo; }
public override CodeExpression GetCodeExpression(BindingExpressionInfo exprInfo) { return new CodeSnippetExpression(exprInfo.Expression); }
public abstract CodeExpression GetCodeExpression(BindingExpressionInfo exprInfo);
public override BindingExpressionInfo ParseExpression (string expression, BindingExpressionContext context) { Uri uri = new Uri (expression, UriKind.RelativeOrAbsolute); if (!uri.IsAbsoluteUri) uri = new Uri (String.Concat (RequestModule.Prefix, ":", uri.OriginalString), UriKind.Absolute); List<string> validValues = new List<string> () { Bind.Query, Bind.Cookie, Bind.Form, Bind.Header, Bind.HttpMethod }; string path = uri.AbsolutePath; if (!validValues.Contains (path)) { nodeName = context.NodeName ?? context.BoundNode.Name; 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 (); BindingExpressionInfo 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.HttpMethod: 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.HttpMethod, XsltPageParser.page.accept_verbs) ); } exprInfo.ParsedValues ["accept"] = query ["accept"].Split (new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); query.Remove ("accept"); } else { if (context.AffectsXsltInitiation) { if (path != Bind.HttpMethod) { 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) ); } 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) ); } } } if (query ["remove"] != null) { switch (path) { case Bind.Cookie: exprInfo.ParsedValues ["remove"] = GetBooleanOrDefault (query ["remove"]); query.Remove ("remove"); break; default: throw new ArgumentException ( String.Format (CultureInfo.InvariantCulture, "The remove option is not valid for {0}.", path) ); } } foreach (string key in query.AllKeys) exprInfo.ParsedValues.Add (key, query [key]); return exprInfo; }
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.HttpMethod: return GetHttpMethodExpression (); default: throw new ArgumentException (String.Format (CultureInfo.InvariantCulture, "Invalid path '{0}'.", path), "uri"); } }