New test.
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / SerializationCodeGeneratorConfiguration.cs
1 //
2 // System.Xml.Serialization.SerializationCodeGeneratorConfiguration.cs: 
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // (C) 2002, 2003 Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Xml.Serialization;
34
35 namespace System.Xml.Serialization
36 {
37         [XmlType ("configuration")]
38         internal class SerializationCodeGeneratorConfiguration
39         {
40                 [XmlElement ("serializer")]
41                 public SerializerInfo[] Serializers;
42         }
43         
44         [XmlType ("serializer")]
45         internal class SerializerInfo
46         {
47                 [XmlAttribute ("class")]
48                 public string ClassName;
49         
50                 [XmlAttribute ("assembly")]
51                 public string Assembly;
52         
53                 [XmlElement ("reader")]
54                 public string ReaderClassName;
55                 
56                 [XmlElement ("writer")]
57                 public string WriterClassName;
58         
59                 [XmlElement ("baseSerializer")]
60                 public string BaseSerializerClassName;
61         
62                 [XmlElement ("implementation")]
63                 public string ImplementationClassName;
64         
65                 [XmlElement ("noreader")]
66                 public bool NoReader;
67                 
68                 [XmlElement ("nowriter")]
69                 public bool NoWriter;
70                 
71                 [XmlElement ("generateAsInternal")]
72                 public bool GenerateAsInternal;
73                 
74                 [XmlElement ("namespace")]
75                 public string Namespace;
76                 
77                 [XmlArray ("namespaceImports")]
78                 [XmlArrayItem ("namespaceImport")]
79                 public string [] NamespaceImports;
80                 
81                 [System.ComponentModel.DefaultValue (SerializationFormat.Literal)]
82                 public SerializationFormat SerializationFormat = SerializationFormat.Literal;
83                 
84                 [XmlElement ("outFileName")]
85                 public string OutFileName;
86                 
87                 [XmlArray ("readerHooks")]
88                 public Hook[] ReaderHooks;
89         
90                 [XmlArray ("writerHooks")]
91                 public Hook[] WriterHooks;
92                 
93                 public ArrayList GetHooks (HookType hookType, HookDir dir, HookAction action, Type type, string member)
94                 {
95                         if (dir == HookDir.Read)
96                                 return FindHook (ReaderHooks, hookType, action, type, member);
97                         else
98                                 return FindHook (WriterHooks, hookType, action, type, member);
99                 }
100                 
101                 ArrayList FindHook (Hook[] hooks, HookType hookType, HookAction action, Type type, string member)
102                 {
103                         ArrayList foundHooks = new ArrayList ();
104                         if (hooks == null) return foundHooks;
105         
106                         foreach (Hook hook in hooks)
107                         {
108                                 if (action == HookAction.InsertBefore && (hook.InsertBefore == null || hook.InsertBefore == ""))
109                                         continue;
110                                 else if (action == HookAction.InsertAfter && (hook.InsertAfter == null || hook.InsertAfter == ""))
111                                         continue;
112                                 else if (action == HookAction.Replace && (hook.Replace == null || hook.Replace == ""))
113                                         continue;
114                                         
115                                 if (hook.HookType != hookType)
116                                         continue;
117                                         
118                                 if (hook.Select != null)
119                                 {
120                                         if (hook.Select.TypeName != null && hook.Select.TypeName != "")
121                                                 if (hook.Select.TypeName != type.FullName) continue;
122                 
123                                         if (hook.Select.TypeMember != null && hook.Select.TypeMember != "")
124                                                 if (hook.Select.TypeMember != member) continue;
125                                                 
126                                         if (hook.Select.TypeAttributes != null && hook.Select.TypeAttributes.Length > 0)
127                                         {
128                                                 object[] ats = type.GetCustomAttributes (true);
129                                                 bool found = false;
130                                                 foreach (object at in ats)
131                                                         if (Array.IndexOf (hook.Select.TypeAttributes, at.GetType().FullName) != -1) { found = true; break; }
132                                                 if (!found) continue;
133                                         }
134                                 }
135                                 foundHooks.Add (hook);
136                         }
137                         return foundHooks;
138                 }
139         }
140         
141         [XmlType ("hook")]
142         internal class Hook
143         {
144                 [XmlAttribute ("type")]
145                 public HookType HookType;
146         
147                 [XmlElement ("select")]
148                 public Select Select;
149                 
150                 [XmlElement ("insertBefore")]
151                 public string InsertBefore;
152
153                 [XmlElement ("insertAfter")]
154                 public string InsertAfter;
155
156                 [XmlElement ("replace")]
157                 public string Replace;
158                 
159                 public string GetCode (HookAction action)
160                 {
161                         if (action == HookAction.InsertBefore)
162                                 return InsertBefore;
163                         else if (action == HookAction.InsertAfter)
164                                 return InsertAfter;
165                         else
166                                 return Replace;
167                 }
168         }
169         
170         [XmlType ("select")]
171         internal class Select
172         {
173                 [XmlElement ("typeName")]
174                 public string TypeName;
175                 
176                 [XmlElement("typeAttribute")]
177                 public string[] TypeAttributes;
178                 
179                 [XmlElement ("typeMember")]
180                 public string TypeMember;
181         }
182         
183         internal enum HookDir
184         {
185                 Read,
186                 Write
187         }
188         
189         internal enum HookAction
190         {
191                 InsertBefore,
192                 InsertAfter,
193                 Replace
194         }
195         
196         [XmlType ("hookType")]
197         internal enum HookType
198         {
199                 attributes,
200                 elements,
201                 unknownAttribute,
202                 unknownElement,
203                 member,
204                 type
205         }
206 }