Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Configuration / ViewEngineConfig.cs
blob8ac9625b67e34839f52c5880e9f2a267d41a4e52
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.Collections.Generic;
19 using System.Configuration;
20 using System.IO;
21 using System.Xml;
22 using Castle.MonoRail.Framework.Internal;
24 /// <summary>
25 /// Represents the view engines configuration
26 /// </summary>
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>();
34 /// <summary>
35 /// Initializes a new instance of the <see cref="ViewEngineConfig"/> class.
36 /// </summary>
37 public ViewEngineConfig()
39 viewPathRoot = virtualPathRoot = "views";
42 #region ISerializedConfig implementation
44 /// <summary>
45 /// Deserializes the specified section.
46 /// </summary>
47 /// <param name="section">The section.</param>
48 public void Deserialize(XmlNode section)
50 XmlElement engines = (XmlElement) section.SelectSingleNode("viewEngines");
52 if (engines != null)
54 ConfigureMultipleViewEngines(engines);
56 else
58 // Backward compatibility
60 ConfigureSingleViewEngine(section);
63 LoadAdditionalSources(section);
64 ResolveViewPath();
67 #endregion
69 /// <summary>
70 /// Gets or sets the view path root.
71 /// </summary>
72 /// <value>The view path root.</value>
73 public string ViewPathRoot
75 get { return viewPathRoot; }
76 set { viewPathRoot = value; }
79 /// <summary>
80 /// Gets or sets the virtual path root.
81 /// </summary>
82 /// <value>The virtual path root.</value>
83 public string VirtualPathRoot
85 get { return virtualPathRoot; }
86 set { virtualPathRoot = value; }
89 /// <summary>
90 /// Gets the view engines.
91 /// </summary>
92 /// <value>The view engines.</value>
93 public List<ViewEngineInfo> ViewEngines
95 get { return viewEngines; }
98 /// <summary>
99 /// Gets or sets the additional assembly sources.
100 /// </summary>
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);
130 if (engine == null)
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)");
160 /// <summary>
161 /// Configures the default view engine.
162 /// </summary>
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");
174 if (section == null)
176 ConfigureDefaultViewEngine();
178 return;
181 XmlAttribute viewPath = section.Attributes["viewPathRoot"];
183 if (viewPath == null)
185 viewPathRoot = virtualPathRoot = "views";
187 else
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));