PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Xaml/System.Xaml/XamlLanguage.cs

https://bitbucket.org/danipen/mono
C# | 253 lines | 194 code | 32 blank | 27 comment | 10 complexity | 44d9742fc1f5de12da77ace0bef432ab 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.Collections.ObjectModel;
  26. using System.Globalization;
  27. using System.Linq;
  28. using System.Reflection;
  29. using System.Xaml.Schema;
  30. using System.Windows.Markup;
  31. [assembly:XmlnsDefinition (System.Xaml.XamlLanguage.Xaml2006Namespace, "System.Windows.Markup")] // FIXME: verify.
  32. namespace System.Xaml
  33. {
  34. public static class XamlLanguage
  35. {
  36. public const string Xaml2006Namespace = "http://schemas.microsoft.com/winfx/2006/xaml";
  37. public const string Xml1998Namespace = "http://www.w3.org/XML/1998/namespace";
  38. internal const string Xmlns2000Namespace = "http://www.w3.org/2000/xmlns/";
  39. // FIXME: I'm not sure if these "special names" should be resolved like this. I couldn't find any rule so far.
  40. internal static readonly SpecialTypeNameList SpecialNames;
  41. internal class SpecialTypeNameList : List<SpecialTypeName>
  42. {
  43. internal SpecialTypeNameList ()
  44. {
  45. Add (new SpecialTypeName ("Member", XamlLanguage.Member));
  46. Add (new SpecialTypeName ("Property", XamlLanguage.Property));
  47. }
  48. public XamlType Find (string name, string ns)
  49. {
  50. if (ns != XamlLanguage.Xaml2006Namespace)
  51. return null;
  52. var stn = this.FirstOrDefault (s => s.Name == name);
  53. return stn != null ? stn.Type : null;
  54. }
  55. }
  56. internal class SpecialTypeName
  57. {
  58. public SpecialTypeName (string name, XamlType type)
  59. {
  60. Name = name;
  61. Type = type;
  62. }
  63. public string Name { get; private set; }
  64. public XamlType Type { get; private set; }
  65. }
  66. static readonly XamlSchemaContext sctx = new XamlSchemaContext (new Assembly [] {typeof (XamlType).Assembly});
  67. static XamlType XT<T> ()
  68. {
  69. return sctx.GetXamlType (typeof (T));
  70. }
  71. internal static readonly bool InitializingDirectives;
  72. internal static readonly bool InitializingTypes;
  73. static XamlLanguage ()
  74. {
  75. InitializingTypes = true;
  76. // types
  77. Array = XT<ArrayExtension> ();
  78. Boolean = XT<bool> ();
  79. Byte = XT<byte> ();
  80. Char = XT<char> ();
  81. Decimal = XT<decimal> ();
  82. Double = XT<double> ();
  83. Int16 = XT<short> ();
  84. Int32 = XT<int> ();
  85. Int64 = XT<long> ();
  86. Member = XT<MemberDefinition> ();
  87. Null = XT<NullExtension> ();
  88. Object = XT<object> ();
  89. Property = XT<PropertyDefinition> ();
  90. Reference = XT<Reference> ();
  91. Single = XT<float> ();
  92. Static = XT<StaticExtension> ();
  93. String = XT<string> ();
  94. TimeSpan = XT<TimeSpan> ();
  95. Type = XT<TypeExtension> ();
  96. Uri = XT<Uri> ();
  97. XData = XT<XData> ();
  98. InitializingTypes = false;
  99. AllTypes = new ReadOnlyCollection<XamlType> (new XamlType [] {Array, Boolean, Byte, Char, Decimal, Double, Int16, Int32, Int64, Member, Null, Object, Property, Reference, Single, Static, String, TimeSpan, Type, Uri, XData});
  100. // directives
  101. // Looks like predefined XamlDirectives have no ValueSerializer.
  102. // To handle this situation, differentiate them from non-primitive XamlMembers.
  103. InitializingDirectives = true;
  104. var nss = new string [] {XamlLanguage.Xaml2006Namespace};
  105. var nssXml = new string [] {XamlLanguage.Xml1998Namespace};
  106. Arguments = new XamlDirective (nss, "Arguments", XT<List<object>> (), null, AllowedMemberLocations.Any);
  107. AsyncRecords = new XamlDirective (nss, "AsyncRecords", XT<string> (), null, AllowedMemberLocations.Attribute);
  108. Base = new XamlDirective (nssXml, "base", XT<string> (), null, AllowedMemberLocations.Attribute);
  109. Class = new XamlDirective (nss, "Class", XT<string> (), null, AllowedMemberLocations.Attribute);
  110. ClassAttributes = new XamlDirective (nss, "ClassAttributes", XT<List<Attribute>> (), null, AllowedMemberLocations.MemberElement);
  111. ClassModifier = new XamlDirective (nss, "ClassModifier", XT<string> (), null, AllowedMemberLocations.Attribute);
  112. Code = new XamlDirective (nss, "Code", XT<string> (), null, AllowedMemberLocations.Attribute);
  113. ConnectionId = new XamlDirective (nss, "ConnectionId", XT<string> (), null, AllowedMemberLocations.Any);
  114. FactoryMethod = new XamlDirective (nss, "FactoryMethod", XT<string> (), null, AllowedMemberLocations.Any);
  115. FieldModifier = new XamlDirective (nss, "FieldModifier", XT<string> (), null, AllowedMemberLocations.Attribute);
  116. Initialization = new XamlDirective (nss, "_Initialization", XT<object> (), null, AllowedMemberLocations.Any);
  117. Items = new XamlDirective (nss, "_Items", XT<List<object>> (), null, AllowedMemberLocations.Any);
  118. Key = new XamlDirective (nss, "Key", XT<object> (), null, AllowedMemberLocations.Any);
  119. Lang = new XamlDirective (nssXml, "lang", XT<string> (), null, AllowedMemberLocations.Attribute);
  120. Members = new XamlDirective (nss, "Members", XT<List<MemberDefinition>> (), null, AllowedMemberLocations.MemberElement);
  121. Name = new XamlDirective (nss, "Name", XT<string> (), null, AllowedMemberLocations.Attribute);
  122. PositionalParameters = new XamlDirective (nss, "_PositionalParameters", XT<List<object>> (), null, AllowedMemberLocations.Any);
  123. Space = new XamlDirective (nssXml, "space", XT<string> (), null, AllowedMemberLocations.Attribute);
  124. Subclass = new XamlDirective (nss, "Subclass", XT<string> (), null, AllowedMemberLocations.Attribute);
  125. SynchronousMode = new XamlDirective (nss, "SynchronousMode", XT<string> (), null, AllowedMemberLocations.Attribute);
  126. Shared = new XamlDirective (nss, "Shared", XT<string> (), null, AllowedMemberLocations.Attribute);
  127. TypeArguments = new XamlDirective (nss, "TypeArguments", XT<string> (), null, AllowedMemberLocations.Attribute);
  128. Uid = new XamlDirective (nss, "Uid", XT<string> (), null, AllowedMemberLocations.Attribute);
  129. UnknownContent = new XamlDirective (nss, "_UnknownContent", XT<object> (), null, AllowedMemberLocations.MemberElement) { InternalIsUnknown = true };
  130. AllDirectives = new ReadOnlyCollection<XamlDirective> (new XamlDirective [] {Arguments, AsyncRecords, Base, Class, ClassAttributes, ClassModifier, Code, ConnectionId, FactoryMethod, FieldModifier, Initialization, Items, Key, Lang, Members, Name, PositionalParameters, Space, Subclass, SynchronousMode, Shared, TypeArguments, Uid, UnknownContent});
  131. InitializingDirectives = false;
  132. SpecialNames = new SpecialTypeNameList ();
  133. }
  134. static readonly string [] xaml_nss = new string [] {Xaml2006Namespace};
  135. public static IList<string> XamlNamespaces {
  136. get { return xaml_nss; }
  137. }
  138. static readonly string [] xml_nss = new string [] {Xml1998Namespace};
  139. public static IList<string> XmlNamespaces {
  140. get { return xml_nss; }
  141. }
  142. public static ReadOnlyCollection<XamlDirective> AllDirectives { get; private set; }
  143. public static XamlDirective Arguments { get; private set; }
  144. public static XamlDirective AsyncRecords { get; private set; }
  145. public static XamlDirective Base { get; private set; }
  146. public static XamlDirective Class { get; private set; }
  147. public static XamlDirective ClassAttributes { get; private set; }
  148. public static XamlDirective ClassModifier { get; private set; }
  149. public static XamlDirective Code { get; private set; }
  150. public static XamlDirective ConnectionId { get; private set; }
  151. public static XamlDirective FactoryMethod { get; private set; }
  152. public static XamlDirective FieldModifier { get; private set; }
  153. public static XamlDirective Initialization { get; private set; }
  154. public static XamlDirective Items { get; private set; }
  155. public static XamlDirective Key { get; private set; }
  156. public static XamlDirective Lang { get; private set; }
  157. public static XamlDirective Members { get; private set; }
  158. public static XamlDirective Name { get; private set; }
  159. public static XamlDirective PositionalParameters { get; private set; }
  160. public static XamlDirective Subclass { get; private set; }
  161. public static XamlDirective SynchronousMode { get; private set; }
  162. public static XamlDirective Shared { get; private set; }
  163. public static XamlDirective Space { get; private set; }
  164. public static XamlDirective TypeArguments { get; private set; }
  165. public static XamlDirective Uid { get; private set; }
  166. public static XamlDirective UnknownContent { get; private set; }
  167. public static ReadOnlyCollection<XamlType> AllTypes { get; private set; }
  168. public static XamlType Array { get; private set; }
  169. public static XamlType Boolean { get; private set; }
  170. public static XamlType Byte { get; private set; }
  171. public static XamlType Char { get; private set; }
  172. public static XamlType Decimal { get; private set; }
  173. public static XamlType Double { get; private set; }
  174. public static XamlType Int16 { get; private set; }
  175. public static XamlType Int32 { get; private set; }
  176. public static XamlType Int64 { get; private set; }
  177. public static XamlType Member { get; private set; }
  178. public static XamlType Null { get; private set; }
  179. public static XamlType Object { get; private set; }
  180. public static XamlType Property { get; private set; }
  181. public static XamlType Reference { get; private set; }
  182. public static XamlType Single { get; private set; }
  183. public static XamlType Static { get; private set; }
  184. public static XamlType String { get; private set; }
  185. public static XamlType TimeSpan { get; private set; }
  186. public static XamlType Type { get; private set; }
  187. public static XamlType Uri { get; private set; }
  188. public static XamlType XData { get; private set; }
  189. internal static bool IsValidXamlName (string name)
  190. {
  191. if (string.IsNullOrEmpty (name))
  192. return false;
  193. if (!IsValidXamlName (name [0], true))
  194. return false;
  195. foreach (char c in name)
  196. if (!IsValidXamlName (c, false))
  197. return false;
  198. return true;
  199. }
  200. static bool IsValidXamlName (char c, bool first)
  201. {
  202. if (c == '_')
  203. return true;
  204. switch (char.GetUnicodeCategory (c)) {
  205. case UnicodeCategory.LowercaseLetter:
  206. case UnicodeCategory.UppercaseLetter:
  207. case UnicodeCategory.TitlecaseLetter:
  208. case UnicodeCategory.OtherLetter:
  209. case UnicodeCategory.LetterNumber:
  210. return true;
  211. case UnicodeCategory.NonSpacingMark:
  212. case UnicodeCategory.DecimalDigitNumber:
  213. case UnicodeCategory.SpacingCombiningMark:
  214. case UnicodeCategory.ModifierLetter:
  215. return !first;
  216. default:
  217. return false;
  218. }
  219. }
  220. }
  221. }