www.mooseframework.org
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
JsonInputFileFormatter Class Reference

This class produces produces a dump of the InputFileParameters that appears like the normal input file syntax. More...

#include <JsonInputFileFormatter.h>

Public Member Functions

 JsonInputFileFormatter ()
 
std::string toString (const moosecontrib::Json::Value &root)
 Returns a string representation of the tree in input file format. More...
 

Protected Member Functions

void addLine (const std::string &line, size_t max_line_len=0, const std::string &comment="")
 Adds a line to the output. More...
 
void addBlock (const std::string &name, const moosecontrib::Json::Value &block, bool top=false)
 Adds a new block to the output. More...
 
void addParameters (const moosecontrib::Json::Value &params)
 Add a comment to the block. More...
 
void addTypes (const std::string &key, const moosecontrib::Json::Value &block)
 Add a dictionary of type blocks to the output. More...
 

Protected Attributes

const int _spaces
 
int _level
 
std::ostringstream _stream
 

Detailed Description

This class produces produces a dump of the InputFileParameters that appears like the normal input file syntax.

Different from InputFileFormatter in that it takes its input from JsonSyntaxTree.

Definition at line 25 of file JsonInputFileFormatter.h.

Constructor & Destructor Documentation

JsonInputFileFormatter::JsonInputFileFormatter ( )

Member Function Documentation

void JsonInputFileFormatter::addBlock ( const std::string &  name,
const moosecontrib::Json::Value &  block,
bool  top = false 
)
protected

Adds a new block to the output.

Parameters
nameName of the block.
blockJson holding data for the block.
topWhether this is a top level block.

Definition at line 75 of file JsonInputFileFormatter.C.

Referenced by addTypes(), and toString().

78 {
79  addLine("");
80  if (toplevel)
81  addLine("[" + name + "]");
82  else
83  addLine("[./" + name + "]");
84 
85  _level++;
86  if (block.isMember("description") && !block["description"].asString().empty())
87  addLine("", 0, block["description"].asString());
88 
89  if (block.isMember("parameters"))
90  addParameters(block["parameters"]);
91 
92  if (block.isMember("actions"))
93  {
94  // there could be duplicate parameters across actions, last one wins
95  moosecontrib::Json::Value all_params;
96  auto & actions = block["actions"];
97  for (auto && name : actions.getMemberNames())
98  {
99  auto & params = actions[name]["parameters"];
100  for (auto && param_name : params.getMemberNames())
101  all_params[param_name] = params[param_name];
102  }
103  addParameters(all_params);
104  }
105 
106  if (block.isMember("star"))
107  addBlock("*", block["star"]);
108 
109  addTypes("subblock_types", block);
110  addTypes("types", block);
111 
112  if (block.isMember("subblocks"))
113  {
114  auto & subblocks = block["subblocks"];
115  if (!subblocks.isNull())
116  for (auto && name : subblocks.getMemberNames())
117  addBlock(name, subblocks[name]);
118  }
119 
120  _level--;
121  if (toplevel)
122  addLine("[]");
123  else
124  addLine("[../]");
125 }
void addLine(const std::string &line, size_t max_line_len=0, const std::string &comment="")
Adds a line to the output.
void addBlock(const std::string &name, const moosecontrib::Json::Value &block, bool top=false)
Adds a new block to the output.
void addParameters(const moosecontrib::Json::Value &params)
Add a comment to the block.
void addTypes(const std::string &key, const moosecontrib::Json::Value &block)
Add a dictionary of type blocks to the output.
void JsonInputFileFormatter::addLine ( const std::string &  line,
size_t  max_line_len = 0,
const std::string &  comment = "" 
)
protected

Adds a line to the output.

This will put in the proper indentation automatically.

Parameters
lineThe line to add.
max_line_lenUsed to determine where to start inline comments. comment Comment to add to the line. It will automatically be broken up over multiple lines if too long.

Definition at line 33 of file JsonInputFileFormatter.C.

Referenced by addBlock(), addParameters(), and addTypes().

