xml_writer.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef THIRD_PARTY_LIBXML_CHROMIUM_XML_WRITER_H_
  5. #define THIRD_PARTY_LIBXML_CHROMIUM_XML_WRITER_H_
  6. #include <string>
  7. extern "C" {
  8. struct _xmlBuffer;
  9. struct _xmlTextWriter;
  10. }
  11. // XmlWriter is a wrapper class around libxml's xmlWriter,
  12. // providing a simplified C++ API.
  13. // StartWriting must be called before other methods, and StopWriting
  14. // must be called before GetWrittenString() will return results.
  15. class XmlWriter {
  16. public:
  17. XmlWriter();
  18. ~XmlWriter();
  19. // Allocates the xmlTextWriter and an xmlBuffer and starts an XML document.
  20. // This must be called before any other functions. By default, indenting is
  21. // set to true.
  22. void StartWriting();
  23. // Ends the XML document and frees the xmlTextWriter.
  24. // This must be called before GetWrittenString() is called.
  25. void StopWriting();
  26. // Wrappers around libxml functions -----------------------------------------
  27. // All following elements will be indented to match their depth.
  28. void StartIndenting();
  29. // All follow elements will not be indented.
  30. void StopIndenting();
  31. // Start an element with the given name. All future elements added will be
  32. // children of this element, until it is ended. Returns false on error.
  33. bool StartElement(const std::string& element_name);
  34. // Ends the current open element. Returns false on error.
  35. bool EndElement();
  36. // Appends to the content of the current open element.
  37. bool AppendElementContent(const std::string& content);
  38. // Adds an attribute to the current open element. Returns false on error.
  39. bool AddAttribute(const std::string& attribute_name,
  40. const std::string& attribute_value);
  41. // Adds a new element with name |element_name| and content |content|
  42. // to the buffer. Example: <|element_name|>|content|</|element_name|>
  43. // Returns false on errors.
  44. bool WriteElement(const std::string& element_name,
  45. const std::string& content);
  46. // Helper functions not provided by xmlTextWriter ---------------------------
  47. // Returns the string that has been written to the buffer.
  48. std::string GetWrittenString();
  49. private:
  50. // The underlying libxml xmlTextWriter.
  51. _xmlTextWriter* writer_;
  52. // Stores the output.
  53. _xmlBuffer* buffer_;
  54. };
  55. #endif // THIRD_PARTY_LIBXML_CHROMIUM_XML_WRITER_H_