Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls.WebParts / PersonalizableAttribute.cs
1 //
2 // System.Web.Compilation.PersonalizableAttribute
3 //
4 // Authors:
5 //   Marek Habersack (grendello@gmail.com)
6 //
7 // (C) 2006 Marek Habersack
8 //
9
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 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Reflection;
34
35 namespace System.Web.UI.WebControls.WebParts
36 {
37         [AttributeUsageAttribute(AttributeTargets.Property)] 
38         public sealed class PersonalizableAttribute : Attribute
39         {
40                 public static readonly PersonalizableAttribute Default;
41                 public static readonly PersonalizableAttribute NotPersonalizable;
42                 public static readonly PersonalizableAttribute Personalizable;
43                 public static readonly PersonalizableAttribute SharedPersonalizable;
44                 public static readonly PersonalizableAttribute UserPersonalizable;
45
46                 bool isPersonalizable;
47                 bool isSensitive;
48                 PersonalizationScope scope;
49                 
50                 static PersonalizableAttribute ()
51                 {
52                         Default = new PersonalizableAttribute (false);
53                         NotPersonalizable = Default;
54                         Personalizable = new PersonalizableAttribute (PersonalizationScope.User, false);
55                         SharedPersonalizable = new PersonalizableAttribute (PersonalizationScope.Shared, false);
56                         UserPersonalizable = new PersonalizableAttribute (PersonalizationScope.User, false);
57                 }
58                 
59                 public PersonalizableAttribute () : this (true)
60                 {
61                 }
62
63                 public PersonalizableAttribute (bool isPersonalizable)
64                 {
65                         this.isPersonalizable = isPersonalizable;
66                         this.scope = PersonalizationScope.User;
67                         this.isSensitive = false;
68                 }
69
70                 public PersonalizableAttribute (PersonalizationScope scope) : this (scope, false)
71                 {
72                 }
73
74                 public PersonalizableAttribute (PersonalizationScope scope, bool isSensitive)
75                 {
76                         this.isPersonalizable = true;
77                         this.scope = scope;
78                         this.isSensitive = isSensitive;
79                 }
80
81                 public bool IsPersonalizable {
82                         get { return isPersonalizable; }
83                 }
84
85                 public bool IsSensitive {
86                         get { return isSensitive; }
87                 }
88
89                 public PersonalizationScope Scope {
90                         get { return scope; }
91                 }
92
93                 public override bool Equals (object obj)
94                 {
95                         PersonalizableAttribute attr = obj as PersonalizableAttribute;
96                         if (attr == null)
97                                 return false;
98
99                         return (this.isPersonalizable == attr.IsPersonalizable &&
100                                 this.isSensitive == attr.IsSensitive &&
101                                 this.scope == attr.Scope);
102                 }
103                 
104                 public override int GetHashCode ()
105                 {
106                         return (this.isPersonalizable.GetHashCode () ^
107                                 this.isSensitive.GetHashCode () ^
108                                 this.scope.GetHashCode ());
109                 }
110
111                 public static ICollection GetPersonalizableProperties (Type type)
112                 {
113                         if (type == null)
114                                 throw new ArgumentNullException ("type");
115                         
116                         PropertyInfo[] properties = type.GetProperties ();
117                         if (properties == null || properties.Length == 0)
118                                 return new PropertyInfo [0];
119                         List <PropertyInfo> ret = null;
120                         
121                         foreach (PropertyInfo pi in properties)
122                                 if (PropertyQualifies (pi)) {
123                                         if (ret == null)
124                                                 ret = new List <PropertyInfo> ();
125                                         ret.Add (pi);
126                                 }
127                         return ret;
128                 }
129
130                 static bool PropertyQualifies (PropertyInfo pi)
131                 {
132                         object[] attributes = pi.GetCustomAttributes (false);
133                         if (attributes == null || attributes.Length == 0)
134                                 return false;
135
136                         PersonalizableAttribute attr;
137                         MethodInfo mi;
138                         foreach (object a in attributes) {
139                                 attr = a as PersonalizableAttribute;
140                                 if (attr == null || !attr.IsPersonalizable)
141                                         continue;
142                                 mi = pi.GetSetMethod (false);
143                                 if (mi == null)
144                                         throw new HttpException ("A public property on the type is marked as personalizable but is read-only.");
145                                 return true;
146                         }
147
148                         return false;
149                 }
150
151                 public override bool IsDefaultAttribute ()
152                 {
153                         return PersonalizableAttribute.Equals (this, Default);
154                 }
155
156                 public override bool Match (object obj)
157                 {
158                         PersonalizableAttribute attr = obj as PersonalizableAttribute;
159                         if (obj == null)
160                                 return false;
161                         return (this.isPersonalizable == attr.IsPersonalizable);
162                 }
163         }
164 }