(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.Security.Permissions / PermissionSetAttribute.cs
blobc5e0139d84a4d285e56ed4bdef62591e025d3d93
1 //
2 // System.Security.Permissions.PermissionSetAttribute.cs
3 //
4 // Authors
5 // Duncan Mak <duncan@ximian.com>
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // (C) 2002 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
31 using System.IO;
32 using System.Security.Policy;
33 using System.Text;
35 using Mono.Xml;
37 namespace System.Security.Permissions {
39 [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class |
40 AttributeTargets.Struct | AttributeTargets.Constructor |
41 AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
42 [Serializable]
43 public sealed class PermissionSetAttribute : CodeAccessSecurityAttribute {
45 // Fields
46 private string file;
47 private string name;
48 private bool isUnicodeEncoded;
49 private string xml;
50 #if NET_2_0
51 private string hex;
52 #endif
54 // Constructor
55 public PermissionSetAttribute (SecurityAction action)
56 : base (action)
60 // Properties
61 public string File {
62 get { return file; }
63 set { file = value; }
65 #if NET_2_0
66 [MonoTODO ("Undocumented")]
67 public string Hex {
68 get { return hex; }
69 set { hex = value; }
71 #endif
72 public string Name {
73 get { return name; }
74 set { name = value; }
77 public bool UnicodeEncoded {
78 get { return isUnicodeEncoded; }
79 set { isUnicodeEncoded = value; }
82 public string XML {
83 get { return xml; }
84 set { xml = value; }
87 // Methods
88 public override IPermission CreatePermission ()
90 return null; // Not used, used for inheritance from SecurityAttribute
93 private PermissionSet CreateFromXml (string xml)
95 SecurityParser sp = new SecurityParser ();
96 sp.LoadXml (xml);
97 SecurityElement se = sp.ToXml ();
99 string className = se.Attribute ("class");
100 if (className == null)
101 return null;
103 PermissionState state = PermissionState.None;
104 if (CodeAccessPermission.IsUnrestricted (se))
105 state = PermissionState.Unrestricted;
107 if (className.EndsWith ("NamedPermissionSet")) {
108 NamedPermissionSet nps = new NamedPermissionSet (se.Attribute ("Name"), state);
109 return (PermissionSet) nps;
111 else if (className.EndsWith ("PermissionSet")) {
112 PermissionSet ps = new PermissionSet (state);
113 return ps;
115 return null;
118 public PermissionSet CreatePermissionSet ()
120 PermissionSet pset = null;
121 if (this.Unrestricted)
122 pset = new PermissionSet (PermissionState.Unrestricted);
123 else {
124 pset = new PermissionSet (PermissionState.None);
125 if (name != null) {
126 return PolicyLevel.CreateAppDomainLevel ().GetNamedPermissionSet (name);
128 else if (file != null) {
129 Encoding e = ((isUnicodeEncoded) ? System.Text.Encoding.Unicode : System.Text.Encoding.ASCII);
130 using (StreamReader sr = new StreamReader (file, e)) {
131 pset = CreateFromXml (sr.ReadToEnd ());
134 else if (xml != null) {
135 pset = CreateFromXml (xml);
138 return pset;