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-xerces.cpp (3351B)


  1. #ifdef USE_XERCES
  2. #include "bulletmlparser.h"
  3. #include "bulletmlparser-xerces.h"
  4. #include "bulletmlerror.h"
  5. #include "bulletmltree.h"
  6. #include <xercesc/sax/HandlerBase.hpp>
  7. #include <xercesc/sax/AttributeList.hpp>
  8. #include <xercesc/parsers/SAXParser.hpp>
  9. #include <xercesc/util/PlatformUtils.hpp>
  10. #include <string>
  11. #include <memory>
  12. #include <sstream>
  13. class BulletMLParserXercesSAXHandler : public HandlerBase {
  14. public:
  15. BulletMLParserXercesSAXHandler(BulletMLParserXerces* parser);
  16. virtual void characters(const XMLCh* const chars,
  17. const unsigned int length);
  18. virtual void startElement(const XMLCh* const name,
  19. AttributeList& attributes);
  20. virtual void endElement(const XMLCh* const name);
  21. virtual void error(const SAXParseException& e) {
  22. handleError(e);
  23. }
  24. virtual void fatalError(const SAXParseException& e) {
  25. handleError(e);
  26. }
  27. virtual void warning(const SAXParseException& e) {
  28. handleError(e);
  29. }
  30. private:
  31. void handleError(const SAXParseException& e) {
  32. std::ostringstream oss;
  33. oss << "Error at file " << parser_->uc2string(e.getSystemId())
  34. << ", line " << e.getLineNumber()
  35. << ", char " << e.getColumnNumber()
  36. << "\nMessage: " << parser_->uc2string(e.getMessage()) << endl;
  37. throw BulletMLError(oss.str());
  38. }
  39. private:
  40. BulletMLParserXerces* parser_;
  41. BulletMLNode* curNode_;
  42. };
  43. BulletMLParserXercesSAXHandler::BulletMLParserXercesSAXHandler(
  44. BulletMLParserXerces* parser)
  45. : parser_(parser), curNode_(0)
  46. {}
  47. void BulletMLParserXercesSAXHandler::characters(const XMLCh* const chars,
  48. const unsigned int length)
  49. {
  50. curNode_->setValue(parser_->uc2string(chars, length));
  51. }
  52. void BulletMLParserXercesSAXHandler::startElement(const XMLCh* const name,
  53. AttributeList& attributes)
  54. {
  55. BulletMLNode* node = parser_->addContent(parser_->uc2string(name));
  56. if (node->getName() == BulletMLNode::bulletml) {
  57. for (unsigned int i = 0; i < attributes.getLength(); i++) {
  58. if (parser_->uc2string(attributes.getName(i)) == "type" &&
  59. parser_->uc2string(attributes.getValue(i)) == "horizontal")
  60. {
  61. parser_->setHorizontal();
  62. }
  63. }
  64. }
  65. else {
  66. BulletMLParserXerces::MyAttributes mattr;
  67. for (unsigned int i = 0; i < attributes.getLength(); i++) {
  68. mattr.push_back(parser_->uc2string(attributes.getName(i)));
  69. mattr.push_back(parser_->uc2string(attributes.getValue(i)));
  70. }
  71. parser_->addAttribute(mattr, node);
  72. }
  73. if (curNode_ != 0) curNode_->addChild(node);
  74. curNode_ = node;
  75. }
  76. void BulletMLParserXercesSAXHandler::endElement(const XMLCh* const) {
  77. curNode_ = curNode_->getParent();
  78. }
  79. BulletMLParserXerces::BulletMLParserXerces(const char* filename)
  80. {
  81. setName(filename);
  82. }
  83. BulletMLParserXerces::~BulletMLParserXerces() {}
  84. void BulletMLParserXerces::parse() {
  85. try {
  86. XMLPlatformUtils::Initialize();
  87. std::auto_ptr<SAXParser> parser(new SAXParser);
  88. parser->setValidationScheme(SAXParser::Val_Always);
  89. parser->setDoNamespaces(false);
  90. parser->setDoSchema(false);
  91. parser->setValidationSchemaFullChecking(false);
  92. BulletMLParserXercesSAXHandler handler(this);
  93. parser->setDocumentHandler(&handler);
  94. parser->setErrorHandler(&handler);
  95. parser->parse(name_);
  96. parser.reset(0);
  97. }
  98. catch (const XMLException& toCatch) {
  99. throw BulletMLError(uc2string(toCatch.getMessage()));
  100. }
  101. XMLPlatformUtils::Terminate();
  102. }
  103. #endif // USE_XERCES