module_spec.h 5.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (C) 2010  Internet Systems Consortium.
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
// DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
// INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
// FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
// WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#ifndef _MODULE_SPEC_H
#define _MODULE_SPEC_H 1
18

19
#include <cc/data.h>
20 21 22

#include <sstream>

23 24 25
using namespace isc::data;

namespace isc { namespace config {
26

27
    ///
28
    /// A standard ModuleSpec exception that is thrown when a
29
    /// specification is not in the correct form.
30 31 32
    ///
    /// TODO: use jinmei's exception class as a base and not c_str in
    /// what() there
33
    class ModuleSpecError : public std::exception {
34
    public:
35
        ModuleSpecError(std::string m = "Module specification is invalid") : msg(m) {}
36
        ~ModuleSpecError() throw() {}
37 38 39 40 41
        const char* what() const throw() { return msg.c_str(); }
    private:
        std::string msg;
    };

42
    ///
43
    /// The \c ModuleSpec class holds a data specification.
44 45 46 47 48
    /// Each module should have a .spec file containing the specification
    /// for configuration and commands for that module.
    /// This class holds that specification, and provides a function to
    /// validate a set of data, to see whether it conforms to the given
    /// specification
49 50 51
    ///
    /// The form of the specification is described in doc/ (TODO)
    ///
52
    class ModuleSpec {
53
    public:
54 55
        explicit ModuleSpec() {};
        /// Create a \c ModuleSpec instance with the given data as
56 57
        /// the specification
        /// \param e The Element containing the data specification
58 59
        explicit ModuleSpec(ElementPtr e, const bool check = true)
                            throw(ModuleSpecError);
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        /// Returns the commands part of the specification as an
        /// ElementPtr, returns an empty ElementPtr if there is none
        /// \return ElementPtr Shared pointer to the commands
        ///                    part of the specification
        const ElementPtr getCommandsSpec();

        /// Returns the configuration part of the specification as an
        /// ElementPtr
        /// \return ElementPtr Shared pointer to the configuration
        ///                    part of the specification
        const ElementPtr getConfigSpec();

        /// Returns the full module specification as an ElementPtr
        /// \return ElementPtr Shared pointer to the specification
        const ElementPtr getFullSpec() { return module_specification; };

        /// Returns the module name as specified by the specification
        const std::string getModuleName();
        
80
        // returns true if the given element conforms to this data
81 82
        // configuration specification
        /// Validates the given configuration data for this specification.
83 84 85
        /// \param data The base \c Element of the data to check
        /// \return true if the data conforms to the specification,
        /// false otherwise.
86 87 88 89
        bool validate_config(const ElementPtr data, const bool full = false);

        /// errors must be of type ListElement
        bool validate_config(const ElementPtr data, const bool full, ElementPtr errors);
90 91

    private:
92 93 94
        bool validate_item(const ElementPtr spec, const ElementPtr data, const bool full, ElementPtr errors);
        bool validate_spec(const ElementPtr spec, const ElementPtr data, const bool full, ElementPtr errors);
        bool validate_spec_list(const ElementPtr spec, const ElementPtr data, const bool full, ElementPtr errors);
95

96
        ElementPtr module_specification;
97 98
    };

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    /// Creates a \c ModuleSpec instance from the contents
    /// of the file given by file_name.
    /// If check is true, and the module specification is not of
    /// the correct form, a ModuleSpecError is thrown. If the file
    /// could not be parse, a ParseError is thrown.
    /// \param file_name The file to be opened and parsed
    /// \param check If true, the module specification in the file
    /// is checked to be of the correct form
    ModuleSpec
    moduleSpecFromFile(const std::string& file_name, const bool check = true)
                       throw(ParseError, ModuleSpecError);

    /// Creates a \c ModuleSpec instance from the given input
    /// stream that contains the contents of a .spec file.
    /// If check is true, and the module specification is not of
    /// the correct form, a ModuleSpecError is thrown. If the
    /// file could not be parsed, a ParseError is thrown.
    /// \param in The std::istream containing the .spec file data
    /// \param check If true, the module specification is checked
    /// to be of the correct form
    ModuleSpec
    moduleSpecFromFile(std::ifstream& in, const bool check = true)
                       throw(ParseError, ModuleSpecError);
122 123 124
} }

#endif // _DATA_DEF_H