5d58d0f1c293f6ce1604babba4728300562e9afe
[mono.git] / mcs / class / referencesource / System.Web / Compilation / XsdBuildProvider.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="XsdBuildProvider.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 namespace System.Web.Compilation {
8
9 using System;
10 using System.IO;
11 using System.Data;
12 using System.Data.Design;
13 using System.Diagnostics.CodeAnalysis;
14 using System.Globalization;
15 using System.Xml;
16 using System.Xml.Schema;
17 using System.Reflection;
18 using System.CodeDom;
19 using System.CodeDom.Compiler;
20 using System.Web.Hosting;
21 using System.Web.Configuration;
22 using System.Collections;
23
24 using Util=System.Web.UI.Util;
25 #if !FEATURE_PAL // FEATURE_PAL does not support System.Data.Design
26 using TypedDataSetGenerator=System.Data.Design.TypedDataSetGenerator;
27 #endif // !FEATURE_PAL 
28
29 [BuildProviderAppliesTo(BuildProviderAppliesTo.Code)]
30 internal class XsdBuildProvider: BuildProvider {
31
32     [SuppressMessage("Microsoft.Security", "MSEC1207:UseXmlReaderForLoad", Justification = "Developer-controlled .xsd files in application directory are implicitly trusted by ASP.Net.")]
33     public override void GenerateCode(AssemblyBuilder assemblyBuilder)  {
34 #if !FEATURE_PAL // FEATURE_PAL does not support System.Data.Design
35         // Get the namespace that we will use
36         string ns = Util.GetNamespaceFromVirtualPath(VirtualPathObject);
37
38         // We need to use XmlDocument to parse the xsd file is order to open it with the
39         // correct encoding (VSWhidbey 566286)
40         XmlDocument doc = new XmlDocument();
41         using (Stream stream = OpenStream()) {
42             doc.Load(stream);
43         }
44         String content = doc.OuterXml;
45
46         // Generate a CodeCompileUnit from the dataset
47         CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
48
49         CodeNamespace codeNamespace = new CodeNamespace(ns);
50         codeCompileUnit.Namespaces.Add(codeNamespace);
51
52         // Devdiv 18365, Dev10 
53
54         bool isVer35OrAbove = CompilationUtil.IsCompilerVersion35OrAbove(assemblyBuilder.CodeDomProvider.GetType());
55
56         if (isVer35OrAbove) {
57             TypedDataSetGenerator.GenerateOption generateOptions = TypedDataSetGenerator.GenerateOption.None;
58             generateOptions |= TypedDataSetGenerator.GenerateOption.HierarchicalUpdate;
59             generateOptions |= TypedDataSetGenerator.GenerateOption.LinqOverTypedDatasets;
60             Hashtable customDBProviders = null;
61             TypedDataSetGenerator.Generate(content, codeCompileUnit, codeNamespace, assemblyBuilder.CodeDomProvider, customDBProviders, generateOptions);
62         }
63         else {
64             TypedDataSetGenerator.Generate(content, codeCompileUnit, codeNamespace, assemblyBuilder.CodeDomProvider);
65         }
66
67         // Add all the assembly references needed by the generated code
68         if (TypedDataSetGenerator.ReferencedAssemblies != null) {
69             var isVer35 = CompilationUtil.IsCompilerVersion35(assemblyBuilder.CodeDomProvider.GetType());
70             foreach (Assembly a in TypedDataSetGenerator.ReferencedAssemblies) {
71                 
72                 if (isVer35) {
73                     var aName = a.GetName();
74                     if (aName.Name == "System.Data.DataSetExtensions") {
75                         // Dev10 
76
77                         aName.Version = new Version(3, 5, 0, 0);
78                         CompilationSection.RecordAssembly(aName.FullName, a);
79                     }
80                 }
81                 assemblyBuilder.AddAssemblyReference(a);
82             }
83         }
84         
85
86         // Add the CodeCompileUnit to the compilation
87         assemblyBuilder.AddCodeCompileUnit(this, codeCompileUnit);
88 #else // !FEATURE_PAL 
89         throw new NotImplementedException("System.Data.Design - ROTORTODO");
90 #endif // !FEATURE_PAL 
91     }
92 }
93 }