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
123
124
125
126
127
128
129
130
131
|
#ifndef _BUILDER_H
#define _BUILDER_H
#include "xml.h"
namespace ledger {
/**
* @class builder_t
*
* @brief Represents an interface for building a data hierarchy.
*
* This interface is much like .NET's XmlWriter facility. It
* abstracts the kind of hierarchy we're building, instead focusing
* only on the relationships.
*/
class builder_t
{
public:
struct position_t
{
typedef uint_least32_t file_pos_t;
typedef uint_least32_t file_line_t;
path pathname;
file_pos_t offset;
file_line_t linenum;
position_t() : offset(0), linenum(0) {}
explicit position_t(path _pathname,
file_pos_t _offset,
file_line_t _linenum)
: pathname(_pathname),
offset(_offset), linenum(_linenum) {}
};
position_t current_position;
virtual void start_position(const std::istream& in,
const path& pathname) {
set_position(position_t(pathname, in.tellg(), 1));
}
virtual void set_position(const position_t& position) {
current_position = position;
}
virtual position_t& position() {
return current_position;
}
virtual void push_attr(const string& name,
const string& value) = 0;
virtual void push_attr(const nameid_t name_id,
const string& value) = 0;
virtual void begin_node(const string& name) = 0;
virtual void begin_node(const nameid_t name_id) = 0;
template <typename T>
virtual void begin_node(typename T::pointer data) = 0;
virtual void push_node(const string& name,
const optional<position_t>& end_pos) = 0;
virtual void push_node(const nameid_t name_id,
const optional<position_t>& end_pos) = 0;
virtual node_t * current_node() = 0;
template <typename T>
virtual T * current_node() = 0;
virtual void append_text(const string& text) = 0;
virtual node_t * end_node(const string& name,
const optional<position_t>& end_pos) = 0;
virtual node_t * end_node(const nameid_t name_id,
const optional<position_t>& end_pos) = 0;
};
/**
* @class xml_builder_t
*
* @brief Build a generic node_t hierarchy.
*
* This builder can be used to parse ordinary XML into a document
* object structure which can then be traversed in memory.
*/
class xml_builder_t : public builder_t
{
};
/**
* @class journal_builder_t
*
* @brief This custom builder creates an XML-mirrored Ledger journal.
*
* Rather than simply creating a node_t hierarchy, as xml_builder_t
* does, this code creates the associated journal elements referred to
* by those nodes, and then refers to those elements via minimalist
* "shadow nodes".
*
* Thus, after building a <transaction> element, the element itself
* will have no children, but instead will point to a transaction_t
* object. If later an XPath expression desires to traverse the
* <transaction> element, all of the appropriate child nodes will be
* constructed on the fly, as if they'd been created in the first
* place by a regular xml_builder_t.
*/
class journal_builder_t : public xml_builder_t
{
};
/**
* @class xml_writer_t
*
* @brief Create textual XML on the given output stream.
*
* This builder, rather than manipulating data structures in memory,
* simply streams its contents on the fly to the given output stream.
* It uses only enough memory to remember the currently push
* attributes and text.
*/
class xml_writer_t : public builder_t
{
};
} // namespace ledger
#endif // _BUILDER_H
|