1
0

xmlServiceParser.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include "main.h"
  2. #include "./xmlServiceParser.h"
  3. #include "../xml/obj_xml.h"
  4. #include "./service.h"
  5. #include "./ifc_omservicehost.h"
  6. #include "./ifc_omstoragehandler.h"
  7. #include "./ifc_omstoragehandlerenum.h"
  8. #include "./ifc_omstorageext.h"
  9. #include <shlwapi.h>
  10. #include <strsafe.h>
  11. typedef void (CALLBACK *TAGCALLBACK)(XmlServiceParser* /*reader*/, OmService* /*editor*/, LPCWSTR /*value*/);
  12. typedef struct __TAGRECORD
  13. {
  14. LPCWSTR name;
  15. TAGCALLBACK callback;
  16. } TAGRECORD;
  17. static void CALLBACK ParserXml_ReadId(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  18. {
  19. INT iVal;
  20. if (StrToIntEx(value, STIF_SUPPORT_HEX, &iVal))
  21. {
  22. service->SetId(iVal);
  23. }
  24. }
  25. static void CALLBACK ParserXml_ReadName(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  26. {
  27. service->SetName(value, FALSE);
  28. }
  29. static void CALLBACK ParserXml_ReadUrl(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  30. {
  31. service->SetUrl(value, FALSE);
  32. }
  33. static void CALLBACK ParserXml_ReadAuth(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  34. {
  35. INT iVal;
  36. if (StrToIntEx(value, STIF_SUPPORT_HEX, &iVal))
  37. {
  38. // service->SetAuth(iVal);
  39. }
  40. }
  41. static void CALLBACK ParserXml_ReadIcon(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  42. {
  43. service->SetIcon(value, FALSE);
  44. }
  45. static void CALLBACK ParserXml_ReadThumbnail(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  46. {
  47. service->SetThumbnail(value, FALSE);
  48. }
  49. static void CALLBACK ParserXml_ReadScreenshot(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  50. {
  51. service->SetScreenshot(value, FALSE);
  52. }
  53. static void CALLBACK ParserXml_ReadVersion(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  54. {
  55. INT iVal;
  56. if (StrToIntEx(value, STIF_SUPPORT_HEX, &iVal))
  57. service->SetVersion(iVal);
  58. }
  59. static void CALLBACK ParserXml_ReadGeneration(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  60. {
  61. INT iVal;
  62. if (StrToIntEx(value, STIF_SUPPORT_HEX, &iVal))
  63. {
  64. service->SetGeneration(iVal);
  65. }
  66. }
  67. static void CALLBACK ParserXml_ReadDescription(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  68. {
  69. service->SetDescription(value, FALSE);
  70. }
  71. static void CALLBACK ParserXml_ReadAuthorFirst(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  72. {
  73. service->SetAuthorFirst(value, FALSE);
  74. }
  75. static void CALLBACK ParserXml_ReadAuthorLast(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  76. {
  77. service->SetAuthorLast(value, FALSE);
  78. }
  79. static void CALLBACK ParserXml_ReadPublished(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  80. {
  81. service->SetPublished(value, FALSE);
  82. }
  83. static void CALLBACK ParserXml_ReadUpdated(XmlServiceParser *reader, OmService *service, LPCWSTR value)
  84. {
  85. service->SetUpdated(value, FALSE);
  86. }
  87. static const TAGRECORD szTagTable[] =
  88. {
  89. { L"id", ParserXml_ReadId },
  90. { L"name", ParserXml_ReadName },
  91. { L"version", ParserXml_ReadVersion },
  92. { L"icon_url", ParserXml_ReadIcon },
  93. { L"entry_url", ParserXml_ReadUrl },
  94. { L"generation", ParserXml_ReadGeneration },
  95. { L"author_comments_long", ParserXml_ReadDescription },
  96. { L"author_firstname", ParserXml_ReadAuthorFirst},
  97. { L"author_lastname", ParserXml_ReadAuthorLast },
  98. { L"date_published", ParserXml_ReadPublished },
  99. { L"date_updated", ParserXml_ReadUpdated },
  100. { L"thumbnail", ParserXml_ReadThumbnail },
  101. { L"screenshot", ParserXml_ReadScreenshot },
  102. };
  103. XmlServiceParser::XmlServiceParser()
  104. : checkList(NULL), checkSize(0), parser(NULL), service(NULL), result(E_UNEXPECTED)
  105. {
  106. }
  107. XmlServiceParser::~XmlServiceParser()
  108. {
  109. Finish(NULL, NULL);
  110. if (NULL != checkList)
  111. free(checkList);
  112. size_t index = handlerList.size();
  113. while (index--)
  114. handlerList[index]->Release();
  115. }
  116. HRESULT XmlServiceParser::RegisterHandlers(ifc_omstoragehandlerenum *handlerEnum)
  117. {
  118. if (E_PENDING == result)
  119. return E_PENDING;
  120. if (NULL != checkList)
  121. {
  122. free(checkList);
  123. checkList = NULL;
  124. checkSize = 0;
  125. }
  126. size_t index = handlerList.size();
  127. while(index--) handlerList[index]->Release();
  128. handlerList.clear();
  129. if (NULL != handlerEnum)
  130. {
  131. ifc_omstoragehandler *handler;
  132. handlerEnum->Reset();
  133. while(S_OK == handlerEnum->Next(1, &handler, NULL))
  134. {
  135. handlerList.push_back(handler);
  136. }
  137. }
  138. return S_OK;
  139. }
  140. HRESULT XmlServiceParser::Initialize(obj_xml *reader, LPCWSTR match, ifc_omservicehost *host)
  141. {
  142. result = E_UNEXPECTED;
  143. if (NULL != parser) return E_UNEXPECTED;
  144. if (NULL == reader) return E_INVALIDARG;
  145. HRESULT hr;
  146. WCHAR szBuffer[1024] = {0};
  147. LPWSTR cursor = szBuffer;
  148. size_t remaining = ARRAYSIZE(szBuffer);
  149. if (NULL == checkList)
  150. {
  151. size_t checkCount = ARRAYSIZE(szTagTable) + handlerList.size();
  152. checkSize = checkCount/8 + ((checkCount%8) ? 1 : 0);
  153. checkList = (BYTE*)calloc(checkSize, sizeof(BYTE));
  154. if (NULL != checkList)
  155. ZeroMemory(checkList, checkSize);
  156. }
  157. hr = StringCchCopyEx(cursor, remaining, match, &cursor, &remaining, STRSAFE_IGNORE_NULLS);
  158. if (FAILED(hr)) return E_UNEXPECTED;
  159. hr = StringCchCopyEx(cursor, remaining, ((cursor != szBuffer) ? L"\f*" : L"*"), &cursor, &remaining, STRSAFE_IGNORE_NULLS);
  160. if (FAILED(hr)) return E_UNEXPECTED;
  161. parser = reader;
  162. parser->AddRef();
  163. parser->xmlreader_registerCallback(szBuffer, this);
  164. hr = OmService::CreateInstance(0, host, &service);
  165. if (FAILED(hr)) return hr;
  166. service->BeginUpdate();
  167. result = E_PENDING;
  168. return S_OK;
  169. }
  170. HRESULT XmlServiceParser::Finish(HRESULT *parserResult, ifc_omservice **ppService)
  171. {
  172. if (NULL == parser)
  173. return E_UNEXPECTED;
  174. if (E_PENDING == result)
  175. result= S_OK;
  176. buffer.Clear();
  177. if (NULL != checkList)
  178. ZeroMemory(checkList, checkSize);
  179. parser->xmlreader_unregisterCallback(this);
  180. parser->Release();
  181. parser = NULL;
  182. if (NULL != service && 0 == service->GetId())
  183. {
  184. if (SUCCEEDED(result))
  185. result = E_UNEXPECTED;
  186. service->Release();
  187. service = NULL;
  188. }
  189. if (NULL != parserResult)
  190. *parserResult = result;
  191. if (NULL != ppService)
  192. {
  193. if (SUCCEEDED(result))
  194. {
  195. *ppService = service;
  196. service->AddRef();
  197. }
  198. else
  199. {
  200. *ppService = NULL;
  201. }
  202. }
  203. if (NULL != service)
  204. {
  205. service->SetModified(0, (UINT)-1);
  206. service->EndUpdate();
  207. service->Release();
  208. service = NULL;
  209. }
  210. result = E_UNEXPECTED;
  211. return S_OK;
  212. }
  213. HRESULT XmlServiceParser::GetActive()
  214. {
  215. return (E_PENDING == result) ? S_OK : S_FALSE;
  216. }
  217. void XmlServiceParser::OnStartElement(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  218. {
  219. buffer.Clear();
  220. }
  221. void XmlServiceParser::OnEndElement(const wchar_t *xmlpath, const wchar_t *xmltag)
  222. {
  223. size_t i;
  224. for (i = 0; i < ARRAYSIZE(szTagTable); i++)
  225. {
  226. if (NULL == checkList || 0 == (checkList[i/8] & (0x01 << (i%8))) && E_PENDING == result)
  227. {
  228. if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, szTagTable[i].name, -1, xmltag, -1))
  229. {
  230. checkList[i/8] |= (0x01 << (i%8));
  231. szTagTable[i].callback(this, service, buffer.Get());
  232. return;
  233. }
  234. }
  235. }
  236. size_t handlerSize = handlerList.size();
  237. size_t iCheck = ARRAYSIZE(szTagTable);
  238. for (i = 0; i < handlerSize; i++, iCheck++)
  239. {
  240. if (NULL == checkList || 0 == (checkList[iCheck/8] & (0x01 << (iCheck%8))) && E_PENDING == result)
  241. {
  242. LPCWSTR pszKey;
  243. if (SUCCEEDED(handlerList[i]->GetKey(&pszKey)) &&
  244. CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, pszKey, -1, xmltag, -1))
  245. {
  246. checkList[iCheck/8] |= (0x01 << (iCheck%8));
  247. handlerList[i]->Invoke(service, xmltag, buffer.Get());
  248. return;
  249. }
  250. }
  251. }
  252. }
  253. void XmlServiceParser::OnCharData(const wchar_t *xmlpath, const wchar_t *xmltag, const wchar_t *value)
  254. {
  255. buffer.Append(value);
  256. }
  257. void XmlServiceParser::OnError(int linenum, int errcode, const wchar_t *errstr)
  258. {
  259. result = E_FAIL;
  260. if (NULL != service)
  261. {
  262. service->Release();
  263. service = NULL;
  264. }
  265. }
  266. #define CBCLASS XmlServiceParser
  267. START_DISPATCH;
  268. VCB(ONSTARTELEMENT, OnStartElement)
  269. VCB(ONENDELEMENT, OnEndElement)
  270. VCB(ONCHARDATA, OnCharData)
  271. VCB(ONERROR, OnError)
  272. END_DISPATCH;
  273. #undef CBCLASS