| 442 | class TXmlWriter { |
| 443 | public: |
| 444 | class TTag { |
| 445 | friend class TXmlWriter; |
| 446 | |
| 447 | explicit TTag(TXmlWriter* parent, TStringBuf name, size_t indent) |
| 448 | : Parent(parent) |
| 449 | , Name(name) |
| 450 | , Indent(indent) |
| 451 | { |
| 452 | Start(); |
| 453 | } |
| 454 | |
| 455 | public: |
| 456 | TTag(TTag&& tag) |
| 457 | : Parent(tag.Parent) |
| 458 | , Name(tag.Name) |
| 459 | { |
| 460 | tag.Parent = nullptr; |
| 461 | } |
| 462 | |
| 463 | ~TTag() { |
| 464 | if (Parent) { |
| 465 | End(); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | template <class T> |
| 470 | TTag& Attribute(TStringBuf name, const T& value) { |
| 471 | return Attribute(name, TStringBuf(ToString(value))); |
| 472 | } |
| 473 | |
| 474 | TTag& Attribute(TStringBuf name, const TStringBuf& value) { |
| 475 | Y_ABORT_UNLESS(!HasChildren); |
| 476 | Parent->Out << ' '; |
| 477 | Parent->Escape(name); |
| 478 | Parent->Out << "=\""; |
| 479 | Parent->Escape(value); |
| 480 | Parent->Out << '\"'; |
| 481 | return *this; |
| 482 | } |
| 483 | |
| 484 | TTag Tag(TStringBuf name) { |
| 485 | if (!HasChildren) { |
| 486 | HasChildren = true; |
| 487 | Close(); |
| 488 | } |
| 489 | return TTag(Parent, name, Indent + 1); |
| 490 | } |
| 491 | |
| 492 | TTag& Text(TStringBuf text) { |
| 493 | if (!HasChildren) { |
| 494 | HasChildren = true; |
| 495 | Close(); |
| 496 | } |
| 497 | Parent->Escape(text); |
| 498 | if (!text.empty() && text.back() == '\n') { |
| 499 | NewLineBeforeIndent = false; |
| 500 | } |
| 501 | return *this; |