XMLFile.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "ArrayPtr.h"
  24. #include "Context.h"
  25. #include "Deserializer.h"
  26. #include "Log.h"
  27. #include "Profiler.h"
  28. #include "Serializer.h"
  29. #include "XMLFile.h"
  30. #include <pugixml.hpp>
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. /// XML writer for pugixml.
  35. class XMLWriter : public pugi::xml_writer
  36. {
  37. public:
  38. /// Construct.
  39. XMLWriter(Serializer& dest) :
  40. dest_(dest),
  41. success_(true)
  42. {
  43. }
  44. /// Write bytes to output.
  45. void write(const void* data, size_t size)
  46. {
  47. if (dest_.Write(data, size) != size)
  48. success_ = false;
  49. }
  50. /// Destination serializer.
  51. Serializer& dest_;
  52. /// Success flag.
  53. bool success_;
  54. };
  55. XMLFile::XMLFile(Context* context) :
  56. Resource(context),
  57. document_(new pugi::xml_document())
  58. {
  59. }
  60. XMLFile::~XMLFile()
  61. {
  62. delete document_;
  63. document_ = 0;
  64. }
  65. void XMLFile::RegisterObject(Context* context)
  66. {
  67. context->RegisterFactory<XMLFile>();
  68. }
  69. bool XMLFile::Load(Deserializer& source)
  70. {
  71. PROFILE(LoadXMLFile);
  72. unsigned dataSize = source.GetSize();
  73. if (!dataSize)
  74. {
  75. LOGERROR("Zero sized XML data in " + source.GetName());
  76. return false;
  77. }
  78. SharedArrayPtr<char> buffer(new char[dataSize]);
  79. if (source.Read(buffer.Get(), dataSize) != dataSize)
  80. return false;
  81. if (!document_->load_buffer(buffer.Get(), dataSize))
  82. {
  83. LOGERROR("Could not parse XML data from " + source.GetName());
  84. return false;
  85. }
  86. // Note: this probably does not reflect internal data structure size accurately
  87. SetMemoryUse(dataSize);
  88. return true;
  89. }
  90. bool XMLFile::Save(Serializer& dest) const
  91. {
  92. XMLWriter writer(dest);
  93. document_->save(writer);
  94. return writer.success_;
  95. }
  96. XMLElement XMLFile::CreateRoot(const String& name)
  97. {
  98. document_->reset();
  99. pugi::xml_node root = document_->append_child(name.CString());
  100. return XMLElement(this, root.internal_object());
  101. }
  102. XMLElement XMLFile::GetRoot(const String& name)
  103. {
  104. pugi::xml_node root = document_->first_child();
  105. if (root.empty())
  106. return XMLElement();
  107. if (!name.Empty() && name != root.name())
  108. return XMLElement();
  109. else
  110. return XMLElement(this, root.internal_object());
  111. }
  112. }