2003-06-29 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / corlib / System.Security.Policy / CodeGroup.cs
1 // System.Security.Policy.CodeGroup
2 //
3 // Author(s):
4 //   Nick Drochak (ndrochak@gol.com)
5 //
6 // (C) 2001 Nick Drochak, All rights reserved.
7
8 using System.Security.Policy;
9 using System.Security.Permissions;\r
10 using System.Collections;
11 using System;  // for MonoTODO attribute
12
13 namespace System.Security.Policy
14 {
15         [Serializable]
16         public abstract class CodeGroup
17         {
18                 PolicyStatement m_policy = null;
19                 IMembershipCondition m_membershipCondition = null;
20                 string m_description = null;
21                 string m_name = null;
22                 ArrayList m_children = new ArrayList();
23                 PolicyLevel m_level;
24
25                 public CodeGroup(IMembershipCondition membershipCondition,
26                                         PolicyStatement policy)
27                 {
28                         if (null == membershipCondition)
29                                 throw new ArgumentNullException("Value cannot be null.");
30                         m_policy = policy;
31                         m_membershipCondition = membershipCondition;
32                 }
33
34                 public abstract CodeGroup Copy();
35                 public abstract string MergeLogic {get;}
36                 public abstract PolicyStatement Resolve(        Evidence evidence);
37                 public abstract CodeGroup ResolveMatchingCodeGroups(Evidence evidence);
38
39                 public PolicyStatement PolicyStatement
40                 {
41                         get
42                         {
43                                 return m_policy;
44                         }
45                         set
46                         {
47                                 m_policy = value;
48                         }
49                 }
50
51                 public string Description
52                 {
53                         get
54                         {
55                                 return m_description;
56                         }
57                         set
58                         {
59                                 m_description = value;
60                         }
61                 }
62
63                 public IMembershipCondition MembershipCondition 
64                 {
65                         get
66                         {
67                                 return m_membershipCondition;
68                         }
69                         set
70                         {
71                                 if (null == value)
72                                         throw new ArgumentException("Value cannot be null");
73                                 m_membershipCondition = value;
74                         }
75                 }
76
77                 public string Name 
78                 {
79                         get
80                         {
81                                 return m_name;
82                         }
83                         set
84                         {
85                                 m_name = value;
86                         }
87                 }
88
89                 public IList Children
90                 {
91                         get
92                         {
93                                 return m_children;
94                         }
95                         set
96                         {
97                                 if (null == value)
98                                         throw new ArgumentException("Value cannot be null");
99                                 m_children = new ArrayList(value);
100                         }
101                 }
102
103                 public virtual string AttributeString
104                 {
105                         get
106                         {
107                                 if (null != m_policy)
108                                         return m_policy.AttributeString;
109
110                                 return null;
111                         }
112                 }
113
114                 public virtual string PermissionSetName
115                 {
116                         get
117                         {
118                                 if (m_policy.PermissionSet is Security.NamedPermissionSet)
119                                         return ((NamedPermissionSet)(m_policy.PermissionSet)).Name;
120
121                                 return null;
122                         }
123                 }
124
125                 public void AddChild(CodeGroup group)
126                 {
127                         if (null == group)
128                                 throw new ArgumentNullException("The group parameter cannot be null");
129                         m_children.Add(group);
130                 }
131
132                 public override bool Equals(object o)
133                 {
134                         if (!(o is CodeGroup))
135                                 return false;
136
137                         return Equals((CodeGroup)o, false);
138                 }
139
140                 public bool Equals(CodeGroup cg, bool compareChildren)
141                 {
142                         if (cg.Name != this.Name)
143                                 return false;
144
145                         if (cg.Description != this.Description)
146                                 return false;
147
148                         if (cg.MembershipCondition != this.MembershipCondition)
149                                 return false;
150
151                         if (compareChildren)
152                         {
153                                 int childCount = cg.Children.Count;
154                                 if (this.Children.Count != childCount)
155                                         return false;
156
157                                 for (int index = 0; index < childCount; index++)
158                                 {
159                                         // LAMESPEC: are we supposed to check child equality recursively?
160                                         //              The docs imply 'no' but it seems natural to do a 'deep' compare.
161                                         //              Will check the children's children, and so-on unless we find out that
162                                         //              we shouldn't
163                                         if (!((CodeGroup)(this.Children[index])).Equals((CodeGroup)(cg.Children[index]), true))
164                                                 return false;
165                                 }
166                         }
167
168                         return true;
169
170                 }
171
172                 public void RemoveChild(CodeGroup group)
173                 {
174                         if (!m_children.Contains(group))
175                                 throw new ArgumentException();
176
177                         m_children.Remove(group);
178                 }
179
180                 [MonoTODO]
181                 public override int GetHashCode()
182                 {
183                         return 42;
184                 }
185
186                 public void FromXml(SecurityElement e)
187                 {
188                         FromXml(e, (PolicyLevel)null);
189                 }
190
191                 [MonoTODO]
192                 public void FromXml(SecurityElement e, PolicyLevel level        )
193                 {
194                         if (null == e)
195                                 throw new ArgumentNullException("e");
196
197                         // Not sure what might be serialized in this XML, so just do the strings for now
198                         // and null's for everything else
199                         m_children = null;
200                         m_policy = null;
201                         m_membershipCondition = null;
202
203                         m_name = e.Attribute("Name");
204                         m_description = e.Attribute("Description");
205
206                         // seems like we might need this to Resolve() in subclasses
207                         m_level = level;
208
209                         ParseXml(e, level);
210                 }
211
212                 protected virtual void ParseXml(SecurityElement e, PolicyLevel level)
213                 {
214                 }
215                 
216                 public SecurityElement ToXml()
217                 {
218                         return ToXml(null);
219                 }
220
221                 [MonoTODO("Not sure what to do with PolicyLevel parameter")]
222                  public SecurityElement ToXml(PolicyLevel level)\r
223                 {
224                         SecurityElement e = new SecurityElement("CodeGroup");
225                         e.AddAttribute("class", this.GetType().AssemblyQualifiedName);
226                         e.AddAttribute("version", "1");
227
228                         if (null != Name)
229                                 e.AddAttribute("Name", Name);
230
231                         if (null != Description)
232                                 e.AddAttribute("Description", Description);
233
234                         if (null != MembershipCondition)
235                                 e.AddChild(MembershipCondition.ToXml());
236
237                         if (null != PolicyStatement)
238                                 e.AddChild(PolicyStatement.PermissionSet.ToXml());
239
240                         foreach (CodeGroup child in Children)
241                                 e.AddChild(child.ToXml());
242
243                         CreateXml(e, level);
244                         return e;
245                 }
246                 
247                 protected virtual void CreateXml(SecurityElement element, PolicyLevel level)
248                 {
249                 }
250         }  // public abstract class CodeGroup
251
252 }  // namespace System.Security.Policy