1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Configuration
18 using System
.Configuration
;
20 using Castle
.Core
.Configuration
;
21 using Castle
.Core
.Configuration
.Xml
;
22 using Castle
.MonoRail
.Framework
.Helpers
.ValidationStrategy
;
23 using Castle
.MonoRail
.Framework
.JSGeneration
.Prototype
;
27 /// Represents the MonoRail external configuration
29 public class MonoRailConfiguration
: IMonoRailConfiguration
, ISerializedConfig
31 private static readonly String SectionName
= "monorail";
32 private static readonly String AlternativeSectionName
= "monoRail";
34 private bool matchHostNameAndPath
, excludeAppPath
;
35 private Type customFilterFactory
;
36 private IConfiguration configurationSection
;
38 private SmtpConfig smtpConfig
;
39 private ViewEngineConfig viewEngineConfig
;
40 private ControllersConfig controllersConfig
;
41 private ViewComponentsConfig viewComponentsConfig
;
42 private ScaffoldConfig scaffoldConfig
;
43 private UrlConfig urlConfig
;
44 private JSGeneratorConfiguration jsGeneratorConfig
;
45 private RoutingRuleCollection routingRules
;
46 private ExtensionEntryCollection extensions
;
47 private DefaultUrlCollection defaultUrls
;
48 private IConfiguration servicesConfig
= new MutableConfiguration("services");
51 /// Initializes a new instance of the <see cref="MonoRailConfiguration"/> class.
53 public MonoRailConfiguration()
55 smtpConfig
= new SmtpConfig();
56 viewEngineConfig
= new ViewEngineConfig();
57 controllersConfig
= new ControllersConfig();
58 viewComponentsConfig
= new ViewComponentsConfig();
59 scaffoldConfig
= new ScaffoldConfig();
60 urlConfig
= new UrlConfig();
61 routingRules
= new RoutingRuleCollection();
62 extensions
= new ExtensionEntryCollection();
63 defaultUrls
= new DefaultUrlCollection();
64 jsGeneratorConfig
= new JSGeneratorConfiguration();
66 jsGeneratorConfig
.AddLibrary("prototype-1.5.1", typeof(PrototypeGenerator
))
67 .AddExtension(typeof(CommonJSExtension
))
68 .AddExtension(typeof(ScriptaculousExtension
))
69 .AddExtension(typeof(BehaviourExtension
))
70 .BrowserValidatorIs(typeof(PrototypeWebValidator
))
73 // old routing support related
74 matchHostNameAndPath
= false;
75 excludeAppPath
= false;
81 /// <param name="section"></param>
82 public MonoRailConfiguration(XmlNode section
) : this()
84 configurationSection
= XmlConfigurationDeserializer
.GetDeserializedNode(section
);
90 /// <returns></returns>
91 public static MonoRailConfiguration
GetConfig()
93 MonoRailConfiguration config
=
94 ConfigurationManager
.GetSection(SectionName
) as MonoRailConfiguration
;
98 config
= ConfigurationManager
.GetSection(AlternativeSectionName
) as MonoRailConfiguration
;
104 #region ISerializedConfig implementation
107 /// Deserializes the specified node.
109 /// <param name="node">The node.</param>
110 public void Deserialize(XmlNode node
)
112 viewEngineConfig
.Deserialize(node
);
113 smtpConfig
.Deserialize(node
);
114 controllersConfig
.Deserialize(node
);
115 viewComponentsConfig
.Deserialize(node
);
116 scaffoldConfig
.Deserialize(node
);
117 urlConfig
.Deserialize(node
);
119 extensions
.Deserialize(node
);
120 routingRules
.Deserialize(node
);
121 defaultUrls
.Deserialize(node
);
123 ProcessFilterFactoryNode(node
.SelectSingleNode("customFilterFactory"));
124 ProcessMatchHostNameAndPath(node
.SelectSingleNode("routing"));
125 ProcessExcludeAppPath(node
.SelectSingleNode("routing"));
127 XmlNode services
= node
.SelectSingleNode("services");
129 if (services
!= null)
131 servicesConfig
= XmlConfigurationDeserializer
.GetDeserializedNode(services
);
138 /// Gets the SMTP config.
140 /// <value>The SMTP config.</value>
141 public SmtpConfig SmtpConfig
143 get { return smtpConfig; }
147 /// Gets the view engine config.
149 /// <value>The view engine config.</value>
150 public ViewEngineConfig ViewEngineConfig
152 get { return viewEngineConfig; }
156 /// Gets the controllers config.
158 /// <value>The controllers config.</value>
159 public ControllersConfig ControllersConfig
161 get { return controllersConfig; }
162 set { controllersConfig = value; }
166 /// Gets the view components config.
168 /// <value>The view components config.</value>
169 public ViewComponentsConfig ViewComponentsConfig
171 get { return viewComponentsConfig; }
172 set { viewComponentsConfig = value; }
176 /// Gets the routing rules.
178 /// <value>The routing rules.</value>
179 public RoutingRuleCollection RoutingRules
181 get { return routingRules; }
182 set { routingRules = value; }
186 /// Gets the extension entries.
188 /// <value>The extension entries.</value>
189 public ExtensionEntryCollection ExtensionEntries
191 get { return extensions; }
195 /// Gets the custom filter factory.
197 /// <value>The custom filter factory.</value>
198 public Type CustomFilterFactory
200 get { return customFilterFactory; }
201 set { customFilterFactory = value; }
205 /// Gets the scaffold config.
207 /// <value>The scaffold config.</value>
208 public ScaffoldConfig ScaffoldConfig
210 get { return scaffoldConfig; }
211 set { scaffoldConfig = value; }
215 /// Gets the url config.
217 /// <value>The url config.</value>
218 public UrlConfig UrlConfig
220 get { return urlConfig; }
221 set { urlConfig = value; }
225 /// Gets or sets the JS generator configuration.
227 /// <value>The JS generator configuration.</value>
228 public JSGeneratorConfiguration JSGeneratorConfiguration
230 get { return jsGeneratorConfig; }
231 set { jsGeneratorConfig = value; }
235 /// Gets a value indicating whether match host name and path should be used on
236 /// MonoRail routing.
239 /// <c>true</c> if it should match host name and path; otherwise, <c>false</c>.
241 public bool MatchHostNameAndPath
243 get { return matchHostNameAndPath; }
244 set { matchHostNameAndPath = value; }
248 /// Gets a value indicating whether routing should exclude app path.
250 /// <value><c>true</c> if exclude app path; otherwise, <c>false</c>.</value>
251 public bool ExcludeAppPath
253 get { return excludeAppPath; }
254 set { excludeAppPath = value; }
258 /// Gets the configuration section.
260 /// <value>The configuration section.</value>
261 public IConfiguration ConfigurationSection
263 get { return configurationSection; }
264 set { configurationSection = value; }
268 /// Gets the default urls.
270 /// <value>The default urls.</value>
271 public DefaultUrlCollection DefaultUrls
273 get { return defaultUrls; }
277 /// Gets or sets the services config.
279 /// <value>The services config.</value>
280 public IConfiguration ServicesConfig
282 get { return servicesConfig; }
283 set { servicesConfig = value; }
286 private void ProcessFilterFactoryNode(XmlNode node
)
288 if (node
== null) return;
290 XmlAttribute type
= node
.Attributes
["type"];
294 String message
= "The custom filter factory node must specify a 'type' attribute";
295 throw new ConfigurationErrorsException(message
);
298 customFilterFactory
= TypeLoadUtil
.GetType(type
.Value
);
301 private void ProcessMatchHostNameAndPath(XmlNode node
)
303 if (node
== null) return;
305 XmlAttribute matchHostNameAndPathAtt
= node
.Attributes
["matchHostNameAndPath"];
307 if (matchHostNameAndPathAtt
!= null && matchHostNameAndPathAtt
.Value
!= String
.Empty
)
309 matchHostNameAndPath
= String
.Compare(matchHostNameAndPathAtt
.Value
, "true", true) == 0;
313 private void ProcessExcludeAppPath(XmlNode node
)
315 if (node
== null) return;
317 // maybe a check to make sure both matchHostNameAndPathAtt & includeAppPath
318 // are not both set as that wouldn't make sense?
319 XmlAttribute excludeAppPathAtt
= node
.Attributes
["excludeAppPath"];
321 if (excludeAppPathAtt
!= null && excludeAppPathAtt
.Value
!= String
.Empty
)
323 excludeAppPath
= String
.Compare(excludeAppPathAtt
.Value
, "true", true) == 0;