]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
added a new flag to ParameterHandler::print_parameters_section for printing all highe...
authorsteigemann <steigemann@0785d39b-7218-0410-832d-ea1e28bc413d>
Sat, 1 Feb 2014 07:57:30 +0000 (07:57 +0000)
committersteigemann <steigemann@0785d39b-7218-0410-832d-ea1e28bc413d>
Sat, 1 Feb 2014 07:57:30 +0000 (07:57 +0000)
git-svn-id: https://svn.dealii.org/trunk@32363 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/include/deal.II/base/parameter_handler.h
deal.II/source/base/parameter_handler.cc

index f935735aa875e4c925c92229ccbd7ffd06e2a00c..73d5cb20e9a546ab3dd2d592d057d52c347633b1 100644 (file)
@@ -102,6 +102,17 @@ inconvenience this causes.
 <h3>Specific improvements</h3>
 
 <ol>
+  <li>New/fixed: The ParameterHandler::print_parameters_section
+  method not worked for XML output. There is now a flag
+  include_top_level_elements which prints all higher
+  subsection elements, default is false.
+  For XML output setting this flag to true is required
+  to ensure that the output is a valid XML document,
+  starting with one root element ParameterHandler and
+  compatible with read_input_from_xml and the parameterGUI.
+  <br>
+  (Martin Steigemann, 2014/02/01)
+
   <li>New: There is now a method to copy the content from a
   PETScWrappers::MPI::Vector and TrilinosWrappers::MPI::Vector to
   deal.II's parallel distributed vector.
