]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Verify that parameters we read in XML format also satisfy their patterns.
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 20 Dec 2010 14:45:33 +0000 (14:45 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Mon, 20 Dec 2010 14:45:33 +0000 (14:45 +0000)
git-svn-id: https://svn.dealii.org/trunk@23024 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/source/base/parameter_handler.cc
tests/bits/parameter_handler_read_xml/cmp/generic
tests/bits/parameter_handler_read_xml/prm
tests/bits/parameter_handler_read_xml_error_04.cc [new file with mode: 0644]
tests/bits/parameter_handler_read_xml_error_04/cmp/generic [new file with mode: 0644]
tests/bits/parameter_handler_read_xml_error_04/prm [new file with mode: 0644]

index 4958dd9b45001cfb0c89f31311b9b835ad7159e3..33ce681886466368f30dff39cb176af31fc4ed35 100644 (file)
@@ -1162,6 +1162,8 @@ namespace
   read_xml_recursively (const boost::property_tree::ptree &source,
                        const std::string                 &current_path,
                        const char                         path_separator,
+                       const std::vector<std_cxx1x::shared_ptr<const Patterns::PatternBase> > &
+                       patterns,
                        boost::property_tree::ptree       &destination)
   {
     for (boost::property_tree::ptree::const_iterator p = source.begin();
@@ -1186,10 +1188,32 @@ namespace
                                                       path_separator +
                                                       "value"))
              {
+                                                // first make sure that the
+                                                // new entry actually
+                                                // satisfies its constraints
+               const std::string new_value
+                 = p->second.get<std::string>("value");
+               
+               const unsigned int pattern_index
+                 = destination.get<unsigned int> (full_path +
+                                                  path_separator +
+                                                  "pattern");
+               if (patterns[pattern_index]->match(new_value) == false)
+                 {
+                   std::cerr << "    The entry value" << std::endl
+                             << "        " << new_value << std::endl
+                             << "    for the entry named" << std::endl
+                             << "        " << full_path << std::endl
+                             << "    does not match the given pattern" << std::endl
+                             << "        " << patterns[pattern_index]->description()
+                             << std::endl;
+                   return false;
+                 }
+
                                                 // set the found parameter in
                                                 // the destination argument
                destination.put (full_path + path_separator + "value",
-                                p->second.get_optional<std::string>("value"));
+                                new_value);
                
                                                 // this node might have
                                                 // sub-nodes in addition to
@@ -1221,6 +1245,7 @@ namespace
                                       p->first :
                                       current_path + path_separator + p->first),
                                      path_separator,
+                                     patterns,
                                      destination);
 
                                             // see if the recursive read
@@ -1283,7 +1308,8 @@ bool ParameterHandler::read_input_from_xml (std::istream &in)
   const boost::property_tree::ptree
     &my_entries = single_node_tree.get_child("ParameterHandler");
 
-  return read_xml_recursively (my_entries, "", path_separator, *entries);
+  return read_xml_recursively (my_entries, "", path_separator, patterns,
+                              *entries);
 }
 
 
index c9f5c7e9839d462620ca9b7786cb7b93aa126d72..a7ccff8cf6f33e69af151dc70478a10eb000387e 100644 (file)
@@ -1,3 +1,3 @@
 
 <?xml version="1.0" encoding="utf-8"?>
