Browse Source

ModuleSpec initializer not takes ElementPtr instead of file name,
for creation from file name or ifstream there are the non-member moduleSpecFromFile functions now



git-svn-id: svn://bind10.isc.org/svn/bind10/branches/jelte-configuration@870 e5f2f494-b856-4b98-b285-d166d9295462

Jelte Jansen 15 years ago
parent
commit
3cd4c9d2c3

+ 2 - 1
src/lib/config/cpp/ccsession.cc

@@ -48,6 +48,7 @@ using isc::data::ElementPtr;
 using isc::data::ModuleSpec;
 using isc::data::ModuleSpec;
 using isc::data::ParseError;
 using isc::data::ParseError;
 using isc::data::ModuleSpecError;
 using isc::data::ModuleSpecError;
+using namespace isc::data;
 
 
 void
 void
 ModuleCCSession::read_module_specification(const std::string& filename) {
 ModuleCCSession::read_module_specification(const std::string& filename) {
@@ -61,7 +62,7 @@ ModuleCCSession::read_module_specification(const std::string& filename) {
     }
     }
 
 
     try {
     try {
-        module_specification_ = ModuleSpec(file, true);
+        module_specification_ = moduleSpecFromFile(file, true);
     } catch (ParseError pe) {
     } catch (ParseError pe) {
         cout << "Error parsing module specification file: " << pe.what() << endl;
         cout << "Error parsing module specification file: " << pe.what() << endl;
         exit(1);
         exit(1);

+ 34 - 24
src/lib/config/cpp/data_def.cc

@@ -24,7 +24,8 @@
 
 
 // todo: add more context to thrown ModuleSpecErrors?
 // todo: add more context to thrown ModuleSpecErrors?
 
 
-using namespace isc::data;
+namespace isc {
+namespace data {
 
 
 //
 //
 // static functions
 // static functions
@@ -170,29 +171,12 @@ check_module_specification(const ElementPtr& def)
 // Public functions
 // Public functions
 //
 //
 
 
-ModuleSpec::ModuleSpec(const std::string& file_name,
-                               const bool check)
-                               throw(ParseError, ModuleSpecError) {
-    std::ifstream file;
-
-    file.open(file_name.c_str());
-    if (!file) {
-        std::stringstream errs;
-        errs << "Error opening " << file_name << ": " << strerror(errno);
-        throw ModuleSpecError(errs.str());
-    }
-
-    module_specification = Element::createFromString(file, file_name);
-    if (check) {
-        check_module_specification(module_specification);
-    }
-}
-
-
-ModuleSpec::ModuleSpec(std::istream& in, const bool check)
-                               throw(ParseError, ModuleSpecError) {
-    module_specification = Element::createFromString(in);
-    // make sure the whole structure is complete and valid
+ModuleSpec::ModuleSpec(ElementPtr module_spec_element,
+                       const bool check)
+                       throw(ModuleSpecError)
+                       
+{
+    module_specification = module_spec_element;
     if (check) {
     if (check) {
         check_module_specification(module_specification);
         check_module_specification(module_specification);
     }
     }
@@ -231,6 +215,30 @@ ModuleSpec::validate(const ElementPtr data)
     return validate_spec_list(spec, data);
     return validate_spec_list(spec, data);
 }
 }
 
 
+ModuleSpec
+moduleSpecFromFile(const std::string& file_name, const bool check)
+                   throw(ParseError, ModuleSpecError)
+{
+    std::ifstream file;
+
+    file.open(file_name.c_str());
+    if (!file) {
+        std::stringstream errs;
+        errs << "Error opening " << file_name << ": " << strerror(errno);
+        throw ModuleSpecError(errs.str());
+    }
+
+    ElementPtr module_spec_element = Element::createFromString(file, file_name);
+    return ModuleSpec(module_spec_element, check);
+}
+
+ModuleSpec
+moduleSpecFromFile(std::ifstream& in, const bool check)
+                   throw(ParseError, ModuleSpecError) {
+    ElementPtr module_spec_element = Element::createFromString(in);
+    return ModuleSpec(module_spec_element, check);
+}
+
 
 
 //
 //
 // private functions
 // private functions
@@ -332,3 +340,5 @@ ModuleSpec::validate_spec_list(const ElementPtr spec, const ElementPtr data) {
     return true;
     return true;
 }
 }
 
 
+}
+}

+ 25 - 23
src/lib/config/cpp/data_def.h

@@ -53,29 +53,8 @@ namespace isc { namespace data {
         /// Create a \c ModuleSpec instance with the given data as
         /// Create a \c ModuleSpec instance with the given data as
         /// the specification
         /// the specification
         /// \param e The Element containing the data specification
         /// \param e The Element containing the data specification
-        explicit ModuleSpec(ElementPtr e) : module_specification(e) {};
-
-        /// 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(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
-        explicit ModuleSpec(std::istream& in, const bool check = true)
-                                throw(ParseError, ModuleSpecError);
+        explicit ModuleSpec(ElementPtr e, const bool check = true)
+                            throw(ModuleSpecError);
 
 
         /// Returns the commands part of the specification as an
         /// Returns the commands part of the specification as an
         /// ElementPtr, returns an empty ElementPtr if there is none
         /// ElementPtr, returns an empty ElementPtr if there is none
@@ -112,6 +91,29 @@ namespace isc { namespace data {
         ElementPtr module_specification;
         ElementPtr module_specification;
     };
     };
 
 
+    /// 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);
 } }
 } }
 
 
 #endif // _DATA_DEF_H
 #endif // _DATA_DEF_H

