logo

libbulletml

Library of Bullet Markup Language (forked from https://shinh.skr.jp/libbulletml/index_en.html )git clone https://hacktivis.me/git/libbulletml.git

bulletmlparser-tinyxml.cpp (1804B)


  1. #include "bulletmlparser.h"
  2. #include "bulletmlparser-tinyxml.h"
  3. #include "bulletmlerror.h"
  4. #include "bulletmltree.h"
  5. #include <string>
  6. BulletMLParserTinyXML::BulletMLParserTinyXML(const char* filename)
  7. : curNode_(0)
  8. {
  9. setName(filename);
  10. }
  11. BulletMLParserTinyXML::~BulletMLParserTinyXML() {}
  12. void BulletMLParserTinyXML::getTree(TiXmlNode* node) {
  13. if (node->ToComment() != 0) return;
  14. translateNode(node);
  15. TiXmlNode* child;
  16. for (child = node->FirstChild(); child; child = child->NextSibling()) {
  17. TiXmlText* text;
  18. if ((text = child->ToText()) != 0) {
  19. curNode_->setValue(text->Value());
  20. break;
  21. }
  22. getTree(child);
  23. }
  24. curNode_ = curNode_->getParent();
  25. }
  26. void BulletMLParserTinyXML::translateNode(TiXmlNode* node) {
  27. TiXmlElement* elem = node->ToElement();
  28. assert(elem != 0);
  29. BulletMLNode* xmlNode = addContent(elem->Value());
  30. if (xmlNode->getName() == BulletMLNode::bulletml) {
  31. TiXmlAttribute* attr;
  32. for (attr = elem->FirstAttribute(); attr; attr = attr->Next()) {
  33. if (attr->Value() == "horizontal") setHorizontal();
  34. }
  35. }
  36. else {
  37. MyAttributes mattr;
  38. TiXmlAttribute* attr;
  39. for (attr = elem->FirstAttribute(); attr; attr = attr->Next()) {
  40. mattr.push_back(attr->Name());
  41. mattr.push_back(attr->Value());
  42. }
  43. addAttribute(mattr, xmlNode);
  44. if (curNode_ != 0) curNode_->addChild(xmlNode);
  45. }
  46. curNode_ = xmlNode;
  47. }
  48. void BulletMLParserTinyXML::parseImpl(TiXmlDocument& doc) {
  49. if (doc.Error()) {
  50. throw BulletMLError(doc.Value() + ": " + doc.ErrorDesc());
  51. }
  52. TiXmlNode* node;
  53. for (node = doc.FirstChild(); node; node = node->NextSibling()) {
  54. if (node->ToElement() != 0) {
  55. getTree(node);
  56. break;
  57. }
  58. }
  59. }
  60. void BulletMLParserTinyXML::parse() {
  61. TiXmlDocument doc(name_);
  62. doc.LoadFile();
  63. parseImpl(doc);
  64. }