3 /*****************************************************************************
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 ********************************************************************************/
24 /************************************************
26 *************************************************/
32 using System
.Collections
.Specialized
;
33 using System
.Configuration
;
37 namespace Castle
.MVC
.Configuration
40 /// MVCConfigSettings.
42 public class MVCConfigSettings
46 /// Token to retrieve configuration node
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";
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();
73 /// Creates MVCConfigSettings from an XmlNode read of the web.config and an IFormatProvider.
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
);
89 /// Load the global commands
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
);
103 /// Load the command mapping
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
);
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
)
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
);
135 throw new ConfigurationException( Resource
.ResourceManager
.FormatMessage( Resource
.MessageKeys
.ViewAlreadyConfigured
, viewName
) );
141 /// Load the windows views
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
)
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
155 if( !_winViews
.Contains( type
) )
157 _types
.Add( viewId
, type
);
158 //_winViews.Add( type, viewId );
162 throw new ConfigurationException( Resource
.ResourceManager
.FormatMessage( Resource
.MessageKeys
.ViewAlreadyConfigured
, viewId
) );
168 ///Looks up a url view based on view name.
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;
177 /// Looks up a web view based on his url.
179 ///<param name="url">The URL.</param>
180 public virtual string GetView(string url
)
182 return _webViews
[url
] as string;
186 /// Looks up a windows view based on his type.
188 ///<param name="type">The view type.</param>
189 public virtual string GetView(Type type
)
191 return _winViews
[type
] as string;
195 ///Looks up a next view based on current command id and and current view id.
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
];
210 CommandsSetting setting
= null;
211 if( _mappings
.Contains( viewID
) )
213 setting
= _mappings
[viewID
] as CommandsSetting
;
217 throw new ConfigurationException( Resource
.ResourceManager
.FormatMessage( Resource
.MessageKeys
.CantFindCommandMapping
, viewID
) );
219 nextView
= setting
[commandID
];
222 throw new ConfigurationException( Resource
.ResourceManager
.FormatMessage( Resource
.MessageKeys
.CantGetNextView
, viewID
, commandID
) );