btool
A parser/converter/transpiler for .bib files
TranslationTable.cpp
Go to the documentation of this file.
1 #include <TranslationTable.hpp>
2 #include <boost/property_tree/json_parser.hpp>
3 #include <boost/optional/optional.hpp>
4 #include <boost/filesystem.hpp>
5 #include <algorithm>
6 #include <iterator>
7 
13 TranslationTable::TranslationTable(std::optional<std::stringstream> file) {
14  if (file.has_value()) {
15  boost::property_tree::read_json(file.value(), contents);
17  } else {
18  contents = {};
19  styleProperties = {};
20  }
21 }
22 
29 TranslationTable::TranslationTable(const std::optional<boost::filesystem::path> &path) {
30  if (path.has_value()) {
31  if (!boost::filesystem::exists(path.value())) {
32  throw std::invalid_argument(
33  "No such file. [translationTablePath=" + path.value().string() + "]"
34  );
35  } else if (!boost::filesystem::is_regular_file(path.value())) {
36  throw std::invalid_argument(
37  "Translation-Table is not a file. [translationTablePath=" + path.value().string() + "]"
38  );
39  } else {
40  boost::property_tree::read_json(path.value().string(), contents);
42  }
43  } else {
44  contents = {};
45  styleProperties = {};
46  }
47 }
48 
53 auto TranslationTable::printAll(std::ostream &out) const -> void {
54  boost::property_tree::json_parser::write_json(out, contents);
55 }
56 
63  const boost::property_tree::ptree &style
64 ) noexcept -> StyleProperties {
65  const auto parseConstraintNode = [](const boost::property_tree::ptree &node) {
66  std::unordered_set<std::string> fields;
67  std::for_each(std::cbegin(node),
68  std::cend(node),
69  [&fields](const boost::property_tree::ptree::value_type &field) {
70  const std::string title = field.second.data();
71  if (!title.empty()) {
72  fields.insert(title);
73  }
74  });
75  return fields;
76  };
77 
78  const boost::optional<const boost::property_tree::ptree &> name =
79  style.get_child_optional("name");
80  const boost::optional<const boost::property_tree::ptree &> requiredFieldsNode =
81  style.get_child_optional("requiredFields");
82  const boost::optional<const boost::property_tree::ptree &> optionalFieldsNode =
83  style.get_child_optional("optionalFields");
84 
85  return {name ? std::string{name.value().data()} : "",
86  requiredFieldsNode ? parseConstraintNode(requiredFieldsNode.value())
87  : std::unordered_set<std::string>{},
88  optionalFieldsNode ? parseConstraintNode(optionalFieldsNode.value())
89  : std::unordered_set<std::string>{}};
90 }
91 
97 auto TranslationTable::parseStyles() const -> std::vector<StyleProperties> {
98  std::vector<StyleProperties> props;
99  const boost::optional<const boost::property_tree::ptree &> styles =
100  contents.get_child_optional("styles");
101  if (styles) {
102  std::transform(std::cbegin(styles.value()),
103  std::cend(styles.value()),
104  std::back_inserter(props),
105  [this](const boost::property_tree::ptree::value_type &style) {
106  return this->parseStyle(style.second);
107  });
108  } else {
109  throw std::logic_error("Property \"styles\" not found in provided Translation-Table.");
110  }
111  return props;
112 }
113 
118 auto TranslationTable::getStyleProperties() const noexcept -> const std::vector<StyleProperties> & {
119  return styleProperties;
120 }
121 
128  const std::string &name
129 ) const noexcept -> std::optional<StyleProperties> {
130  const auto propItr = std::find_if(std::cbegin(styleProperties), std::cend(styleProperties),
131  [&name](const StyleProperties &prop) { return prop.name == name; });
132  return (propItr != std::cend(styleProperties)) ? std::optional(*propItr) : std::nullopt;
133 }
134 
142  const std::vector<std::string> &names
143 ) const noexcept -> std::vector<StyleProperties> {
144  if (names.empty()) return getStyleProperties();
145  std::vector<StyleProperties> result;
146  for (const auto &style : names) {
147  const auto props = stylePropertiesOf(style);
148  if (props) result.push_back(props.value());
149  }
150  return result;
151 }
StyleProperties
style-properties-container
Definition: StyleProperties.hpp:11
StyleProperties::name
std::string name
name of the style
Definition: StyleProperties.hpp:12
TranslationTable::stylePropertiesOf
auto stylePropertiesOf(const std::string &name) const noexcept -> std::optional< StyleProperties >
Definition: TranslationTable.cpp:127
TranslationTable::parseStyles
auto parseStyles() const -> std::vector< StyleProperties >
Definition: TranslationTable.cpp:97
name
char const *const name
Definition: HtmlGenerator.hpp:53
TranslationTable::TranslationTable
TranslationTable(std::optional< std::stringstream > file)
Definition: TranslationTable.cpp:13
TranslationTable::parseStyle
static auto parseStyle(const boost::property_tree::ptree &style) noexcept -> StyleProperties
Definition: TranslationTable.cpp:62
TranslationTable::getStyleProperties
auto getStyleProperties() const noexcept -> const std::vector< StyleProperties > &
Definition: TranslationTable.cpp:118
TranslationTable::styleProperties
std::vector< StyleProperties > styleProperties
parsed style-properties
Definition: TranslationTable.hpp:18
TranslationTable::contents
boost::property_tree::ptree contents
pointer to parsed json-tree
Definition: TranslationTable.hpp:17
TranslationTable.hpp
TranslationTable::printAll
auto printAll(std::ostream &out=std::cout) const -> void
Definition: TranslationTable.cpp:53