OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef TOOLS_GN_XML_ELEMENT_WRITER_H_ | 5 #ifndef TOOLS_GN_XML_ELEMENT_WRITER_H_ |
6 #define TOOLS_GN_XML_ELEMENT_WRITER_H_ | 6 #define TOOLS_GN_XML_ELEMENT_WRITER_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
| 9 #include <memory> |
9 #include <string> | 10 #include <string> |
10 #include <utility> | 11 #include <utility> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/ptr_util.h" |
15 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
16 | 17 |
17 // Vector of XML attribute key-value pairs. | 18 // Vector of XML attribute key-value pairs. |
18 class XmlAttributes | 19 class XmlAttributes |
19 : public std::vector<std::pair<base::StringPiece, base::StringPiece>> { | 20 : public std::vector<std::pair<base::StringPiece, base::StringPiece>> { |
20 public: | 21 public: |
21 XmlAttributes(); | 22 XmlAttributes(); |
22 XmlAttributes(const base::StringPiece& attr_key, | 23 XmlAttributes(const base::StringPiece& attr_key, |
23 const base::StringPiece& attr_value); | 24 const base::StringPiece& attr_value); |
24 | 25 |
(...skipping 26 matching lines...) Expand all Loading... |
51 const Writer& attribute_value_writer, | 52 const Writer& attribute_value_writer, |
52 int indent); | 53 int indent); |
53 // Ends XML element. All sub-elements should be ended at this point. | 54 // Ends XML element. All sub-elements should be ended at this point. |
54 ~XmlElementWriter(); | 55 ~XmlElementWriter(); |
55 | 56 |
56 // Writes arbitrary XML element text. | 57 // Writes arbitrary XML element text. |
57 void Text(const base::StringPiece& content); | 58 void Text(const base::StringPiece& content); |
58 | 59 |
59 // Starts new XML sub-element. Caller must ensure that parent element outlives | 60 // Starts new XML sub-element. Caller must ensure that parent element outlives |
60 // its children. | 61 // its children. |
61 scoped_ptr<XmlElementWriter> SubElement(const std::string& tag); | 62 std::unique_ptr<XmlElementWriter> SubElement(const std::string& tag); |
62 scoped_ptr<XmlElementWriter> SubElement(const std::string& tag, | 63 std::unique_ptr<XmlElementWriter> SubElement(const std::string& tag, |
63 const XmlAttributes& attributes); | 64 const XmlAttributes& attributes); |
64 template <class Writer> | 65 template <class Writer> |
65 scoped_ptr<XmlElementWriter> SubElement(const std::string& tag, | 66 std::unique_ptr<XmlElementWriter> SubElement( |
66 const std::string& attribute_name, | 67 const std::string& tag, |
67 const Writer& attribute_value_writer); | 68 const std::string& attribute_name, |
| 69 const Writer& attribute_value_writer); |
68 | 70 |
69 // Finishes opening tag if it isn't finished yet and optionally starts new | 71 // Finishes opening tag if it isn't finished yet and optionally starts new |
70 // document line. Returns the stream where XML element content can be written. | 72 // document line. Returns the stream where XML element content can be written. |
71 // This is an alternative to Text() and SubElement() methods. | 73 // This is an alternative to Text() and SubElement() methods. |
72 std::ostream& StartContent(bool start_new_line); | 74 std::ostream& StartContent(bool start_new_line); |
73 | 75 |
74 private: | 76 private: |
75 // Output stream. XmlElementWriter objects for XML element and its | 77 // Output stream. XmlElementWriter objects for XML element and its |
76 // sub-elements share the same output stream. | 78 // sub-elements share the same output stream. |
77 std::ostream& out_; | 79 std::ostream& out_; |
(...skipping 24 matching lines...) Expand all Loading... |
102 indent_(indent), | 104 indent_(indent), |
103 opening_tag_finished_(false), | 105 opening_tag_finished_(false), |
104 one_line_(true) { | 106 one_line_(true) { |
105 out << std::string(indent, ' ') << '<' << tag; | 107 out << std::string(indent, ' ') << '<' << tag; |
106 out << ' ' << attribute_name << "=\""; | 108 out << ' ' << attribute_name << "=\""; |
107 attribute_value_writer(out); | 109 attribute_value_writer(out); |
108 out << '\"'; | 110 out << '\"'; |
109 } | 111 } |
110 | 112 |
111 template <class Writer> | 113 template <class Writer> |
112 scoped_ptr<XmlElementWriter> XmlElementWriter::SubElement( | 114 std::unique_ptr<XmlElementWriter> XmlElementWriter::SubElement( |
113 const std::string& tag, | 115 const std::string& tag, |
114 const std::string& attribute_name, | 116 const std::string& attribute_name, |
115 const Writer& attribute_value_writer) { | 117 const Writer& attribute_value_writer) { |
116 StartContent(true); | 118 StartContent(true); |
117 return make_scoped_ptr(new XmlElementWriter( | 119 return base::WrapUnique(new XmlElementWriter( |
118 out_, tag, attribute_name, attribute_value_writer, indent_ + 2)); | 120 out_, tag, attribute_name, attribute_value_writer, indent_ + 2)); |
119 } | 121 } |
120 | 122 |
121 #endif // TOOLS_GN_XML_ELEMENT_WRITER_H_ | 123 #endif // TOOLS_GN_XML_ELEMENT_WRITER_H_ |
OLD | NEW |