36 {
37  if (line.empty() && comment.empty())
38  {
39  _stream << "\n";
40  return;
41  }
42 
43  std::string indent(_level * _spaces, ' ');
44  auto doc = MooseUtils::trim(comment);
45  if (doc.empty()) // Not comment so just print out the line normally
46  {
47  _stream << indent << line << "\n";
48  return;
49  }
50 
51  // We have a comment so we need to break it up over multiple lines if necessary
52  // and make sure that they all start at the same spot.
53  _stream << indent << line;
54  std::vector<std::string> elements;
55 
56  // if the line is empty we can just start the comment right away
57  int extra = 1;
58  if (line.empty())
59  extra = 0;
60 
61  // be careful of really long lines.
62  int len = 100 - max_line_len - indent.size();
63  if (len < 0)
64  len = 20;
65 
66  MooseUtils::tokenize(doc, elements, len, " \t");
67  std::string first(max_line_len - line.size() + extra, ' ');
68  _stream << first << "# " << elements[0] << "\n";
69  std::string cindent(max_line_len + indent.size() + extra, ' ');
70  for (size_t i = 1; i < elements.size(); ++i)
71  _stream << cindent << "# " << elements[i] << "\n";
72 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:34
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
Definition: MooseUtils.h:350
std::string trim(const std::string &str, const std::string &white_space=" \t\n\v\f\r")
Standard scripting language trim function.
Definition: MooseUtils.C:95
void JsonInputFileFormatter::addParameters ( const moosecontrib::Json::Value &  params)
protected

Add a comment to the block.

It will add the proper indentation and #.

Parameters
commentThe comment to add.

Definition at line 146 of file JsonInputFileFormatter.C.

Referenced by addBlock().

147 {
148  size_t max_name = 0;
149  for (auto && name : params.getMemberNames())
150  if (name.size() > max_name)
151  max_name = name.size();
152 
153  size_t max_len = 0;
154  std::map<std::string, std::string> lines;
155  for (auto && name : params.getMemberNames())
156  {
157  auto & param = params[name];
158  auto def = MooseUtils::trim(param["default"].asString());
159  if (def.find(' ') != std::string::npos)
160  def = "'" + def + "'";
161  std::string indent(max_name - name.size(), ' ');
162  std::string required;
163  if (param["required"].asBool())
164  required = "(required)";
165  std::string l = name + indent + " = " + def + required;
166  if (l.size() > max_len)
167  max_len = l.size();
168  lines[name] = l;
169  }
170  for (auto && name : params.getMemberNames())
171  {
172  auto & param = params[name];
173  auto & l = lines[name];
174  auto desc = param["description"].asString();
175  addLine(l, max_len, desc);
176 
177  auto group = param["group_name"].asString();
178  if (!group.empty())
179  addLine("", max_len + 1, "Group: " + group); // a +1 to account for an empty line
180  }
181 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:34
void addLine(const std::string &line, size_t max_line_len=0, const std::string &comment="")
Adds a line to the output.
std::string trim(const std::string &str, const std::string &white_space=" \t\n\v\f\r")
Standard scripting language trim function.
Definition: MooseUtils.C:95
void JsonInputFileFormatter::addTypes ( const std::string &  key,
const moosecontrib::Json::Value &  block 
)
protected

Add a dictionary of type blocks to the output.

Parameters
keyThis will be used to get the dictionary of types from block.
blockThe Json data that is the parent of the types data.

Definition at line 128 of file JsonInputFileFormatter.C.

Referenced by addBlock().

129 {
130  if (!block.isMember(key))
131  return;
132  auto & types = block[key];
133  if (types.isNull())
134  return;
135 
136  addLine("");
137  addLine("[./<types>]");
138  _level++;
139  for (auto && name : types.getMemberNames())
140  addBlock("<" + name + ">", types[name]);
141  _level--;
142  addLine("[../]");
143 }
void addLine(const std::string &line, size_t max_line_len=0, const std::string &comment="")
Adds a line to the output.
void addBlock(const std::string &name, const moosecontrib::Json::Value &block, bool top=false)
Adds a new block to the output.
std::string JsonInputFileFormatter::toString ( const moosecontrib::Json::Value &  root)

Returns a string representation of the tree in input file format.

Parameters
rootThe root node of the tree to output.

Definition at line 23 of file JsonInputFileFormatter.C.

Referenced by MooseApp::setupOptions().

24 {
25  _stream.clear();
26  _stream.str("");
27  for (auto && name : root["blocks"].getMemberNames())
28  addBlock(name, root["blocks"][name], true);
29  return _stream.str();
30 }
void addBlock(const std::string &name, const moosecontrib::Json::Value &block, bool top=false)
Adds a new block to the output.

Member Data Documentation

int JsonInputFileFormatter::_level
protected

Definition at line 69 of file JsonInputFileFormatter.h.

Referenced by addBlock(), addLine(), and addTypes().

const int JsonInputFileFormatter::_spaces
protected

Definition at line 68 of file JsonInputFileFormatter.h.

Referenced by addLine().

std::ostringstream JsonInputFileFormatter::_stream
protected

Definition at line 70 of file JsonInputFileFormatter.h.

Referenced by addLine(), and toString().


The documentation for this class was generated from the following files: