2972098749ea154616e932aad59dba8164c71a02
[mono.git] / mcs / class / referencesource / System.IdentityModel / System / ServiceModel / Security / ReferenceList.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4
5 namespace System.ServiceModel.Security
6 {
7     using System.Collections.Generic;
8     using System.IdentityModel;
9     using System.Runtime.CompilerServices;
10     using System.Xml;
11     using DictionaryManager = System.IdentityModel.DictionaryManager;
12     using ISecurityElement = System.IdentityModel.ISecurityElement;
13
14     [TypeForwardedFrom("System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
15     sealed class ReferenceList : ISecurityElement
16     {
17         internal static readonly XmlDictionaryString ElementName = XD.XmlEncryptionDictionary.ReferenceList;
18         const string NamespacePrefix = XmlEncryptionStrings.Prefix;
19         internal static readonly XmlDictionaryString NamespaceUri = EncryptedType.NamespaceUri;
20         internal static readonly XmlDictionaryString UriAttribute = XD.XmlEncryptionDictionary.URI;
21         List<string> referredIds = new List<string>();
22
23         public ReferenceList()
24         {
25         }
26
27         public int DataReferenceCount
28         {
29             get { return this.referredIds.Count; }
30         }
31
32         public bool HasId
33         {
34             get { return false; }
35         }
36
37         public string Id
38         {
39             get
40             {
41                 // PreSharp 
42                 #pragma warning suppress 56503
43                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
44             }
45         }
46
47         public void AddReferredId(string id)
48         {
49             if (id == null)
50             {
51                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("id"));
52             }
53             this.referredIds.Add(id);
54         }
55
56         public bool ContainsReferredId(string id)
57         {
58             if (id == null)
59             {
60                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("id"));
61             }
62             return this.referredIds.Contains(id);
63         }
64
65         public string GetReferredId(int index)
66         {
67             return this.referredIds[index];
68         }
69
70         public void ReadFrom(XmlDictionaryReader reader)
71         {
72             reader.ReadStartElement(ElementName, NamespaceUri);
73             while (reader.IsStartElement())
74             {
75                 string id = DataReference.ReadFrom(reader);
76                 if (this.referredIds.Contains(id))
77                 {
78                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
79                         new SecurityMessageSerializationException(SR.GetString(SR.InvalidDataReferenceInReferenceList, "#" + id)));
80                 }
81                 this.referredIds.Add(id);
82             }
83             reader.ReadEndElement(); // ReferenceList
84             if (this.DataReferenceCount == 0)
85             {
86                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityMessageSerializationException(SR.GetString(SR.ReferenceListCannotBeEmpty)));
87             }
88         }
89
90         public bool TryRemoveReferredId(string id)
91         {
92             if (id == null)
93             {
94                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("id"));
95             }
96             return this.referredIds.Remove(id);
97         }
98
99         public void WriteTo(XmlDictionaryWriter writer, DictionaryManager dictionaryManager)
100         {
101             if (this.DataReferenceCount == 0)
102             {
103                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ReferenceListCannotBeEmpty)));
104             }
105             writer.WriteStartElement(NamespacePrefix, ElementName, NamespaceUri);
106             for (int i = 0; i < this.DataReferenceCount; i++)
107             {
108                 DataReference.WriteTo(writer, this.referredIds[i]);
109             }
110             writer.WriteEndElement(); // ReferenceList
111         }
112
113         static class DataReference
114         {
115             internal static readonly XmlDictionaryString ElementName = XD.XmlEncryptionDictionary.DataReference;
116             internal static readonly XmlDictionaryString NamespaceUri = EncryptedType.NamespaceUri;
117
118             public static string ReadFrom(XmlDictionaryReader reader)
119             {
120                 string prefix;
121                 string uri = XmlHelper.ReadEmptyElementAndRequiredAttribute(reader, ElementName, NamespaceUri, ReferenceList.UriAttribute, out prefix);
122                 if (uri.Length < 2 || uri[0] != '#')
123                 {
124                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
125                         new SecurityMessageSerializationException(SR.GetString(SR.InvalidDataReferenceInReferenceList, uri)));
126                 }
127                 return uri.Substring(1);
128             }
129
130             public static void WriteTo(XmlDictionaryWriter writer, string referredId)
131             {
132                 writer.WriteStartElement(XD.XmlEncryptionDictionary.Prefix.Value, ElementName, NamespaceUri);
133                 writer.WriteStartAttribute(ReferenceList.UriAttribute, null);
134                 writer.WriteString("#");
135                 writer.WriteString(referredId);
136                 writer.WriteEndAttribute();
137                 writer.WriteEndElement();
138             }
139         }
140     }
141 }