Exemplo n.º 1
0
		public static IList<XamlTypeName> ParseList (string typeNameList, IXamlNamespaceResolver namespaceResolver)
		{
			IList<XamlTypeName> list;
			if (!TryParseList (typeNameList, namespaceResolver, out list))
				throw new FormatException (String.Format ("Invalid type name list: '{0}'", typeNameList));
			return list;
		}
Exemplo n.º 2
0
		public static XamlTypeName Parse (string typeName, IXamlNamespaceResolver namespaceResolver)
		{
			XamlTypeName n;
			if (!TryParse (typeName, namespaceResolver, out n))
				throw new FormatException (String.Format ("Invalid typeName: '{0}'", typeName));
			return n;
		}
		public ParsedMarkupExtensionInfo(ParsedMarkupExtensionInfo info)
		{
			this.value = info.value;
			this.index = info.index;
			this.nsResolver = info.nsResolver;
			this.sctx = info.sctx;
		}
		public ParsedMarkupExtensionInfo(string value, IXamlNamespaceResolver nsResolver, XamlSchemaContext sctx)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			this.value = value;
			this.nsResolver = nsResolver;
			this.sctx = sctx;
		}
Exemplo n.º 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))
				return false;
			string ns = namespaceResolver.GetNamespace (prefix);
			if (ns == null)
				return false;

			result = new XamlTypeName (ns, local, args);
			return true;
		}
Exemplo n.º 6
0
        public static IList <XamlTypeName> ParseList(string typeNameList, IXamlNamespaceResolver namespaceResolver)
        {
            IList <XamlTypeName> list;

            if (!TryParseList(typeNameList, namespaceResolver, out list))
            {
                throw new FormatException(String.Format("Invalid type name list: '{0}'", typeNameList));
            }
            return(list);
        }
Exemplo n.º 7
0
        public static XamlTypeName Parse(string typeName, IXamlNamespaceResolver namespaceResolver)
        {
            XamlTypeName n;

            if (!TryParse(typeName, namespaceResolver, out n))
            {
                throw new FormatException(String.Format("Invalid typeName: '{0}'", typeName));
            }
            return(n);
        }
Exemplo n.º 8
0
        public override object Load(XamlReader xamlReader, IServiceProvider context)
        {
            FuncFactory            factory    = FuncFactory.CreateFactory(xamlReader, context);
            IXamlNamespaceResolver nsResolver = context.GetService(typeof(IXamlNamespaceResolver)) as IXamlNamespaceResolver;

            if (nsResolver != null)
            {
                factory.ParentNamespaces = nsResolver.GetNamespacePrefixes().ToList();
            }
            return(factory.GetFunc());
        }
        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));
        }
Exemplo n.º 10
0
        public static bool TryParseList(string typeNameList, IXamlNamespaceResolver namespaceResolver,
                                        out IList <XamlTypeName> result)
        {
            if (typeNameList == null)
            {
                throw new ArgumentNullException(nameof(typeNameList));
            }
            if (namespaceResolver == null)
            {
                throw new ArgumentNullException(nameof(namespaceResolver));
            }

            result = ParseListInternal(typeNameList, namespaceResolver.GetNamespace, out _);
            return(result != null);
        }
        public static bool TryParseList(string typeNameList, IXamlNamespaceResolver namespaceResolver, out IList <XamlTypeName> result)
        {
            string str;

            if (typeNameList == null)
            {
                throw new ArgumentNullException("typeNameList");
            }
            if (namespaceResolver == null)
            {
                throw new ArgumentNullException("namespaceResolver");
            }
            result = ParseListInternal(typeNameList, new Func <string, string>(namespaceResolver.GetNamespace), out str);
            return(result != null);
        }
Exemplo n.º 12
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 = ParseInternal(typeName, namespaceResolver.GetNamespace, out _);
            return(result != null);
        }
		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;
		}
        public static XamlTypeName Parse(string typeName, IXamlNamespaceResolver namespaceResolver)
        {
            string str;

            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }
            if (namespaceResolver == null)
            {
                throw new ArgumentNullException("namespaceResolver");
            }
            XamlTypeName name = ParseInternal(typeName, new Func <string, string>(namespaceResolver.GetNamespace), out str);

            if (name == null)
            {
                throw new FormatException(str);
            }
            return(name);
        }
