Added files and folders for TARGET_JVM code base
[mono.git] / mcs / class / System.Data / System.Data.Configuration.jvm / ObjectNameResolutionSectionHandler.cs
1 using System.Collections;\r
2 using System.Configuration;\r
3 using System.Xml;\r
4 \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
10 \r
11                         ObjectNameResolversCollection col = new ObjectNameResolversCollection(parent as ObjectNameResolversCollection);\r
12 \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
17                                         continue;\r
18 \r
19                                 if (ntype != XmlNodeType.Element)\r
20                                         HandlersUtil.ThrowException ("Only elements allowed", child);\r
21 \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
25 \r
26                                 ObjectNameResolver resolver = new ObjectNameResolver(dbname, match, int.Parse(pri));\r
27                                 col.Add(resolver);\r
28                         }\r
29 \r
30                         col.Sort();\r
31                         \r
32                         return col;\r
33                 }\r
34         }\r
35 \r
36         internal sealed class HandlersUtil {\r
37                 private HandlersUtil () {\r
38                 }\r
39 \r
40                 static internal string ExtractAttributeValue (string attKey, XmlNode node) {\r
41                         return ExtractAttributeValue (attKey, node, false);\r
42                 }\r
43                         \r
44                 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional) {\r
45                         return ExtractAttributeValue (attKey, node, optional, false);\r
46                 }\r
47                 \r
48                 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,\r
49                         bool allowEmpty) {\r
50                         if (node.Attributes == null) {\r
51                                 if (optional)\r
52                                         return null;\r
53 \r
54                                 ThrowException ("Required attribute not found: " + attKey, node);\r
55                         }\r
56 \r
57                         XmlNode att = node.Attributes.RemoveNamedItem (attKey);\r
58                         if (att == null) {\r
59                                 if (optional)\r
60                                         return null;\r
61                                 ThrowException ("Required attribute not found: " + attKey, node);\r
62                         }\r
63 \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
68                         }\r
69 \r
70                         return value;\r
71                 }\r
72 \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
77                 }\r
78         }\r
79 }