-<ParameterHandler><int1><value> 2</value><default_value>1</default_value><documentation>doc 1</documentation><pattern>0</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int1><int2><value> 3</value><default_value>2</default_value><documentation>doc 2</documentation><pattern>1</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int2><ss1><double_201><value> 2.234</value><default_value>1.234</default_value><documentation>doc 3</documentation><pattern>2</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_201><ss2><double_202><value> 5.321</value><default_value>4.321</default_value><documentation>doc 4</documentation><pattern>3</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_202></ss2></ss1><Testing_25testing><string_26list><value> __&lt; &amp; &gt; ; /</value><default_value>&lt; &amp; &gt; ; /</default_value><documentation>docs 1</documentation><pattern>4</pattern><pattern_description>[Anything]</pattern_description></string_26list><int_2aint><value> __2</value><default_value>2</default_value><documentation/><pattern>5</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int_2aint><double_2bdouble><value> 7.1415926</value><default_value>6.1415926</default_value><documentation>docs 3</documentation><pattern>6</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_2bdouble></Testing_25testing></ParameterHandler>
+<ParameterHandler><int1><value> 2</value><default_value>1</default_value><documentation>doc 1</documentation><pattern>0</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int1><int2><value> 3</value><default_value>2</default_value><documentation>doc 2</documentation><pattern>1</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int2><ss1><double_201><value> 2.234</value><default_value>1.234</default_value><documentation>doc 3</documentation><pattern>2</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_201><ss2><double_202><value> 5.321</value><default_value>4.321</default_value><documentation>doc 4</documentation><pattern>3</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_202></ss2></ss1><Testing_25testing><string_26list><value> __&lt; &amp; &gt; ; /</value><default_value>&lt; &amp; &gt; ; /</default_value><documentation>docs 1</documentation><pattern>4</pattern><pattern_description>[Anything]</pattern_description></string_26list><int_2aint><value> 2</value><default_value>2</default_value><documentation/><pattern>5</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int_2aint><double_2bdouble><value> 7.1415926</value><default_value>6.1415926</default_value><documentation>docs 3</documentation><pattern>6</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_2bdouble></Testing_25testing></ParameterHandler>
index cad7566e20f055f17c846c5ff083f834590ac318..0c4f5a41bd48da58d451bbc9ddf3003c16c76cb0 100644 (file)
@@ -1,4 +1,4 @@
 
 <?xml version="1.0" encoding="utf-8"?>
-<ParameterHandler><int1><value>2</value><default_value>1</default_value><documentation>doc 1</documentation><pattern>0</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int1><int2><value>3</value><default_value>2</default_value><documentation>doc 2</documentation><pattern>1</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int2><ss1><double_201><value>2.234</value><default_value>1.234</default_value><documentation>doc 3</documentation><pattern>2</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_201><ss2><double_202><value>5.321</value><default_value>4.321</default_value><documentation>doc 4</documentation><pattern>3</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_202></ss2></ss1><Testing_25testing><string_26list><value>__&lt; &amp; &gt; ; /</value><default_value>&lt; &amp; &gt; ; /</default_value><documentation>docs 1</documentation><pattern>4</pattern><pattern_description>[Anything]</pattern_description></string_26list><int_2aint><value>__2</value><default_value>2</default_value><documentation/>
+<ParameterHandler><int1><value>2</value><default_value>1</default_value><documentation>doc 1</documentation><pattern>0</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int1><int2><value>3</value><default_value>2</default_value><documentation>doc 2</documentation><pattern>1</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int2><ss1><double_201><value>2.234</value><default_value>1.234</default_value><documentation>doc 3</documentation><pattern>2</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_201><ss2><double_202><value>5.321</value><default_value>4.321</default_value><documentation>doc 4</documentation><pattern>3</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_202></ss2></ss1><Testing_25testing><string_26list><value>__&lt; &amp; &gt; ; /</value><default_value>&lt; &amp; &gt; ; /</default_value><documentation>docs 1</documentation><pattern>4</pattern><pattern_description>[Anything]</pattern_description></string_26list><int_2aint><value>2</value><default_value>2</default_value><documentation/>
 <pattern>5</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int_2aint><double_2bdouble><value>7.1415926</value><default_value>6.1415926</default_value><documentation>docs 3</documentation><pattern>6</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_2bdouble></Testing_25testing></ParameterHandler>