Exemplo n.º 15
0
        public static IList <XamlTypeName> ParseList(string typeNameList, IXamlNamespaceResolver namespaceResolver)
        {
            if (typeNameList == null)
            {
                throw new ArgumentNullException("typeNameList");
            }
            if (namespaceResolver == null)
            {
                throw new ArgumentNullException("namespaceResolver");
            }

            string error;
            IList <XamlTypeName> result = ParseListInternal(typeNameList, namespaceResolver.GetNamespace, out error);

            if (result == null)
            {
                throw new FormatException(error);
            }
            return(result);
        }
        public static IList <XamlTypeName> ParseList(string typeNameList, IXamlNamespaceResolver namespaceResolver)
        {
            string str;

            if (typeNameList == null)
            {
                throw new ArgumentNullException("typeNameList");
            }
            if (namespaceResolver == null)
            {
                throw new ArgumentNullException("namespaceResolver");
            }
            IList <XamlTypeName> list = ParseListInternal(typeNameList, new Func <string, string>(namespaceResolver.GetNamespace), out str);

            if (list == null)
            {
                throw new FormatException(str);
            }
            return(list);
        }
Exemplo n.º 17
0
        public static XamlTypeName Parse(string typeName, IXamlNamespaceResolver namespaceResolver)
        {
            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }
            if (namespaceResolver == null)
            {
                throw new ArgumentNullException("namespaceResolver");
            }

            string       error;
            XamlTypeName result = ParseInternal(typeName, namespaceResolver.GetNamespace, out error);

            if (result == null)
            {
                throw new FormatException(error);
            }
            return(result);
        }
Exemplo n.º 18
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            IXamlSchemaContextProvider schemaContextProvider =
                (IXamlSchemaContextProvider)context.GetService(typeof(IXamlSchemaContextProvider));
            IAmbientProvider ambientValueProvider =
                (IAmbientProvider)context.GetService(typeof(IAmbientProvider));
            IXamlNamespaceResolver namespaceResolver =
                (IXamlNamespaceResolver)context.GetService(typeof(IXamlNamespaceResolver));

            XamlTypeName typeName        = XamlTypeName.Parse("TemplateClass2", namespaceResolver);
            XamlType     xamlType        = schemaContextProvider.SchemaContext.GetXamlType(typeName);
            XamlMember   ambientProperty = xamlType.GetMember("Suffix");
            IEnumerable <AmbientPropertyValue> propVals = ambientValueProvider.GetAllAmbientValues(null, ambientProperty);
            string s = (string)value;

            foreach (AmbientPropertyValue val in propVals)
            {
                s += (string)val.Value;
            }
            return(s);
        }
        private static XamlType ConvertStringToXamlType(ITypeDescriptorContext context, string typeName)
        {
            IXamlNamespaceResolver service = GetService <IXamlNamespaceResolver>(context);

            if (service == null)
            {
                return(null);
            }
            XamlTypeName name = XamlTypeName.Parse(typeName, service);
            IXamlSchemaContextProvider provider = GetService <IXamlSchemaContextProvider>(context);

            if (provider == null)
            {
                return(null);
            }
            if (provider.SchemaContext == null)
            {
                return(null);
            }
            return(GetXamlTypeOrUnknown(provider.SchemaContext, name));
        }
Exemplo n.º 20
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;
		}
Exemplo n.º 21
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value.GetType() != typeof(string))
            {
                return(base.ConvertFrom(context, culture, value));
            }

            IXamlNamespaceResolver namespaceResolver = (IXamlNamespaceResolver)context.GetService(typeof(IXamlNamespaceResolver));

            int expectedNamespaceCount = int.Parse(value.ToString());
            int count = 0;

            foreach (NamespaceDeclaration namespaceDeclaration in namespaceResolver.GetNamespacePrefixes())
            {
                count++;
            }

            if (count != expectedNamespaceCount)
            {
                throw new Exception("Count of items returned from IXamlNamespaceResolver.GetNamespacePrefixes should be " + value);
            }

            return(expectedNamespaceCount);
        }
