summaryrefslogtreecommitdiff
path: root/src/builder.h
blob: 1952fe634a9fc7f4081c8f6d2ae2f841b82edabf (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#ifndef _BUILDER_H
#define _BUILDER_H

#include "document.h"

namespace ledger {
namespace xml {

/**
 * @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;

    file_pos_t	offset;
    file_line_t linenum;

    position_t() : offset(0), linenum(0) {}

    explicit position_t(file_pos_t  _offset,
			file_line_t _linenum)
      : offset(_offset), linenum(_linenum) {}
  };

protected:
  position_t current_position;

public:
  virtual void     set_start_position(std::istream& in) {}
  virtual void     set_position(const position_t& position) {}
  virtual position_t& position() { return current_position; }

  virtual void     push_attr(const string&  name,
			     const string& value) = 0;
  virtual void     push_attr(const node_t::nameid_t name_id,
			     const string& value) = 0;

  virtual void     begin_node(const string& name, bool terminal = false)     = 0;
  virtual void     begin_node(const node_t::nameid_t name_id, bool terminal = false) = 0;

  virtual void     push_node(const string& name,
			     const optional<position_t>& end_pos = none) = 0;
  virtual void     push_node(const node_t::nameid_t name_id,
			     const optional<position_t>& end_pos = none) = 0;

  virtual node_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 = none) = 0;
  virtual node_t * end_node(const node_t::nameid_t name_id,
			     const optional<position_t>& end_pos = none) = 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 document_builder_t : public builder_t
{
  typedef std::list<std::pair<node_t::nameid_t, string> > attrs_list;

  document_t& document_;
  attrs_list  current_attrs;
  node_t *    current;
  string      current_text;

public:
  document_builder_t(document_t& _document)
    : document_(_document), current(&document_) {}

  virtual void     push_attr(const string&  name,
			     const string& value) {
    push_attr(document().register_name(name), value);
  }
  virtual void     push_attr(const node_t::nameid_t name_id,
			     const string& value) {
    current_attrs.push_back(attrs_list::value_type(name_id, value.c_str()));
  }

  virtual void     begin_node(const string& name, bool terminal = false) {
    begin_node(document().register_name(name), terminal);
  }
  virtual void     begin_node(const node_t::nameid_t name_id,
			      bool terminal = false) {
    if (terminal)
      current = current->as_parent_node().create_child<terminal_node_t>(name_id);
    else
      current = current->as_parent_node().create_child<parent_node_t>(name_id);

    foreach (const attrs_list::value_type& pair, current_attrs)
      current->set_attr(pair.first, pair.second.c_str());
    current_attrs.clear();
  }

  virtual void     push_node(const string& name,
			     const optional<position_t>& end_pos = none) {
    begin_node(name, true);
    end_node(name, end_pos);
  }
  virtual void     push_node(const node_t::nameid_t name_id,
			     const optional<position_t>& end_pos = none) {
    begin_node(name_id, true);
    end_node(name_id, end_pos);
  }

  virtual document_t& document() {
    return document_;
  }
  virtual node_t * current_node() {
    return current;
  }

  virtual void     append_text(const string& text) {
    assert(! current->is_parent_node());
    polymorphic_downcast<terminal_node_t *>(current)->set_text(text);
  }

  virtual node_t * end_node(const string& name,
			    const optional<position_t>& end_pos = none) {
    current = &*current->parent();
  }
  virtual node_t * end_node(const node_t::nameid_t name_id,
			    const optional<position_t>& end_pos = none) {
    current = &*current->parent();
  }
};

/**
 * @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 document_builder_t
{
public:
  virtual void set_start_position(std::istream& in) {
    set_position(position_t(in.tellg(), 1));
  }

  virtual void set_position(const position_t& position) {
    current_position = position;
  }
};

/**
 * @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
{
  typedef std::list<std::pair<string, string> > attrs_list;

  attrs_list	current_attrs;
  std::ostream& outs;

public:
  xml_writer_t(std::ostream& _outs) : outs(_outs) {
    outs << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    begin_node("ledger");
  }
  ~xml_writer_t() {
    end_node("ledger");
  }

  virtual void     push_attr(const string&  name,
			     const string& value) {
    current_attrs.push_back(attrs_list::value_type(name, value));
  }
  virtual void     push_attr(const node_t::nameid_t name_id,
			     const string& value) {
    push_attr("hello", value);
  }

  virtual void     begin_node(const string& name, bool terminal = false) {
    outs << '<' << name;
    foreach (const attrs_list::value_type& attr, current_attrs)
      outs << ' ' << attr.first << "=\"" << attr.second << "\"";
    current_attrs.clear();
    outs << '>';
  }
  virtual void     begin_node(const node_t::nameid_t name_id, bool terminal = false) {
    begin_node("hello");
  }

  virtual void     push_node(const string& name,
			     const optional<position_t>& end_pos = none) {
    begin_node(name, true);
    end_node(name, end_pos);
  }
  virtual void     push_node(const node_t::nameid_t name_id,
			     const optional<position_t>& end_pos = none) {
    push_node("hello", end_pos);
  }

  virtual node_t * current_node() { return NULL; }

  virtual void     append_text(const string& text) {
    outs << text;
  }

  virtual node_t * end_node(const string& name,
			    const optional<position_t>& end_pos = none) {
    outs << "</" << name << '>';
  }
  virtual node_t * end_node(const node_t::nameid_t name_id,
			    const optional<position_t>& end_pos = none) {
    end_node("hello", end_pos);
  }
};

} // namespace xml
} // namespace ledger

#endif // _BUILDER_H