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;
		}
		void ProcessAttributesToMember (XamlSchemaContext sctx, StartTagInfo sti, XamlType xt)
		{
			foreach (var p in sti.Attributes) {
				int idx = p.Key.IndexOf (':');
				string prefix = idx > 0 ? p.Key.Substring (0, idx) : String.Empty;
				string aname = idx > 0 ? p.Key.Substring (idx + 1) : p.Key;
				idx = aname.IndexOf ('.');
				if (idx > 0) {
					string apns = prefix.Length > 0 ? r.LookupNamespace (prefix) : r.NamespaceURI;
					var apname = aname.Substring (0, idx);
					var axtn = new XamlTypeName (apns, apname, null);
					var at = sctx.GetXamlType (axtn);
					var am = at.GetAttachableMember (aname.Substring (idx + 1));
					if (am != null)
						sti.Members.Add (new Pair (am, p.Value));
					// ignore unknown attribute
				}
				var xm = xt.GetMember (aname);
				if (xm != null)
					sti.Members.Add (new Pair (xm, p.Value));
				// ignore unknown attribute
			}
		}
		StartTagInfo GetStartTagInfo ()
		{
			string name = r.LocalName;
			string ns = r.NamespaceURI;
			string typeArgNames = null;

			var members = new List<Pair> ();
			var atts = ProcessAttributes (r, members);

			// check TypeArguments to resolve Type, and remove them from the list. They don't appear as a node.
			var l = new List<Pair> ();
			foreach (var p in members) {
				if (p.Key == XamlLanguage.TypeArguments) {
					typeArgNames = p.Value;
					l.Add (p);
					break;
				}
			}
			foreach (var p in l)
				members.Remove (p);

			IList<XamlTypeName> typeArgs = typeArgNames == null ? null : XamlTypeName.ParseList (typeArgNames, xaml_namespace_resolver);
			var xtn = new XamlTypeName (ns, name, typeArgs);
			return new StartTagInfo () { Name = name, Namespace = ns, TypeName = xtn, Members = members, Attributes = atts};
		}
		XamlMember FindAttachableMember(string prefix, string typeName, string memberName)
		{
			string apns = prefix.Length > 0 ? r.LookupNamespace (prefix) : r.NamespaceURI;
			var axtn = new XamlTypeName (apns, typeName, null);
			var at = sctx.GetXamlType (axtn);
			return at?.GetAttachableMember (memberName);
		}
Beispiel #5
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);
        }