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
.Collections
.Generic
;
19 using System
.Configuration
;
22 using Castle
.MonoRail
.Framework
.Internal
;
25 /// Represents the view engines configuration
27 public class ViewEngineConfig
: ISerializedConfig
29 private string viewPathRoot
;
30 private string virtualPathRoot
;
31 private List
<AssemblySourceInfo
> sources
= new List
<AssemblySourceInfo
>();
32 private readonly List
<ViewEngineInfo
> viewEngines
= new List
<ViewEngineInfo
>();
35 /// Initializes a new instance of the <see cref="ViewEngineConfig"/> class.
37 public ViewEngineConfig()
39 viewPathRoot
= virtualPathRoot
= "views";
42 #region ISerializedConfig implementation
45 /// Deserializes the specified section.
47 /// <param name="section">The section.</param>
48 public void Deserialize(XmlNode section
)
50 XmlElement engines
= (XmlElement
) section
.SelectSingleNode("viewEngines");
54 ConfigureMultipleViewEngines(engines
);
58 // Backward compatibility
60 ConfigureSingleViewEngine(section
);
63 LoadAdditionalSources(section
);
70 /// Gets or sets the view path root.
72 /// <value>The view path root.</value>
73 public string ViewPathRoot
75 get { return viewPathRoot; }
76 set { viewPathRoot = value; }
80 /// Gets or sets the virtual path root.
82 /// <value>The virtual path root.</value>
83 public string VirtualPathRoot
85 get { return virtualPathRoot; }
86 set { virtualPathRoot = value; }
90 /// Gets the view engines.
92 /// <value>The view engines.</value>
93 public List
<ViewEngineInfo
> ViewEngines
95 get { return viewEngines; }
99 /// Gets or sets the additional assembly sources.
101 /// <value>The sources.</value>
102 public List
<AssemblySourceInfo
> Sources
104 get { return sources; }
105 set { sources = value; }
108 private void ConfigureMultipleViewEngines(XmlElement engines
)
110 viewPathRoot
= engines
.GetAttribute("viewPathRoot");
112 if (string.IsNullOrEmpty(viewPathRoot
))
114 viewPathRoot
= "views";
117 foreach(XmlElement addNode
in engines
.SelectNodes("add"))
119 string typeName
= addNode
.GetAttribute("type");
120 string xhtmlVal
= addNode
.GetAttribute("xhtml");
122 if (typeName
== null || typeName
.Length
== 0)
124 String message
= "The attribute 'type' is required for the element 'add' under 'viewEngines'";
125 throw new ConfigurationErrorsException(message
);
128 Type engine
= TypeLoadUtil
.GetType(typeName
, true);
132 String message
= "The type '" + typeName
+ "' could not be loaded";
133 throw new ConfigurationErrorsException(message
);
136 viewEngines
.Add(new ViewEngineInfo(engine
, xhtmlVal
== "true"));
139 if (viewEngines
.Count
== 0)
141 ConfigureDefaultViewEngine();
145 private void ResolveViewPath()
147 if (!Path
.IsPathRooted(viewPathRoot
))
149 virtualPathRoot
= viewPathRoot
;
150 viewPathRoot
= Path
.Combine(AppDomain
.CurrentDomain
.BaseDirectory
, viewPathRoot
);
153 if (!Directory
.Exists(viewPathRoot
))
155 throw new MonoRailException("View folder configured could not be found. " +
156 "Check (or add) a viewPathRoot attribute to the viewEngines node on the MonoRail configuration (web.config)");
161 /// Configures the default view engine.
163 public void ConfigureDefaultViewEngine()
165 Type engineType
= typeof(Views
.Aspx
.WebFormsViewEngine
);
167 viewEngines
.Add(new ViewEngineInfo(engineType
, false));
170 private void ConfigureSingleViewEngine(XmlNode section
)
172 section
= section
.SelectSingleNode("viewEngine");
176 ConfigureDefaultViewEngine();
181 XmlAttribute viewPath
= section
.Attributes
["viewPathRoot"];
183 if (viewPath
== null)
185 viewPathRoot
= virtualPathRoot
= "views";
189 viewPathRoot
= virtualPathRoot
= viewPath
.Value
;
192 XmlAttribute xhtmlRendering
= section
.Attributes
["xhtmlRendering"];
194 bool enableXhtmlRendering
= false;
196 if (xhtmlRendering
!= null)
200 enableXhtmlRendering
= xhtmlRendering
.Value
.ToLowerInvariant() == "true";
202 catch(FormatException ex
)
204 String message
= "The xhtmlRendering attribute of the views node must be a boolean value.";
205 throw new ConfigurationErrorsException(message
, ex
);
209 XmlAttribute customEngineAtt
= section
.Attributes
["customEngine"];
211 Type engineType
= typeof(Views
.Aspx
.WebFormsViewEngine
);
213 if (customEngineAtt
!= null)
215 engineType
= TypeLoadUtil
.GetType(customEngineAtt
.Value
);
218 viewEngines
.Add(new ViewEngineInfo(engineType
, enableXhtmlRendering
));
221 private void LoadAdditionalSources(XmlNode section
)
223 foreach(XmlElement assemblyNode
in section
.SelectNodes("/monorail/*/additionalSources/assembly"))
225 String assemblyName
= assemblyNode
.GetAttribute("name");
226 String ns
= assemblyNode
.GetAttribute("namespace");
228 sources
.Add(new AssemblySourceInfo(assemblyName
, ns
));