Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / MVC / Castle.MVC / Configuration / MVCConfigSettings.cs
blob23c7512ce59a690c00e78c5bc2baed32d1ee046a
2 #region Apache Notice
3 /*****************************************************************************
4 *
5 * Castle.MVC
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ********************************************************************************/
20 #endregion
22 #region Autors
24 /************************************************
25 * Gilles Bayon
26 *************************************************/
27 #endregion
29 #region Using
31 using System;
32 using System.Collections.Specialized;
33 using System.Configuration;
34 using System.Xml;
35 #endregion
37 namespace Castle.MVC.Configuration
39 /// <summary>
40 /// MVCConfigSettings.
41 /// </summary>
42 public class MVCConfigSettings
44 #region Const
45 /// <summary>
46 /// Token to retrieve configuration node
47 /// </summary>
48 private const string NODE_WEB_VIEW_XPATH = "webViews/view";
49 private const string NODE_WIN_VIEW_XPATH = "winViews/view";
50 private const string ATTRIBUTE_ID = "id";
51 private const string ATTRIBUTE_VIEW = "view";
52 private const string ATTRIBUTE_PATH = "path";
53 private const string ATTRIBUTE_TYPE = "type";
54 private const string NODE_COMMANDS_XPATH = "command-mappings/commands";
55 private const string NODE_GLOBAL_COMMANDS_XPATH = "global-commands/command";
57 #endregion
59 #region Fields
61 private StringDictionary _urls = new StringDictionary();
62 private StringDictionary _webViews= new StringDictionary();
63 private HybridDictionary _types = new HybridDictionary();
64 private HybridDictionary _winViews= new HybridDictionary();
65 private HybridDictionary _mappings = new HybridDictionary();
66 private StringDictionary _globalCommands = new StringDictionary();
68 #endregion
70 #region Constructor
72 /// <summary>
73 /// Creates MVCConfigSettings from an XmlNode read of the web.config and an IFormatProvider.
74 /// </summary>
75 /// <param name="configNode">The XmlNode from the configuration file.</param>
76 /// <param name="formatProvider">The provider.</param>
77 public MVCConfigSettings(XmlNode configNode, IFormatProvider formatProvider)
79 LoadWebViews(configNode, formatProvider);
80 LoadWinViews(configNode, formatProvider);
81 LoadGlobalCommand(configNode);
82 LoadCommandMapping(configNode);
84 #endregion
86 #region Methods
88 /// <summary>
89 /// Load the global commands
90 /// </summary>
91 /// <param name="configNode">The XmlNode from the configuration file.</param>
92 private void LoadGlobalCommand(XmlNode configNode)
94 foreach(XmlNode commandNode in configNode.SelectNodes( NODE_GLOBAL_COMMANDS_XPATH ) )
96 string id = commandNode.Attributes[ATTRIBUTE_ID].Value;
97 string view = commandNode.Attributes[ATTRIBUTE_VIEW].Value;
98 _globalCommands.Add( id, view );
102 /// <summary>
103 /// Load the command mapping
104 /// </summary>
105 /// <param name="configNode">The XmlNode from the configuration file.</param>
106 private void LoadCommandMapping(XmlNode configNode)
108 foreach( XmlNode currentNode in configNode.SelectNodes( NODE_COMMANDS_XPATH ) )
110 CommandsSetting setting = new CommandsSetting( currentNode );
111 _mappings.Add( setting.View, setting );
115 /// <summary>
116 /// Load the views
117 /// </summary>
118 /// <param name="configNode">The XmlNode from the configuration file.</param>
119 /// <param name="formatProvider">The provider.</param>
120 private void LoadWebViews(XmlNode configNode, IFormatProvider formatProvider)
122 //Get the views
123 foreach( XmlNode viewNode in configNode.SelectNodes( NODE_WEB_VIEW_XPATH ) )
125 string viewName = viewNode.Attributes[ATTRIBUTE_ID].Value;
126 string viewUrl = viewNode.Attributes[ATTRIBUTE_PATH].Value;
128 if( !_webViews.ContainsKey( viewName ) )
130 _urls.Add( viewName, viewUrl );
131 _webViews.Add( viewUrl, viewName );
133 else
135 throw new ConfigurationException( Resource.ResourceManager.FormatMessage( Resource.MessageKeys.ViewAlreadyConfigured, viewName ) );
140 /// <summary>
141 /// Load the windows views
142 /// </summary>
143 /// <param name="configNode">The XmlNode from the configuration file.</param>
144 /// <param name="formatProvider">The provider.</param>
145 private void LoadWinViews(XmlNode configNode, IFormatProvider formatProvider)
147 //Get the views
148 foreach( XmlNode viewNode in configNode.SelectNodes( NODE_WIN_VIEW_XPATH ) )
150 string viewId = viewNode.Attributes[ATTRIBUTE_ID].Value;
151 string viewType = viewNode.Attributes[ATTRIBUTE_TYPE].Value;
153 // infer type from viewType
154 Type type = null;
155 if( !_winViews.Contains( type ) )
157 _types.Add( viewId, type );
158 //_winViews.Add( type, viewId );
160 else
162 throw new ConfigurationException( Resource.ResourceManager.FormatMessage( Resource.MessageKeys.ViewAlreadyConfigured, viewId ) );
167 ///<summary>
168 ///Looks up a url view based on view name.
169 ///</summary>
170 ///<param name="viewName">The name of the view to retrieve the settings for.</param>
171 public virtual string GetUrl( string viewName )
173 return _urls[viewName] as string;
176 ///<summary>
177 /// Looks up a web view based on his url.
178 ///</summary>
179 ///<param name="url">The URL.</param>
180 public virtual string GetView(string url)
182 return _webViews[url] as string;
185 ///<summary>
186 /// Looks up a windows view based on his type.
187 ///</summary>
188 ///<param name="type">The view type.</param>
189 public virtual string GetView(Type type)
191 return _winViews[type] as string;
194 ///<summary>
195 ///Looks up a next view based on current command id and and current view id.
196 ///</summary>
197 ///<param name="commandID">The id of the current command.</param>
198 ///<param name="viewID">The id of the current view.</param>
199 ///<returns>The next web view to go.</returns>
200 public virtual string GetNextView(string viewID, string commandID)
202 string nextView = string.Empty;
204 if (_globalCommands.ContainsKey(commandID))
206 nextView = _globalCommands[commandID];
208 else
210 CommandsSetting setting= null;
211 if( _mappings.Contains( viewID ) )
213 setting = _mappings[viewID] as CommandsSetting;
215 else
217 throw new ConfigurationException( Resource.ResourceManager.FormatMessage( Resource.MessageKeys.CantFindCommandMapping, viewID ) );
219 nextView = setting[commandID];
220 if( nextView==null )
222 throw new ConfigurationException( Resource.ResourceManager.FormatMessage( Resource.MessageKeys.CantGetNextView, viewID, commandID ) );
226 return nextView;
228 #endregion