xmlreader.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <precomp.h>
  2. #include "xmlreader.h"
  3. #include <bfc/parse/pathparse.h>
  4. #include <bfc/file/wildcharsenum.h>
  5. #include <bfc/string/url.h>
  6. #include <api/service/svcs/svc_xmlprov.h>
  7. #ifdef WASABI_COMPILE_WNDMGR
  8. #include <api/util/varmgr.h>
  9. #endif
  10. #include <api/xml/xmlparamsi.h>
  11. #include <api/xml/XMLAutoInclude.h>
  12. #include "../nu/regexp.h"
  13. void LoadXmlFile(obj_xml *parser, const wchar_t *filename);
  14. void XmlReader::registerCallback(const wchar_t *matchstr, XmlReaderCallbackI *callback)
  15. {
  16. if (matchstr == NULL || callback == NULL) return ;
  17. xmlreader_cb_struct *s = new xmlreader_cb_struct(matchstr, TYPE_CLASS_CALLBACK, callback);
  18. callback_list.addItem(s, 0); // mig: add to FRONT of list... so we don't step on hierarchical toes.
  19. }
  20. void XmlReader::registerCallback(const wchar_t *matchstr, void (* callback)(int, const wchar_t *, skin_xmlreaderparams *))
  21. {
  22. if (matchstr == NULL || callback == NULL) return ;
  23. xmlreader_cb_struct *s = new xmlreader_cb_struct(matchstr, TYPE_STATIC_CALLBACK, (XmlReaderCallbackI *)callback);
  24. callback_list.addItem(s, 0); // mig: add to FRONT of list... so we don't step on hierarchical toes.
  25. }
  26. void XmlReader::unregisterCallback(void *callback)
  27. {
  28. for (int i = 0;i < callback_list.getNumItems();i++)
  29. {
  30. if (callback_list[i]->callback == callback)
  31. {
  32. delete callback_list[i];
  33. callback_list.delByPos(i);
  34. i--;
  35. }
  36. }
  37. }
  38. int XmlReader::loadFile(const wchar_t *filename, const wchar_t *incpath, int isinclude)
  39. {
  40. includePath=incpath;
  41. waServiceFactory *parserFactory = WASABI_API_SVC->service_getServiceByGuid(obj_xmlGUID);
  42. if (parserFactory)
  43. {
  44. obj_xml *parser = (obj_xml *)parserFactory->getInterface();
  45. if (parser)
  46. {
  47. includer = new XMLAutoInclude(parser, incpath);
  48. parser->xmlreader_registerCallback(L"*", this);
  49. parser->xmlreader_open();
  50. LoadXmlFile(parser, filename);
  51. parser->xmlreader_unregisterCallback(this);
  52. delete includer;
  53. includer=0;
  54. parser->xmlreader_close();
  55. parserFactory->releaseInterface(parser);
  56. parser = 0;
  57. return 1;
  58. }
  59. }
  60. return 0;
  61. }
  62. const wchar_t *XmlReader::getIncludePath()
  63. {
  64. // return include_stack.top()->getValue();
  65. return (includer != NULL ? includer->path.getValue() : L"");
  66. //return includePath;
  67. }
  68. void XmlReader::xmlReaderOnStartElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  69. {
  70. size_t numParams = params->getNbItems();
  71. XmlReaderParamsI p;
  72. for (size_t i = 0;i!=numParams;i++)
  73. { //CT> removed api-> for speed
  74. // BU String* for no exploit
  75. const wchar_t *paramStr = params->getItemValue(i);
  76. StringW *str = PublicVarManager::translate_nocontext(paramStr);
  77. if (str)
  78. {
  79. Url::decode(str->getNonConstVal());
  80. p.addItemSwapValue(params->getItemName(i), *str);
  81. delete str;
  82. }
  83. else
  84. {
  85. StringW temp = paramStr;
  86. Url::decode(temp.getNonConstVal());
  87. p.addItemSwapValue(params->getItemName(i), temp);
  88. }
  89. }
  90. foreach(callback_list)
  91. if (Match(callback_list.getfor()->matchstr.v(), xmlpath))
  92. {
  93. switch (callback_list.getfor()->type)
  94. {
  95. case TYPE_CLASS_CALLBACK:
  96. callback_list.getfor()->callback->xmlReaderOnStartElementCallback(xmltag, &p);
  97. break;
  98. case TYPE_STATIC_CALLBACK:
  99. ((void (*)(int, const wchar_t *, skin_xmlreaderparams *))callback_list.getfor()->callback)(1, xmltag, &p);
  100. break;
  101. }
  102. }
  103. endfor
  104. }
  105. void XmlReader::xmlReaderOnEndElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag)
  106. {
  107. foreach(callback_list)
  108. if (Match(callback_list.getfor()->matchstr.v(), xmlpath))
  109. {
  110. switch (callback_list.getfor()->type)
  111. {
  112. case TYPE_CLASS_CALLBACK:
  113. callback_list.getfor()->callback->xmlReaderOnEndElementCallback(xmltag);
  114. break;
  115. case TYPE_STATIC_CALLBACK:
  116. ((void (*)(int, const wchar_t *, skin_xmlreaderparams *))callback_list.getfor()->callback)(0, xmltag, NULL);
  117. break;
  118. }
  119. }
  120. endfor
  121. }
  122. void XmlReader::xmlReaderOnError(int linenum, int errcode, const wchar_t *errstr)
  123. {
  124. int disperr = 1;
  125. StringPrintfW txt(L"error parsing xml layer\n");
  126. StringPrintfW err(L"%s at line %d\n", errstr, linenum);
  127. if (disperr)
  128. {
  129. #ifdef WASABI_COMPILE_WND
  130. Wasabi::Std::messageBox(err, txt, 0);
  131. #else
  132. DebugStringW("%s - %s", err.getValue(), txt.getValue());
  133. #endif
  134. }
  135. else
  136. {
  137. #ifdef _WIN32
  138. OutputDebugStringW(txt);
  139. OutputDebugStringW(err);
  140. #endif
  141. }
  142. }
  143. #if 0 // TODO: benski> need to do onError for obj_xml
  144. int XmlReader::parseBuffer(void *parser, const char *xml, int size, int final, const char *filename, const char *incpath)
  145. {
  146. if (!XML_Parse(parser, xml, size, final))
  147. {
  148. int disperr = 1;
  149. StringPrintf txt("error parsing xml layer (%s)\n", filename);
  150. StringPrintf err("%s at line %d\n",
  151. XML_ErrorString(XML_GetErrorCode(parser)),
  152. XML_GetCurrentLineNumber(parser));
  153. if (disperr)
  154. {
  155. #ifdef WASABI_COMPILE_WND
  156. Std::messageBox(err, txt, 0);
  157. #else
  158. DebugString("%s - %s", err.getValue(), txt.getValue());
  159. #endif
  160. }
  161. else
  162. {
  163. OutputDebugString(txt);
  164. OutputDebugString(err);
  165. }
  166. currentpos = "";
  167. return 0;
  168. }
  169. return 1;
  170. }
  171. #endif
  172. XmlReader skinXML;