mono /mcs/class/System.Xaml/System.Xaml/XamlNameResolver.cs

Language C# Lines 171
MD5 Hash b43e621b9c85f807aec7a02ccbb603c1 Estimated Cost $3,069 (why?)
Repository https://bitbucket.org/danipen/mono.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// Copyright (C) 2010 Novell Inc. http://novell.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Markup;

namespace System.Xaml
{
	internal class XamlNameResolver : IXamlNameResolver, IXamlNameProvider
	{
		public XamlNameResolver ()
		{
		}
		
		public bool IsCollectingReferences { get; set; }

		internal class NamedObject
		{
			public NamedObject (string name, object value, bool fullyInitialized)
			{
				Name = name;
				Value = value;
				FullyInitialized = fullyInitialized;
			}
			public string Name { get; set; }
			public object Value { get; set; }
			public bool FullyInitialized { get; set; }
		}

		Dictionary<string,NamedObject> objects = new Dictionary<string,NamedObject> ();
		List<object> referenced = new List<object> ();

		[MonoTODO]
		public bool IsFixupTokenAvailable {
			get { throw new NotImplementedException (); }
		}

		public event EventHandler OnNameScopeInitializationComplete;

		internal void NameScopeInitializationCompleted (object sender)
		{
			if (OnNameScopeInitializationComplete != null)
				OnNameScopeInitializationComplete (sender, EventArgs.Empty);
			objects.Clear ();
		}
		
		int saved_count, saved_referenced_count;
		public void Save ()
		{
			if (saved_count != 0)
				throw new Exception ();
			saved_count = objects.Count;
			saved_referenced_count = referenced.Count;
		}
		public void Restore ()
		{
			while (saved_count < objects.Count)
				objects.Remove (objects.Last ().Key);
				referenced.Remove (objects.Last ().Key);
			saved_count = 0;
			referenced.RemoveRange (saved_referenced_count, referenced.Count - saved_referenced_count);
			saved_referenced_count = 0;
		}

		internal void SetNamedObject (string name, object value, bool fullyInitialized)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			objects [name] = new NamedObject (name, value, fullyInitialized);
		}
		
		internal bool Contains (string name)
		{
			return objects.ContainsKey (name);
		}
		
		public string GetName (object value)
		{
			foreach (var no in objects.Values)
				if (object.ReferenceEquals (no.Value, value))
					return no.Name;
			return null;
		}

		internal void SaveAsReferenced (object val)
		{
			referenced.Add (val);
		}
		
		internal string GetReferencedName (object val)
		{
			if (!referenced.Contains (val))
				return null;
			return GetName (val);
		}
		
		public object GetFixupToken (IEnumerable<string> names)
		{
			return new NameFixupRequired (names, false);
		}

		public object GetFixupToken (IEnumerable<string> names, bool canAssignDirectly)
		{
			return new NameFixupRequired (names, canAssignDirectly);
		}

		public IEnumerable<KeyValuePair<string, object>> GetAllNamesAndValuesInScope ()
		{
			foreach (var pair in objects)
				yield return new KeyValuePair<string,object> (pair.Key, pair.Value.Value);
		}

		public object Resolve (string name)
		{
			NamedObject ret;
			return objects.TryGetValue (name, out ret) ? ret.Value : null;
		}

		public object Resolve (string name, out bool isFullyInitialized)
		{
			NamedObject ret;
			if (objects.TryGetValue (name, out ret)) {
				isFullyInitialized = ret.FullyInitialized;
				return ret.Value;
			} else {
				isFullyInitialized = false;
				return null;
			}
		}
	}
	
	internal class NameFixupRequired
	{
		public NameFixupRequired (IEnumerable<string> names, bool canAssignDirectly)
		{
			CanAssignDirectly = canAssignDirectly;
			Names = names.ToArray ();
		}
		
		public XamlType ParentType { get; set; }
		public XamlMember ParentMember { get; set; }
		public object ParentValue { get; set; }

		public bool CanAssignDirectly { get; set; }
		public IList<string> Names { get; set; }
	}
}
Back to Top