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 ("noreader")]
60                 public bool NoReader;
61                 
62                 [XmlElement ("nowriter")]
63                 public bool NoWriter;
64                 
65                 [XmlElement ("generateAsInternal")]
66                 public bool GenerateAsInternal;
67                 
68                 [XmlElement ("namespace")]
69                 public string Namespace;
70                 
71                 [XmlArray ("namespaceImports")]
72                 [XmlArrayItem ("namespaceImport")]
73                 public string [] NamespaceImports;
74                 
75                 [System.ComponentModel.DefaultValue (SerializationFormat.Literal)]
76                 public SerializationFormat SerializationFormat = SerializationFormat.Literal;
77                 
78                 [XmlElement ("outFileName")]
79                 public string OutFileName;
80                 
81                 [XmlArray ("readerHooks")]
82                 public Hook[] ReaderHooks;
83         
84                 [XmlArray ("writerHooks")]
85                 public Hook[] WriterHooks;
86                 
87                 public ArrayList GetHooks (HookType hookType, HookDir dir, HookAction action, Type type, string member)
88                 {
89                         if (dir == HookDir.Read)
90                                 return FindHook (ReaderHooks, hookType, action, type, member);
91                         else
92                                 return FindHook (WriterHooks, hookType, action, type, member);
93                 }
94                 
95                 ArrayList FindHook (Hook[] hooks, HookType hookType, HookAction action, Type type, string member)
96                 {
97                         ArrayList foundHooks = new ArrayList ();
98                         if (hooks == null) return foundHooks;
99         
100                         foreach (Hook hook in hooks)
101                         {
102                                 if (action == HookAction.InsertBefore && (hook.InsertBefore == null || hook.InsertBefore == ""))
103                                         continue;
104                                 else if (action == HookAction.InsertAfter && (hook.InsertAfter == null || hook.InsertAfter == ""))
105                                         continue;
106                                 else if (action == HookAction.Replace && (hook.Replace == null || hook.Replace == ""))
107                                         continue;
108                                         
109                                 if (hook.HookType != hookType)
110                                         continue;
111                                         
112                                 if (hook.Select != null)
113                                 {
114                                         if (hook.Select.TypeName != null && hook.Select.TypeName != "")
115                                                 if (hook.Select.TypeName != type.FullName) continue;
116                 
117                                         if (hook.Select.TypeMember != null && hook.Select.TypeMember != "")
118                                                 if (hook.Select.TypeMember != member) continue;
119                                                 
120                                         if (hook.Select.TypeAttributes != null && hook.Select.TypeAttributes.Length > 0)
121                                         {
122                                                 object[] ats = type.GetCustomAttributes (true);
123                                                 bool found = false;
124                                                 foreach (object at in ats)
125                                                         if (Array.IndexOf (hook.Select.TypeAttributes, at.GetType().FullName) != -1) { found = true; break; }
126                                                 if (!found) continue;
127                                         }
128                                 }
129                                 foundHooks.Add (hook);
130                         }
131                         return foundHooks;
132                 }
133         }
134         
135         [XmlType ("hook")]
136         internal class Hook
137         {
138                 [XmlAttribute ("type")]
139                 public HookType HookType;
140         
141                 [XmlElement ("select")]
142                 public Select Select;
143                 
144                 [XmlElement ("insertBefore")]
145                 public string InsertBefore;
146
147                 [XmlElement ("insertAfter")]
148                 public string InsertAfter;
149
150                 [XmlElement ("replace")]
151                 public string Replace;
152                 
153                 public string GetCode (HookAction action)
154                 {
155                         if (action == HookAction.InsertBefore)
156                                 return InsertBefore;
157                         else if (action == HookAction.InsertAfter)
158                                 return InsertAfter;
159                         else
160                                 return Replace;
161                 }
162         }
163         
164         [XmlType ("select")]
165         internal class Select
166         {
167                 [XmlElement ("typeName")]
168                 public string TypeName;
169                 
170                 [XmlElement("typeAttribute")]
171                 public string[] TypeAttributes;
172                 
173                 [XmlElement ("typeMember")]
174                 public string TypeMember;
175         }
176         
177         internal enum HookDir
178         {
179                 Read,
180                 Write
181         }
182         
183         internal enum HookAction
184         {
185                 InsertBefore,
186                 InsertAfter,
187                 Replace
188         }
189         
190         [XmlType ("hookType")]
191         internal enum HookType
192         {
193                 attributes,
194                 elements,
195                 unknownAttribute,
196                 unknownElement,
197                 member,
198                 type
199         }
200 }