11 using System.Configuration;
12 using System.Globalization;
15 namespace Lextm.SharpSnmpLib.Mib
20 public sealed
class Symbol : IEquatable<Symbol>
22 private readonly
string _text;
23 private readonly
int _row;
24 private readonly
int _column;
25 private readonly
string _file;
27 private Symbol(
string text) :
this(null, text, -1, -1)
38 public Symbol(
string file,
string text,
int row,
int column)
94 public override bool Equals(
object obj)
101 if (ReferenceEquals(
this, obj))
106 return GetType() == obj.GetType() && Equals((
Symbol)obj);
115 return _text.GetHashCode();
127 return Equals(left, right);
146 if (l == null || r == null)
151 return left._text.Equals(right._text);
163 return !(left == right);
166 #region IEquatable<Symbol> Members 167 public bool Equals(
Symbol other)
175 return Equals(
this, other);
191 public static readonly
Symbol ModuleIdentity =
new Symbol(
"MODULE-IDENTITY");
193 public static readonly
Symbol ObjectGroup =
new Symbol(
"OBJECT-GROUP");
194 public static readonly
Symbol NotificationGroup =
new Symbol(
"NOTIFICATION-GROUP");
195 public static readonly
Symbol ModuleCompliance =
new Symbol(
"MODULE-COMPLIANCE");
197 public static readonly
Symbol NotificationType =
new Symbol(
"NOTIFICATION-TYPE");
199 public static readonly
Symbol ObjectIdentity =
new Symbol(
"OBJECT-IDENTITY");
204 public static readonly
Symbol AgentCapabilities =
new Symbol(
"AGENT-CAPABILITIES");
206 public static readonly
Symbol TextualConvention =
new Symbol(
"TEXTUAL-CONVENTION");
242 internal void Expect(
Symbol expected, params
Symbol[] orExpected)
244 bool isExpected = (
this == expected);
246 if (!isExpected && (orExpected != null) && (orExpected.Length > 0))
249 for (
int i=0; i<orExpected.Length; i++)
251 if (
this == orExpected[i])
261 if ((orExpected == null) || (orExpected.Length == 0))
263 Assert(
false,
"Unexpected symbol found! Expected '" + expected.
ToString() +
"'");
267 StringBuilder msg =
new StringBuilder(
"Unexpected symbol found! Expected one of the following: '");
268 msg.Append(expected);
271 for (
int i=0; i<orExpected.Length; i++)
274 msg.Append(expected);
278 Assert(
false, msg.ToString());
283 internal void Assert(
bool condition,
string message)
291 internal void AssertIsValidIdentifier()
294 bool isValid = IsValidIdentifier(ToString(), out message);
295 Assert(isValid, message);
298 internal bool IsValidIdentifier()
301 return IsValidIdentifier(ToString(), out message);
304 private static bool IsValidIdentifier(
string name, out
string message)
306 if (UseStricterValidation && (name.Length < 1 || name.Length > 64))
308 message =
"an identifier must consist of 1 to 64 letters, digits, and hyphens";
312 if (!Char.IsLetter(name[0]))
314 message =
"the initial character must be a letter";
318 if (name.EndsWith(
"-", StringComparison.Ordinal))
320 message =
"a hyphen cannot be the last character of an identifier";
324 if (name.Contains(
"--"))
326 message =
"a hyphen cannot be immediately followed by another hyphen in an identifier";
330 if (UseStricterValidation && name.Contains(
"_"))
332 message =
"underscores are not allowed in identifiers";
341 private static bool? _useStricterValidation;
343 private static bool UseStricterValidation
347 if (_useStricterValidation == null)
349 object setting = ConfigurationManager.AppSettings[
"StricterValidationEnabled"];
350 _useStricterValidation = setting != null && Convert.ToBoolean(setting.ToString(), CultureInfo.InvariantCulture);
353 return _useStricterValidation.Value;
static bool Equals(Symbol left, Symbol right)
Determines whether the specified Symbol is equal to the current Symbol.
override int GetHashCode()
Serves as a hash function for a particular type.
Description of MibException.
static MibException Create(string message, Symbol symbol)
Creates a MibException with a specific Symbol.
Symbol(string file, string text, int row, int column)
Creates a Symbol.
override string ToString()
Returns a String that represents this Symbol.
override bool Equals(object obj)
Determines whether the specified Object is equal to the current Symbol.