add partial support for XmlSchemaProviderAttribute
[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, XmlMappingAccess dir, HookAction action, Type type, string member)
94                 {
95                         if ((dir & XmlMappingAccess.Read) != 0)
96                                 return FindHook (ReaderHooks, hookType, action, type, member);
97                         if ((dir & XmlMappingAccess.Write) != 0)
98                                 return FindHook (WriterHooks, hookType, action, type, member);
99                         else
100                                 throw new Exception ("INTERNAL ERROR");
101                 }
102                 
103                 ArrayList FindHook (Hook[] hooks, HookType hookType, HookAction action, Type type, string member)
104                 {
105                         ArrayList foundHooks = new ArrayList ();
106                         if (hooks == null) return foundHooks;
107         
108                         foreach (Hook hook in hooks)
109                         {
110                                 if (action == HookAction.InsertBefore && (hook.InsertBefore == null || hook.InsertBefore == ""))
111                                         continue;
112                                 else if (action == HookAction.InsertAfter && (hook.InsertAfter == null || hook.InsertAfter == ""))
113                                         continue;
114                                 else if (action == HookAction.Replace && (hook.Replace == null || hook.Replace == ""))
115                                         continue;
116                                         
117                                 if (hook.HookType != hookType)
118                                         continue;
119                                         
120                                 if (hook.Select != null)
121                                 {
122                                         if (hook.Select.TypeName != null && hook.Select.TypeName != "")
123                                                 if (hook.Select.TypeName != type.FullName) continue;
124                 
125                                         if (hook.Select.TypeMember != null && hook.Select.TypeMember != "")
126                                                 if (hook.Select.TypeMember != member) continue;
127                                                 
128                                         if (hook.Select.TypeAttributes != null && hook.Select.TypeAttributes.Length > 0)
129                                         {
130                                                 object[] ats = type.GetCustomAttributes (true);
131                                                 bool found = false;
132                                                 foreach (object at in ats)
133                                                         if (Array.IndexOf (hook.Select.TypeAttributes, at.GetType().FullName) != -1) { found = true; break; }
134                                                 if (!found) continue;
135                                         }
136                                 }
137                                 foundHooks.Add (hook);
138                         }
139                         return foundHooks;
140                 }
141         }
142         
143         [XmlType ("hook")]
144         internal class Hook
145         {
146                 [XmlAttribute ("type")]
147                 public HookType HookType;
148         
149                 [XmlElement ("select")]
150                 public Select Select;
151                 
152                 [XmlElement ("insertBefore")]
153                 public string InsertBefore;
154
155                 [XmlElement ("insertAfter")]
156                 public string InsertAfter;
157
158                 [XmlElement ("replace")]
159                 public string Replace;
160                 
161                 public string GetCode (HookAction action)
162                 {
163                         if (action == HookAction.InsertBefore)
164                                 return InsertBefore;
165                         else if (action == HookAction.InsertAfter)
166                                 return InsertAfter;
167                         else
168                                 return Replace;
169                 }
170         }
171         
172         [XmlType ("select")]
173         internal class Select
174         {
175                 [XmlElement ("typeName")]
176                 public string TypeName;
177                 
178                 [XmlElement("typeAttribute")]
179                 public string[] TypeAttributes;
180                 
181                 [XmlElement ("typeMember")]
182                 public string TypeMember;
183         }
184         
185         internal enum HookAction
186         {
187                 InsertBefore,
188                 InsertAfter,
189                 Replace
190         }
191         
192         [XmlType ("hookType")]
193         internal enum HookType
194         {
195                 attributes,
196                 elements,
197                 unknownAttribute,
198                 unknownElement,
199                 member,
200                 type
201         }
202 }