2 /*****************************************************************************
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
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 ********************************************************************************/
23 /************************************************
25 *************************************************/
29 using System
.Configuration
;
32 using System
.Xml
.Schema
;
34 namespace Castle
.MVC
.Configuration
37 /// The configuration section handler for the Castle.MVC section of the configuration file.
41 /// MVCConfigSettings ctx = ConfigurationSettings.GetConfig("mvc") as MVCConfigSettings;
43 public class ConfigurationSectionHandler
: IConfigurationSectionHandler
48 private bool _isXmlValid
= true;
49 private string _schemaErrors
= string.Empty
;
56 /// Default constructor.
58 public ConfigurationSectionHandler(){}
62 /// Factory method that creates a configuration handler for a specific section of
63 /// XML in the app.config.
65 /// <param name="parent">
66 /// The configuration settings in a corresponding parent
67 /// configuration section.
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.
74 /// <param name="section">
75 /// The <see cref="System.Xml.XmlNode"/> for the section.
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
);
84 /// Factory method that creates a configuration handler for a specific section of XML in the app.config.
86 /// <param name="parent">
87 /// The configuration settings in a corresponding parent
88 /// configuration section.
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.
95 /// <param name="section">
96 /// The <see cref="System.Xml.XmlNode"/> for the section.
98 /// <param name="formatProvider">The format provider.
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
);
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()){}
130 throw new ConfigurationException(( Resource
.ResourceManager
.FormatMessage( Resource
.MessageKeys
.DocumentNotValidated
, _schemaErrors
)) );
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
)
144 _schemaErrors
+= args
.Message
+ Environment
.NewLine
;