XMLFile.h 2.7 KB

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