[genmdesc] Fixed generator to allow instructions lengths equal to 0.
[mono.git] / mcs / class / System.Data / System.Data.Configuration.jvm / ObjectNameResolutionSectionHandler.cs
1 // 
2 // System.Data.Configuration.ObjectNameResolutionSectionHandler.cs
3 //
4 // Authors:
5 //      Konstantin Triger <kostat@mainsoft.com>
6 //      
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //\r
30 \r
31 using System.Collections;\r
32 using System.Configuration;\r
33 using System.Xml;\r
34 \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
40 \r
41                         ObjectNameResolversCollection col = new ObjectNameResolversCollection(parent as ObjectNameResolversCollection);\r
42 \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
47                                         continue;\r
48 \r
49                                 if (ntype != XmlNodeType.Element)\r
50                                         HandlersUtil.ThrowException ("Only elements allowed", child);\r
51 \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
55 \r
56                                 ObjectNameResolver resolver = new ObjectNameResolver(dbname, match, int.Parse(pri));\r
57                                 col.Add(resolver);\r
58                         }\r
59 \r
60                         col.Sort();\r
61                         \r
62                         return col;\r
63                 }\r
64         }\r
65 \r
66         internal sealed class HandlersUtil {\r
67                 private HandlersUtil () {\r
68                 }\r
69 \r
70                 static internal string ExtractAttributeValue (string attKey, XmlNode node) {\r
71                         return ExtractAttributeValue (attKey, node, false);\r
72                 }\r
73                         \r
74                 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional) {\r
75                         return ExtractAttributeValue (attKey, node, optional, false);\r
76                 }\r
77                 \r
78                 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,\r
79                         bool allowEmpty) {\r
80                         if (node.Attributes == null) {\r
81                                 if (optional)\r
82                                         return null;\r
83 \r
84                                 ThrowException ("Required attribute not found: " + attKey, node);\r
85                         }\r
86 \r
87                         XmlNode att = node.Attributes.RemoveNamedItem (attKey);\r
88                         if (att == null) {\r
89                                 if (optional)\r
90                                         return null;\r
91                                 ThrowException ("Required attribute not found: " + attKey, node);\r
92                         }\r
93 \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
98                         }\r
99 \r
100                         return value;\r
101                 }\r
102 \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
107                 }\r
108         }\r
109 }