Ejemplo n.º 1
0
 public void TryParseNullResolver()
 {
     Assert.Throws(typeof(ArgumentNullException), () =>
     {
         XamlTypeName.TryParse("Foo", null, out dummy);
     });
 }
Ejemplo n.º 2
0
        public void Parse()
        {
            //Get all inside brackets
            if (value.Length > 1 && (value[0] != '{' || value[value.Length - 1] != '}'))
            {
                throw new XamlParseException("Invalid markup extension attribute. It should begin with '{{' and end with '}}'");
            }
            value = value.Substring(1, value.Length - 2);
            Name  = ReadUntil(' ', true);
            XamlTypeName xtn;

            if (!XamlTypeName.TryParse(Name, nsResolver, out xtn))
            {
                throw Error("Failed to parse type name '{0}'", Name);
            }

            var xtnFirst = new XamlTypeName(xtn.Namespace, xtn.Name + "Extension", xtn.TypeArguments);
            var xtFirst  = sctx.GetXamlType(xtnFirst);

            //if type with Extension postfix is not resolved or unknown we try to get it without the prefix
            Type = ((xtFirst == null || xtFirst.IsUnknown) ? null : xtFirst) ??
                   sctx.GetXamlType(xtn) ??
                   new XamlType(xtn.Namespace, xtn.Name, null, sctx);

            ParseArgument();
        }
Ejemplo n.º 3
0
        public void TryParseInvalidGenericName()
        {
            var r = new MyNSResolver();

            r.Add("x", "urn:foo");
            Assert.IsFalse(XamlTypeName.TryParse("x:Foo()", r, out dummy), "#1");
        }
Ejemplo n.º 4
0
 public void TryParseNullName()
 {
     Assert.Throws(typeof(ArgumentNullException), () =>
     {
         XamlTypeName.TryParse(null, new MyNSResolver(), out dummy);
     });
 }
Ejemplo n.º 5
0
        public void TryParseGenericName()
        {
            var r = new MyNSResolver();

            r.Add("x", "urn:foo");
            Assert.IsTrue(XamlTypeName.TryParse("x:Foo(x:Foo,x:Bar)", r, out dummy), "#1");
            Assert.AreEqual(2, dummy.TypeArguments.Count, "#2");
        }
Ejemplo n.º 6
0
        public void TryParseColon()
        {
            var r = new MyNSResolver();

            r.Add("a", "urn:foo");
            Assert.IsFalse(XamlTypeName.TryParse(":", r, out dummy), "#1");
            Assert.IsFalse(XamlTypeName.TryParse("a:", r, out dummy), "#2");
            Assert.IsFalse(XamlTypeName.TryParse(":b", r, out dummy), "#3");
        }
Ejemplo n.º 7
0
        public void TryParseFillEmpty()
        {
            var r = new MyNSResolver();

            Assert.IsTrue(XamlTypeName.TryParse("Foo", r, out dummy), "#1");
            Assert.IsNotNull(dummy, "#2");
            Assert.AreEqual(String.Empty, dummy.Namespace, "#2-2");
            Assert.AreEqual("Foo", dummy.Name, "#2-3");
        }
Ejemplo n.º 8
0
        public void TryParseResolveSuccess()
        {
            var r = new MyNSResolver();

            r.Add("x", "urn:foo");
            Assert.IsTrue(XamlTypeName.TryParse("x:Foo", r, out dummy), "#1");
            Assert.IsNotNull(dummy, "#2");
            Assert.AreEqual("urn:foo", dummy.Namespace, "#2-2");
            Assert.AreEqual("Foo", dummy.Name, "#2-3");
        }
Ejemplo n.º 9
0
        public void TryParseInvalidName()
        {
            var r = new MyNSResolver();

            r.Add("a", "urn:foo");
            r.Add("#", "urn:bar");
            Assert.IsFalse(XamlTypeName.TryParse("$%#___!", r, out dummy), "#1");
            Assert.IsFalse(XamlTypeName.TryParse("a:#$#", r, out dummy), "#2");
            Assert.IsFalse(XamlTypeName.TryParse("#:foo", r, out dummy), "#3");
        }
