1 using System.Collections;
\r
2 using System.Configuration;
\r
5 namespace System.Data.Configuration {
\r
6 class ObjectNameResolutionSectionHandler : IConfigurationSectionHandler {
\r
7 public virtual object Create (object parent, object configContext, XmlNode section) {
\r
8 if (section.Attributes != null && section.Attributes.Count != 0)
\r
9 HandlersUtil.ThrowException ("Unrecognized attribute", section);
\r
11 ObjectNameResolversCollection col = new ObjectNameResolversCollection(parent as ObjectNameResolversCollection);
\r
13 XmlNodeList resolvers = section.ChildNodes;
\r
14 foreach (XmlNode child in resolvers) {
\r
15 XmlNodeType ntype = child.NodeType;
\r
16 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
\r
19 if (ntype != XmlNodeType.Element)
\r
20 HandlersUtil.ThrowException ("Only elements allowed", child);
\r
22 string dbname = HandlersUtil.ExtractAttributeValue ("dbname", child,false,true);
\r
23 string match = HandlersUtil.ExtractAttributeValue ("match", child);
\r
24 string pri = HandlersUtil.ExtractAttributeValue ("priority", child);
\r
26 ObjectNameResolver resolver = new ObjectNameResolver(dbname, match, int.Parse(pri));
\r
36 internal sealed class HandlersUtil {
\r
37 private HandlersUtil () {
\r
40 static internal string ExtractAttributeValue (string attKey, XmlNode node) {
\r
41 return ExtractAttributeValue (attKey, node, false);
\r
44 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional) {
\r
45 return ExtractAttributeValue (attKey, node, optional, false);
\r
48 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,
\r
50 if (node.Attributes == null) {
\r
54 ThrowException ("Required attribute not found: " + attKey, node);
\r
57 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
\r
61 ThrowException ("Required attribute not found: " + attKey, node);
\r
64 string value = att.Value;
\r
65 if (!allowEmpty && value == String.Empty) {
\r
66 string opt = optional ? "Optional" : "Required";
\r
67 ThrowException (opt + " attribute is empty: " + attKey, node);
\r
73 static internal void ThrowException (string msg, XmlNode node) {
\r
74 if (node != null && node.Name != String.Empty)
\r
75 msg = msg + " (node name: " + node.Name + ") ";
\r
76 throw new ConfigurationException (msg, node);
\r