This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32
33 namespace System.Security.Policy {
34
35         [Serializable]
36         public sealed class FirstMatchCodeGroup : CodeGroup {
37                 
38                 public FirstMatchCodeGroup (IMembershipCondition membershipCondition, PolicyStatement policy) :
39                         base (membershipCondition, policy) {}
40
41                 // for PolicyLevel (to avoid validation duplication)
42                 internal FirstMatchCodeGroup (SecurityElement e) : base (e) {}
43
44                 //
45                 // Public Properties
46                 //
47
48                 public override string MergeLogic
49                 {
50                         get { return "First Match"; }
51                 }
52
53                 //
54                 // Public Methods
55                 //
56                 
57                 public override CodeGroup Copy()
58                 {
59                         FirstMatchCodeGroup copy = CopyNoChildren ();
60                         foreach (CodeGroup child in Children) {
61                                 copy.AddChild (child.Copy ());  // deep copy
62                         }
63                         return copy;
64                 }
65
66                 public override PolicyStatement Resolve(Evidence evidence)
67                 {
68                         PolicyStatement policy = null;
69                         PolicyStatement child_policy;
70
71                         if (null == evidence)
72                                 throw new ArgumentNullException ();
73
74                         if (MembershipCondition.Check (evidence)) {
75                                 if (null != PolicyStatement) {
76                                         policy = PolicyStatement;
77                                 } else {
78                                         // Loop through all children breaking on the first one that resolves
79                                         foreach (CodeGroup child in Children) {
80                                                 if (null == (child_policy = child.Resolve (evidence)))
81                                                         continue;
82                                                 policy = child_policy;
83                                                 break;
84                                         }
85                                 }
86                         }
87                         
88                         return policy;
89                 }
90                 
91                 public override CodeGroup ResolveMatchingCodeGroups(Evidence evidence)
92                 {
93                         CodeGroup group = null;
94
95                         if (null == evidence)
96                                 throw new ArgumentNullException ();
97                         
98                         if (MembershipCondition.Check (evidence)) {
99                                 group = CopyNoChildren ();
100                                 
101                                 // Add the first child that resolves
102                                 foreach (CodeGroup child in Children) {
103                                         if ( null == child.Resolve (evidence))
104                                                 continue;
105                                         group.AddChild (child);
106                                         break;
107                                 }
108                         }
109
110                         return group;
111                 }
112         
113                 //
114                 // Private Methods
115                 //
116         
117                 private FirstMatchCodeGroup CopyNoChildren()
118                 {
119                         FirstMatchCodeGroup copy = new FirstMatchCodeGroup (MembershipCondition, PolicyStatement);
120
121                         copy.Name = Name;
122                         copy.Description = Description;
123
124                         return copy;
125                 }
126         }
127
128 }
129