XMLFile.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // Copyright (c) 2008-2014 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.h"
  24. #include "XMLElement.h"
  25. namespace pugi
  26. {
  27. class xml_document;
  28. class xml_node;
  29. class xpath_node;
  30. }
  31. namespace Urho3D
  32. {
  33. /// XML document resource.
  34. class URHO3D_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. Return true if successful. Only supports saving to a File.
  47. virtual bool Save(Serializer& dest) const;
  48. /// Deserialize from a string. Return true if successful.
  49. bool FromString(const String& source);
  50. /// Clear the document and create a root element.
  51. XMLElement CreateRoot(const String& name);
  52. /// Return the root element, with optionally specified name. Return null element if not found.
  53. XMLElement GetRoot(const String& name = String::EMPTY);
  54. /// Return the pugixml document.
  55. pugi::xml_document* GetDocument() const { return document_; }
  56. /// Serialize the XML content to a string.
  57. String ToString() const;
  58. /// Patch the XMLFile with another XMLFile. Based on RFC 5261.
  59. void Patch(XMLFile* patchFile);
  60. /// Patch the XMLFile with another XMLElement. Based on RFC 5261.
  61. void Patch(XMLElement patchElement);
  62. private:
  63. /// Add an node in the Patch.
  64. void PatchAdd(const pugi::xml_node& patch, pugi::xpath_node& original);
  65. /// Replace a node or attribute in the Patch.
  66. void PatchReplace(const pugi::xml_node& patch, pugi::xpath_node& original);
  67. /// Remove a node or attribute in the Patch.
  68. void PatchRemove(const pugi::xpath_node& original);
  69. /// Add a node in the Patch.
  70. void AddNode(const pugi::xml_node& patch, pugi::xpath_node& original);
  71. /// Add an attribute in the Patch.
  72. void AddAttribute(const pugi::xml_node& patch, pugi::xpath_node& original);
  73. /// Combine two text nodes.
  74. bool CombineText(const pugi::xml_node& patch, pugi::xml_node original, bool prepend);
  75. /// Pugixml document.
  76. pugi::xml_document* document_;
  77. };
  78. }