reader-method-gen.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // mono reader-method-gen.exe > System.Xml/XmlDictionaryReaderAutoGen.cs
  2. using System;
  3. using System.Globalization;
  4. using System.CodeDom;
  5. using System.CodeDom.Compiler;
  6. using System.Reflection;
  7. using System.Xml;
  8. using Microsoft.CSharp;
  9. public class Generator
  10. {
  11. public static void Main ()
  12. {
  13. Console.Out.NewLine = "\n";
  14. Type [] types = new Type [] {
  15. typeof (bool), typeof (DateTime), typeof (decimal), typeof (double),
  16. typeof (Guid), typeof (short), typeof (int), typeof (long), typeof (float), typeof (TimeSpan) };
  17. Console.WriteLine (@"
  18. #pragma warning disable 612
  19. using System;
  20. using System.Collections.Generic;
  21. namespace System.Xml
  22. {
  23. public abstract partial class XmlDictionaryReader : XmlReader
  24. {
  25. static readonly char [] wsChars = new char [] {' ', '\t', '\n', '\r'};
  26. void CheckReadArrayArguments (Array array, int offset, int length)
  27. {
  28. if (array == null)
  29. throw new ArgumentNullException (""array"");
  30. if (offset < 0)
  31. throw new ArgumentOutOfRangeException (""offset is negative"");
  32. if (offset > array.Length)
  33. throw new ArgumentOutOfRangeException (""offset exceeds the length of the destination array"");
  34. if (length < 0)
  35. throw new ArgumentOutOfRangeException (""length is negative"");
  36. if (length > array.Length - offset)
  37. throw new ArgumentOutOfRangeException (""length + offset exceeds the length of the destination array"");
  38. }
  39. void CheckDictionaryStringArgs (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  40. {
  41. if (localName == null)
  42. throw new ArgumentNullException (""localName"");
  43. if (namespaceUri == null)
  44. throw new ArgumentNullException (""namespaceUri"");
  45. }
  46. ");
  47. foreach (Type type in types) {
  48. Console.WriteLine (@"
  49. public virtual int ReadArray (XmlDictionaryString localName, XmlDictionaryString namespaceUri, {0} [] array, int offset, int length)
  50. {{
  51. CheckDictionaryStringArgs (localName, namespaceUri);
  52. return ReadArray (localName.Value, namespaceUri.Value, array, offset, length);
  53. }}
  54. public virtual int ReadArray (string localName, string namespaceUri, {0} [] array, int offset, int length)
  55. {{
  56. CheckReadArrayArguments (array, offset, length);
  57. for (int i = 0; i < length; i++) {{
  58. MoveToContent ();
  59. if (NodeType != XmlNodeType.Element)
  60. return i;
  61. ReadStartElement (localName, namespaceUri);
  62. array [offset + i] = XmlConvert.To{1} (ReadContentAsString ());
  63. ReadEndElement ();
  64. }}
  65. return length;
  66. }}
  67. public virtual {0} [] Read{1}Array (string localName, string namespaceUri)
  68. {{
  69. List<{0}> list = new List<{0}> ();
  70. while (true) {{
  71. MoveToContent ();
  72. if (NodeType != XmlNodeType.Element)
  73. break;
  74. ReadStartElement (localName, namespaceUri);
  75. list.Add (XmlConvert.To{1} (ReadContentAsString ()));
  76. ReadEndElement ();
  77. if (list.Count == Quotas.MaxArrayLength)
  78. // FIXME: check if raises an error or not
  79. break;
  80. }}
  81. return list.ToArray ();
  82. }}
  83. public virtual {0} [] Read{1}Array (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  84. {{
  85. CheckDictionaryStringArgs (localName, namespaceUri);
  86. return Read{1}Array (localName.Value, namespaceUri.Value);
  87. }}", ToCSharp (type), type.Name);
  88. }
  89. Type xr = typeof (XmlReader);
  90. string name = "ReadElementContentAs";
  91. foreach (MethodInfo mi in xr.GetMethods ()) {
  92. if (!mi.Name.StartsWith (name))
  93. continue;
  94. ParameterInfo [] pl = mi.GetParameters ();
  95. if (pl.Length != 2 || pl [0].ParameterType != typeof (string))
  96. continue;
  97. if (mi.Name.EndsWith ("AsObject"))
  98. continue; // special case to filter out.
  99. if (mi.Name.EndsWith ("AsString"))
  100. continue; // special case to filter out.
  101. bool isOverride = xr.GetMethod (mi.Name, Type.EmptyTypes) != null;
  102. Console.WriteLine (@"
  103. public {3}{0} {1} ()
  104. {{
  105. ReadStartElement (LocalName, NamespaceURI);
  106. {0} ret = {2} ();
  107. ReadEndElement ();
  108. return ret;
  109. }}",
  110. ToCSharp (mi.ReturnType),
  111. mi.Name,
  112. mi.Name.Replace ("Element", String.Empty),
  113. isOverride ? "override " : null);
  114. }
  115. Console.WriteLine (@"
  116. }
  117. }");
  118. }
  119. static CodeDomProvider cs = new CSharpCodeProvider ();
  120. static string ToCSharp (Type type)
  121. {
  122. string r = cs.GetTypeOutput (new CodeTypeReference (type));
  123. return r != type.FullName ? r : type.Name;
  124. }
  125. static string ToOldName (Type type)
  126. {
  127. switch (type.Name) {
  128. case "Single":
  129. return "Float";
  130. case "Int32":
  131. return "Int";
  132. case "Int64":
  133. return "Long";
  134. case "Int16":
  135. return "Short";
  136. default:
  137. return type.Name;
  138. }
  139. }
  140. }