1 //------------------------------------------------------------------------------
2 // <copyright file="DeviceFiltersSection.cs" company="Microsoft Corporation">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 using System.ComponentModel;
8 using System.Configuration;
9 using System.Diagnostics;
10 using System.Web.UI.MobileControls;
11 
12 namespace System.Web.Mobile {
13     [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
14     public sealed class DeviceFiltersSection : ConfigurationSection {
15         internal static readonly TypeConverter              StdTypeNameConverter        = new MobileTypeNameConverter();
16         internal static readonly ConfigurationValidatorBase NonEmptyStringValidator     = new StringValidator( 1 );
17 
18         private static ConfigurationPropertyCollection _properties;
19 
20         #region Property Declarations
21         private static readonly ConfigurationProperty   _propFilters =
22             new ConfigurationProperty( null,
23                                        typeof( DeviceFilterElementCollection ),
24                                        null,
25                                        ConfigurationPropertyOptions.IsDefaultCollection );
26         #endregion
27 
28         private object _deviceFilterslock = new object();
29         private DeviceFilterDictionary _deviceFilters;
30 
DeviceFiltersSection()31         static DeviceFiltersSection() {
32             // Property initialization
33             _properties = new ConfigurationPropertyCollection();
34             _properties.Add( _propFilters );
35         }
36 
DeviceFiltersSection()37         public DeviceFiltersSection() {
38         }
39 
40         protected override ConfigurationPropertyCollection Properties {
41             get {
42                 return _properties;
43             }
44         }
45 
46         [ConfigurationProperty("", IsDefaultCollection = true)]
47         public DeviceFilterElementCollection Filters {
48             get {
49                 return (DeviceFilterElementCollection)base[ _propFilters ];
50             }
51         }
52 
GetDeviceFilters()53         internal DeviceFilterDictionary GetDeviceFilters() {
54             if (_deviceFilters == null) {
55                 lock (_deviceFilterslock) {
56                     if (_deviceFilters == null) {
57                         _deviceFilters = CreateDeviceFilters();
58                     }
59                 }
60             }
61             return _deviceFilters;
62         }
63 
64         // Essentially this method does what MobileDeviceCapabilitiesSectionHandler.Create()
65         // does, but use this DeviceFiltersSection for retrieving config data instead
CreateDeviceFilters()66         private DeviceFilterDictionary CreateDeviceFilters() {
67             DeviceFilterDictionary filterDictionary = new DeviceFilterDictionary();
68 
69             foreach (DeviceFilterElement deviceFilter in Filters) {
70                 if (deviceFilter.FilterClass != null) {
71                     filterDictionary.AddCapabilityDelegate(deviceFilter.Name, deviceFilter.GetDelegate());
72                 }
73                 else {
74                     try {
75                         filterDictionary.AddComparisonDelegate(
76                             deviceFilter.Name, deviceFilter.Compare, deviceFilter.Argument);
77                     }
78                     catch (Exception e) {
79                         throw new ConfigurationErrorsException(
80                             SR.GetString(SR.DevCapSect_UnableAddDelegate, deviceFilter.Name, e.Message));
81                     }
82                 }
83             }
84 
85             return filterDictionary;
86         }
87     }
88 
89 
90     [ConfigurationCollection(typeof(DeviceFilterElement), AddItemName = "filter",
91      CollectionType = ConfigurationElementCollectionType.BasicMap)]
92     [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
93     public sealed class DeviceFilterElementCollection : ConfigurationElementCollection {
94         private static readonly ConfigurationPropertyCollection _properties;
95 
DeviceFilterElementCollection()96         static DeviceFilterElementCollection() {
97             _properties = new ConfigurationPropertyCollection();
98         }
99 
DeviceFilterElementCollection()100         public DeviceFilterElementCollection() {
101         }
102 
103         protected override ConfigurationPropertyCollection Properties {
104             get {
105                 return _properties;
106             }
107         }
108 
109         public object[] AllKeys {
110             get {
111                 return BaseGetAllKeys();
112             }
113         }
114 
Add( DeviceFilterElement deviceFilter )115         public void Add( DeviceFilterElement deviceFilter ) {
116             BaseAdd( deviceFilter );
117         }
118 
Remove( string name )119         public void Remove( string name ) {
120             BaseRemove( name );
121         }
122 
Remove( DeviceFilterElement deviceFilter )123         public void Remove( DeviceFilterElement deviceFilter )  {
124             BaseRemove( GetElementKey( deviceFilter ) );
125         }
126 
RemoveAt( int index )127         public void RemoveAt( int index ) {
128             BaseRemoveAt( index );
129         }
130 
131         public new DeviceFilterElement this[ string name ]  {
132             get {
133                 return (DeviceFilterElement)BaseGet( name );
134             }
135         }
136 
137         public DeviceFilterElement this[ int index ] {
138             get {
139                 return (DeviceFilterElement)BaseGet( index );
140             }
141             set {
142                 if ( BaseGet( index ) != null) {
143                     BaseRemoveAt( index );
144                 }
145 
146                 BaseAdd( index, value );
147             }
148         }
149 
Clear()150         public void Clear() {
151             BaseClear();
152         }
153 
CreateNewElement()154         protected override ConfigurationElement CreateNewElement() {
155             return new DeviceFilterElement();
156         }
157 
GetElementKey( ConfigurationElement element )158         protected override Object GetElementKey( ConfigurationElement element ) {
159             return ( (DeviceFilterElement)element ).Name;
160         }
161 
162         protected override string ElementName {
163             get
164             {
165                 return "filter";
166             }
167         }
168 
169         public override ConfigurationElementCollectionType CollectionType {
170             get {
171                 return ConfigurationElementCollectionType.BasicMap;
172             }
173         }
174     }
175 
176     [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
177     public sealed class DeviceFilterElement : ConfigurationElement {
178         private static readonly ConfigurationElementProperty s_elemProperty = new ConfigurationElementProperty( new CallbackValidator( typeof( DeviceFilterElement ), ValidateElement ) );
179         private static ConfigurationPropertyCollection _properties;
180 
181         #region Property Declarations
182         private static readonly ConfigurationProperty   _propName =
183             new ConfigurationProperty(  "name",
184                                         typeof( string ),
185                                         null,
186                                         null,
187                                         DeviceFiltersSection.NonEmptyStringValidator,
188                                         ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey );
189 
190         private static readonly ConfigurationProperty   _propFilterClass =
191             new ConfigurationProperty(  "type",
192                                         typeof( Type ),
193                                         null,
194                                         DeviceFiltersSection.StdTypeNameConverter,
195                                         null,
196                                         ConfigurationPropertyOptions.IsTypeStringTransformationRequired);
197 
198         private static readonly ConfigurationProperty   _propMethod =
199             new ConfigurationProperty(  "method",
200                                         typeof( string ),
201                                         null,
202                                         null,
203                                         DeviceFiltersSection.NonEmptyStringValidator,
204                                         ConfigurationPropertyOptions.None );
205 
206         private static readonly ConfigurationProperty   _propCompare =
207             new ConfigurationProperty(  "compare",
208                                         typeof( string ),
209                                         null,
210                                         null,
211                                         DeviceFiltersSection.NonEmptyStringValidator,
212                                         ConfigurationPropertyOptions.None );
213 
214         private static readonly ConfigurationProperty   _propArgument =
215             new ConfigurationProperty(  "argument",
216                                         typeof( string ),
217                                         null,
218                                         null,
219                                         DeviceFiltersSection.NonEmptyStringValidator,
220                                         ConfigurationPropertyOptions.None );
221         #endregion
222 
DeviceFilterElement()223         static DeviceFilterElement() {
224             // Property initialization
225             _properties = new ConfigurationPropertyCollection();
226             _properties.Add( _propName );
227             _properties.Add( _propFilterClass );
228             _properties.Add( _propMethod );
229             _properties.Add( _propCompare );
230             _properties.Add( _propArgument );
231         }
232 
DeviceFilterElement()233         internal DeviceFilterElement() {
234         }
235 
DeviceFilterElement( string name, Type filterClass, string method )236         public DeviceFilterElement( string name, Type filterClass, string method ) {
237             base[_propName] = name;
238             base[_propFilterClass] = filterClass;
239             base[_propMethod] = method;
240         }
241 
DeviceFilterElement( string name, string compareName, string argument )242         public DeviceFilterElement( string name, string compareName, string argument ) {
243             base[_propName] = name;
244             base[_propCompare] = compareName;
245             base[_propArgument] = argument;
246         }
247 
248         protected override ConfigurationPropertyCollection Properties {
249             get {
250                 return _properties;
251             }
252         }
253 
254         [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
255         [StringValidator(MinLength = 1)]
256         public string Name {
257             get {
258                 return (string)base[ _propName ];
259             }
260         }
261 
262         [ConfigurationProperty("type")]
263         [TypeConverter(typeof(MobileTypeNameConverter))]
264         public Type FilterClass {
265             get {
266                 return (Type)base[ _propFilterClass ];
267             }
268             set {
269                 base[ _propFilterClass ] = value;
270             }
271         }
272 
273         [ConfigurationProperty("method")]
274         [StringValidator(MinLength = 1)]
275         public string Method {
276             get {
277                 return (string)base[ _propMethod ];
278             }
279             set {
280                 base[ _propMethod ] = value;
281             }
282         }
283 
284         [ConfigurationProperty("compare")]
285         [StringValidator(MinLength = 1)]
286         public string Compare {
287             get {
288                 return (string)base[ _propCompare ];
289             }
290             set {
291                 base[ _propCompare ] = value;
292             }
293         }
294 
295         [ConfigurationProperty("argument")]
296         [StringValidator(MinLength = 1)]
297         public string Argument {
298             get {
299                 return (string)base[ _propArgument ];
300             }
301             set {
302                 base[ _propArgument ] = value;
303             }
304         }
305 
306         protected override ConfigurationElementProperty ElementProperty
307         {
308             get
309             {
310                 return s_elemProperty;
311             }
312         }
313 
GetDelegate()314         internal MobileCapabilities.EvaluateCapabilitiesDelegate GetDelegate() {
315             try {
316                 return (MobileCapabilities.EvaluateCapabilitiesDelegate)MobileCapabilities.EvaluateCapabilitiesDelegate.CreateDelegate(
317                         typeof(MobileCapabilities.EvaluateCapabilitiesDelegate),
318                         FilterClass,
319                         Method);
320             }
321             catch (Exception e) {
322                 throw new ConfigurationErrorsException(
323                             SR.GetString(SR.DevCapSect_NoCapabilityEval, Method, e.Message));
324             }
325         }
326 
ValidateElement( object value )327         static private void ValidateElement( object value ) {
328             Debug.Assert((value != null) && (value is DeviceFilterElement));
329 
330             DeviceFilterElement elem = (DeviceFilterElement)value;
331 
332             // If the filter class is specified, we need the method attribute but
333             // not the compare and argument attributes.
334             if (elem.FilterClass != null) {
335                 if(string.IsNullOrEmpty(elem.Method)) {
336                     throw new ConfigurationErrorsException(
337                                 SR.GetString(SR.ConfigSect_MissingAttr, "method"));
338                 }
339 
340                 if (!string.IsNullOrEmpty(elem.Compare)) {
341                     throw new ConfigurationErrorsException(SR.GetString(SR.DevCapSect_ExtraCompareDelegator));
342                 }
343                 else if (!string.IsNullOrEmpty(elem.Argument)) {
344                     throw new ConfigurationErrorsException(SR.GetString(SR.DevCapSect_ExtraArgumentDelegator));
345                 }
346 
347                 // Resolve the method
348                 elem.GetDelegate();
349             }
350             // Otherwise, we need the compare and argument attributes but not
351             // the method attribute.
352             else {
353                 if (string.IsNullOrEmpty(elem.Compare)) {
354                     throw new ConfigurationErrorsException(
355                                 SR.GetString(SR.DevCapSect_MustSpecify));
356                 }
357 
358                 if (!string.IsNullOrEmpty(elem.Method)) {
359                     throw new ConfigurationErrorsException(
360                                 SR.GetString(SR.DevCapSect_ComparisonAlreadySpecified));
361                 }
362             }
363         }
364     }
365 }
366 
367 
368 
369