Exemplo n.º 22
0
		public static bool TryParseList (string typeNameList, IXamlNamespaceResolver namespaceResolver, out IList<XamlTypeName> list)
		{
			if (typeNameList == null)
				throw new ArgumentNullException ("typeNameList");
			if (namespaceResolver == null)
				throw new ArgumentNullException ("namespaceResolver");

			list = null;
			int idx = 0;
			int parens = 0;
			XamlTypeName tn;

			List<string> l = new List<string> ();
			int lastToken = 0;
			while (true) {
				int i = typeNameList.IndexOfAny (comma_or_parens, idx);
				if (i < 0) {
					l.Add (typeNameList.Substring (lastToken));
					break;
				}
				
				switch (typeNameList [i]) {
				case ',':
					if (parens != 0)
						break;
					l.Add (typeNameList.Substring (idx, i - idx));
					break;
				case '(':
					parens++;
					break;
				case ')':
					parens--;
					break;
				}
				idx = i + 1;
				while (idx < typeNameList.Length && typeNameList [idx] == ' ')
					idx++;
				if (parens == 0 && typeNameList [i] == ',')
					lastToken = idx;
			}

			var ret = new List<XamlTypeName> ();
		 	foreach (var s in l) {
				if (!TryParse (s, namespaceResolver, out tn))
					return false;
				ret.Add (tn);
			}

			list = ret;
			return true;
		}
