1 // Copyright 2004-2007 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
;
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 AssemblySourceInfo
[] sources
= new AssemblySourceInfo
[0];
31 private ViewEngineInfo
[] viewEngines
= new ViewEngineInfo
[0];
33 #region ISerializedConfig implementation
36 /// Deserializes the specified section.
38 /// <param name="section">The section.</param>
39 public void Deserialize(XmlNode section
)
41 XmlElement engines
= (XmlElement
) section
.SelectSingleNode("viewEngines");
45 ConfigureMultipleViewEngines(engines
);
49 // Backward compatibility
51 ConfigureSingleViewEngine(section
);
54 LoadAdditionalSources(section
);
61 /// Gets or sets the view path root.
63 /// <value>The view path root.</value>
64 public String ViewPathRoot
66 get { return viewPathRoot; }
67 set { viewPathRoot = value; }
71 /// Gets the view engines.
73 /// <value>The view engines.</value>
74 public ViewEngineInfo
[] ViewEngines
76 get { return viewEngines; }
80 /// Gets or sets the additional assembly sources.
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);
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();
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
);
142 /// Configures the default view engine.
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");
159 ConfigureDefaultViewEngine();
164 XmlAttribute viewPath
= section
.Attributes
["viewPathRoot"];
166 if (viewPath
== null)
168 viewPathRoot
= "views";
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
));