[NUI]Add xaml support for nui and nui xaml test sample (#230)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Xaml / TypeArgumentsParser.cs
1 using System.Collections.Generic;
2 using System.Xml;
3
4 namespace Tizen.NUI.Xaml
5 {
6         static class TypeArgumentsParser
7         {
8                 public static IList<XmlType> ParseExpression(string expression, IXmlNamespaceResolver resolver, IXmlLineInfo lineInfo)
9                 {
10                         var typeList = new List<XmlType>();
11                         while (!string.IsNullOrWhiteSpace(expression))
12                         {
13                                 var match = expression;
14                                 typeList.Add(Parse(match, ref expression, resolver, lineInfo));
15                         }
16                         return typeList;
17                 }
18
19                 static XmlType Parse(string match, ref string remaining, IXmlNamespaceResolver resolver, IXmlLineInfo lineinfo)
20                 {
21                         remaining = null;
22                         int parensCount = 0;
23                         int pos = 0;
24                         bool isGeneric = false;
25
26                         for (pos = 0; pos < match.Length; pos++)
27                         {
28                                 if (match[pos] == '(')
29                                 {
30                                         parensCount++;
31                                         isGeneric = true;
32                                 }
33                                 else if (match[pos] == ')')
34                                         parensCount--;
35                                 else if (match[pos] == ',' && parensCount == 0)
36                                 {
37                                         remaining = match.Substring(pos + 1);
38                                         break;
39                                 }
40                         }
41                         var type = match.Substring(0, pos).Trim();
42
43                         IList<XmlType> typeArguments = null;
44                         if (isGeneric)
45                         {
46                                 typeArguments = ParseExpression(
47                                         type.Substring(type.IndexOf('(') + 1, type.LastIndexOf(')') - type.IndexOf('(') - 1), resolver, lineinfo);
48                                 type = type.Substring(0, type.IndexOf('('));
49                         }
50
51                         var split = type.Split(':');
52                         if (split.Length > 2)
53                                 return null;
54
55                         string prefix, name;
56                         if (split.Length == 2) {
57                                 prefix = split [0];
58                                 name = split [1];
59                         } else {
60                                 prefix = "";
61                                 name = split [0];
62                         }
63
64                         var namespaceuri = resolver.LookupNamespace(prefix);
65                         if (namespaceuri == null)
66                                 throw new XamlParseException($"No xmlns declaration for prefix '{prefix}'.", lineinfo, null);
67                         return new XmlType(namespaceuri, name, typeArguments);
68                 }
69         }
70 }