Exemplo n.º 23
0
		public static bool TryParseList (string typeNameList, IXamlNamespaceResolver namespaceResolver, out IList<XamlTypeName> list)
		{
			if (typeNameList == null)
				throw new ArgumentNullException ("typeNameList");
			if (namespaceResolver == null)
				throw new ArgumentNullException ("namespaceResolver");

			list = null;
			var split = typeNameList.Split (commas);
			if (split.Length == 0)
				return false;

			var arr = new XamlTypeName [split.Length];

			for (int i = 0; i < split.Length; i++) {
				var s = split [i].Trim ();
				XamlTypeName tn;
				if (!TryParse (s, namespaceResolver, out tn))
					return false;
				arr [i] = tn;
			}

			list = arr;
			return true;
		}
        public static VisualBasicSettings CollectXmlNamespacesAndAssemblies(ITypeDescriptorContext context)
        {
            IList <Assembly>           referenceAssemblies = null;
            IXamlSchemaContextProvider service             = context.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;

            if ((service != null) && (service.SchemaContext != null))
            {
                referenceAssemblies = service.SchemaContext.ReferenceAssemblies;
                if ((referenceAssemblies != null) && (referenceAssemblies.Count == 0))
                {
                    referenceAssemblies = null;
                }
            }
            VisualBasicSettings    settings = null;
            IXamlNamespaceResolver resolver = (IXamlNamespaceResolver)context.GetService(typeof(IXamlNamespaceResolver));

            if (resolver == null)
            {
                return(null);
            }
            lock (AssemblyCache.XmlnsMappings)
            {
                foreach (System.Xaml.NamespaceDeclaration declaration in resolver.GetNamespacePrefixes())
                {
                    XmlnsMapping mapping;
                    XNamespace   key = XNamespace.Get(declaration.Namespace);
                    if (!AssemblyCache.XmlnsMappings.TryGetValue(key, out mapping))
                    {
                        Match match = assemblyQualifiedNamespaceRegex.Match(declaration.Namespace);
                        if (match.Success)
                        {
                            mapping.ImportReferences = new System.Collections.Generic.HashSet <VisualBasicImportReference>();
                            VisualBasicImportReference item = new VisualBasicImportReference {
                                Assembly = match.Groups["assembly"].Value,
                                Import   = match.Groups["namespace"].Value,
                                Xmlns    = key
                            };
                            mapping.ImportReferences.Add(item);
                        }
                        else
                        {
                            mapping.ImportReferences = new System.Collections.Generic.HashSet <VisualBasicImportReference>();
                        }
                        AssemblyCache.XmlnsMappings[key] = mapping;
                    }
                    if (!mapping.IsEmpty)
                    {
                        if (settings == null)
                        {
                            settings = new VisualBasicSettings();
                        }
                        foreach (VisualBasicImportReference reference2 in mapping.ImportReferences)
                        {
                            if (referenceAssemblies != null)
                            {
                                VisualBasicImportReference reference3;
                                AssemblyName assemblyName = reference2.AssemblyName;
                                if (reference2.EarlyBoundAssembly != null)
                                {
                                    if (referenceAssemblies.Contains(reference2.EarlyBoundAssembly))
                                    {
                                        reference3 = reference2.Clone();
                                        reference3.EarlyBoundAssembly = reference2.EarlyBoundAssembly;
                                        settings.ImportReferences.Add(reference3);
                                    }
                                }
                                else
                                {
                                    for (int i = 0; i < referenceAssemblies.Count; i++)
                                    {
                                        if (AssemblySatisfiesReference(VisualBasicHelper.GetFastAssemblyName(referenceAssemblies[i]), assemblyName))
                                        {
                                            reference3 = reference2.Clone();
                                            reference3.EarlyBoundAssembly = referenceAssemblies[i];
                                            settings.ImportReferences.Add(reference3);
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                VisualBasicImportReference reference4 = reference2.Clone();
                                if (reference2.EarlyBoundAssembly != null)
                                {
                                    reference4.EarlyBoundAssembly = reference2.EarlyBoundAssembly;
                                }
                                settings.ImportReferences.Add(reference4);
                            }
                        }
                    }
                }
            }
            return(settings);
        }
Exemplo n.º 25
0
        public static VisualBasicSettings CollectXmlNamespacesAndAssemblies(ITypeDescriptorContext context)
        {
            // access XamlSchemaContext.ReferenceAssemblies
            // for the Compiled Xaml scenario
            IList <Assembly>           xsCtxReferenceAssemblies  = null;
            IXamlSchemaContextProvider xamlSchemaContextProvider = context.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;

            if (xamlSchemaContextProvider != null && xamlSchemaContextProvider.SchemaContext != null)
            {
                xsCtxReferenceAssemblies = xamlSchemaContextProvider.SchemaContext.ReferenceAssemblies;
                if (xsCtxReferenceAssemblies != null && xsCtxReferenceAssemblies.Count == 0)
                {
                    xsCtxReferenceAssemblies = null;
                }
            }

            VisualBasicSettings    settings          = null;
            IXamlNamespaceResolver namespaceResolver = (IXamlNamespaceResolver)context.GetService(typeof(IXamlNamespaceResolver));

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

            lock (AssemblyCache.XmlnsMappingsLockObject)
            {
                // Fetch xmlnsMappings for the prefixes returned by the namespaceResolver service

                foreach (NamespaceDeclaration prefix in namespaceResolver.GetNamespacePrefixes())
                {
                    ReadOnlyXmlnsMapping mapping;
                    WrapCachedMapping(prefix, out mapping);
                    if (!mapping.IsEmpty)
                    {
                        if (settings == null)
                        {
                            settings = new VisualBasicSettings();
                        }

                        if (!mapping.IsEmpty)
                        {
                            foreach (ReadOnlyVisualBasicImportReference importReference in mapping.ImportReferences)
                            {
                                if (xsCtxReferenceAssemblies != null)
                                {
                                    // this is "compiled Xaml"
                                    VisualBasicImportReference newImportReference;

                                    if (importReference.EarlyBoundAssembly != null)
                                    {
                                        if (xsCtxReferenceAssemblies.Contains(importReference.EarlyBoundAssembly))
                                        {
                                            newImportReference = importReference.Clone();
                                            newImportReference.EarlyBoundAssembly = importReference.EarlyBoundAssembly;
                                            settings.ImportReferences.Add(newImportReference);
                                        }
                                        continue;
                                    }

                                    for (int i = 0; i < xsCtxReferenceAssemblies.Count; i++)
                                    {
                                        AssemblyName xsCtxAssemblyName = VisualBasicHelper.GetFastAssemblyName(xsCtxReferenceAssemblies[i]);
                                        if (importReference.AssemblySatisfiesReference(xsCtxAssemblyName))
                                        {
                                            // bind this assembly early to the importReference
                                            // so later AssemblyName resolution can be skipped
                                            newImportReference = importReference.Clone();
                                            newImportReference.EarlyBoundAssembly = xsCtxReferenceAssemblies[i];
                                            settings.ImportReferences.Add(newImportReference);
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    // this is "loose Xaml"
                                    VisualBasicImportReference newImportReference = importReference.Clone();
                                    if (importReference.EarlyBoundAssembly != null)
                                    {
                                        // VBImportReference.Clone() method deliberately doesn't copy
                                        // its EarlyBoundAssembly to the cloned instance.
                                        // we need to explicitly copy the original's EarlyBoundAssembly
                                        newImportReference.EarlyBoundAssembly = importReference.EarlyBoundAssembly;
                                    }
                                    settings.ImportReferences.Add(newImportReference);
                                }
                            }
                        }
                    }
                }
            }
            return(settings);
        }
 public static IList<XamlTypeName> ParseList(string typeNameList, IXamlNamespaceResolver namespaceResolver)
 {
     string str;
     if (typeNameList == null)
     {
         throw new ArgumentNullException("typeNameList");
     }
     if (namespaceResolver == null)
     {
         throw new ArgumentNullException("namespaceResolver");
     }
     IList<XamlTypeName> list = ParseListInternal(typeNameList, new Func<string, string>(namespaceResolver.GetNamespace), out str);
     if (list == null)
     {
         throw new FormatException(str);
     }
     return list;
 }
 public static XamlTypeName Parse(string typeName, IXamlNamespaceResolver namespaceResolver)
 {
     string str;
     if (typeName == null)
     {
         throw new ArgumentNullException("typeName");
     }
     if (namespaceResolver == null)
     {
         throw new ArgumentNullException("namespaceResolver");
     }
     XamlTypeName name = ParseInternal(typeName, new Func<string, string>(namespaceResolver.GetNamespace), out str);
     if (name == null)
     {
         throw new FormatException(str);
     }
     return name;
 }
Exemplo n.º 28
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);
        }
Exemplo n.º 29
0
        public static bool TryParseList(string typeNameList, IXamlNamespaceResolver namespaceResolver, out IList <XamlTypeName> list)
        {
            if (typeNameList == null)
            {
                throw new ArgumentNullException("typeNameList");
            }
            if (namespaceResolver == null)
            {
                throw new ArgumentNullException("namespaceResolver");
            }

            list = null;
            int          idx    = 0;
            int          parens = 0;
            XamlTypeName tn;

            List <string> l         = new List <string>();
            int           lastToken = 0;

            while (true)
            {
                int i = typeNameList.IndexOfAny(comma_or_parens, idx);
                if (i < 0)
                {
                    l.Add(typeNameList.Substring(lastToken));
                    break;
                }

                switch (typeNameList[i])
                {
                case ',':
                    if (parens != 0)
                    {
                        break;
                    }
                    l.Add(typeNameList.Substring(idx, i - idx));
                    break;

                case '(':
                    parens++;
                    break;

                case ')':
                    parens--;
                    break;
                }
                idx = i + 1;
                while (idx < typeNameList.Length && typeNameList[idx] == ' ')
                {
                    idx++;
                }
                if (parens == 0 && typeNameList[i] == ',')
                {
                    lastToken = idx;
                }
            }

            var ret = new List <XamlTypeName>();

            foreach (var s in l)
            {
                if (!TryParse(s, namespaceResolver, out tn))
                {
                    return(false);
                }
                ret.Add(tn);
            }

            list = ret;
            return(true);
        }
Exemplo n.º 30
0
        public static bool TryParse(string typeName, GenericTypeNamePartDelimeter genericTypeNamePartDelimeter, IXamlNamespaceResolver namespaceResolver, out XamlTypeName result)
        {
            string str;

            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }
            if (namespaceResolver == null)
            {
                throw new ArgumentNullException("namespaceResolver");
            }
            result = ParseInternal(typeName, genericTypeNamePartDelimeter, new Func <string, string>(namespaceResolver.GetNamespace), out str);
            return(result != null);
        }
Exemplo n.º 31
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);
        }
 public static bool TryParseList(string typeNameList, IXamlNamespaceResolver namespaceResolver, out IList<XamlTypeName> result)
 {
     string str;
     if (typeNameList == null)
     {
         throw new ArgumentNullException("typeNameList");
     }
     if (namespaceResolver == null)
     {
         throw new ArgumentNullException("namespaceResolver");
     }
     result = ParseListInternal(typeNameList, new Func<string, string>(namespaceResolver.GetNamespace), out str);
     return (result != null);
 }
Exemplo n.º 33
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 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);
        }