OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef TOOLS_GN_XML_ELEMENT_WRITER_H_ | |
6 #define TOOLS_GN_XML_ELEMENT_WRITER_H_ | |
7 | |
8 #include <iosfwd> | |
9 #include <string> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/macros.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/strings/string_piece.h" | |
16 | |
17 // Vector of XML attribute key-value pairs. | |
18 class XmlAttributes | |
19 : public std::vector<std::pair<base::StringPiece, base::StringPiece>> { | |
20 public: | |
21 XmlAttributes(); | |
22 XmlAttributes(const base::StringPiece& attr_key, | |
23 const base::StringPiece& attr_value); | |
24 | |
25 XmlAttributes& add(const base::StringPiece& attr_key, | |
26 const base::StringPiece& attr_value); | |
27 }; | |
28 | |
29 // Helper class for writing XML elements. New XML element is started in | |
30 // XmlElementWriter constructor and ended in its destructor. XmlElementWriter | |
31 // handles XML file formatting in order to produce human-readable document. | |
32 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
| |
33 public: | |
34 // Starts new XML element. This constructor adds no indentation and is | |
35 // designed for XML root element. | |
36 XmlElementWriter(std::ostream& out, | |
37 const std::string& tag, | |
38 const XmlAttributes& attributes); | |
39 // Starts new XML element with specified indentation. | |
40 XmlElementWriter(std::ostream& out, | |
41 const std::string& tag, | |
42 const XmlAttributes& attributes, | |
43 int indent); | |
44 // Starts new XML element with specified indentation. Specialized constructor | |
45 // that allows writting XML element with single attribute without copying | |
46 // attribute value. | |
47 template <class Writer> | |
48 XmlElementWriter(std::ostream& out, | |
49 const std::string& tag, | |
50 const std::string& attribute_name, | |
51 const Writer& attribute_value_writer, | |
52 int indent); | |
53 // Ends XML element. All sub-elements should be ended at this point. | |
54 ~XmlElementWriter(); | |
55 | |
56 // Writes arbitrary XML element text. | |
57 void Text(const base::StringPiece& content); | |
58 | |
59 // Starts new XML sub-element. Caller must ensure that parent element outlives | |
60 // its children. | |
61 scoped_ptr<XmlElementWriter> SubElement(const std::string& tag); | |
62 scoped_ptr<XmlElementWriter> SubElement(const std::string& tag, | |
63 const XmlAttributes& attributes); | |
64 template <class Writer> | |
65 scoped_ptr<XmlElementWriter> SubElement(const std::string& tag, | |
66 const std::string& attribute_name, | |
67 const Writer& attribute_value_writer); | |
68 | |
69 // 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. | |
71 // This is an alternative to Text() and SubElement() methods. | |
72 std::ostream& StartContent(bool start_new_line); | |
73 | |
74 private: | |
75 // Output stream. XmlElementWriter objects for XML element and its | |
76 // sub-elements share the same output stream. | |
77 std::ostream& out_; | |
78 | |
79 // XML element tag name. | |
80 std::string tag_; | |
81 | |
82 // XML element indentation in the document. | |
83 int indent_; | |
84 | |
85 // Flag indicating if opening tag is finished with '>' character already. | |
86 bool opening_tag_finished_; | |
87 | |
88 // Flag indicating if XML element should be written in one document line. | |
89 bool one_line_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(XmlElementWriter); | |
92 }; | |
93 | |
94 template <class Writer> | |
95 XmlElementWriter::XmlElementWriter(std::ostream& out, | |
96 const std::string& tag, | |
97 const std::string& attribute_name, | |
98 const Writer& attribute_value_writer, | |
99 int indent) | |
100 : out_(out), | |
101 tag_(tag), | |
102 indent_(indent), | |
103 opening_tag_finished_(false), | |
104 one_line_(true) { | |
105 out << std::string(indent, ' ') << '<' << tag; | |
106 out << ' ' << attribute_name << "=\""; | |
107 attribute_value_writer(out); | |
108 out << '\"'; | |
109 } | |
110 | |
111 template <class Writer> | |
112 scoped_ptr<XmlElementWriter> XmlElementWriter::SubElement( | |
113 const std::string& tag, | |
114 const std::string& attribute_name, | |
115 const Writer& attribute_value_writer) { | |
116 StartContent(true); | |
117 return make_scoped_ptr(new XmlElementWriter( | |
118 out_, tag, attribute_name, attribute_value_writer, indent_ + 2)); | |
119 } | |
120 | |
121 #endif // TOOLS_GN_XML_ELEMENT_WRITER_H_ | |
OLD | NEW |