A slightly more elegant way of dealing with 'item==null' issue
[mono.git] / mcs / class / System.Web / System.Web.SessionState / SessionStateSectionHandler.cs
1 //
2 // System.Web.SessionState.SessionStateSectionHandler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
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
31 using System;
32 using System.Configuration;
33 using System.Web.Configuration;
34 using System.Xml;
35
36 namespace System.Web.SessionState
37 {
38         class SessionStateSectionHandler : IConfigurationSectionHandler
39         {
40                 public object Create (object parent, object context, XmlNode section)
41                 {
42                         //TODO: context?
43                         SessionConfig config = new SessionConfig (parent);
44                         if (section.HasChildNodes)
45                                 ThrowException ("No children nodes allowed", section);
46
47                         string attvalue = AttValue ("mode", section, false);
48                         if (!config.SetMode (attvalue))
49                                 ThrowException ("Invalid mode value", section);
50                         
51                         if (section.Attributes == null)
52                                 return config;
53
54                         attvalue = AttValue ("cookieless", section);
55                         if (attvalue != null)
56                                 if (!config.SetCookieLess (attvalue))
57                                         ThrowException ("Invalid cookieless value", section);
58
59                         attvalue = AttValue ("timeout", section);
60                         if (attvalue != null)
61                                 if (!config.SetTimeout (attvalue))
62                                         ThrowException ("Invalid timeout value", section);
63
64                         attvalue = AttValue ("stateConnectionString", section);
65                         if (attvalue != null)
66                                 config.SetStateConnectionString (attvalue);
67
68                         attvalue = AttValue ("sqlConnectionString", section);
69                         if (attvalue != null)
70                                 config.SetSqlConnectionString (attvalue);
71
72                         attvalue = AttValue ("stateNetworkTimeout", section);
73                         if (attvalue != null)
74                                 config.SetStateNetworkTimeout (attvalue);
75
76                         if (section.Attributes != null && section.Attributes.Count > 0)
77                                 HandlersUtil.ThrowException ("Unknown attribute.", section);
78
79                         return config;
80                 }
81
82                 // A few methods to save some typing
83                 static string AttValue (string name, XmlNode node, bool optional)
84                 {
85                         return HandlersUtil.ExtractAttributeValue (name, node, optional);
86                 }
87
88                 static string AttValue (string name, XmlNode node)
89                 {
90                         return HandlersUtil.ExtractAttributeValue (name, node, true);
91                 }
92
93                 static void ThrowException (string message, XmlNode node)
94                 {
95                         HandlersUtil.ThrowException (message, node);
96                 }
97                 //
98         }
99 }
100