XMLDOM.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "XMLDOM.h"
  2. XMLDOM::XMLDOM() : curtext(0), curtext_len(0), curNode(0)
  3. {
  4. xmlNode = new XMLNode;
  5. curNode = xmlNode;
  6. }
  7. XMLDOM::~XMLDOM()
  8. {
  9. if (curtext)
  10. {
  11. free(curtext);
  12. curtext = 0;
  13. curtext_len = 0;
  14. }
  15. delete xmlNode;
  16. }
  17. void XMLDOM::StartTag(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  18. {
  19. XMLNode *newNode = new XMLNode;
  20. int num = (int)params->getNbItems();
  21. for (int i = 0;i < num;i++)
  22. newNode->SetProperty(params->getItemName(i), params->getItemValue(i));
  23. newNode->parent = curNode;
  24. curNode->SetContent_Own(curtext);
  25. curtext = 0;
  26. curNode->AddNode(xmltag, newNode);
  27. curNode = newNode;
  28. }
  29. void XMLDOM::EndTag(const wchar_t *xmlpath, const wchar_t *xmltag)
  30. {
  31. curNode->AppendContent(curtext);
  32. if (curtext)
  33. {
  34. free(curtext);
  35. curtext = 0;
  36. curtext_len = 0;
  37. }
  38. curNode = curNode->parent;
  39. }
  40. void XMLDOM::TextHandler(const wchar_t *xmlpath, const wchar_t *xmltag, const wchar_t *str)
  41. {
  42. if (str && *str)
  43. {
  44. if (curtext)
  45. {
  46. size_t len = wcslen(str), new_len = len + curtext_len;
  47. wchar_t* newcurtext = (wchar_t *)realloc(curtext, (new_len+1)*sizeof(wchar_t));
  48. if (newcurtext)
  49. {
  50. curtext = newcurtext;
  51. wcsncpy(curtext+curtext_len, str, len);
  52. *(curtext+curtext_len+len) = 0;
  53. curtext_len = new_len;
  54. }
  55. else
  56. {
  57. newcurtext = (wchar_t *)malloc((new_len+1)*sizeof(wchar_t));
  58. if (newcurtext)
  59. {
  60. memcpy(newcurtext, curtext, curtext_len*sizeof(wchar_t));
  61. free(curtext);
  62. curtext = newcurtext;
  63. wcsncpy(curtext+curtext_len, str, len);
  64. *(curtext+curtext_len+len) = 0;
  65. curtext_len = new_len;
  66. }
  67. }
  68. }
  69. else
  70. {
  71. curtext_len = wcslen(str);
  72. curtext = (wchar_t *)malloc((curtext_len+1)*sizeof(wchar_t));
  73. if (curtext)
  74. memcpy(curtext, str, (curtext_len+1)*sizeof(wchar_t));
  75. }
  76. }
  77. }
  78. const XMLNode *XMLDOM::GetRoot() const
  79. {
  80. return xmlNode;
  81. }
  82. #define CBCLASS XMLDOM
  83. START_DISPATCH;
  84. VCB(ONSTARTELEMENT, StartTag)
  85. VCB(ONENDELEMENT, EndTag)
  86. VCB(ONCHARDATA, TextHandler)
  87. END_DISPATCH;
  88. #undef CBCLASS