Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.ServiceModel / System / ServiceModel / Description / MetadataSection.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4
5 namespace System.ServiceModel.Description
6 {
7     using System;
8     using System.Collections.Generic;
9     using System.Collections.ObjectModel;
10     using System.Text;
11     using System.Xml;
12     using WsdlNS = System.Web.Services.Description;
13     using XsdNS = System.Xml.Schema;
14     using System.Reflection;
15     using System.Xml.Serialization;
16
17     [XmlRoot(ElementName = MetadataStrings.MetadataExchangeStrings.MetadataSection, Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
18     public class MetadataSection
19     {
20         Collection<XmlAttribute> attributes = new Collection<XmlAttribute>();
21         string dialect;
22         string identifier;
23         object metadata;
24         string sourceUrl;
25         static XmlDocument xmlDocument = new XmlDocument();
26
27         public MetadataSection()
28             : this(null, null, null)
29         {
30         }
31
32         public MetadataSection(string dialect, string identifier, object metadata)
33         {
34             this.dialect = dialect;
35             this.identifier = identifier;
36             this.metadata = metadata;
37         }
38
39         static public string ServiceDescriptionDialect { get { return System.Web.Services.Description.ServiceDescription.Namespace; } }
40         static public string XmlSchemaDialect { get { return System.Xml.Schema.XmlSchema.Namespace; } }
41         static public string PolicyDialect { get { return MetadataStrings.WSPolicy.NamespaceUri; } }
42         static public string MetadataExchangeDialect { get { return MetadataStrings.MetadataExchangeStrings.Namespace; } }
43
44         [XmlAnyAttribute]
45         public Collection<XmlAttribute> Attributes
46         {
47             get { return attributes; }
48         }
49
50         [XmlAttribute]
51         public string Dialect
52         {
53             get { return this.dialect; }
54             set { this.dialect = value; }
55         }
56
57         [XmlAttribute]
58         public string Identifier
59         {
60             get { return this.identifier; }
61             set { this.identifier = value; }
62         }
63
64         [XmlAnyElement]
65         [XmlElement(MetadataStrings.XmlSchema.Schema, typeof(XsdNS.XmlSchema), Namespace = XsdNS.XmlSchema.Namespace)]
66         //typeof(WsdlNS.ServiceDescription) produces an XmlSerializer which can't export / import the Extensions in the ServiceDescription.  
67         //We use change this to typeof(string) and then fix the generated serializer to use the Read/Write 
68         //methods provided by WsdlNS.ServiceDesciption which use a pregenerated serializer which can export / import the Extensions.
69         [XmlElement(MetadataStrings.ServiceDescription.Definitions, typeof(WsdlNS.ServiceDescription), Namespace = WsdlNS.ServiceDescription.Namespace)]
70         [XmlElement(MetadataStrings.MetadataExchangeStrings.MetadataReference, typeof(MetadataReference), Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
71         [XmlElement(MetadataStrings.MetadataExchangeStrings.Location, typeof(MetadataLocation), Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
72         [XmlElement(MetadataStrings.MetadataExchangeStrings.Metadata, typeof(MetadataSet), Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
73         public object Metadata
74         {
75             get { return this.metadata; }
76             set { this.metadata = value; }
77         }
78
79         internal string SourceUrl
80         {
81             get { return sourceUrl; }
82             set { sourceUrl = value; }
83         }
84
85         public static MetadataSection CreateFromPolicy(XmlElement policy, string identifier)
86         {
87             if (policy == null)
88             {
89                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("policy");
90             }
91
92             if (!IsPolicyElement(policy))
93             {
94                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("policy",
95 #pragma warning suppress 56506 // [....], policy cannot be null at this point since it has been validated above.
96  SR.GetString(SR.SFxBadMetadataMustBePolicy, MetadataStrings.WSPolicy.NamespaceUri, MetadataStrings.WSPolicy.Elements.Policy, policy.NamespaceURI, policy.LocalName));
97             }
98
99             MetadataSection section = new MetadataSection();
100
101             section.Dialect = policy.NamespaceURI;
102             section.Identifier = identifier;
103             section.Metadata = policy;
104
105             return section;
106         }
107         public static MetadataSection CreateFromSchema(XsdNS.XmlSchema schema)
108         {
109             if (schema == null)
110             {
111                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("schema");
112             }
113
114             MetadataSection section = new MetadataSection();
115
116             section.Dialect = MetadataSection.XmlSchemaDialect;
117             section.Identifier = schema.TargetNamespace;
118             section.Metadata = schema;
119
120             return section;
121         }
122         public static MetadataSection CreateFromServiceDescription(WsdlNS.ServiceDescription serviceDescription)
123         {
124             if (serviceDescription == null)
125             {
126                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceDescription");
127             }
128
129             MetadataSection section = new MetadataSection();
130
131             section.Dialect = MetadataSection.ServiceDescriptionDialect;
132             section.Identifier = serviceDescription.TargetNamespace;
133             section.Metadata = serviceDescription;
134
135             return section;
136         }
137
138         internal static bool IsPolicyElement(XmlElement policy)
139         {
140             return (policy.NamespaceURI == MetadataStrings.WSPolicy.NamespaceUri
141                 || policy.NamespaceURI == MetadataStrings.WSPolicy.NamespaceUri15)
142                 && policy.LocalName == MetadataStrings.WSPolicy.Elements.Policy;
143         }
144
145     }
146 }