summaryrefslogtreecommitdiffstats
path: root/include/orcus/xml_writer.hpp
blob: b55485c133d714c5fd37d6ae8cb2a8b67a9aa356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDED_ORCUS_XML_WRITER_HPP
#define INCLUDED_ORCUS_XML_WRITER_HPP

#include "orcus/types.hpp"

#include <memory>

namespace orcus {

class xmlns_repository;

/**
 * This class lets you produce XML contents from scratch.  It writes its
 * content to any object supporting the std::ostream interface.
 */
class ORCUS_PSR_DLLPUBLIC xml_writer
{
    struct impl;
    std::unique_ptr<impl> mp_impl;

    void close_current_element();
    void pop_elements();

public:
    class ORCUS_PSR_DLLPUBLIC scope
    {
        friend class xml_writer;

        struct impl;
        std::unique_ptr<impl> mp_impl;

        scope(xml_writer* parent, const xml_name_t& name);
    public:
        scope(const scope&) = delete;
        scope(scope&& other);
        ~scope();

        scope& operator= (scope&& other);
    };

    xml_writer(const xml_writer&) = delete;
    xml_writer& operator= (const xml_writer&) = delete;

    xml_writer(xmlns_repository& ns_repo, std::ostream& os);
    xml_writer(xml_writer&& other);

    xml_writer& operator= (xml_writer&& other);

    /**
     * Destructor. Any remaining element(s) on the stack will get popped when
     * the destructor is called.
     */
    ~xml_writer();

    /**
     * Push a new element to the stack, and write an opening element to the
     * output stream.  It differs from the {@link push_element} method in that
     * the new element will be automatically popped when the returned object
     * goes out of scope.
     *
     * @param name name of the new element.
     *
     * @return scope object which automatically pops the element when it goes
     *         out of scope.
     */
    scope push_element_scope(const xml_name_t& name);

    /**
     * Push a new element to the stack, and write an opening element to the
     * output stream.
     *
     * @param name name of the element.
     */
    void push_element(const xml_name_t& name);

    /**
     * Add a namespace definition for the next element to be pushed.
     *
     * @param alias alias for the namespace.
     * @param value value of the namespace definition.
     *
     * @return ID for the namespace being added.
     */
    xmlns_id_t add_namespace(std::string_view alias, std::string_view value);

    /**
     * Add a new attribute for the next element to be pushed.
     *
     * @param name name of the attribute to be added.
     * @param value value of the attribute to be added.
     */
    void add_attribute(const xml_name_t& name, std::string_view value);

    /**
     * Add a content to the current element on the stack.  The content will be
     * properly encoded.
     *
     * @param content content to be added to the current element.
     */
    void add_content(std::string_view content);

    /**
     * Pop the current element from the stack, and write a closing element to
     * the output stream.
     *
     * @return the name of the element being popped.
     */
    xml_name_t pop_element();
};

}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */