2004-03-19 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / EncryptionProperty.cs
1 //
2 // EncryptionProperty.cs - EncryptionProperty implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-EncryptionProperty
4 //
5 // Author:
6 //      Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Tim Coleman, 2004
9
10 #if NET_1_2
11
12 using System.Xml;
13
14 namespace System.Security.Cryptography.Xml {
15         public sealed class EncryptionProperty {
16
17                 #region Fields
18
19                 XmlElement elemProp;
20                 string id;
21                 string target;
22
23                 #endregion // Fields
24         
25                 #region Constructors
26
27                 public EncryptionProperty ()
28                 {
29                 }
30
31                 public EncryptionProperty (XmlElement elemProp)
32                 {
33                         LoadXml (elemProp);
34                 }
35
36                 #endregion // Constructors
37
38                 #region Properties
39
40                 public string Id {
41                         get { return id; }
42                 }
43
44                 public XmlElement PropertyElement {
45                         get { return elemProp; }
46                         set { LoadXml (value); }
47                 }
48
49                 public string Target {
50                         get { return target; }
51                 }
52
53                 #endregion // Properties
54
55                 #region Methods
56
57                 public XmlElement GetXml ()
58                 {
59                         return GetXml (new XmlDocument ());
60                 }
61
62                 internal XmlElement GetXml (XmlDocument document)
63                 {
64                         XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl);
65
66                         if (Id != null)
67                                 xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
68                         if (Target != null)
69                                 xel.SetAttribute (XmlEncryption.AttributeNames.Target, Target);
70
71                         return xel;
72                 }
73
74                 public void LoadXml (XmlElement value)
75                 {
76                         if (value == null)
77                                 throw new ArgumentNullException ("value");
78
79                         if ((value.LocalName != XmlEncryption.ElementNames.EncryptionProperty) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
80                                 throw new CryptographicException ("Malformed EncryptionProperty element.");
81                         else {  
82                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
83                                         this.id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
84                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Target))
85                                         this.target = value.Attributes [XmlEncryption.AttributeNames.Target].Value;
86                         }
87                 }
88
89                 #endregion // Methods
90         }
91 }
92
93 #endif