2010-04-15 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Runtime.Serialization / reader-method-gen.cs
blobe360b21d5263e5d3e752b0685fa62ca5ca71979b
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;
10 public class Generator
12 public static void Main ()
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) };
19 Console.WriteLine (@"
20 #pragma warning disable 612
21 using System;
22 using System.Collections.Generic;
24 namespace System.Xml
26 public abstract partial class XmlDictionaryReader : XmlReader
28 static readonly char [] wsChars = new char [] {' ', '\t', '\n', '\r'};
30 void CheckReadArrayArguments (Array array, int offset, int length)
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"");
44 void CheckDictionaryStringArgs (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
46 if (localName == null)
47 throw new ArgumentNullException (""localName"");
48 if (namespaceUri == null)
49 throw new ArgumentNullException (""namespaceUri"");
51 ");
53 foreach (Type type in types) {
54 Console.WriteLine (@"
55 public virtual int ReadArray (XmlDictionaryString localName, XmlDictionaryString namespaceUri, {0} [] array, int offset, int length)
57 CheckDictionaryStringArgs (localName, namespaceUri);
58 return ReadArray (localName.Value, namespaceUri.Value, array, offset, length);
61 public virtual int ReadArray (string localName, string namespaceUri, {0} [] array, int offset, int length)
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 ();
72 return length;
75 public virtual {0} [] Read{1}Array (string localName, string namespaceUri)
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;
89 return list.ToArray ();
92 public virtual {0} [] Read{1}Array (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
94 CheckDictionaryStringArgs (localName, namespaceUri);
95 return Read{1}Array (localName.Value, namespaceUri.Value);
96 }}", ToCSharp (type), type.Name);
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.
113 bool isOverride = xr.GetMethod (mi.Name, Type.EmptyTypes) != null;
114 Console.WriteLine (@"
115 public {3}{0} {1} ()
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);
128 Console.WriteLine (@"
130 }");
133 static CodeDomProvider cs = new CSharpCodeProvider ();
135 static string ToCSharp (Type type)
137 string r = cs.GetTypeOutput (new CodeTypeReference (type));
138 return r != type.FullName ? r : type.Name;
141 static string ToOldName (Type type)
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;