PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/danipen/mono
C# | 170 lines | 126 code | 22 blank | 22 comment | 11 complexity | b43e621b9c85f807aec7a02ccbb603c1 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  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. namespace System.Xaml
  29. {
  30. internal class XamlNameResolver : IXamlNameResolver, IXamlNameProvider
  31. {
  32. public XamlNameResolver ()
  33. {
  34. }
  35. public bool IsCollectingReferences { get; set; }
  36. internal class NamedObject
  37. {
  38. public NamedObject (string name, object value, bool fullyInitialized)
  39. {
  40. Name = name;
  41. Value = value;
  42. FullyInitialized = fullyInitialized;
  43. }
  44. public string Name { get; set; }
  45. public object Value { get; set; }
  46. public bool FullyInitialized { get; set; }
  47. }
  48. Dictionary<string,NamedObject> objects = new Dictionary<string,NamedObject> ();
  49. List<object> referenced = new List<object> ();
  50. [MonoTODO]
  51. public bool IsFixupTokenAvailable {
  52. get { throw new NotImplementedException (); }
  53. }
  54. public event EventHandler OnNameScopeInitializationComplete;
  55. internal void NameScopeInitializationCompleted (object sender)
  56. {
  57. if (OnNameScopeInitializationComplete != null)
  58. OnNameScopeInitializationComplete (sender, EventArgs.Empty);
  59. objects.Clear ();
  60. }
  61. int saved_count, saved_referenced_count;
  62. public void Save ()
  63. {
  64. if (saved_count != 0)
  65. throw new Exception ();
  66. saved_count = objects.Count;
  67. saved_referenced_count = referenced.Count;
  68. }
  69. public void Restore ()
  70. {
  71. while (saved_count < objects.Count)
  72. objects.Remove (objects.Last ().Key);
  73. referenced.Remove (objects.Last ().Key);
  74. saved_count = 0;
  75. referenced.RemoveRange (saved_referenced_count, referenced.Count - saved_referenced_count);
  76. saved_referenced_count = 0;
  77. }
  78. internal void SetNamedObject (string name, object value, bool fullyInitialized)
  79. {
  80. if (value == null)
  81. throw new ArgumentNullException ("value");
  82. objects [name] = new NamedObject (name, value, fullyInitialized);
  83. }
  84. internal bool Contains (string name)
  85. {
  86. return objects.ContainsKey (name);
  87. }
  88. public string GetName (object value)
  89. {
  90. foreach (var no in objects.Values)
  91. if (object.ReferenceEquals (no.Value, value))
  92. return no.Name;
  93. return null;
  94. }
  95. internal void SaveAsReferenced (object val)
  96. {
  97. referenced.Add (val);
  98. }
  99. internal string GetReferencedName (object val)
  100. {
  101. if (!referenced.Contains (val))
  102. return null;
  103. return GetName (val);
  104. }
  105. public object GetFixupToken (IEnumerable<string> names)
  106. {
  107. return new NameFixupRequired (names, false);
  108. }
  109. public object GetFixupToken (IEnumerable<string> names, bool canAssignDirectly)
  110. {
  111. return new NameFixupRequired (names, canAssignDirectly);
  112. }
  113. public IEnumerable<KeyValuePair<string, object>> GetAllNamesAndValuesInScope ()
  114. {
  115. foreach (var pair in objects)
  116. yield return new KeyValuePair<string,object> (pair.Key, pair.Value.Value);
  117. }
  118. public object Resolve (string name)
  119. {
  120. NamedObject ret;
  121. return objects.TryGetValue (name, out ret) ? ret.Value : null;
  122. }
  123. public object Resolve (string name, out bool isFullyInitialized)
  124. {
  125. NamedObject ret;
  126. if (objects.TryGetValue (name, out ret)) {
  127. isFullyInitialized = ret.FullyInitialized;
  128. return ret.Value;
  129. } else {
  130. isFullyInitialized = false;
  131. return null;
  132. }
  133. }
  134. }
  135. internal class NameFixupRequired
  136. {
  137. public NameFixupRequired (IEnumerable<string> names, bool canAssignDirectly)
  138. {
  139. CanAssignDirectly = canAssignDirectly;
  140. Names = names.ToArray ();
  141. }
  142. public XamlType ParentType { get; set; }
  143. public XamlMember ParentMember { get; set; }
  144. public object ParentValue { get; set; }
  145. public bool CanAssignDirectly { get; set; }
  146. public IList<string> Names { get; set; }
  147. }
  148. }