e86f93d05925dcb35ed5320bac309e8986d43601
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Xaml / RegisterXNamesVisitor.cs
1 using System;
2 using System.Collections.Generic;
3 using Tizen.NUI.Binding;
4
5 namespace Tizen.NUI.Xaml
6 {
7         class RegisterXNamesVisitor : IXamlNodeVisitor
8         {
9                 public RegisterXNamesVisitor(HydrationContext context)
10                 {
11                         Values = context.Values;
12                 }
13
14                 Dictionary<INode, object> Values { get; }
15
16                 public TreeVisitingMode VisitingMode => TreeVisitingMode.TopDown;
17                 public bool StopOnDataTemplate => true;
18                 public bool StopOnResourceDictionary => false;
19                 public bool VisitNodeOnDataTemplate => false;
20                 public bool SkipChildren(INode node, INode parentNode) => false;
21
22                 public void Visit(ValueNode node, INode parentNode)
23                 {
24                         if (!IsXNameProperty(node, parentNode))
25                                 return;
26                         try
27                         {
28                                 ((IElementNode)parentNode).Namescope.RegisterName((string)node.Value, Values[parentNode]);
29                         }
30                         catch (ArgumentException ae)
31                         {
32                                 if (ae.ParamName != "name")
33                                         throw ae;
34                                 throw new XamlParseException($"An element with the name \"{(string)node.Value}\" already exists in this NameScope", node);
35                         }
36                         var element = Values[parentNode] as Element;
37                         if (element != null)
38                                 element.StyleId = element.StyleId ?? (string)node.Value;
39                 }
40
41                 public void Visit(MarkupNode node, INode parentNode)
42                 {
43                 }
44
45                 public void Visit(ElementNode node, INode parentNode)
46                 {
47                 }
48
49                 public void Visit(RootNode node, INode parentNode)
50                 {
51                 }
52
53                 public void Visit(ListNode node, INode parentNode)
54                 {
55                 }
56
57                 static bool IsXNameProperty(ValueNode node, INode parentNode)
58                 {
59                         var parentElement = parentNode as IElementNode;
60                         INode xNameNode;
61                         if (parentElement != null && parentElement.Properties.TryGetValue(XmlName.xName, out xNameNode) && xNameNode == node)
62                                 return true;
63                         return false;
64                 }
65         }
66 }