XMLFile.h 3.8 KB

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