SnmpNode.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Martin Hentschel <info@cl-soft.de>
  30. *
  31. */
  32. using System;
  33. using System.Text.RegularExpressions;
  34. using CCodeGeneration;
  35. namespace LwipSnmpCodeGeneration
  36. {
  37. public abstract class SnmpNode
  38. {
  39. public static readonly Regex NameValidationRegex = new Regex(@"^\w+$");
  40. private string name;
  41. private readonly SnmpTreeNode parentNode;
  42. protected SnmpNode(SnmpTreeNode parentNode)
  43. {
  44. this.parentNode = parentNode;
  45. }
  46. public SnmpTreeNode ParentNode
  47. {
  48. get { return this.parentNode; }
  49. }
  50. public virtual uint Oid { get; set; }
  51. public abstract string FullNodeName
  52. {
  53. get;
  54. }
  55. public virtual string Name
  56. {
  57. get { return this.name; }
  58. set
  59. {
  60. if (value != this.name)
  61. {
  62. // check for valid name
  63. if (!NameValidationRegex.IsMatch(value))
  64. {
  65. throw new ArgumentOutOfRangeException("Name");
  66. }
  67. this.name = value;
  68. }
  69. }
  70. }
  71. public virtual void Generate(MibCFile generatedFile, MibHeaderFile generatedHeaderFile)
  72. {
  73. int declCount = generatedFile.Declarations.Count;
  74. int implCount = generatedFile.Implementation.Count;
  75. this.GenerateHeaderCode(generatedHeaderFile);
  76. this.GenerateCode(generatedFile);
  77. if (generatedFile.Declarations.Count != declCount)
  78. {
  79. generatedFile.Declarations.Add(EmptyLine.SingleLine);
  80. }
  81. if (generatedFile.Implementation.Count != implCount)
  82. {
  83. generatedFile.Implementation.Add(EmptyLine.SingleLine);
  84. }
  85. }
  86. public abstract void GenerateCode(MibCFile mibFile);
  87. public virtual void GenerateHeaderCode(MibHeaderFile mibHeaderFile)
  88. {
  89. }
  90. /// <summary>
  91. /// Called after node structure creation is completed and before code is created.
  92. /// Offers the possibility to perform operations depending on properties/subnodes.
  93. /// If the node shall be transformed to another node(-type) than the own instance
  94. /// may be replaced on parent node by the transformed instance.
  95. /// Calling sequence is always from leafs up to root. So a tree node can assume
  96. /// that the analyze method was already called on all child nodes.
  97. /// E.g. a tree node only has scalar sub nodes -> it transforms itself to a scalar array node
  98. /// </summary>
  99. /// <returns>The transformed node or null if nothing shall be changed in parent structure.</returns>
  100. public virtual void Analyze()
  101. {
  102. }
  103. }
  104. }