Chromium Code Reviews| Index: tools/gn/xml_element_writer.h |
| diff --git a/tools/gn/xml_element_writer.h b/tools/gn/xml_element_writer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..186bd35ca944a28461bd50ad973b0a1dfbb0e11d |
| --- /dev/null |
| +++ b/tools/gn/xml_element_writer.h |
| @@ -0,0 +1,121 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef TOOLS_GN_XML_ELEMENT_WRITER_H_ |
| +#define TOOLS_GN_XML_ELEMENT_WRITER_H_ |
| + |
| +#include <iosfwd> |
| +#include <string> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/strings/string_piece.h" |
| + |
| +// Vector of XML attribute key-value pairs. |
| +class XmlAttributes |
| + : public std::vector<std::pair<base::StringPiece, base::StringPiece>> { |
| + public: |
| + XmlAttributes(); |
| + XmlAttributes(const base::StringPiece& attr_key, |
| + const base::StringPiece& attr_value); |
| + |
| + XmlAttributes& add(const base::StringPiece& attr_key, |
| + const base::StringPiece& attr_value); |
| +}; |
| + |
| +// Helper class for writing XML elements. New XML element is started in |
| +// XmlElementWriter constructor and ended in its destructor. XmlElementWriter |
| +// handles XML file formatting in order to produce human-readable document. |
| +class XmlElementWriter { |
|
tfarina
2016/01/29 16:32:18
Hum, XmlWriter [1] had no good use for you case?
[
Tomasz Moniuszko
2016/02/01 14:13:24
Well, XmlWriter could be used for sure but its API
|
| + public: |
| + // Starts new XML element. This constructor adds no indentation and is |
| + // designed for XML root element. |
| + XmlElementWriter(std::ostream& out, |
| + const std::string& tag, |
| + const XmlAttributes& attributes); |
| + // Starts new XML element with specified indentation. |
| + XmlElementWriter(std::ostream& out, |
| + const std::string& tag, |
| + const XmlAttributes& attributes, |
| + int indent); |
| + // Starts new XML element with specified indentation. Specialized constructor |
| + // that allows writting XML element with single attribute without copying |
| + // attribute value. |
| + template <class Writer> |
| + XmlElementWriter(std::ostream& out, |
| + const std::string& tag, |
| + const std::string& attribute_name, |
| + const Writer& attribute_value_writer, |
| + int indent); |
| + // Ends XML element. All sub-elements should be ended at this point. |
| + ~XmlElementWriter(); |
| + |
| + // Writes arbitrary XML element text. |
| + void Text(const base::StringPiece& content); |
| + |
| + // Starts new XML sub-element. Caller must ensure that parent element outlives |
| + // its children. |
| + scoped_ptr<XmlElementWriter> SubElement(const std::string& tag); |
| + scoped_ptr<XmlElementWriter> SubElement(const std::string& tag, |
| + const XmlAttributes& attributes); |
| + template <class Writer> |
| + scoped_ptr<XmlElementWriter> SubElement(const std::string& tag, |
| + const std::string& attribute_name, |
| + const Writer& attribute_value_writer); |
| + |
| + // Finishes opening tag if it isn't finished yet and optionally starts new |
| + // document line. Returns the stream where XML element content can be written. |
| + // This is an alternative to Text() and SubElement() methods. |
| + std::ostream& StartContent(bool start_new_line); |
| + |
| + private: |
| + // Output stream. XmlElementWriter objects for XML element and its |
| + // sub-elements share the same output stream. |
| + std::ostream& out_; |
| + |
| + // XML element tag name. |
| + std::string tag_; |
| + |
| + // XML element indentation in the document. |
| + int indent_; |
| + |
| + // Flag indicating if opening tag is finished with '>' character already. |
| + bool opening_tag_finished_; |
| + |
| + // Flag indicating if XML element should be written in one document line. |
| + bool one_line_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(XmlElementWriter); |
| +}; |
| + |
| +template <class Writer> |
| +XmlElementWriter::XmlElementWriter(std::ostream& out, |
| + const std::string& tag, |
| + const std::string& attribute_name, |
| + const Writer& attribute_value_writer, |
| + int indent) |
| + : out_(out), |
| + tag_(tag), |
| + indent_(indent), |
| + opening_tag_finished_(false), |
| + one_line_(true) { |
| + out << std::string(indent, ' ') << '<' << tag; |
| + out << ' ' << attribute_name << "=\""; |
| + attribute_value_writer(out); |
| + out << '\"'; |
| +} |
| + |
| +template <class Writer> |
| +scoped_ptr<XmlElementWriter> XmlElementWriter::SubElement( |
| + const std::string& tag, |
| + const std::string& attribute_name, |
| + const Writer& attribute_value_writer) { |
| + StartContent(true); |
| + return make_scoped_ptr(new XmlElementWriter( |
| + out_, tag, attribute_name, attribute_value_writer, indent_ + 2)); |
| +} |
| + |
| +#endif // TOOLS_GN_XML_ELEMENT_WRITER_H_ |