XMLNameSpace.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * XMLNameSpace.cpp
  3. *
  4. * Created on: May 27, 2018
  5. * Author: hp
  6. */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "android-xml.h"
  10. #include "ResStringPool.h"
  11. #include "XMLNameSpace.h"
  12. XMLNameSpace::XMLNameSpace() {
  13. }
  14. XMLNameSpace::XMLNameSpace(const ResStringPool& string_pool, const char* data) {
  15. ResXMLTree_node header;
  16. memcpy(&header, data, sizeof(ResXMLTree_node));
  17. ResXMLTree_namespaceExt namespaceext;
  18. memcpy(&namespaceext, data + header.header.headerSize,
  19. sizeof(ResXMLTree_namespaceExt));
  20. _name = strdup(string_pool.get(namespaceext.prefix.index));
  21. _uri = strdup(string_pool.get(namespaceext.uri.index));
  22. }
  23. uint32_t XMLNameSpace::serialize(ResStringPool& string_pool, char** data) {
  24. uint32_t datasize = sizeof(ResXMLTree_node)
  25. + sizeof(ResXMLTree_namespaceExt);
  26. *data = (char*) malloc(datasize);
  27. ResXMLTree_node* header = (ResXMLTree_node*) *data;
  28. ResXMLTree_namespaceExt* namespaceext =
  29. (ResXMLTree_namespaceExt*) ((char*) *data + sizeof(ResXMLTree_node));
  30. header->comment.index = -1;
  31. header->lineNumber = ++g_lineno;
  32. header->header.headerSize = sizeof(ResXMLTree_node);
  33. header->header.size = datasize;
  34. header->header.type = RES_XML_START_NAMESPACE_TYPE;
  35. namespaceext->prefix = string_pool.put(_name);
  36. namespaceext->uri = string_pool.put(_uri);
  37. // string_pool.put_empty();
  38. return datasize;
  39. }
  40. uint32_t XMLNameSpace::serialize_end(ResStringPool& string_pool, char** data) {
  41. uint32_t datasize = sizeof(ResXMLTree_node)
  42. + sizeof(ResXMLTree_namespaceExt);
  43. *data = (char*) malloc(datasize);
  44. ResXMLTree_node* header = (ResXMLTree_node*) *data;
  45. ResXMLTree_namespaceExt* namespaceext =
  46. (ResXMLTree_namespaceExt*) ((char*) *data + sizeof(ResXMLTree_node));
  47. header->comment.index = -1;
  48. header->lineNumber = ++g_lineno;
  49. header->header.headerSize = sizeof(ResXMLTree_node);
  50. header->header.size = datasize;
  51. header->header.type = RES_XML_END_NAMESPACE_TYPE;
  52. namespaceext->prefix = string_pool.put(_name);
  53. namespaceext->uri = string_pool.put(_uri);
  54. return datasize;
  55. }
  56. const char* XMLNameSpace::get(const char* uri) const {
  57. if (strcmp(_uri, uri) == 0) {
  58. return _name;
  59. }
  60. return nullptr;
  61. }
  62. XMLNameSpace::~XMLNameSpace() {
  63. free((void*) _name);
  64. free((void*) _uri);
  65. }