data_def.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef _DATA_DEF_H
  2. #define _DATA_DEF_H 1
  3. #include "data.h"
  4. #include <sstream>
  5. namespace isc { namespace data {
  6. ///
  7. /// A standard DataDefinition exception that is thrown when a
  8. /// specification is not in the correct form.
  9. ///
  10. /// TODO: use jinmei's exception class as a base and not c_str in
  11. /// what() there
  12. class DataDefinitionError : public std::exception {
  13. public:
  14. DataDefinitionError(std::string m = "Data definition is invalid") : msg(m) {}
  15. ~DataDefinitionError() throw() {}
  16. const char* what() const throw() { return msg.c_str(); }
  17. private:
  18. std::string msg;
  19. };
  20. ///
  21. /// The \c DataDefinition class holds a data specification.
  22. /// Each module should have a .spec file containing the specification
  23. /// for configuration and commands for that module.
  24. /// This class holds that specification, and provides a function to
  25. /// validate a set of data, to see whether it conforms to the given
  26. /// specification
  27. ///
  28. /// The form of the specification is described in doc/ (TODO)
  29. ///
  30. class DataDefinition {
  31. public:
  32. explicit DataDefinition() {};
  33. /// Create a \c DataDefinition instance with the given data as
  34. /// the specification
  35. /// \param e The Element containing the data specification
  36. explicit DataDefinition(ElementPtr e) : definition(e) {};
  37. // todo: make check default false, or leave out option completely and always check?
  38. /// Creates a \c DataDefinition instance from the given .spec
  39. /// file stream. If check is true, and the definition is not of
  40. /// the correct form, a DataDefinitionError is thrown. If the
  41. /// file could not be parsed, a ParseError is thrown.
  42. /// \param in The std::istream containing the .spec file data
  43. /// \param check If true, the data definition is checked to be
  44. /// of the correct form
  45. explicit DataDefinition(std::istream& in, const bool check = true)
  46. throw(ParseError, DataDefinitionError);
  47. /// Returns the base \c element of the data definition contained
  48. /// by this instance
  49. /// \return ElementPtr Shared pointer to the data definition
  50. const ElementPtr getDefinition() { return definition; };
  51. // returns true if the given element conforms to this data
  52. // definition scheme
  53. /// Validates the given data for this specification.
  54. /// \param data The base \c Element of the data to check
  55. /// \return true if the data conforms to the specification,
  56. /// false otherwise.
  57. bool validate(const ElementPtr data);
  58. private:
  59. bool validate_item(const ElementPtr spec, const ElementPtr data);
  60. bool validate_spec(const ElementPtr spec, const ElementPtr data);
  61. bool validate_spec_list(const ElementPtr spec, const ElementPtr data);
  62. ElementPtr definition;
  63. };
  64. } }
  65. #endif // _DATA_DEF_H