Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / MVC / Castle.MVC / Configuration / ConfigurationSectionHandler.cs
blob71081958de8f6c687a966bee7517624a012e5c34
1 #region Apache Notice
2 /*****************************************************************************
3 *
4 * Castle.MVC
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ********************************************************************************/
19 #endregion
21 #region Autors
23 /************************************************
24 * Gilles Bayon
25 *************************************************/
26 #endregion
28 using System;
29 using System.Configuration;
30 using System.IO;
31 using System.Xml;
32 using System.Xml.Schema;
34 namespace Castle.MVC.Configuration
36 /// <summary>
37 /// The configuration section handler for the Castle.MVC section of the configuration file.
38 /// </summary>
39 /// <remarks>
40 /// Usage
41 /// MVCConfigSettings ctx = ConfigurationSettings.GetConfig("mvc") as MVCConfigSettings;
42 /// </remarks>
43 public class ConfigurationSectionHandler : IConfigurationSectionHandler
46 #region Fields
48 private bool _isXmlValid = true;
49 private string _schemaErrors = string.Empty;
51 #endregion
53 #region Constructor
55 /// <summary>
56 /// Default constructor.
57 /// </summary>
58 public ConfigurationSectionHandler(){}
59 #endregion
61 /// <summary>
62 /// Factory method that creates a configuration handler for a specific section of
63 /// XML in the app.config.
64 /// </summary>
65 /// <param name="parent">
66 /// The configuration settings in a corresponding parent
67 /// configuration section.
68 /// </param>
69 /// <param name="configContext">
70 /// The configuration context when called from the ASP.NET
71 /// configuration system. Otherwise, this parameter is reserved and
72 /// is a null reference.
73 /// </param>
74 /// <param name="section">
75 /// The <see cref="System.Xml.XmlNode"/> for the section.
76 /// </param>
77 /// <returns>MVCConfigSettings for the section.</returns>
78 public object Create(object parent, object configContext, XmlNode section)
80 return Create(parent, configContext, section, System.Globalization.CultureInfo.CurrentCulture);
83 /// <summary>
84 /// Factory method that creates a configuration handler for a specific section of XML in the app.config.
85 /// </summary>
86 /// <param name="parent">
87 /// The configuration settings in a corresponding parent
88 /// configuration section.
89 /// </param>
90 /// <param name="configContext">
91 /// The configuration context when called from the ASP.NET
92 /// configuration system. Otherwise, this parameter is reserved and
93 /// is a null reference.
94 /// </param>
95 /// <param name="section">
96 /// The <see cref="System.Xml.XmlNode"/> for the section.
97 /// </param>
98 /// <param name="formatProvider">The format provider.
99 /// </param>
100 /// <returns>MVCConfigSettings for the section.</returns>
101 public object Create(object parent, object configContext, XmlNode section, IFormatProvider formatProvider)
103 ValidateSchema( section );
104 MVCConfigSettings config = new MVCConfigSettings(section, formatProvider);
105 return config;
108 private void ValidateSchema( XmlNode section )
110 XmlValidatingReader validatingReader = null;
111 Stream xsdFile = null;
112 StreamReader streamReader = null;
115 //Validate the document using a schema
116 validatingReader = new XmlValidatingReader( new XmlTextReader( new StringReader( section.OuterXml ) ) );
117 validatingReader.ValidationType = ValidationType.Schema;
119 xsdFile = Resource.ResourceManager.GetStream( "castle.mvc.xsd" );
120 streamReader = new StreamReader( xsdFile );
122 validatingReader.Schemas.Add( XmlSchema.Read( new XmlTextReader( streamReader ), null ) );
123 validatingReader.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
125 // Validate the document
126 while (validatingReader.Read()){}
128 if( !_isXmlValid )
130 throw new ConfigurationException(( Resource.ResourceManager.FormatMessage( Resource.MessageKeys.DocumentNotValidated, _schemaErrors )) );
133 finally
135 if( validatingReader != null ) validatingReader.Close();
136 if( xsdFile != null ) xsdFile.Close();
137 if( streamReader != null ) streamReader.Close();
141 private void ValidationCallBack( object sender, ValidationEventArgs args )
143 _isXmlValid = false;
144 _schemaErrors += args.Message + Environment.NewLine;