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