internal static object ConvertFromHelper(ITypeDescriptorContext context, object value)
        {
            if (value == null)
            {
                return(null);
            }

            String stringValue = value as String;

            if (stringValue == null)
            {
                return(null);
            }

            stringValue = stringValue.Trim();
            if (stringValue == String.Empty)
            {
                return(null);
            }

            IXamlNamespaceResolver resolver =
                context.GetService(typeof(IXamlNamespaceResolver)) as IXamlNamespaceResolver;

            if (resolver == null)
            {
                return(null);
            }

            if (stringValue[0] == '{')
            {
                return(XName.Get(stringValue));
            }

            int    indexOfColon = stringValue.IndexOf(':');
            string prefix, localName;

            if (indexOfColon >= 0)
            {
                prefix    = stringValue.Substring(0, indexOfColon);
                localName = stringValue.Substring(indexOfColon + 1);
            }
            else
            {
                prefix    = string.Empty;
                localName = stringValue;
            }

            string ns = resolver.GetNamespace(prefix);

            if (ns == null)
            {
                throw FxTrace.Exception.AsError(new FormatException(SRCore.CouldNotResolveNamespacePrefix(prefix)));
            }

            return(XName.Get(localName, ns));
        }
        internal static object ConvertFromHelper(ITypeDescriptorContext context, object value)
        {
            string str2;
            string str3;

            if (value == null)
            {
                return(null);
            }
            string expandedName = value as string;

            if (expandedName == null)
            {
                return(null);
            }
            expandedName = expandedName.Trim();
            if (expandedName == string.Empty)
            {
                return(null);
            }
            IXamlNamespaceResolver service = context.GetService(typeof(IXamlNamespaceResolver)) as IXamlNamespaceResolver;

            if (service == null)
            {
                return(null);
            }
            if (expandedName[0] == '{')
            {
                return(XName.Get(expandedName));
            }
            int index = expandedName.IndexOf(':');

            if (index >= 0)
            {
                str2 = expandedName.Substring(0, index);
                str3 = expandedName.Substring(index + 1);
            }
            else
            {
                str2 = string.Empty;
                str3 = expandedName;
            }
            string namespaceName = service.GetNamespace(str2);

            if (namespaceName == null)
            {
                throw Fx.Exception.AsError(new FormatException(SRCore.CouldNotResolveNamespacePrefix(str2)));
            }
            return(XName.Get(str3, namespaceName));
        }
Esempio n. 3
0
		public static bool TryParse (string typeName, IXamlNamespaceResolver namespaceResolver, out XamlTypeName result)
		{
			if (typeName == null)
				throw new ArgumentNullException ("typeName");
			if (namespaceResolver == null)
				throw new ArgumentNullException ("namespaceResolver");

			result = null;
			IList<XamlTypeName> args = null;

			int idx = typeName.IndexOf ('(');
			if (idx >= 0) {
				if (typeName [typeName.Length - 1] != ')')
					return false;
				if (!TryParseList (typeName.Substring (idx + 1, typeName.Length - idx - 2), namespaceResolver, out args))
					return false;
				typeName = typeName.Substring (0, idx);
			}

			idx = typeName.IndexOf (':');
			string prefix, local;
			if (idx < 0) {
				prefix = String.Empty;
				local = typeName;
			} else {
				prefix = typeName.Substring (0, idx);
				local = typeName.Substring (idx + 1);
				if (!XamlLanguage.IsValidXamlName (prefix))
					return false;
			}
			if (!XamlLanguage.IsValidXamlName (local))
				return false;
			string ns = namespaceResolver.GetNamespace (prefix);
			if (ns == null)
				return false;

			result = new XamlTypeName (ns, local, args);
			return true;
		}
