1
0

xmlResponseParser.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "main.h"
  2. #include "./xmlResponseParser.h"
  3. #include "./ifc_omservicehost.h"
  4. #include "./ifc_omservice.h"
  5. #include "./ifc_omstoragehandlerenum.h"
  6. #include "./ifc_omstorageext.h"
  7. #include "./ifc_omfilestorage.h"
  8. #include "../xml/obj_xml.h"
  9. #include <shlwapi.h>
  10. #include <strsafe.h>
  11. XmlResponseParser::XmlResponseParser()
  12. : reader(NULL), code((UINT)-1), text(NULL), host(NULL)
  13. {
  14. }
  15. XmlResponseParser::~XmlResponseParser()
  16. {
  17. if (NULL != reader)
  18. {
  19. reader->Release();
  20. reader = NULL;
  21. }
  22. if (NULL != host)
  23. host->Release();
  24. while(false == deque.empty())
  25. {
  26. deque.front()->Release();
  27. deque.pop_front();
  28. }
  29. }
  30. HRESULT XmlResponseParser::Initialize(obj_xml *xml, ifc_omservicehost *serviceHost)
  31. {
  32. if (NULL == xml) return E_INVALIDARG;
  33. if (NULL != reader)
  34. {
  35. if (reader == xml)
  36. return S_FALSE;
  37. reader->Release();
  38. }
  39. reader = xml;
  40. reader->AddRef();
  41. reader->xmlreader_registerCallback(L"response\fstatusCode", this);
  42. reader->xmlreader_registerCallback(L"response\fstatusText", this);
  43. reader->xmlreader_registerCallback(L"response\fdata\fservices\fservice", this);
  44. if (NULL != host)
  45. host->Release();
  46. ifc_omstoragehandlerenum *handlerEnum(NULL);
  47. host = serviceHost;
  48. if (NULL != host)
  49. {
  50. host->AddRef();
  51. ifc_omstorageext *storageExt;
  52. if (SUCCEEDED(host->QueryInterface(IFC_OmStorageExt, (void**)&storageExt)))
  53. {
  54. if (FAILED(storageExt->Enumerate(&SUID_OmStorageXml, &handlerEnum)))
  55. handlerEnum = NULL;
  56. storageExt->Release();
  57. }
  58. }
  59. parser.RegisterHandlers(handlerEnum);
  60. if (NULL != handlerEnum)
  61. {
  62. handlerEnum->Release();
  63. }
  64. Reset();
  65. return S_OK;
  66. }
  67. HRESULT XmlResponseParser::Finish()
  68. {
  69. if (NULL != reader)
  70. {
  71. reader->Release();
  72. reader = NULL;
  73. }
  74. return S_OK;
  75. }
  76. HRESULT XmlResponseParser::Reset()
  77. {
  78. code = (UINT)-1;
  79. if (NULL != text)
  80. {
  81. Plugin_FreeString(text);
  82. text = NULL;
  83. }
  84. string.Clear();
  85. return S_OK;
  86. }
  87. HRESULT XmlResponseParser::GetCode(UINT *value)
  88. {
  89. if (NULL == value) return E_POINTER;
  90. if ((UINT)-1 == code)
  91. {
  92. *value = 0;
  93. return E_PENDING;
  94. }
  95. *value = code;
  96. return S_OK;
  97. }
  98. HRESULT XmlResponseParser::GetText(LPWSTR pszBuffer, UINT cchBufferMax)
  99. {
  100. if (NULL == pszBuffer) return E_POINTER;
  101. if (NULL == text)
  102. {
  103. pszBuffer[0] = L'\0';
  104. return E_PENDING;
  105. }
  106. return StringCchCopy(pszBuffer, cchBufferMax, text);
  107. }
  108. HRESULT XmlResponseParser::PeekService(ifc_omservice **service)
  109. {
  110. if (NULL == service) return E_POINTER;
  111. if (0 == deque.size()) return S_FALSE;
  112. *service = deque.back();
  113. deque.pop_back();
  114. return S_OK;
  115. }
  116. void XmlResponseParser::OnStartElement(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  117. {
  118. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, xmltag, -1, L"service", -1))
  119. {
  120. parser.Initialize(reader, xmlpath, host);
  121. }
  122. else
  123. {
  124. string.Clear();
  125. }
  126. }
  127. void XmlResponseParser::OnEndElement(const wchar_t *xmlpath, const wchar_t *xmltag)
  128. {
  129. if (S_OK == parser.GetActive())
  130. {
  131. ifc_omservice *service;
  132. if (SUCCEEDED(parser.Finish(NULL, &service)) && NULL != service)
  133. {
  134. deque.push_front(service);
  135. }
  136. }
  137. else
  138. {
  139. if ((UINT)-1 == code && CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, xmltag, -1, L"statusCode", -1))
  140. {
  141. LPCWSTR sCode = string.Get();
  142. if (NULL == sCode || L'\0' == *sCode ||
  143. FALSE == StrToIntEx(sCode, STIF_SUPPORT_HEX, (INT*)&code))
  144. {
  145. code = (UINT)-1;
  146. }
  147. }
  148. else if (NULL == text && CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, xmltag, -1, L"statusText", -1))
  149. {
  150. text = Plugin_CopyString(string.Get());
  151. }
  152. }
  153. }
  154. void XmlResponseParser::OnCharData(const wchar_t *xmlpath, const wchar_t *xmltag, const wchar_t *value)
  155. {
  156. if (S_OK != parser.GetActive())
  157. {
  158. string.Append(value);
  159. }
  160. }
  161. void XmlResponseParser::OnError(int linenum, int errcode, const wchar_t *errstr)
  162. {
  163. string.Clear();
  164. }
  165. #define CBCLASS XmlResponseParser
  166. START_DISPATCH;
  167. VCB(ONSTARTELEMENT, OnStartElement)
  168. VCB(ONENDELEMENT, OnEndElement)
  169. VCB(ONCHARDATA, OnCharData)
  170. END_DISPATCH;
  171. #undef CBCLASS