1 //
2 // Copyright (C) 2010 Novell Inc. http://novell.com
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 using System;
24 using System.Collections.Generic;
25 using System.ComponentModel;
26 using System.Linq;
27 using System.Windows.Markup;
28 
29 namespace System.Xaml
30 {
31 	internal class XamlNameResolver : IXamlNameResolver, IXamlNameProvider
32 	{
XamlNameResolver()33 		public XamlNameResolver ()
34 		{
35 		}
36 
37 		public bool IsCollectingReferences { get; set; }
38 
39 		internal class NamedObject
40 		{
NamedObject(string name, object value, bool fullyInitialized)41 			public NamedObject (string name, object value, bool fullyInitialized)
42 			{
43 				Name = name;
44 				Value = value;
45 				FullyInitialized = fullyInitialized;
46 			}
47 			public string Name { get; set; }
48 			public object Value { get; set; }
49 			public bool FullyInitialized { get; set; }
50 		}
51 
52 		Dictionary<string,NamedObject> objects = new Dictionary<string,NamedObject> ();
53 		List<object> referenced = new List<object> ();
54 
55 		[MonoTODO]
56 		public bool IsFixupTokenAvailable {
57 			get { throw new NotImplementedException (); }
58 		}
59 
60 		public event EventHandler OnNameScopeInitializationComplete;
61 
NameScopeInitializationCompleted(object sender)62 		internal void NameScopeInitializationCompleted (object sender)
63 		{
64 			if (OnNameScopeInitializationComplete != null)
65 				OnNameScopeInitializationComplete (sender, EventArgs.Empty);
66 			objects.Clear ();
67 		}
68 
69 		int saved_count, saved_referenced_count;
Save()70 		public void Save ()
71 		{
72 			if (saved_count != 0)
73 				throw new Exception ();
74 			saved_count = objects.Count;
75 			saved_referenced_count = referenced.Count;
76 		}
Restore()77 		public void Restore ()
78 		{
79 			while (saved_count < objects.Count)
80 				objects.Remove (objects.Last ().Key);
81 				referenced.Remove (objects.Last ().Key);
82 			saved_count = 0;
83 			referenced.RemoveRange (saved_referenced_count, referenced.Count - saved_referenced_count);
84 			saved_referenced_count = 0;
85 		}
86 
SetNamedObject(string name, object value, bool fullyInitialized)87 		internal void SetNamedObject (string name, object value, bool fullyInitialized)
88 		{
89 			if (value == null)
90 				throw new ArgumentNullException ("value");
91 			objects [name] = new NamedObject (name, value, fullyInitialized);
92 		}
93 
Contains(string name)94 		internal bool Contains (string name)
95 		{
96 			return objects.ContainsKey (name);
97 		}
98 
GetName(object value)99 		public string GetName (object value)
100 		{
101 			foreach (var no in objects.Values)
102 				if (object.ReferenceEquals (no.Value, value))
103 					return no.Name;
104 			return null;
105 		}
106 
SaveAsReferenced(object val)107 		internal void SaveAsReferenced (object val)
108 		{
109 			referenced.Add (val);
110 		}
111 
GetReferencedName(object val)112 		internal string GetReferencedName (object val)
113 		{
114 			if (!referenced.Contains (val))
115 				return null;
116 			return GetName (val);
117 		}
118 
GetFixupToken(IEnumerable<string> names)119 		public object GetFixupToken (IEnumerable<string> names)
120 		{
121 			return new NameFixupRequired (names, false);
122 		}
123 
GetFixupToken(IEnumerable<string> names, bool canAssignDirectly)124 		public object GetFixupToken (IEnumerable<string> names, bool canAssignDirectly)
125 		{
126 			return new NameFixupRequired (names, canAssignDirectly);
127 		}
128 
GetAllNamesAndValuesInScope()129 		public IEnumerable<KeyValuePair<string, object>> GetAllNamesAndValuesInScope ()
130 		{
131 			foreach (var pair in objects)
132 				yield return new KeyValuePair<string,object> (pair.Key, pair.Value.Value);
133 		}
134 
Resolve(string name)135 		public object Resolve (string name)
136 		{
137 			NamedObject ret;
138 			return objects.TryGetValue (name, out ret) ? ret.Value : null;
139 		}
140 
Resolve(string name, out bool isFullyInitialized)141 		public object Resolve (string name, out bool isFullyInitialized)
142 		{
143 			NamedObject ret;
144 			if (objects.TryGetValue (name, out ret)) {
145 				isFullyInitialized = ret.FullyInitialized;
146 				return ret.Value;
147 			} else {
148 				isFullyInitialized = false;
149 				return null;
150 			}
151 		}
152 	}
153 
154 	internal class NameFixupRequired
155 	{
NameFixupRequired(IEnumerable<string> names, bool canAssignDirectly)156 		public NameFixupRequired (IEnumerable<string> names, bool canAssignDirectly)
157 		{
158 			CanAssignDirectly = canAssignDirectly;
159 			Names = names.ToArray ();
160 		}
161 
162 		public XamlType ParentType { get; set; }
163 		public XamlMember ParentMember { get; set; }
164 		public object ParentValue { get; set; }
165 
166 		public bool CanAssignDirectly { get; set; }
167 		public IList<string> Names { get; set; }
168 	}
169 }
170 
171