index 2e1ce3b072c385d4c6b5cc4f6a4e722d92cbdd36..a45d621ef1443b6161b655b14da4e283810e5d96 100644 (file)
@@ -1769,7 +1769,7 @@ public:
 
   /**
    * Print all parameters with the given style to <tt>out</tt>. Presently
-   * only <tt>Text</tt> and <tt>LaTeX</tt> are implemented.
+   * only <tt>Text</tt>, <tt>LaTeX</tt> and <tt>XML</tt> are implemented.
    *
    * In <tt>Text</tt> format, the output is formatted in such a way that it
    * is possible to use it for later input again. This is most useful to
@@ -1782,6 +1782,10 @@ public:
    * well as the documenting string given to the declare_entry() function
    * if available.
    *
+   * In <tt>XML</tt> format, the output starts with one root element
+   * <tt>ParameterHandler</tt> in order to get a valid XML document
+   * and all subsections under it.
+   * 
    * In <tt>Text</tt> format, the output contains the same information but
    * in a format so that the resulting file can be input into a latex
    * document such as a manual for the code for which this object handles
@@ -1817,12 +1821,18 @@ public:
    * by entering and leaving subsections through the enter_subsection() and
    * leave_subsection() functions.
    *
+   * If <tt>include_top_level_elements</tt> is <tt>true</tt>, also
+   * the higher subsection elements are printed. In <tt>XML</tt> format
+   * this is required to get a valid XML document and output starts
+   * with one root element <tt>ParameterHandler</tt>.
+   * 
    * In most cases, you will not want to use this function directly, but
    * have it called recursively by the previous function.
    */
   void print_parameters_section (std::ostream       &out,
                                  const OutputStyle   style,
-                                 const unsigned int  indent_level);
+                                 const unsigned int  indent_level,
+                                 const bool          include_top_level_elements = false);
 
   /**
    * Print parameters to a logstream. This function allows to print all
index 8742ec4c087bc883a29187cd35b48fe397335998..f448fa2b50674dab99d9e98f7f85324d2b78606b 100644 (file)
@@ -1858,18 +1858,82 @@ ParameterHandler::print_parameters (std::ostream     &out,
 void
 ParameterHandler::print_parameters_section (std::ostream      &out,
                                             const OutputStyle  style,
-                                            const unsigned int indent_level)
+                                            const unsigned int indent_level,
+                                            const bool         include_top_level_elements)
 {
   AssertThrow (out, ExcIO());
 
   const boost::property_tree::ptree &current_section
     = entries->get_child (get_current_path());
 
+  unsigned int overall_indent_level = indent_level;
+
   switch (style)
     {
+    case XML:
+    {
+      if (include_top_level_elements)
+        {
+          // call the writer
+          // function and exit as
+          // there is nothing
+          // further to do down in
+          // this function
+          //
+          // XML has a requirement that
+          // there can only be one
+          // single top-level entry,
+          // but a section has multiple
+          // entries and sections. we
+          // work around this by
+          // creating a tree just for
+          // this purpose with the
+          // single top-level node
+          // "ParameterHandler" and
+          // assign the full path of
+          // down to the current section
+          // under it
+          boost::property_tree::ptree single_node_tree;
+
+          // if there is no subsection selected,
+          // add the whole tree of entries,
+          // otherwise add a root element
+          // and the selected subsection under it
+          if (subsection_path.size() == 0)
+            {
+              single_node_tree.add_child("ParameterHandler",
+                                         *entries);
+            }
+          else
+            {
+              std::string  path ("ParameterHandler");
+
+              single_node_tree.add_child(path,
+                                         boost::property_tree::ptree());
+
+              path += path_separator + get_current_path ();
+              single_node_tree.add_child (path, current_section);
+            };
+
+          write_xml (out, single_node_tree);
+        }
+      else
+        Assert (false, ExcNotImplemented());
+
+      break;
+    }
     case Text:
     case ShortText:
     {
+      // if there are top level elements to print, do it
+      if (include_top_level_elements && (subsection_path.size() > 0))
+        for (unsigned int i=0; i<subsection_path.size(); ++i)
+          {
+            out << std::setw(overall_indent_level*2) << ""
+                << "subsection " << demangle (subsection_path[i]) << std::endl;
+            overall_indent_level += 1;
+          };
+
       // first find out the longest
       // entry name to be able to
       // align the equal signs
@@ -1935,10 +1999,10 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
                 const std::vector<std::string> doc_lines
                   = Utilities::
                     break_text_into_lines (p->second.get<std::string>("documentation"),
-                                           78 - indent_level*2 - 2);
+                                           78 - overall_indent_level*2 - 2);
 
                 for (unsigned int i=0; i<doc_lines.size(); ++i)
-                  out << std::setw(indent_level*2) << ""
+                  out << std::setw(overall_indent_level*2) << ""
                       << "# "
                       << doc_lines[i]
                       << std::endl;
@@ -1948,7 +2012,7 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
 
             // print name and value
             // of this entry
-            out << std::setw(indent_level*2) << ""
+            out << std::setw(overall_indent_level*2) << ""
                 << "set "
                 << demangle(p->first)
                 << std::setw(longest_name-demangle(p->first).length()+1) << " "
@@ -2043,6 +2107,15 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
 
     case Description:
     {
+      // if there are top level elements to print, do it
+      if (include_top_level_elements && (subsection_path.size() > 0))
+        for (unsigned int i=0; i<subsection_path.size(); ++i)
+          {
+            out << std::setw(overall_indent_level*2) << ""
+                << "subsection " << demangle (subsection_path[i]) << std::endl;
+            overall_indent_level += 1;
+          };
+
       // first find out the longest
       // entry name to be able to
       // align the equal signs
@@ -2066,7 +2139,7 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
             const std::string value = p->second.get<std::string>("value");
 
             // print name and value
-            out << std::setw(indent_level*2) << ""
+            out << std::setw(overall_indent_level*2) << ""
                 << "set "
                 << demangle(p->first)
                 << std::setw(longest_name-demangle(p->first).length()+1) << " "
@@ -2076,12 +2149,12 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
             const std::vector<std::string> description_str
               = Utilities::break_text_into_lines (p->second.get<std::string>
                                                   ("pattern_description"),
-                                                  78 - indent_level*2 - 2, '|');
+                                                  78 - overall_indent_level*2 - 2, '|');
             if (description_str.size() > 1)
               {
                 out << std::endl;
                 for (unsigned int i=0; i<description_str.size(); ++i)
-                  out << std::setw(indent_level*2+6) << ""
+                  out << std::setw(overall_indent_level*2+6) << ""
                       << description_str[i] << std::endl;
               }
             else if (description_str.empty() == false)
@@ -2093,7 +2166,7 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
             // documenting string,
             // print it as well
             if (p->second.get<std::string>("documentation").length() != 0)
-              out << std::setw(indent_level*2 + longest_name + 10) << ""
+              out << std::setw(overall_indent_level*2 + longest_name + 10) << ""
                   << "(" << p->second.get<std::string>("documentation") << ")" << std::endl;
           }
 
@@ -2109,6 +2182,7 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
   // sections to come, put two newlines
   // between the last entry and the first
   // subsection
+  if (style != XML)
   {
     unsigned int n_parameters = 0;
     unsigned int n_sections   = 0;
@@ -2128,7 +2202,6 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
         &&
         (n_sections != 0))
       out << std::endl << std::endl;
-  }
 
   // now traverse subsections tree,
   // in alphabetical order
@@ -2143,7 +2216,7 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
           case Text:
           case Description:
           case ShortText:
-            out << std::setw(indent_level*2) << ""
+                out << std::setw(overall_indent_level*2) << ""
                 << "subsection " << demangle(p->first) << std::endl;
             break;
           case LaTeX:
@@ -2177,7 +2250,7 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
         // then the contents of the
         // subsection
         enter_subsection (demangle(p->first));
-        print_parameters_section (out, style, indent_level+1);
+            print_parameters_section (out, style, overall_indent_level+1);
         leave_subsection ();
         switch (style)
           {
@@ -2186,14 +2259,14 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
             // subsection. one
             // blank line after
             // each subsection
-            out << std::setw(indent_level*2) << ""
+                out << std::setw(overall_indent_level*2) << ""
                 << "end" << std::endl
                 << std::endl;
 
             // if this is a toplevel
             // subsection, then have two
             // newlines
-            if (indent_level == 0)
+                if (overall_indent_level == 0)
               out << std::endl;
 
             break;
@@ -2202,7 +2275,7 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
           case ShortText:
             // write end of
             // subsection.
-            out << std::setw(indent_level*2) << ""
+                out << std::setw(overall_indent_level*2) << ""
                 << "end" << std::endl;
             break;
           case LaTeX:
@@ -2213,6 +2286,33 @@ ParameterHandler::print_parameters_section (std::ostream      &out,
       }
 }
 
+  // close top level elements, if there are any
+  switch (style)
+    {
+    case XML:
+    case LaTeX:
+    case Description:
+      break;
+    case Text:
+    case ShortText:
+    {
+      if (include_top_level_elements && (subsection_path.size() > 0))
+        for (unsigned int i=0; i<subsection_path.size(); ++i)
+          {
+            overall_indent_level -= 1;
+            out << std::setw(overall_indent_level*2) << ""
+                << "end" << std::endl;
+          };
+
+      break;
+    }
+
+    default:
+      Assert (false, ExcNotImplemented());
+    }
+
+}
+
 
 
 void

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.