Ejemplo n.º 10
0
        internal Type ResolvePrefixedNameWithAdditionalWpfSemantics(string prefixedName, DependencyObject element)
        {
            // First, get the xmlns dictionary off of the provided Tree Element.
            object dictObject       = element.GetValue(System.Windows.Markup.XmlAttributeProperties.XmlnsDictionaryProperty);
            var    prefixDictionary = dictObject as System.Windows.Markup.XmlnsDictionary;
            object mapsObject       = element.GetValue(System.Windows.Markup.XmlAttributeProperties.XmlNamespaceMapsProperty);
            var    namespaceMaps    = mapsObject as Hashtable;

            // If there was no xmlns map on the given Tree Element.
            // Then, as a last resort, if the prefix was "" use the Wpf Element URI.
            if (prefixDictionary == null)
            {
                if (_wpfDefaultNamespace == null)
                {
                    var wpfDefaultNamespace = new System.Windows.Markup.XmlnsDictionary();
                    wpfDefaultNamespace.Add(String.Empty, Baml2006SchemaContext.WpfNamespace);
                    _wpfDefaultNamespace = wpfDefaultNamespace;
                }
                prefixDictionary = _wpfDefaultNamespace;
            }
            else
            {
                if (namespaceMaps != null && namespaceMaps.Count > 0)
                {
                    // This DO was loaded with a custom XamlTypeMapper. Try the custom mappings first.
                    Type result = System.Windows.Markup.XamlTypeMapper.GetTypeFromName(prefixedName, element);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }
            XamlTypeName xamlTypeName;

            if (XamlTypeName.TryParse(prefixedName, prefixDictionary, out xamlTypeName))
            {
                XamlType xamlType = GetXamlType(xamlTypeName);
                if (xamlType != null)
                {
                    return(xamlType.UnderlyingType);
                }
            }
            return(null);
        }
Ejemplo n.º 11
0
        public void Parse()
        {
            if (!Read('{'))
            {
                throw Error("Invalid markup extension attribute. It should begin with '{{', but was {0}", value);
            }
            Name = ReadUntil(' ', true);
            XamlTypeName xtn;

            if (!XamlTypeName.TryParse(Name, nsResolver, out xtn))
            {
                throw Error("Failed to parse type name '{0}'", Name);
            }
            Type = sctx.GetXamlType(xtn);

            ParseArgument();
            if (!Read('}'))
            {
                throw Error("Expected '}}' in the markup extension attribute: '{0}'", value);
            }
        }
Ejemplo n.º 12
0
 public void TryParseNullName()
 {
     Assert.Throws <ArgumentNullException> (() => XamlTypeName.TryParse(null, new MyNSResolver(), out dummy));
 }
Ejemplo n.º 13
0
 public void TryParseResolveFailure()
 {
     Assert.IsFalse(XamlTypeName.TryParse("x:Foo", new MyNSResolver(), out dummy), "#1");
 }
Ejemplo n.º 14
0
 public void TryParseAlreadyQualified()
 {
     Assert.IsFalse(XamlTypeName.TryParse("{urn:foo}Foo", new MyNSResolver(), out dummy), "#1");
 }
        public static ParsedMarkupExtensionInfo Parse(string raw, IXamlNamespaceResolver nsResolver, XamlSchemaContext sctx)
        {
            if (raw == null)
            {
                throw new ArgumentNullException("raw");
            }
            if (raw.Length == 0 || raw [0] != '{')
            {
                throw Error("Invalid markup extension attribute. It should begin with '{{', but was {0}", raw);
            }
            var ret = new ParsedMarkupExtensionInfo();
            int idx = raw.IndexOf('}');

            if (idx < 0)
            {
                throw Error("Expected '}}' in the markup extension attribute: '{0}'", raw);
            }
            raw = raw.Substring(1, idx - 1);
            idx = raw.IndexOf(' ');
            string name = idx < 0 ? raw : raw.Substring(0, idx);

            XamlTypeName xtn;

            if (!XamlTypeName.TryParse(name, nsResolver, out xtn))
            {
                throw Error("Failed to parse type name '{0}'", name);
            }
            var xt = sctx.GetXamlType(xtn);

            ret.Type = xt;

            if (idx < 0)
            {
                return(ret);
            }

            string []     vpairs  = raw.Substring(idx + 1, raw.Length - idx - 1).Split(',');
            List <string> posPrms = null;

            foreach (string vpair in vpairs)
            {
                idx = vpair.IndexOf('=');
                // FIXME: unescape string (e.g. comma)
                if (idx < 0)
                {
                    if (posPrms == null)
                    {
                        posPrms = new List <string> ();
                        ret.Arguments.Add(XamlLanguage.PositionalParameters, posPrms);
                    }
                    posPrms.Add(UnescapeValue(vpair.Trim()));
                }
                else
                {
                    var key = vpair.Substring(0, idx).Trim();
                    // FIXME: is unknown member always isAttacheable = false?
                    var xm = xt.GetMember(key) ?? new XamlMember(key, xt, false);
                    ret.Arguments.Add(xm, UnescapeValue(vpair.Substring(idx + 1).Trim()));
                }
            }
            return(ret);
        }
Ejemplo n.º 16
0
 public void TryParseNoFillEmpty()
 {
     Assert.IsFalse(XamlTypeName.TryParse("Foo", new MyNSResolver(true), out dummy), "#1");
 }
Ejemplo n.º 17
0
        public static ParsedMarkupExtensionInfo Parse(string raw, IXamlNamespaceResolver nsResolver, XamlSchemaContext sctx)
        {
            if (raw == null)
            {
                throw new ArgumentNullException(nameof(raw));
            }

            if (raw.Length == 0 || raw[0] != '{')
            {
                throw Error("Invalid markup extension attribute. It should begin with '{{', but was {0}", raw);
            }

            if (raw.Length >= 2 && raw[1] == '}')
            {
                throw Error("Markup extension can not begin with an '{}' escape: '{0}'", raw);
            }

            var ret = new ParsedMarkupExtensionInfo();

            if (raw[raw.Length - 1] != '}')
            {
                // Any character after the final closing bracket is not accepted. Therefore, the last character should be '}'.
                // Ideally, we should still ran the entire markup through the parser to get a more meaningful error.
                throw Error("Expected '}}' in the markup extension attribute: '{0}'", raw);
            }

            var nameSeparatorIndex = raw.IndexOf(' ');
            var name = nameSeparatorIndex != -1 ? raw.Substring(1, nameSeparatorIndex - 1) : raw.Substring(1, raw.Length - 2);

            if (!XamlTypeName.TryParse(name, nsResolver, out var xtn))
            {
                throw Error("Failed to parse type name '{0}'", name);
            }

            var xt = sctx.GetXamlType(xtn) ?? new XamlType(xtn.Namespace, xtn.Name, null, sctx);

            ret.Type = xt;

            if (nameSeparatorIndex < 0)
            {
                return(ret);
            }

            var valueWithoutBinding = raw.Substring(nameSeparatorIndex + 1, raw.Length - 1 - (nameSeparatorIndex + 1));
            var vpairs = SliceParameters(valueWithoutBinding, raw);

            List <string> posPrms    = null;
            XamlMember    lastMember = null;

            foreach (var vpair in vpairs)
            {
                var idx = vpair.IndexOf('=');

                // FIXME: unescape string (e.g. comma)
                if (idx < 0)
                {
                    if (vpair.ElementAtOrDefault(0) == ')')
                    {
                        if (lastMember != null)
                        {
                            if (ret.Arguments[lastMember] is string s)
                            {
                                ret.Arguments[lastMember] = s + ')';
                            }
                        }
                        else
                        {
                            posPrms[posPrms.Count - 1] += ')';
                        }
                    }
                    else
                    {
                        if (posPrms == null)
                        {
                            posPrms = new List <string>();
                            ret.Arguments.Add(XamlLanguage.PositionalParameters, posPrms);
                        }

                        posPrms.Add(UnescapeValue(vpair.Trim()));
                    }
                }
                else
                {
                    var key = vpair.Substring(0, idx).Trim();
                    // FIXME: is unknown member always isAttacheable = false?
                    var xm = xt.GetMember(key) ?? new XamlMember(key, xt, false);

                    // Binding member values may be wrapped in quotes (single or double) e.g. 'A,B,C,D'.
                    // Remove those wrapping quotes from the resulting string value.
                    var valueString = RemoveWrappingStringQuotes(vpair.Substring(idx + 1).Trim());

                    var value = IsValidMarkupExtension(valueString)
                                                ? (object)Parse(valueString, nsResolver, sctx) : UnescapeValue(valueString);

                    ret.Arguments.Add(xm, value);
                    lastMember = xm;
                }
            }
            return(ret);
        }
Ejemplo n.º 18
0
 public void TryParseNullName()
 {
     XamlTypeName.TryParse(null, new MyNSResolver(), out dummy);
 }
Ejemplo n.º 19
0
 public void TryParseNullResolver()
 {
     XamlTypeName.TryParse("Foo", null, out dummy);
 }
Ejemplo n.º 20
0
        public static ParsedMarkupExtensionInfo Parse(string raw, IXamlNamespaceResolver nsResolver, XamlSchemaContext sctx)
        {
            if (raw == null)
            {
                throw new ArgumentNullException(nameof(raw));
            }

            if (raw.Length == 0 || raw[0] != '{')
            {
                throw Error("Invalid markup extension attribute. It should begin with '{{', but was {0}", raw);
            }

            var ret = new ParsedMarkupExtensionInfo();
            int idx = raw.LastIndexOf('}');

            if (idx < 0)
            {
                throw Error("Expected '}}' in the markup extension attribute: '{0}'", raw);
            }

            raw = raw.Substring(1, idx - 1);
            idx = raw.IndexOf(' ');
            string name = idx < 0 ? raw : raw.Substring(0, idx);

            XamlTypeName xtn;

            if (!XamlTypeName.TryParse(name, nsResolver, out xtn))
            {
                throw Error("Failed to parse type name '{0}'", name);
            }

            var xt = sctx.GetXamlType(xtn) ?? new XamlType(nsResolver.GetNamespace(""), name, null, sctx);

            ret.Type = xt;

            if (idx < 0)
            {
                return(ret);
            }

            var valueWithoutBinding = raw.Substring(idx + 1, raw.Length - idx - 1);

            var vpairs = BindingMembersRegex.Matches(valueWithoutBinding)
                         .Cast <Match>()
                         .Select(m => m.Value.Trim())
                         .ToList();

            if (vpairs.Count == 0)
            {
                vpairs.Add(valueWithoutBinding);
            }

            List <string> posPrms    = null;
            XamlMember    lastMember = null;

            foreach (var vpair in vpairs)
            {
                idx = vpair.IndexOf('=');

                // FIXME: unescape string (e.g. comma)
                if (idx < 0)
                {
                    if (vpair.ElementAtOrDefault(0) == ')')
                    {
                        if (lastMember != null)
                        {
                            if (ret.Arguments[lastMember] is string s)
                            {
                                ret.Arguments[lastMember] = s + ')';
                            }
                        }
                        else
                        {
                            posPrms[posPrms.Count - 1] += ')';
                        }
                    }
                    else
                    {
                        if (posPrms == null)
                        {
                            posPrms = new List <string>();
                            ret.Arguments.Add(XamlLanguage.PositionalParameters, posPrms);
                        }

                        posPrms.Add(UnescapeValue(vpair.Trim()));
                    }
                }
                else
                {
                    var key = vpair.Substring(0, idx).Trim();
                    // FIXME: is unknown member always isAttacheable = false?
                    var xm = xt.GetMember(key) ?? new XamlMember(key, xt, false);

                    // Binding member values may be wrapped in quotes (single or double) e.g. 'A,B,C,D'.
                    // Remove those wrapping quotes from the resulting string value.
                    var valueString = RemoveWrappingStringQuotes(vpair.Substring(idx + 1).Trim());

                    var value = IsValidMarkupExtension(valueString)
                                                ? (object)Parse(valueString, nsResolver, sctx) : UnescapeValue(valueString);

                    ret.Arguments.Add(xm, value);
                    lastMember = xm;
                }
            }
            return(ret);
        }
Ejemplo n.º 21
0
 public void TryParseEmptyName()
 {
     Assert.IsFalse(XamlTypeName.TryParse(String.Empty, new MyNSResolver(), out dummy), "#1");
 }
Ejemplo n.º 22
0
 public void TryParseNullResolver()
 {
     Assert.Throws <ArgumentNullException> (() => XamlTypeName.TryParse("Foo", null, out dummy));
 }