1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //------------------------------------------------------------
5 namespace System.ServiceModel.Security
7 using System.Collections.Generic;
8 using System.IdentityModel;
9 using System.Runtime.CompilerServices;
11 using DictionaryManager = System.IdentityModel.DictionaryManager;
12 using ISecurityElement = System.IdentityModel.ISecurityElement;
14 [TypeForwardedFrom("System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
15 sealed class ReferenceList : ISecurityElement
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>();
23 public ReferenceList()
27 public int DataReferenceCount
29 get { return this.referredIds.Count; }
42 #pragma warning suppress 56503
43 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
47 public void AddReferredId(string id)
51 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("id"));
53 this.referredIds.Add(id);
56 public bool ContainsReferredId(string id)
60 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("id"));
62 return this.referredIds.Contains(id);
65 public string GetReferredId(int index)
67 return this.referredIds[index];
70 public void ReadFrom(XmlDictionaryReader reader)
72 reader.ReadStartElement(ElementName, NamespaceUri);
73 while (reader.IsStartElement())
75 string id = DataReference.ReadFrom(reader);
76 if (this.referredIds.Contains(id))
78 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
79 new SecurityMessageSerializationException(SR.GetString(SR.InvalidDataReferenceInReferenceList, "#" + id)));
81 this.referredIds.Add(id);
83 reader.ReadEndElement(); // ReferenceList
84 if (this.DataReferenceCount == 0)
86 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityMessageSerializationException(SR.GetString(SR.ReferenceListCannotBeEmpty)));
90 public bool TryRemoveReferredId(string id)
94 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("id"));
96 return this.referredIds.Remove(id);
99 public void WriteTo(XmlDictionaryWriter writer, DictionaryManager dictionaryManager)
101 if (this.DataReferenceCount == 0)
103 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ReferenceListCannotBeEmpty)));
105 writer.WriteStartElement(NamespacePrefix, ElementName, NamespaceUri);
106 for (int i = 0; i < this.DataReferenceCount; i++)
108 DataReference.WriteTo(writer, this.referredIds[i]);
110 writer.WriteEndElement(); // ReferenceList
113 static class DataReference
115 internal static readonly XmlDictionaryString ElementName = XD.XmlEncryptionDictionary.DataReference;
116 internal static readonly XmlDictionaryString NamespaceUri = EncryptedType.NamespaceUri;
118 public static string ReadFrom(XmlDictionaryReader reader)
121 string uri = XmlHelper.ReadEmptyElementAndRequiredAttribute(reader, ElementName, NamespaceUri, ReferenceList.UriAttribute, out prefix);
122 if (uri.Length < 2 || uri[0] != '#')
124 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
125 new SecurityMessageSerializationException(SR.GetString(SR.InvalidDataReferenceInReferenceList, uri)));
127 return uri.Substring(1);
130 public static void WriteTo(XmlDictionaryWriter writer, string referredId)
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();