XMLFile.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Copyright (c) 2008-2022 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. #pragma once
  23. #include "../Resource/Resource.h"
  24. #include "../Resource/XMLElement.h"
  25. #include <memory>
  26. namespace pugi
  27. {
  28. class xml_document;
  29. class xml_node;
  30. class xpath_node;
  31. }
  32. namespace Urho3D
  33. {
  34. /// XML document resource.
  35. class URHO3D_API XMLFile : public Resource
  36. {
  37. URHO3D_OBJECT(XMLFile, Resource);
  38. public:
  39. /// Construct.
  40. explicit XMLFile(Context* context);
  41. /// Destruct.
  42. ~XMLFile() override;
  43. /// Register object factory.
  44. /// @nobind
  45. static void RegisterObject(Context* context);
  46. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  47. bool BeginLoad(Deserializer& source) override;
  48. /// Save resource with default indentation (one tab). Return true if successful.
  49. bool Save(Serializer& dest) const override;
  50. /// Save resource with user-defined indentation. Return true if successful.
  51. bool Save(Serializer& dest, const String& indentation) const;
  52. /// Deserialize from a string. Return true if successful.
  53. bool FromString(const String& source);
  54. /// Clear the document and create a root element.
  55. XMLElement CreateRoot(const String& name);
  56. /// Get the root element if it has matching name, otherwise create it and clear the document.
  57. XMLElement GetOrCreateRoot(const String& name);
  58. /// Return the root element, with optionally specified name. Return null element if not found.
  59. XMLElement GetRoot(const String& name = String::EMPTY);
  60. /// Return the pugixml document.
  61. pugi::xml_document* GetDocument() const { return document_.get(); }
  62. /// Serialize the XML content to a string.
  63. String ToString(const String& indentation = "\t") const;
  64. /// Patch the XMLFile with another XMLFile. Based on RFC 5261.
  65. void Patch(XMLFile* patchFile);
  66. /// Patch the XMLFile with another XMLElement. Based on RFC 5261.
  67. void Patch(const XMLElement& patchElement);
  68. private:
  69. /// Add an node in the Patch.
  70. void PatchAdd(const pugi::xml_node& patch, pugi::xpath_node& original) const;
  71. /// Replace a node or attribute in the Patch.
  72. void PatchReplace(const pugi::xml_node& patch, pugi::xpath_node& original) const;
  73. /// Remove a node or attribute in the Patch.
  74. void PatchRemove(const pugi::xpath_node& original) const;
  75. /// Add a node in the Patch.
  76. void AddNode(const pugi::xml_node& patch, const pugi::xpath_node& original) const;
  77. /// Add an attribute in the Patch.
  78. void AddAttribute(const pugi::xml_node& patch, const pugi::xpath_node& original) const;
  79. /// Combine two text nodes.
  80. bool CombineText(const pugi::xml_node& patch, const pugi::xml_node& original, bool prepend) const;
  81. /// Pugixml document.
  82. std::unique_ptr<pugi::xml_document> document_;
  83. };
  84. }