diff --git a/tests/bits/parameter_handler_read_xml_error_04.cc b/tests/bits/parameter_handler_read_xml_error_04.cc
new file mode 100644 (file)
index 0000000..21cb953
--- /dev/null
@@ -0,0 +1,83 @@
+//----------------------------  parameter_handler_read_xml_error_04.cc  ---------------------------
+//    $Id$
+//    Version: $Name$
+//
+//    Copyright (C) 2002, 2003, 2004, 2005, 2010 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//----------------------------  parameter_handler_read_xml_error_04.cc  ---------------------------
+
+
+// check ParameterHandler::read_input_from_xml. try to read a file with a
+// parameter that does not satisfy its pattern
+
+#include "../tests.h"
+#include <base/logstream.h>
+#include <base/parameter_handler.h>
+#include <fstream>
+
+
+int main ()
+{
+  std::ofstream logfile("parameter_handler_read_xml_error_04/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  ParameterHandler prm;
+  prm.declare_entry ("int1",
+                    "1",
+                    Patterns::Integer(),
+                    "doc 1");
+  prm.declare_entry ("int2",
+                    "2",
+                    Patterns::Integer(),
+                    "doc 2");
+  prm.enter_subsection ("ss1");
+  {
+    prm.declare_entry ("double 1",
+                      "1.234",
+                      Patterns::Double(),
+                      "doc 3");
+
+    prm.enter_subsection ("ss2");
+    {
+      prm.declare_entry ("double 2",
+                        "4.321",
+                        Patterns::Double(),
+                        "doc 4");
+    }
+    prm.leave_subsection ();
+  }
+  prm.leave_subsection ();
+
+                                  // things with strange characters
+  prm.enter_subsection ("Testing%testing");
+  {
+    prm.declare_entry ("string&list",
+                      "< & > ; /",
+                      Patterns::Anything(),
+                      "docs 1");
+    prm.declare_entry ("int*int",
+                      "2",
+                      Patterns::Integer());
+    prm.declare_entry ("double+double",
+                      "6.1415926",
+                      Patterns::Double(),
+                      "docs 3");
+  }
+  prm.leave_subsection ();
+
+                                  // read from XML
+  std::ifstream in ("parameter_handler_read_xml_error_04/prm");
+  bool result = prm.read_input_from_xml (in);
+  Assert (result == false, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+  
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_read_xml_error_04/cmp/generic b/tests/bits/parameter_handler_read_xml_error_04/cmp/generic
new file mode 100644 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK
diff --git a/tests/bits/parameter_handler_read_xml_error_04/prm b/tests/bits/parameter_handler_read_xml_error_04/prm
new file mode 100644 (file)
index 0000000..2d17ac6
--- /dev/null
@@ -0,0 +1,4 @@
+
+<?xml version="1.0" encoding="utf-8"?>
+<ParameterHandler><int1><value>2x</value><default_value>1</default_value><documentation>doc 1</documentation><pattern>0</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int1><int2><value>3</value><default_value>2</default_value><documentation>doc 2</documentation><pattern>1</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int2><ss1><double_201><value>2.234</value><default_value>1.234</default_value><documentation>doc 3</documentation><pattern>2</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_201><ss2><double_202><value>5.321</value><default_value>4.321</default_value><documentation>doc 4</documentation><pattern>3</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_202></ss2></ss1><Testing_25testing><string_26list><value>__&lt; &amp; &gt; ; /</value><default_value>&lt; &amp; &gt; ; /</default_value><documentation>docs 1</documentation><pattern>4</pattern><pattern_description>[Anything]</pattern_description></string_26list><int_2aint><value>2</value><default_value>2</default_value><documentation/>
+<pattern>5</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int_2aint><double_2bdouble><value>7.1415926</value><default_value>6.1415926</default_value><documentation>docs 3</documentation><pattern>6</pattern><pattern_description>[Double -1.79769e+308...1.79769e+308 (inclusive)]</pattern_description></double_2bdouble></Testing_25testing></ParameterHandler>

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.