d820af9e042cb7eb24b07743c68ada8b6f6a3eac
[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.IO;
10 using System.Xml.Schema;
11 using System.Xml.Serialization;
12
13 #if WEB_EXTENSIONS_CODE
14 namespace System.Web.Compilation.WCFModel
15 #else
16 namespace Microsoft.VSDesigner.WCFModel
17 #endif
18 {
19 #if WEB_EXTENSIONS_CODE
20     internal class SvcMapFileLoader : MapFileLoader
21 #else
22     public class SvcMapFileLoader : MapFileLoader
23 #endif
24     {
25         private string _mapFilePath;
26         private XmlSchemaSet _mapFileSchemaSet;
27         private XmlSerializer _mapFileSerializer;
28
29         public SvcMapFileLoader(string mapFilePath)
30         {
31             Debug.Assert(!string.IsNullOrEmpty(mapFilePath), "mapFilePath is null!");
32
33             _mapFilePath = mapFilePath;
34         }
35
36         #region protected overrides methods
37
38         protected override string MapFileName
39         {
40             get { return _mapFilePath; }
41         }
42
43         protected override MapFile Wrap(object mapFileImpl)
44         {
45             return mapFileImpl is SvcMapFileImpl ? new SvcMapFile((SvcMapFileImpl)mapFileImpl) : null;
46         }
47
48         protected override object Unwrap(MapFile mapFile)
49         {
50             return mapFile is SvcMapFile ? ((SvcMapFile)mapFile).Impl : null;
51         }
52
53         protected override XmlSchemaSet GetMapFileSchemaSet()
54         {
55             if (_mapFileSchemaSet == null)
56             {
57                 _mapFileSchemaSet = new XmlSchemaSet();
58
59                 using (var stream = typeof(SvcMapFileImpl).Assembly.GetManifestResourceStream(typeof(SvcMapFileImpl), @"Schema.ServiceMapSchema.xsd"))
60                 {
61                     _mapFileSchemaSet.Add(XmlSchema.Read(stream, null));
62                 }
63             }
64
65             return _mapFileSchemaSet;
66         }
67
68         protected override XmlSerializer GetMapFileSerializer()
69         {
70             if (_mapFileSerializer == null)
71             {
72 #if WEB_EXTENSIONS_CODE
73                 _mapFileSerializer = new System.Web.Compilation.WCFModel.SvcMapFileXmlSerializer.SvcMapFileImplSerializer();
74 #else
75                 _mapFileSerializer = new XmlSerializer(typeof(SvcMapFileImpl), SvcMapFileImpl.NamespaceUri);
76 #endif
77             }
78
79             return _mapFileSerializer;
80         }
81
82         protected override TextReader GetMapFileReader()
83         {
84             return File.OpenText(_mapFilePath);
85         }
86
87         protected override byte[] ReadMetadataFile(string name)
88         {
89             return File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(_mapFilePath), name));
90         }
91
92         protected override byte[] ReadExtensionFile(string name)
93         {
94             return File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(_mapFilePath), name));
95         }
96
97         #endregion protected overrides methods
98     }
99 }