+ 6 - 6
src/lib/config/cpp/data_def_unittests.cc

@@ -34,9 +34,9 @@ data_def_error(const std::string& file,
                const std::string& error2 = "",
                const std::string& error2 = "",
                const std::string& error3 = "")
                const std::string& error3 = "")
 {
 {
-    EXPECT_THROW(ModuleSpec(specfile(file)), ModuleSpecError);
+    EXPECT_THROW(moduleSpecFromFile(specfile(file)), ModuleSpecError);
     try {
     try {
-        ModuleSpec dd = ModuleSpec(specfile(file));
+        ModuleSpec dd = moduleSpecFromFile(specfile(file));
     } catch (ModuleSpecError dde) {
     } catch (ModuleSpecError dde) {
         std::string ddew = dde.what();
         std::string ddew = dde.what();
         EXPECT_EQ(error1 + error2 + error3, ddew);
         EXPECT_EQ(error1 + error2 + error3, ddew);
@@ -46,11 +46,11 @@ data_def_error(const std::string& file,
 TEST(ModuleSpec, ReadingSpecfiles) {
 TEST(ModuleSpec, ReadingSpecfiles) {
     // Tests whether we can open specfiles and if we get the
     // Tests whether we can open specfiles and if we get the
     // right parse errors
     // right parse errors
-    ModuleSpec dd = ModuleSpec(specfile("spec1.spec"));
+    ModuleSpec dd = moduleSpecFromFile(specfile("spec1.spec"));
     EXPECT_EQ(dd.getFullSpec()->get("module_spec")
     EXPECT_EQ(dd.getFullSpec()->get("module_spec")
                                 ->get("module_name")
                                 ->get("module_name")
                                 ->stringValue(), "Spec1");
                                 ->stringValue(), "Spec1");
-    dd = ModuleSpec(specfile("spec2.spec"));
+    dd = moduleSpecFromFile(specfile("spec2.spec"));
     EXPECT_EQ(dd.getFullSpec()->get("module_spec")
     EXPECT_EQ(dd.getFullSpec()->get("module_spec")
                                 ->get("config_data")->size(), 6);
                                 ->get("config_data")->size(), 6);
     data_def_error("doesnotexist",
     data_def_error("doesnotexist",
@@ -60,7 +60,7 @@ TEST(ModuleSpec, ReadingSpecfiles) {
 
 
     std::ifstream file;
     std::ifstream file;
     file.open(specfile("spec1.spec").c_str());
     file.open(specfile("spec1.spec").c_str());
-    dd = ModuleSpec(file);
+    dd = moduleSpecFromFile(file);
     EXPECT_EQ(dd.getFullSpec()->get("module_spec")
     EXPECT_EQ(dd.getFullSpec()->get("module_spec")
                                 ->get("module_name")
                                 ->get("module_name")
                                 ->stringValue(), "Spec1");
                                 ->stringValue(), "Spec1");
@@ -132,7 +132,7 @@ data_test(ModuleSpec dd, const std::string& data_file_name)
 }
 }
 
 
 TEST(ModuleSpec, DataValidation) {
 TEST(ModuleSpec, DataValidation) {
-    ModuleSpec dd = ModuleSpec(specfile("spec22.spec"));
+    ModuleSpec dd = moduleSpecFromFile(specfile("spec22.spec"));
 
 
     EXPECT_TRUE(data_test(dd, "data22_1.data"));
     EXPECT_TRUE(data_test(dd, "data22_1.data"));
     EXPECT_FALSE(data_test(dd, "data22_2.data"));
     EXPECT_FALSE(data_test(dd, "data22_2.data"));