XMLFile.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // Copyright (c) 2008-2015 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. OBJECT(XMLFile);
  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. /// Return the root element, with optionally specified name. Return null element if not found.
  55. XMLElement GetRoot(const String& name = String::EMPTY);
  56. /// Return the pugixml document.
  57. pugi::xml_document* GetDocument() const { return document_; }
  58. /// Serialize the XML content to a string.
  59. String ToString(const String& indentation = "\t") const;
  60. /// Patch the XMLFile with another XMLFile. Based on RFC 5261.
  61. void Patch(XMLFile* patchFile);
  62. /// Patch the XMLFile with another XMLElement. Based on RFC 5261.
  63. void Patch(XMLElement patchElement);
  64. private:
  65. /// Add an node in the Patch.
  66. void PatchAdd(const pugi::xml_node& patch, pugi::xpath_node& original) const;
  67. /// Replace a node or attribute in the Patch.
  68. void PatchReplace(const pugi::xml_node& patch, pugi::xpath_node& original) const;
  69. /// Remove a node or attribute in the Patch.
  70. void PatchRemove(const pugi::xpath_node& original) const;
  71. /// Add a node in the Patch.
  72. void AddNode(const pugi::xml_node& patch, const pugi::xpath_node& original) const;
  73. /// Add an attribute in the Patch.
  74. void AddAttribute(const pugi::xml_node& patch, const pugi::xpath_node& original) const;
  75. /// Combine two text nodes.
  76. bool CombineText(const pugi::xml_node& patch, const pugi::xml_node& original, bool prepend) const;
  77. /// Pugixml document.
  78. pugi::xml_document* document_;
  79. };
  80. }