MibResolver.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Reflection;
  7. namespace Lextm.SharpSnmpLib.Mib
  8. {
  9. public interface IMibResolver
  10. {
  11. IModule Resolve(string moduleName);
  12. }
  13. public class FileSystemMibResolver : IMibResolver
  14. {
  15. private string _path;
  16. private bool _recursive;
  17. public FileSystemMibResolver(string path, bool recursive)
  18. {
  19. _path = path;
  20. _recursive = recursive;
  21. }
  22. #region IMibResolver Member
  23. public IModule Resolve(string moduleName)
  24. {
  25. if (Directory.Exists(_path))
  26. {
  27. string[] matchedFiles = Directory.GetFiles(
  28. _path,
  29. "*",
  30. (_recursive) ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
  31. if ((matchedFiles != null) && (matchedFiles.Length >= 1))
  32. {
  33. foreach (string matchedFile in matchedFiles)
  34. {
  35. if (Path.GetFileNameWithoutExtension(matchedFile.ToLowerInvariant()) == moduleName.ToLowerInvariant())
  36. {
  37. try
  38. {
  39. MibDocument md = new MibDocument (matchedFile);
  40. if (md.Modules.Count > 0)
  41. {
  42. return md.Modules [0];
  43. }
  44. } catch
  45. {
  46. }
  47. }
  48. }
  49. }
  50. }
  51. return null;
  52. }
  53. #endregion
  54. }
  55. // earlier code for search of versioned MIBs:
  56. //
  57. //private const string Pattern = "-V[0-9]+$";
  58. //public static bool AllDependentsAvailable(MibModule module, IDictionary<string, MibModule> modules)
  59. //{
  60. // foreach (string dependent in module.Dependents)
  61. // {
  62. // if (!DependentFound(dependent, modules))
  63. // {
  64. // return false;
  65. // }
  66. // }
  67. // return true;
  68. //}
  69. //private static bool DependentFound(string dependent, IDictionary<string, MibModule> modules)
  70. //{
  71. // if (!Regex.IsMatch(dependent, Pattern))
  72. // {
  73. // return modules.ContainsKey(dependent);
  74. // }
  75. // if (modules.ContainsKey(dependent))
  76. // {
  77. // return true;
  78. // }
  79. // string dependentNonVersion = Regex.Replace(dependent, Pattern, string.Empty);
  80. // return modules.ContainsKey(dependentNonVersion);
  81. //}
  82. }