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 #include "tools/gn/xml_element_writer.h" |
| 6 |
| 7 XmlAttributes::XmlAttributes() {} |
| 8 |
| 9 XmlAttributes::XmlAttributes(const base::StringPiece& attr_key, |
| 10 const base::StringPiece& attr_value) { |
| 11 add(attr_key, attr_value); |
| 12 } |
| 13 |
| 14 XmlAttributes& XmlAttributes::add(const base::StringPiece& attr_key, |
| 15 const base::StringPiece& attr_value) { |
| 16 push_back(std::make_pair(attr_key, attr_value)); |
| 17 return *this; |
| 18 } |
| 19 |
| 20 XmlElementWriter::XmlElementWriter(std::ostream& out, |
| 21 const std::string& tag, |
| 22 const XmlAttributes& attributes) |
| 23 : XmlElementWriter(out, tag, attributes, 0) {} |
| 24 |
| 25 XmlElementWriter::XmlElementWriter(std::ostream& out, |
| 26 const std::string& tag, |
| 27 const XmlAttributes& attributes, |
| 28 int indent) |
| 29 : out_(out), |
| 30 tag_(tag), |
| 31 indent_(indent), |
| 32 opening_tag_finished_(false), |
| 33 one_line_(true) { |
| 34 out << std::string(indent, ' ') << '<' << tag; |
| 35 for (auto attribute : attributes) |
| 36 out << ' ' << attribute.first << "=\"" << attribute.second << '"'; |
| 37 } |
| 38 |
| 39 XmlElementWriter::~XmlElementWriter() { |
| 40 if (!opening_tag_finished_) { |
| 41 out_ << "/>" << std::endl; |
| 42 } else { |
| 43 if (!one_line_) |
| 44 out_ << std::string(indent_, ' '); |
| 45 out_ << "</" << tag_ << '>' << std::endl; |
| 46 } |
| 47 } |
| 48 |
| 49 void XmlElementWriter::Text(const base::StringPiece& content) { |
| 50 StartContent(false); |
| 51 out_ << content; |
| 52 } |
| 53 |
| 54 scoped_ptr<XmlElementWriter> XmlElementWriter::SubElement( |
| 55 const std::string& tag) { |
| 56 return SubElement(tag, XmlAttributes()); |
| 57 } |
| 58 |
| 59 scoped_ptr<XmlElementWriter> XmlElementWriter::SubElement( |
| 60 const std::string& tag, |
| 61 const XmlAttributes& attributes) { |
| 62 StartContent(true); |
| 63 return make_scoped_ptr( |
| 64 new XmlElementWriter(out_, tag, attributes, indent_ + 2)); |
| 65 } |
| 66 |
| 67 std::ostream& XmlElementWriter::StartContent(bool start_new_line) { |
| 68 if (!opening_tag_finished_) { |
| 69 out_ << '>'; |
| 70 opening_tag_finished_ = true; |
| 71 |
| 72 if (start_new_line && one_line_) { |
| 73 out_ << std::endl; |
| 74 one_line_ = false; |
| 75 } |
| 76 } |
| 77 |
| 78 return out_; |
| 79 } |
OLD | NEW |