Added copyright
[mono.git] / mcs / class / corlib / System.Security.Policy / FirstMatchCodeGroup.cs
1 // System.Security.Policy.FirstMatchCodeGroup
2 //
3 // Author(s):
4 //  Jackson Harper (Jackson@LatitudeGeo.com)
5 //
6 // (C) 2002 Jackson Harper, All rights reserved.
7
8 using System;
9
10 namespace System.Security.Policy {
11
12         public sealed class FirstMatchCodeGroup : CodeGroup {
13                 
14                 public FirstMatchCodeGroup(IMembershipCondition membershipCondition, PolicyStatement policy) :
15                         base (membershipCondition, policy)
16                 {
17                 }
18
19                 //
20                 // Public Properties
21                 //
22
23                 public override string MergeLogic
24                 {
25                         get { return "First Match"; }
26                 }
27
28                 //
29                 // Public Methods
30                 //
31                 
32                 public override CodeGroup Copy()
33                 {
34                         FirstMatchCodeGroup copy = CopyNoChildren ();
35
36                         foreach (CodeGroup group in Children) {
37                                 copy.AddChild ( group );
38                         }
39
40                         return copy;
41                 }
42
43                 public override PolicyStatement Resolve(Evidence evidence)
44                 {
45                         PolicyStatement policy = null;
46                         PolicyStatement child_policy;
47
48                         if (null == evidence)
49                                 throw new ArgumentNullException ();
50
51                         if (MembershipCondition.Check (evidence)) {
52                                 if (null != PolicyStatement) {
53                                         policy = PolicyStatement;
54                                 } else {
55                                         // Loop through all children breaking on the first one that resolves
56                                         foreach (CodeGroup child in Children) {
57                                                 if (null == (child_policy = child.Resolve (evidence)))
58                                                         continue;
59                                                 policy = child_policy;
60                                                 break;
61                                         }
62                                 }
63                         }
64                         
65                         return policy;
66                 }
67                 
68                 public override CodeGroup ResolveMatchingCodeGroups(Evidence evidence)
69                 {
70                         CodeGroup group = null;
71
72                         if (null == evidence)
73                                 throw new ArgumentNullException ();
74                         
75                         if (MembershipCondition.Check (evidence)) {
76                                 group = CopyNoChildren ();
77                                 
78                                 // Add the first child that resolves
79                                 foreach (CodeGroup child in Children) {
80                                         if ( null == child.Resolve (evidence))
81                                                 continue;
82                                         group.AddChild (child);
83                                         break;
84                                 }
85                         }
86
87                         return group;
88                 }
89         
90                 //
91                 // Private Methods
92                 //
93         
94                 private FirstMatchCodeGroup CopyNoChildren()
95                 {
96                         FirstMatchCodeGroup copy = new FirstMatchCodeGroup (MembershipCondition, PolicyStatement);
97
98                         copy.Name = Name;
99                         copy.Description = Description;
100
101                         return copy;
102                 }
103         }
104
105 }
106