2 // EngineConfig.cs - Holds information on the capabilities and behavior of an
6 // Gert Driesen (drieseng@users.sourceforge.net
8 // Copyright (c) 2008 Gert Driesen
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Configuration;
32 using System.Globalization;
35 namespace MonoTests.System.Data
37 internal sealed class EngineConfig
40 private string quoteCharacter;
41 private bool removesTrailingSpaces;
42 private bool emptyBinaryAsNull;
43 private bool supportsMicroseconds;
44 private bool supportsUniqueIdentifier;
45 private bool supportsDate;
46 private bool supportsTime;
47 private bool supportsTimestamp;
48 private EngineType type;
49 private int clientVersion;
51 private EngineConfig ()
60 /// Returns the character(s) for quoting identifiers.
62 public string QuoteCharacter {
63 get { return quoteCharacter; }
66 public EngineType Type {
70 public bool RemovesTrailingSpaces {
71 get { return removesTrailingSpaces; }
74 public bool EmptyBinaryAsNull {
75 get { return emptyBinaryAsNull; }
78 public bool SupportsMicroseconds {
79 get { return supportsMicroseconds; }
82 public bool SupportsUniqueIdentifier {
83 get { return supportsUniqueIdentifier; }
86 public bool SupportsDate {
87 get { return supportsDate; }
90 public bool SupportsTime {
91 get { return supportsTime; }
94 public bool SupportsTimestamp {
95 get { return supportsTimestamp; }
98 public int ClientVersion {
99 get { return clientVersion; }
102 public static EngineConfig FromXml (XmlNode config)
104 EngineConfig engine = new EngineConfig ();
105 engine.name = GetAttribValue (config, "name", true);
106 engine.quoteCharacter = GetAttribValue (config, "quoteCharacter", true);
107 engine.removesTrailingSpaces = ParseBoolean (config, "removesTrailingSpaces", false, true);
108 engine.emptyBinaryAsNull = ParseBoolean (config, "emptyBinaryAsNull", false, true);
109 engine.supportsMicroseconds = ParseBoolean (config, "supportsMicroseconds", false, true);
110 engine.supportsUniqueIdentifier = ParseBoolean (config, "supportsUniqueIdentifier", false, true);
111 engine.supportsDate = ParseBoolean (config, "supportsDate", false, true);
112 engine.supportsTime = ParseBoolean (config, "supportsTime", false, true);
113 engine.supportsTimestamp = ParseBoolean (config, "supportsTimestamp", false, true);
114 engine.type = ParseEngineType (config, "type");
115 engine.clientVersion = ParseClientVersion (config, "clientversion");
119 static string GetAttribValue (XmlNode node, string name, bool required)
121 XmlAttribute attr = node.Attributes [name];
124 throw CreateAttributeMissingException (name, node);
130 static bool ParseBoolean (XmlNode config, string attrName, bool required, bool defaultValue)
132 XmlAttribute attr = config.Attributes [attrName];
135 throw CreateAttributeMissingException (attrName, config);
139 string value = attr.Value;
142 return bool.Parse (value);
143 } catch (Exception ex) {
144 throw CreateInvalidValueException (attrName,
149 static EngineType ParseEngineType (XmlNode config, string attrName)
151 XmlAttribute attr = config.Attributes [attrName];
153 throw CreateAttributeMissingException (attrName, config);
155 string value = attr.Value;
158 return (EngineType) Enum.Parse (typeof (EngineType), value);
159 } catch (Exception ex) {
160 throw CreateInvalidValueException (attrName,
165 static int ParseClientVersion (XmlNode config, string attrName)
167 XmlAttribute attr = config.Attributes [attrName];
171 string value = attr.Value;
174 return Int32.Parse (value);
175 } catch (Exception ex) {
176 throw CreateInvalidValueException (attrName,
181 static Exception CreateInvalidValueException (string name, string value, XmlNode node, Exception cause)
183 string msg = string.Format (CultureInfo.InvariantCulture,
184 "Invalid value '{0}' for attribute {1}.",
186 throw new ConfigurationErrorsException (msg, cause, node);
189 static Exception CreateAttributeMissingException (string name, XmlNode node)
191 string msg = string.Format (CultureInfo.InvariantCulture,
192 "Missing '{0}' attribute.", name);
193 throw new ConfigurationErrorsException (msg, node);