Merge pull request #3389 from lambdageek/bug-43099
[mono.git] / mcs / class / referencesource / System.Web.Extensions / Compilation / WCFModel / SvcMapFileLoader.cs
1 #region Copyright (c) Microsoft Corporation
2 /// <copyright company='Microsoft Corporation'>
3 ///    Copyright (c) Microsoft Corporation. All Rights Reserved.
4 ///    Information Contained Herein is Proprietary and Confidential.
5 /// </copyright>
6 #endregion
7
8 using System.Diagnostics;
9 using System.Diagnostics.CodeAnalysis;
10 using System.IO;
11 using System.Xml.Schema;
12 using System.Xml.Serialization;
13
14 #if WEB_EXTENSIONS_CODE
15 namespace System.Web.Compilation.WCFModel
16 #else
17 namespace Microsoft.VSDesigner.WCFModel
18 #endif
19 {
20 #if WEB_EXTENSIONS_CODE
21     internal class SvcMapFileLoader : MapFileLoader
22 #else
23     public class SvcMapFileLoader : MapFileLoader
24 #endif
25     {
26         private string _mapFilePath;
27         private XmlSchemaSet _mapFileSchemaSet;
28         private XmlSerializer _mapFileSerializer;
29
30         public SvcMapFileLoader(string mapFilePath)
31         {
32             Debug.Assert(!string.IsNullOrEmpty(mapFilePath), "mapFilePath is null!");
33
34             _mapFilePath = mapFilePath;
35         }
36
37         #region protected overrides methods
38
39         protected override string MapFileName
40         {
41             get { return _mapFilePath; }
42         }
43
44         protected override MapFile Wrap(object mapFileImpl)
45         {
46             return mapFileImpl is SvcMapFileImpl ? new SvcMapFile((SvcMapFileImpl)mapFileImpl) : null;
47         }
48
49         protected override object Unwrap(MapFile mapFile)
50         {
51             return mapFile is SvcMapFile ? ((SvcMapFile)mapFile).Impl : null;
52         }
53
54         [SuppressMessage("Microsoft.Security.Xml", "CA3060:UseXmlReaderForSchemaRead", Justification = "asp.net controls this .xsd file")]
55         protected override XmlSchemaSet GetMapFileSchemaSet()
56         {
57             if (_mapFileSchemaSet == null)
58             {
59                 _mapFileSchemaSet = new XmlSchemaSet();
60
61                 using (var stream = typeof(SvcMapFileImpl).Assembly.GetManifestResourceStream(typeof(SvcMapFileImpl), @"Schema.ServiceMapSchema.xsd"))
62                 {
63                     _mapFileSchemaSet.Add(XmlSchema.Read(stream, null));
64                 }
65             }
66
67             return _mapFileSchemaSet;
68         }
69
70         protected override XmlSerializer GetMapFileSerializer()
71         {
72             if (_mapFileSerializer == null)
73             {
74 #if WEB_EXTENSIONS_CODE
75                 _mapFileSerializer = new System.Web.Compilation.WCFModel.SvcMapFileXmlSerializer.SvcMapFileImplSerializer();
76 #else
77                 _mapFileSerializer = new XmlSerializer(typeof(SvcMapFileImpl), SvcMapFileImpl.NamespaceUri);
78 #endif
79             }
80
81             return _mapFileSerializer;
82         }
83
84         protected override TextReader GetMapFileReader()
85         {
86             return File.OpenText(_mapFilePath);
87         }
88
89         protected override byte[] ReadMetadataFile(string name)
90         {
91             return File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(_mapFilePath), name));
92         }
93
94         protected override byte[] ReadExtensionFile(string name)
95         {
96             return File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(_mapFilePath), name));
97         }
98
99         #endregion protected overrides methods
100     }
101 }