module_spec.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (C) 2010 Internet Systems Consortium.
  2. //
  3. // Permission to use, copy, modify, and distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. // DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. // INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. // FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. // NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. // WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. #ifndef _MODULE_SPEC_H
  16. #define _MODULE_SPEC_H 1
  17. #include <cc/data.h>
  18. #include <sstream>
  19. using namespace isc::data;
  20. namespace isc { namespace config {
  21. ///
  22. /// A standard ModuleSpec exception that is thrown when a
  23. /// specification is not in the correct form.
  24. ///
  25. /// TODO: use jinmei's exception class as a base and not c_str in
  26. /// what() there
  27. class ModuleSpecError : public std::exception {
  28. public:
  29. ModuleSpecError(std::string m = "Module specification is invalid") : msg(m) {}
  30. ~ModuleSpecError() throw() {}
  31. const char* what() const throw() { return msg.c_str(); }
  32. private:
  33. std::string msg;
  34. };
  35. ///
  36. /// The \c ModuleSpec class holds a data specification.
  37. /// Each module should have a .spec file containing the specification
  38. /// for configuration and commands for that module.
  39. /// This class holds that specification, and provides a function to
  40. /// validate a set of data, to see whether it conforms to the given
  41. /// specification
  42. ///
  43. /// The form of the specification is described in doc/ (TODO)
  44. ///
  45. class ModuleSpec {
  46. public:
  47. explicit ModuleSpec() {};
  48. /// Create a \c ModuleSpec instance with the given data as
  49. /// the specification
  50. /// \param e The Element containing the data specification
  51. explicit ModuleSpec(ElementPtr e, const bool check = true)
  52. throw(ModuleSpecError);
  53. /// Returns the commands part of the specification as an
  54. /// ElementPtr, returns an empty ElementPtr if there is none
  55. /// \return ElementPtr Shared pointer to the commands
  56. /// part of the specification
  57. const ElementPtr getCommandsSpec();
  58. /// Returns the configuration part of the specification as an
  59. /// ElementPtr
  60. /// \return ElementPtr Shared pointer to the configuration
  61. /// part of the specification
  62. const ElementPtr getConfigSpec();
  63. /// Returns the full module specification as an ElementPtr
  64. /// \return ElementPtr Shared pointer to the specification
  65. const ElementPtr getFullSpec() { return module_specification; };
  66. /// Returns the module name as specified by the specification
  67. const std::string getModuleName();
  68. // returns true if the given element conforms to this data
  69. // configuration specification
  70. /// Validates the given configuration data for this specification.
  71. /// \param data The base \c Element of the data to check
  72. /// \return true if the data conforms to the specification,
  73. /// false otherwise.
  74. bool validate_config(const ElementPtr data, const bool full = false);
  75. /// errors must be of type ListElement
  76. bool validate_config(const ElementPtr data, const bool full, ElementPtr errors);
  77. private:
  78. bool validate_item(const ElementPtr spec, const ElementPtr data, const bool full, ElementPtr errors);
  79. bool validate_spec(const ElementPtr spec, const ElementPtr data, const bool full, ElementPtr errors);
  80. bool validate_spec_list(const ElementPtr spec, const ElementPtr data, const bool full, ElementPtr errors);
  81. ElementPtr module_specification;
  82. };
  83. /// Creates a \c ModuleSpec instance from the contents
  84. /// of the file given by file_name.
  85. /// If check is true, and the module specification is not of
  86. /// the correct form, a ModuleSpecError is thrown. If the file
  87. /// could not be parse, a ParseError is thrown.
  88. /// \param file_name The file to be opened and parsed
  89. /// \param check If true, the module specification in the file
  90. /// is checked to be of the correct form
  91. ModuleSpec
  92. moduleSpecFromFile(const std::string& file_name, const bool check = true)
  93. throw(ParseError, ModuleSpecError);
  94. /// Creates a \c ModuleSpec instance from the given input
  95. /// stream that contains the contents of a .spec file.
  96. /// If check is true, and the module specification is not of
  97. /// the correct form, a ModuleSpecError is thrown. If the
  98. /// file could not be parsed, a ParseError is thrown.
  99. /// \param in The std::istream containing the .spec file data
  100. /// \param check If true, the module specification is checked
  101. /// to be of the correct form
  102. ModuleSpec
  103. moduleSpecFromFile(std::ifstream& in, const bool check = true)
  104. throw(ParseError, ModuleSpecError);
  105. } }
  106. #endif // _DATA_DEF_H