2 // System.Data.Configuration.ObjectNameResolutionSectionHandler.cs
5 // Konstantin Triger <kostat@mainsoft.com>
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
\r
32 using System.Configuration;
\r
35 namespace System.Data.Configuration {
\r
36 class ObjectNameResolutionSectionHandler : IConfigurationSectionHandler {
\r
37 public virtual object Create (object parent, object configContext, XmlNode section) {
\r
38 if (section.Attributes != null && section.Attributes.Count != 0)
\r
39 HandlersUtil.ThrowException ("Unrecognized attribute", section);
\r
41 ObjectNameResolversCollection col = new ObjectNameResolversCollection(parent as ObjectNameResolversCollection);
\r
43 XmlNodeList resolvers = section.ChildNodes;
\r
44 foreach (XmlNode child in resolvers) {
\r
45 XmlNodeType ntype = child.NodeType;
\r
46 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
\r
49 if (ntype != XmlNodeType.Element)
\r
50 HandlersUtil.ThrowException ("Only elements allowed", child);
\r
52 string dbname = HandlersUtil.ExtractAttributeValue ("dbname", child,false,true);
\r
53 string match = HandlersUtil.ExtractAttributeValue ("match", child);
\r
54 string pri = HandlersUtil.ExtractAttributeValue ("priority", child);
\r
56 ObjectNameResolver resolver = new ObjectNameResolver(dbname, match, int.Parse(pri));
\r
66 internal sealed class HandlersUtil {
\r
67 private HandlersUtil () {
\r
70 static internal string ExtractAttributeValue (string attKey, XmlNode node) {
\r
71 return ExtractAttributeValue (attKey, node, false);
\r
74 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional) {
\r
75 return ExtractAttributeValue (attKey, node, optional, false);
\r
78 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,
\r
80 if (node.Attributes == null) {
\r
84 ThrowException ("Required attribute not found: " + attKey, node);
\r
87 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
\r
91 ThrowException ("Required attribute not found: " + attKey, node);
\r
94 string value = att.Value;
\r
95 if (!allowEmpty && value == String.Empty) {
\r
96 string opt = optional ? "Optional" : "Required";
\r
97 ThrowException (opt + " attribute is empty: " + attKey, node);
\r
103 static internal void ThrowException (string msg, XmlNode node) {
\r
104 if (node != null && node.Name != String.Empty)
\r
105 msg = msg + " (node name: " + node.Name + ") ";
\r
106 throw new ConfigurationException (msg, node);
\r