1 //------------------------------------------------------------------------------
2 // <copyright file="DeviceFilterDictionary.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.Web.Mobile
8 {
9     using System.Web;
10     using System.Collections;
11     using System.Reflection;
12     using System.Diagnostics;
13     using System.ComponentModel;
14 
15     [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.")]
16     internal class DeviceFilterDictionary
17     {
18         internal class ComparisonEvaluator
19         {
20             internal readonly String capabilityName;
21             internal readonly String capabilityArgument;
22 
ComparisonEvaluator(String name, String argument)23             internal ComparisonEvaluator(String name, String argument)
24             {
25                 Debug.Assert(name != null);
26 
27                 capabilityName = name;
28                 capabilityArgument = argument;
29             }
30         }
31 
32         private Hashtable _comparisonEvaluators = null;
33         private Hashtable _delegateEvaluators = null;
34 
35 
DeviceFilterDictionary()36         internal DeviceFilterDictionary()
37         {
38             _comparisonEvaluators = new Hashtable();
39             _delegateEvaluators = new Hashtable();
40         }
41 
42 
DeviceFilterDictionary(DeviceFilterDictionary original)43         internal DeviceFilterDictionary(DeviceFilterDictionary original)
44         {
45             _comparisonEvaluators = (Hashtable)original._comparisonEvaluators.Clone();
46             _delegateEvaluators = (Hashtable)original._delegateEvaluators.Clone();
47         }
48 
49 
AddCapabilityDelegate(String delegateName, MobileCapabilities.EvaluateCapabilitiesDelegate evaluator)50         internal void AddCapabilityDelegate(String delegateName,
51             MobileCapabilities.EvaluateCapabilitiesDelegate evaluator)
52         {
53             _delegateEvaluators[delegateName] = evaluator;
54         }
55 
56 
CheckForComparisonDelegateLoops(String delegateName)57         private void CheckForComparisonDelegateLoops(String delegateName)
58         {
59             String nextDelegateName = delegateName;
60             Hashtable alreadyReferencedDelegates = new Hashtable();
61 
62             while(true)
63             {
64                 ComparisonEvaluator nextComparisonEvaluator =
65                     (ComparisonEvaluator)_comparisonEvaluators[nextDelegateName];
66                 if(nextComparisonEvaluator == null)
67                 {
68                     break;
69                 }
70 
71                 if(alreadyReferencedDelegates.Contains(nextDelegateName))
72                 {
73                     String msg = SR.GetString(SR.DevFiltDict_FoundLoop,
74                                               nextComparisonEvaluator.capabilityName,
75                                               delegateName);
76                     throw new Exception(msg);
77                 }
78 
79                 alreadyReferencedDelegates[nextDelegateName] = null;
80                 nextDelegateName = nextComparisonEvaluator.capabilityName;
81             }
82         }
83 
84 
AddComparisonDelegate(String delegateName, String comparisonName, String argument)85         internal void AddComparisonDelegate(String delegateName, String comparisonName,
86             String argument)
87         {
88             _comparisonEvaluators[delegateName] = new ComparisonEvaluator(comparisonName,
89                 argument);
90 
91             CheckForComparisonDelegateLoops(delegateName);
92         }
93 
94 
FindComparisonEvaluator(String evaluatorName, out String capabilityName, out String capabilityArgument)95         internal bool FindComparisonEvaluator(String evaluatorName, out String capabilityName,
96             out String capabilityArgument)
97         {
98             capabilityName = null;
99             capabilityArgument = null;
100 
101             ComparisonEvaluator evaluator = (ComparisonEvaluator)_comparisonEvaluators[evaluatorName];
102             if(evaluator == null)
103             {
104                 return false;
105             }
106 
107             capabilityName = evaluator.capabilityName;
108             capabilityArgument = evaluator.capabilityArgument;
109 
110             return true;
111         }
112 
113 
FindDelegateEvaluator(String evaluatorName, out MobileCapabilities.EvaluateCapabilitiesDelegate evaluatorDelegate)114         internal bool FindDelegateEvaluator(String evaluatorName,
115             out MobileCapabilities.EvaluateCapabilitiesDelegate evaluatorDelegate)
116         {
117             evaluatorDelegate = null;
118 
119             MobileCapabilities.EvaluateCapabilitiesDelegate evaluator;
120             evaluator = (MobileCapabilities.EvaluateCapabilitiesDelegate)
121                             _delegateEvaluators[evaluatorName];
122             if(evaluator == null)
123             {
124                 return false;
125             }
126 
127             evaluatorDelegate = evaluator;
128 
129             return true;
130         }
131 
132 
IsComparisonEvaluator(String evaluatorName)133         internal bool IsComparisonEvaluator(String evaluatorName)
134         {
135             return _comparisonEvaluators.Contains(evaluatorName);
136         }
137 
IsDelegateEvaluator(String evaluatorName)138         internal bool IsDelegateEvaluator(String evaluatorName)
139         {
140             return _delegateEvaluators.Contains(evaluatorName);
141         }
142     }
143 }
144 
145