Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework / Configuration / ViewEngineConfig.cs
blobb1e550aecab190e2f888294e56c280b1c41599e9
1 // Copyright 2004-2007 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;
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 AssemblySourceInfo[] sources = new AssemblySourceInfo[0];
31 private ViewEngineInfo[] viewEngines = new ViewEngineInfo[0];
33 #region ISerializedConfig implementation
35 /// <summary>
36 /// Deserializes the specified section.
37 /// </summary>
38 /// <param name="section">The section.</param>
39 public void Deserialize(XmlNode section)
41 XmlElement engines = (XmlElement) section.SelectSingleNode("viewEngines");
43 if (engines != null)
45 ConfigureMultipleViewEngines(engines);
47 else
49 // Backward compatibility
51 ConfigureSingleViewEngine(section);
54 LoadAdditionalSources(section);
55 ResolveViewPath();
58 #endregion
60 /// <summary>
61 /// Gets or sets the view path root.
62 /// </summary>
63 /// <value>The view path root.</value>
64 public String ViewPathRoot
66 get { return viewPathRoot; }
67 set { viewPathRoot = value; }
70 /// <summary>
71 /// Gets the view engines.
72 /// </summary>
73 /// <value>The view engines.</value>
74 public ViewEngineInfo[] ViewEngines
76 get { return viewEngines; }
79 /// <summary>
80 /// Gets or sets the additional assembly sources.
81 /// </summary>
82 /// <value>The sources.</value>
83 public AssemblySourceInfo[] Sources
85 get { return sources; }
86 set { sources = value; }
89 private void ConfigureMultipleViewEngines(XmlElement engines)
91 viewPathRoot = engines.GetAttribute("viewPathRoot");
93 if (viewPathRoot == null || viewPathRoot == String.Empty)
95 viewPathRoot = "views";
98 ArrayList viewEnginesList = new ArrayList();
100 foreach (XmlElement addNode in engines.SelectNodes("add"))
102 string typeName = addNode.GetAttribute("type");
103 string xhtmlVal = addNode.GetAttribute("xhtml");
105 if (typeName == null || typeName.Length == 0)
107 String message = "The attribute 'type' is required for the element 'add' under 'viewEngines'";
108 throw new ConfigurationErrorsException(message);
111 Type engine = TypeLoadUtil.GetType(typeName, true);
113 if (engine == null)
115 String message = "The type '" + typeName + "' could not be loaded";
116 throw new ConfigurationErrorsException(message);
119 viewEnginesList.Add(new ViewEngineInfo(engine, xhtmlVal == "true"));
122 if (viewEnginesList.Count == 0)
124 ConfigureDefaultViewEngine();
126 else
128 viewEngines = new ViewEngineInfo[viewEnginesList.Count];
129 viewEnginesList.CopyTo(viewEngines);
133 private void ResolveViewPath()
135 if (!Path.IsPathRooted(viewPathRoot))
137 viewPathRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, viewPathRoot);
141 /// <summary>
142 /// Configures the default view engine.
143 /// </summary>
144 private void ConfigureDefaultViewEngine()
146 viewPathRoot = "views";
148 Type engineType = typeof(Castle.MonoRail.Framework.Views.Aspx.WebFormsViewEngine);
150 viewEngines = new ViewEngineInfo[] {new ViewEngineInfo(engineType, false)};
153 private void ConfigureSingleViewEngine(XmlNode section)
155 section = section.SelectSingleNode("viewEngine");
157 if (section == null)
159 ConfigureDefaultViewEngine();
161 return;
164 XmlAttribute viewPath = section.Attributes["viewPathRoot"];
166 if (viewPath == null)
168 viewPathRoot = "views";
170 else
172 viewPathRoot = viewPath.Value;
175 XmlAttribute xhtmlRendering = section.Attributes["xhtmlRendering"];
177 bool enableXhtmlRendering = false;
179 if (xhtmlRendering != null)
183 enableXhtmlRendering = xhtmlRendering.Value.ToLowerInvariant() == "true";
185 catch(FormatException ex)
187 String message = "The xhtmlRendering attribute of the views node must be a boolean value.";
188 throw new ConfigurationErrorsException(message, ex);
192 XmlAttribute customEngineAtt = section.Attributes["customEngine"];
194 Type engineType = typeof(Castle.MonoRail.Framework.Views.Aspx.WebFormsViewEngine);
196 if (customEngineAtt != null)
198 engineType = TypeLoadUtil.GetType(customEngineAtt.Value);
201 viewEngines = new ViewEngineInfo[] {new ViewEngineInfo(engineType, enableXhtmlRendering)};
205 private void LoadAdditionalSources(XmlNode section)
207 ArrayList items = new ArrayList();
209 foreach(XmlElement assemblyNode in section.SelectNodes("/monorail/*/additionalSources/assembly"))
211 String assemblyName = assemblyNode.GetAttribute("name");
212 String ns = assemblyNode.GetAttribute("namespace");
214 items.Add(new AssemblySourceInfo(assemblyName, ns));
217 sources = (AssemblySourceInfo[]) items.ToArray(typeof(AssemblySourceInfo));