42 #ifndef INCLUDED_AI_IRRXML_WRAPPER
43 #define INCLUDED_AI_IRRXML_WRAPPER
46 #include "BaseImporter.h"
48 #include <pugixml.hpp>
60 bool operator()(pugi::xml_node node)
const {
61 return node.name() == mName;
65 template <
class TNodeType>
68 static int to_int(TNodeType &node,
const char *attribName) {
69 ai_assert(
nullptr != attribName);
70 return node.attribute(attribName).to_int();
74 using XmlNode = pugi::xml_node;
75 using XmlAttribute = pugi::xml_attribute;
77 template <
class TNodeType>
96 TNodeType *findNode(
const std::string &name) {
101 if (
nullptr == mDoc) {
106 mCurrent = mDoc->find_node(predicate);
107 if (mCurrent.empty()) {
114 bool hasNode(
const std::string &name) {
115 return nullptr != findNode(name);
119 if (
nullptr == stream) {
120 ASSIMP_LOG_DEBUG(
"Stream is nullptr.");
125 const size_t len = stream->
FileSize();
126 mData.resize(len + 1);
127 memset(&mData[0],
'\0', len + 1);
128 stream->
Read(&mData[0], 1, len);
130 mDoc =
new pugi::xml_document();
131 pugi::xml_parse_result parse_result = mDoc->load_string(&mData[0], pugi::parse_full);
132 if (parse_result.status == pugi::status_ok) {
133 ASSIMP_LOG_DEBUG(
"Error while parse xml.");
140 pugi::xml_document *getDocument()
const {
144 const TNodeType getRootNode()
const {
148 TNodeType getRootNode() {
152 static inline bool hasNode(XmlNode &node,
const char *name) {
154 return !child.empty();
157 static inline bool hasAttribute(XmlNode &xmlNode,
const char *name) {
158 pugi::xml_attribute attr = xmlNode.attribute(name);
159 return !attr.empty();
162 static inline bool getUIntAttribute(XmlNode &xmlNode,
const char *name,
unsigned int &val) {
163 pugi::xml_attribute attr = xmlNode.attribute(name);
168 val = attr.as_uint();
172 static inline bool getIntAttribute(XmlNode &xmlNode,
const char *name,
int &val ) {
173 pugi::xml_attribute attr = xmlNode.attribute(name);
182 static inline bool getFloatAttribute(XmlNode &xmlNode,
const char *name,
float &val ) {
183 pugi::xml_attribute attr = xmlNode.attribute(name);
188 val = attr.as_float();
193 static inline bool getDoubleAttribute( XmlNode &xmlNode,
const char *name,
double &val ) {
194 pugi::xml_attribute attr = xmlNode.attribute(name);
199 val = attr.as_double();
203 static inline bool getStdStrAttribute(XmlNode &xmlNode,
const char *name, std::string &val) {
204 pugi::xml_attribute attr = xmlNode.attribute(name);
209 val = attr.as_string();
213 static inline bool getBoolAttribute( XmlNode &xmlNode,
const char *name,
bool &val ) {
214 pugi::xml_attribute attr = xmlNode.attribute(name);
219 val = attr.as_bool();
224 static inline bool getValueAsString( XmlNode &node, std::string &text ) {
230 text = node.text().as_string();
235 static inline bool getValueAsFloat( XmlNode &node, ai_real &v ) {
240 v = node.text().as_float();
247 pugi::xml_document *mDoc;
249 std::vector<char> mData;
263 void collectChildrenPreOrder( XmlNode &node ) {
265 if (node != mParent && node.type() == pugi::node_element) {
266 mNodes.push_back(node);
268 for (XmlNode currentNode : node.children()) {
269 collectChildrenPreOrder(currentNode);
273 void collectChildrenPostOrder(XmlNode &node) {
274 for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
275 collectChildrenPostOrder(currentNode);
277 if (node != mParent) {
278 mNodes.push_back(node);
282 bool getNext(XmlNode &next) {
283 if (mIndex == mNodes.size()) {
287 next = mNodes[mIndex];
293 size_t size()
const {
294 return mNodes.size();
297 bool isEmpty()
const {
298 return mNodes.empty();
302 if (mNodes.empty()) {
312 std::vector<XmlNode> mNodes;
318 #endif // !! INCLUDED_AI_IRRXML_WRAPPER