Esempio n. 4
0
        public static bool TryParse(string typeName, IXamlNamespaceResolver namespaceResolver, out XamlTypeName result)
        {
            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }
            if (namespaceResolver == null)
            {
                throw new ArgumentNullException("namespaceResolver");
            }

            result = null;
            IList <XamlTypeName> args = null;
            int nArray = 0;
            int idx;

            if (typeName.Length > 2 && typeName[typeName.Length - 1] == ']')
            {
                idx = typeName.LastIndexOf('[');
                if (idx < 0)
                {
                    return(false);                    // mismatch brace
                }
                nArray = 1;
                for (int i = idx + 1; i < typeName.Length - 1; i++)
                {
                    if (typeName[i] != ',')
                    {
                        return(false);                        // only ',' is expected
                    }
                    nArray++;
                }
                if (!TryParse(typeName.Substring(0, idx), namespaceResolver, out result))
                {
                    return(false);
                }
                // Weird result, but Name ends with '[]'
                result = new XamlTypeName(result.Namespace, result.Name + '[' + new string(',', nArray - 1) + ']', result.TypeArguments);
                return(true);
            }

            idx = typeName.IndexOf('(');
            if (idx >= 0)
            {
                if (typeName[typeName.Length - 1] != ')')
                {
                    return(false);
                }
                if (!TryParseList(typeName.Substring(idx + 1, typeName.Length - idx - 2), namespaceResolver, out args))
                {
                    return(false);
                }
                typeName = typeName.Substring(0, idx);
            }

            idx = typeName.IndexOf(':');
            string prefix, local;

            if (idx < 0)
            {
                prefix = String.Empty;
                local  = typeName;
            }
            else
            {
                prefix = typeName.Substring(0, idx);
                local  = typeName.Substring(idx + 1);
                if (!XamlLanguage.IsValidXamlName(prefix))
                {
                    return(false);
                }
            }
            if (!XamlLanguage.IsValidXamlName(local, true))
            {
                return(false);
            }
            string ns = namespaceResolver.GetNamespace(prefix);

            if (ns == null)
            {
                return(false);
            }

            result = new XamlTypeName(ns, local, args);
            return(true);
        }
		public static bool TryParse (string typeName, IXamlNamespaceResolver namespaceResolver, out XamlTypeName result)
		{
			if (typeName == null)
				throw new ArgumentNullException ("typeName");
			if (namespaceResolver == null)
				throw new ArgumentNullException ("namespaceResolver");

			result = null;
			IList<XamlTypeName> args = null;
			int nArray = 0;
			int idx;

			if (typeName.Length > 2 && typeName [typeName.Length - 1] == ']') {
				idx = typeName.LastIndexOf ('[');
				if (idx < 0)
					return false; // mismatch brace
				nArray = 1;
				for (int i = idx + 1; i < typeName.Length - 1; i++) {
					if (typeName [i] != ',')
						return false; // only ',' is expected
					nArray++;
				}
				if (!TryParse (typeName.Substring (0, idx), namespaceResolver, out result))
					return false;
				// Weird result, but Name ends with '[]'
				result = new XamlTypeName (result.Namespace, result.Name + '[' + new string (',', nArray - 1) + ']', result.TypeArguments);
				return true;
			}

			idx = typeName.IndexOf ('(');
			if (idx >= 0) {
				if (typeName [typeName.Length - 1] != ')')
					return false;
				if (!TryParseList (typeName.Substring (idx + 1, typeName.Length - idx - 2), namespaceResolver, out args))
					return false;
				typeName = typeName.Substring (0, idx);
			}

			idx = typeName.IndexOf (':');
			string prefix, local;
			if (idx < 0) {
				prefix = String.Empty;
				local = typeName;
			} else {
				prefix = typeName.Substring (0, idx);
				local = typeName.Substring (idx + 1);
				if (!XamlLanguage.IsValidXamlName (prefix))
					return false;
			}
			if (!XamlLanguage.IsValidXamlName (local))
				return false;
			string ns = namespaceResolver.GetNamespace (prefix);
			if (ns == null)
				return false;

			result = new XamlTypeName (ns, local, args);
			return true;
		}
Esempio n. 6
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);
        }