[NUI] Add license, delete unnecessary code (#2679)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Xaml / TypeArgumentsParser.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System.Collections.Generic;
19 using System.Xml;
20
21 namespace Tizen.NUI.Xaml
22 {
23     internal static class TypeArgumentsParser
24     {
25         public static IList<XmlType> ParseExpression(string expression, IXmlNamespaceResolver resolver, IXmlLineInfo lineInfo)
26         {
27             var typeList = new List<XmlType>();
28             while (!string.IsNullOrWhiteSpace(expression))
29             {
30                 var match = expression;
31                 typeList.Add(Parse(match, ref expression, resolver, lineInfo));
32             }
33             return typeList;
34         }
35
36         static XmlType Parse(string match, ref string remaining, IXmlNamespaceResolver resolver, IXmlLineInfo lineinfo)
37         {
38             remaining = null;
39             int parensCount = 0;
40             int pos = 0;
41             bool isGeneric = false;
42
43             for (pos = 0; pos < match.Length; pos++)
44             {
45                 if (match[pos] == '(')
46                 {
47                     parensCount++;
48                     isGeneric = true;
49                 }
50                 else if (match[pos] == ')')
51                     parensCount--;
52                 else if (match[pos] == ',' && parensCount == 0)
53                 {
54                     remaining = match.Substring(pos + 1);
55                     break;
56                 }
57             }
58             var type = match.Substring(0, pos).Trim();
59
60             IList<XmlType> typeArguments = null;
61             if (isGeneric)
62             {
63                 typeArguments = ParseExpression(
64                     type.Substring(type.IndexOf('(') + 1, type.LastIndexOf(')') - type.IndexOf('(') - 1), resolver, lineinfo);
65                 type = type.Substring(0, type.IndexOf('('));
66             }
67
68             var split = type.Split(':');
69             if (split.Length > 2)
70                 return null;
71
72             string prefix, name;
73             if (split.Length == 2)
74             {
75                 prefix = split[0];
76                 name = split[1];
77             }
78             else
79             {
80                 prefix = "";
81                 name = split[0];
82             }
83
84             var namespaceuri = resolver.LookupNamespace(prefix);
85             if (namespaceuri == null)
86                 throw new XamlParseException($"No xmlns declaration for prefix '{prefix}'.", lineinfo, null);
87             return new XmlType(namespaceuri, name, typeArguments);
88         }
89     }
90 }