Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Xml / System / Xml / Serialization / Advanced / SchemaImporterExtension.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="IXmlSerializable.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>                                                                
6 //------------------------------------------------------------------------------
7
8 namespace System.Xml.Serialization.Advanced {
9
10     using System.Xml.Schema;
11     using System.Xml;
12     using System.Collections;
13     using System.Collections.Specialized;
14     using System.CodeDom;
15     using System.CodeDom.Compiler;
16     using System.Xml.Serialization;
17
18     /// <include file='doc\SchemaImporterExtension.uex' path='docs/doc[@for="SchemaImporterExtension"]/*' />
19     ///<internalonly/>
20     /// <devdoc>
21     ///    <para>[To be supplied.]</para>
22     /// </devdoc>
23     public abstract class SchemaImporterExtension {
24         /// <include file='doc\SchemaImporterExtension.uex' path='docs/doc[@for="SchemaImporterExtension.ImportSchemaType"]/*' />
25         public virtual string ImportSchemaType(string name, string ns, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, 
26             CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider) {
27             return null;
28         }
29
30         /// <include file='doc\SchemaImporterExtension.uex' path='docs/doc[@for="SchemaImporterExtension.ImportSchemaType1"]/*' />
31         public virtual string ImportSchemaType(XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer,
32             CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider) {
33             return null;
34         }
35
36         /// <include file='doc\SchemaImporterExtension.uex' path='docs/doc[@for="SchemaImporterExtension.ImportSchemaType1"]/*' />
37         public virtual string ImportAnyElement(XmlSchemaAny any, bool mixed, XmlSchemas schemas, XmlSchemaImporter importer,
38             CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider) {
39             return null;
40         }
41
42         /// <include file='doc\SchemaImporterExtension.uex' path='docs/doc[@for="SchemaImporterExtension.ImportDefaultValue"]/*' />
43         public virtual CodeExpression ImportDefaultValue(string value, string type) {
44             return null;
45         }
46     }
47
48     public class SchemaImporterExtensionCollection : CollectionBase {
49         Hashtable exNames;
50
51         internal Hashtable Names {
52             get {
53                 if (exNames == null)
54                     exNames = new Hashtable();
55                 return exNames;
56             }
57         }
58
59         public int Add(SchemaImporterExtension extension) {
60             return Add(extension.GetType().FullName, extension);
61         }
62
63         public int Add(string name, Type type) {
64             if (type.IsSubclassOf(typeof(SchemaImporterExtension))) {
65                 return Add(name, (SchemaImporterExtension)Activator.CreateInstance(type));
66             }
67             else {
68                 throw new ArgumentException(Res.GetString(Res.XmlInvalidSchemaExtension, type));
69             }
70         }
71         
72         public void Remove(string name) {
73             if (Names[name] != null) {
74                 List.Remove(Names[name]);
75                 Names[name] = null;
76             }
77         }
78
79         public new void Clear() {
80             Names.Clear();
81             List.Clear();
82         }
83
84         internal SchemaImporterExtensionCollection Clone() {
85             SchemaImporterExtensionCollection clone = new SchemaImporterExtensionCollection();
86             clone.exNames = (Hashtable)this.Names.Clone();
87             foreach (object o in this.List) {
88                 clone.List.Add(o);
89             }
90             return clone;
91         }
92
93         public SchemaImporterExtension this[int index] {
94             get { return (SchemaImporterExtension)List[index]; }
95             set { List[index] = value; }
96         }
97
98         internal int Add(string name, SchemaImporterExtension extension) {
99             if (Names[name] != null) {
100                 if (Names[name].GetType() != extension.GetType()) {
101                     throw new InvalidOperationException(Res.GetString(Res.XmlConfigurationDuplicateExtension, name));
102                 }
103                 return -1;
104             }
105             Names[name] = extension;
106             return List.Add(extension);
107         }
108         
109         public void Insert(int index, SchemaImporterExtension extension) {
110             List.Insert(index, extension);
111         }
112         
113         public int IndexOf(SchemaImporterExtension extension) {
114             return List.IndexOf(extension);
115         }
116         
117         public bool Contains(SchemaImporterExtension extension) {
118             return List.Contains(extension);
119         }
120         
121         public void Remove(SchemaImporterExtension extension) {
122             List.Remove(extension);
123         }
124         
125         public void CopyTo(SchemaImporterExtension[] array, int index) {
126             List.CopyTo(array, index);
127         }
128     }
129
130     internal class MappedTypeDesc {
131         string name;
132         string ns;
133         XmlSchemaType xsdType;
134         XmlSchemaObject context;
135         string clrType;
136         SchemaImporterExtension extension;
137         CodeNamespace code;
138         bool exported = false;
139         StringCollection references;
140
141         internal MappedTypeDesc(string clrType, string name, string ns, XmlSchemaType xsdType, XmlSchemaObject context, SchemaImporterExtension extension, CodeNamespace code, StringCollection references) {
142             this.clrType = clrType.Replace('+', '.');
143             this.name = name;
144             this.ns = ns;
145             this.xsdType = xsdType;
146             this.context = context;
147             this.code = code;
148             this.references = references;
149             this.extension = extension;
150         }
151
152         internal SchemaImporterExtension Extension { get { return extension; } }
153         internal string Name { get { return clrType; } }
154         
155         internal StringCollection ReferencedAssemblies { 
156             get { 
157                 if (references == null)
158                     references = new StringCollection();
159                 return references; 
160             } 
161         }
162
163         internal CodeTypeDeclaration ExportTypeDefinition(CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit) {
164             if (exported)
165                 return null;
166             exported = true;
167
168             foreach(CodeNamespaceImport import in code.Imports) {
169                 codeNamespace.Imports.Add(import);
170             }
171             CodeTypeDeclaration codeClass = null;
172             string comment = Res.GetString(Res.XmlExtensionComment, extension.GetType().FullName);
173             foreach(CodeTypeDeclaration type in code.Types) {
174                 if (clrType == type.Name) {
175                     if (codeClass != null)
176                         throw new InvalidOperationException(Res.GetString(Res.XmlExtensionDuplicateDefinition, extension.GetType().FullName, clrType));
177                     codeClass = type;
178                 }
179                 type.Comments.Add(new CodeCommentStatement(comment, false));
180                 codeNamespace.Types.Add(type);
181             }
182             if (codeCompileUnit != null) {
183                 foreach(string reference in ReferencedAssemblies) {
184                     if (codeCompileUnit.ReferencedAssemblies.Contains(reference))
185                         continue;
186                     codeCompileUnit.ReferencedAssemblies.Add(reference);
187                 }
188             }
189             return codeClass;
190         }
191     }
192 }