11#ifndef OPENXLSX_XLXMLHELPERS_HPP
12#define OPENXLSX_XLXMLHELPERS_HPP
25#include <initializer_list>
47 inline int findStringInVector(std::string_view nodeName, std::vector<std::string_view>
const& nodeOrder)
49 for (
size_t i = 0; i < nodeOrder.size(); ++i)
50 if (nodeName == nodeOrder[i])
return static_cast<int>(i);
78 std::vector<std::string_view>
const& nodeOrder = {},
79 bool force_ns =
false)
81 if (parent.
empty() || nodeName ==
nullptr || *nodeName ==
'\0')
return XMLNode{};
84 if (nextNode.empty())
return parent.
prepend_child(nodeName, force_ns);
90 while (not nextNode.empty() and
findStringInVector(nextNode.raw_name(), nodeOrder) < nodeSortIndex)
93 if (not nextNode.empty()) {
94 if (std::string_view(nextNode.raw_name()) == std::string_view(nodeName))
103 node = parent.
child(nodeName);
115 std::string
const& nodeName,
116 std::vector<std::string_view>
const& nodeOrder = {},
117 bool force_ns =
false)
119 return ensureChild(parent, nodeName.c_str(), nodeOrder, force_ns);
129 for (
const char* name : path) {
145 if (node.
empty() || attrName ==
nullptr || *attrName ==
'\0')
return XMLAttribute{};
149 attr.
set_value(defaultVal !=
nullptr ? defaultVal :
"");
156 return ensureAttr(node, attrName.c_str(), defaultVal.c_str());
164 if (node.
empty() || attrName ==
nullptr || *attrName ==
'\0')
return XMLAttribute{};
167 attr.
set_value(attrVal !=
nullptr ? attrVal :
"");
173 if (node.
empty() || attrName ==
nullptr || *attrName ==
'\0')
return XMLAttribute{};
176 const std::string tmp(attrVal);
183 return setAttr(node, attrName, attrVal.c_str());
188 return setAttr(node, attrName.c_str(), attrVal.c_str());
195 template<
typename T,
typename = std::enable_if_t<std::is_arithmetic_v<T>>>
198 if (node.
empty() || attrName ==
nullptr || *attrName ==
'\0')
return XMLAttribute{};
208 return setAttr(node, attrName, value ?
"1" :
"0");
214 return setAttr(node, attrName, value ?
"true" :
"false");
225 const char* nodeName,
226 const char* attrName,
227 const char* attrDefaultVal,
228 std::vector<std::string_view>
const& nodeOrder = {})
230 if (parent.
empty())
return XMLAttribute{};
232 return ensureAttr(node, attrName, attrDefaultVal);
236 std::string
const& nodeName,
237 std::string
const& attrName,
238 std::string
const& attrDefaultVal,
239 std::vector<std::string_view>
const& nodeOrder = {})
241 return ensureChildAttr(parent, nodeName.c_str(), attrName.c_str(), attrDefaultVal.c_str(), nodeOrder);
248 const char* nodeName,
249 const char* attrName,
252 std::vector<std::string_view>
const& nodeOrder = {})
254 if (parent.
empty())
return XMLAttribute{};
256 if (removeAttributes) node.remove_attributes();
257 return setAttr(node, attrName, attrVal);
261 std::string
const& nodeName,
262 std::string
const& attrName,
263 std::string
const& attrVal,
265 std::vector<std::string_view>
const& nodeOrder = {})
267 return setChildAttr(parent, nodeName.c_str(), attrName.c_str(), attrVal.c_str(), removeAttributes, nodeOrder);
283 const char* childName,
285 std::vector<std::string_view>
const& nodeOrder = {})
291 const char* childName,
292 std::string_view value,
293 std::vector<std::string_view>
const& nodeOrder = {})
295 const std::string tmp(value);
296 return setChildVal(parent, childName, tmp.c_str(), nodeOrder);
303 const char* childName,
304 std::string_view text,
305 std::vector<std::string_view>
const& nodeOrder = {})
308 if (not node.empty()) {
309 const std::string tmp(text);
310 node.text().set(tmp.c_str());
321 if (parent.
empty() || tagName ==
nullptr)
return false;
323 if (tagNode.
empty())
return false;
325 if (valAttr.
empty()) {
326 setAttr(tagNode, attrName,
"true");
342 std::string
const& nodeName,
343 std::vector<std::string_view>
const& nodeOrder = {},
344 bool force_ns =
false)
346 return ensureChild(parent, nodeName.c_str(), nodeOrder, force_ns);
351 return ensureAttr(node, attrName.c_str(), attrDefaultVal.c_str());
356 return setAttr(node, attrName.c_str(), attrVal.c_str());
360 std::string
const& nodeName,
361 std::string
const& attrName,
362 std::string
const& attrDefaultVal,
363 std::vector<std::string_view>
const& nodeOrder = {})
365 return ensureChildAttr(parent, nodeName.c_str(), attrName.c_str(), attrDefaultVal.c_str(), nodeOrder);
369 std::string
const& nodeName,
370 std::string
const& attrName,
371 std::string
const& attrVal,
373 std::vector<std::string_view>
const& nodeOrder = {})
375 return setChildAttr(parent, nodeName.c_str(), attrName.c_str(), attrVal.c_str(), removeAttributes, nodeOrder);
XMLNode previous_sibling(const char *name_) const
Definition XLXmlParser.cpp:168
XMLAttribute append_attribute(const char *name_)
Definition XLXmlParser.cpp:114
XMLNode prepend_child(T type_=node_element)
Definition XLXmlParser.hpp:164
XMLNode child(const char *name_) const
Definition XLXmlParser.cpp:150
XMLNode last_child_of_type(T type_=node_element) const
Definition XLXmlParser.hpp:208
bool set_value(const char *rhs)
Definition XLXmlParser.cpp:90
XMLAttribute attribute(const char *name_) const
Definition XLXmlParser.cpp:96
XMLNode insert_child_before(T type_, const XMLNode &node)
Definition XLXmlParser.hpp:176
XMLNode first_child_of_type(T type_=node_element) const
Definition XLXmlParser.hpp:204
bool empty() const
Definition XLXmlParser.cpp:58
XMLNodeType type() const
Definition XLXmlParser.cpp:283
const char * value() const
Definition XLXmlParser.cpp:72
XMLNode insert_child_after(T type_, const XMLNode &node)
Definition XLXmlParser.hpp:172
Definition XLXmlParser.hpp:283
bool set_value(const char *rhs)
Definition XLXmlParser.cpp:533
bool as_bool(bool def=false) const
Definition XLXmlParser.cpp:617
bool empty() const
Definition XLXmlParser.cpp:513
Definition IXLCellProvider.hpp:24
XMLAttribute setAttrBoolTF(XMLNode &node, const char *attrName, bool value)
Boolean attribute as "true" / "false".
Definition XLXmlHelpers.hpp:212
void copyLeadingWhitespaces(XMLNode &parent, XMLNode fromNode, XMLNode toNode)
Copy leading pcdata (whitespace) siblings from fromNode before toNode.
Definition XLXmlHelpers.hpp:57
XMLAttribute appendAndSetNodeAttribute(XMLNode &parent, std::string const &nodeName, std::string const &attrName, std::string const &attrVal, bool removeAttributes=XLKeepAttributes, std::vector< std::string_view > const &nodeOrder={})
Definition XLXmlHelpers.hpp:368
XMLAttribute ensureChildAttr(XMLNode &parent, const char *nodeName, const char *attrName, const char *attrDefaultVal, std::vector< std::string_view > const &nodeOrder={})
Ensure child nodeName exists and return its attribute attrName (create with default).
Definition XLXmlHelpers.hpp:224
XMLNode setChildText(XMLNode &parent, const char *childName, std::string_view text, std::vector< std::string_view > const &nodeOrder={})
Ensure child exists (optional order) and set its text content.
Definition XLXmlHelpers.hpp:302
bool getBoolAttributeWhenOmittedMeansTrue(XMLNode &parent, const char *tagName, const char *attrName="val")
Bool tag with omitted @val meaning true (legacy OOXML quirk).
Definition XLXmlHelpers.hpp:319
XMLNode ensureChildPath(XMLNode &parent, std::initializer_list< const char * > path)
Ensure a chain of simple (unordered) child elements exists.
Definition XLXmlHelpers.hpp:126
constexpr XMLNodeType node_element
Definition XLXmlParser.hpp:71
XMLAttribute ensureAttr(XMLNode &node, const char *attrName, const char *defaultVal="")
Ensure attribute exists; write defaultVal only when creating.
Definition XLXmlHelpers.hpp:143
XMLAttribute appendAndGetAttribute(XMLNode &node, std::string const &attrName, std::string const &attrDefaultVal)
Definition XLXmlHelpers.hpp:349
XMLAttribute setAttr(XMLNode &node, const char *attrName, const char *attrVal)
Ensure attribute exists and always set its value (string).
Definition XLXmlHelpers.hpp:162
XMLNode ensureChild(XMLNode &parent, const char *nodeName, std::vector< std::string_view > const &nodeOrder={}, bool force_ns=false)
Ensure a child element named nodeName exists under parent and return it.
Definition XLXmlHelpers.hpp:76
XMLAttribute setAttrBool01(XMLNode &node, const char *attrName, bool value)
Excel-style boolean attribute as "1" / "0".
Definition XLXmlHelpers.hpp:206
XMLAttribute appendAndGetNodeAttribute(XMLNode &parent, std::string const &nodeName, std::string const &attrName, std::string const &attrDefaultVal, std::vector< std::string_view > const &nodeOrder={})
Definition XLXmlHelpers.hpp:359
XMLAttribute setChildVal(XMLNode &parent, const char *childName, const char *value, std::vector< std::string_view > const &nodeOrder={})
Ensure child childName exists under parent and set its @val attribute.
Definition XLXmlHelpers.hpp:282
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
constexpr bool XLRemoveAttributes
Definition XLXmlHelpers.hpp:39
XMLAttribute appendAndSetAttribute(XMLNode &node, std::string const &attrName, std::string const &attrVal)
Definition XLXmlHelpers.hpp:354
constexpr int SORT_INDEX_NOT_FOUND
Definition XLXmlHelpers.hpp:42
constexpr bool XLKeepAttributes
Definition XLXmlHelpers.hpp:40
constexpr XMLNodeType node_pcdata
Definition XLXmlParser.hpp:72
XMLAttribute setVal(XMLNode &node, const char *value)
Set the @val attribute on node.
Definition XLXmlHelpers.hpp:275
XMLNode appendAndGetNode(XMLNode &parent, std::string const &nodeName, std::vector< std::string_view > const &nodeOrder={}, bool force_ns=false)
Definition XLXmlHelpers.hpp:341
int findStringInVector(std::string_view nodeName, std::vector< std::string_view > const &nodeOrder)
Index of nodeName in nodeOrder, or SORT_INDEX_NOT_FOUND.
Definition XLXmlHelpers.hpp:47
XMLAttribute setChildAttr(XMLNode &parent, const char *nodeName, const char *attrName, const char *attrVal, bool removeAttributes=XLKeepAttributes, std::vector< std::string_view > const &nodeOrder={})
Ensure child exists, optionally clear its attributes, then set attrName.
Definition XLXmlHelpers.hpp:247