summaryrefslogtreecommitdiff
path: root/src/xml.h
blob: 5e2cb64203e2b52641d78650ebafc3d1fb1d29bd (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
 * Copyright (c) 2003-2007, John Wiegley.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of New Artisans LLC nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _XML_H
#define _XML_H

#include "journal.h"
#include "value.h"
#include "parser.h"

namespace ledger {

class transaction_t;
class entry_t;
class account_t;
class journal_t;

namespace xml {

#define XML_NODE_IS_PARENT 0x1

DECLARE_EXCEPTION(conversion_error);

class parent_node_t;
class document_t;

class node_t
{
public:
  unsigned int	  name_id;
#ifdef THREADSAFE
  document_t *	  document;
#else
  static document_t * document;
#endif
  parent_node_t * parent;
  node_t *	  next;
  node_t *	  prev;
  unsigned int	  flags;

  typedef std::map<string, string>  attrs_map;
  typedef std::pair<string, string> attrs_pair;

  attrs_map * attrs;

  node_t(document_t * _document, parent_node_t * _parent = NULL,
	 unsigned int _flags = 0);

  virtual ~node_t() {
    TRACE_DTOR(node_t);
    if (parent) extract();
    if (attrs) checked_delete(attrs);
  }

  void extract();		// extract this node from its parent's child list

  virtual const char * text() const {
    assert(0);
    return NULL;
  }

  const char * name() const;
  int set_name(const char * _name);
  int set_name(int _name_id) {
    name_id = _name_id;
    return name_id;
  }

  void set_attr(const char * n, const char * v) {
    if (! attrs)
      attrs = new attrs_map;
    std::pair<attrs_map::iterator, bool> result =
      attrs->insert(attrs_pair(n, v));
    assert(result.second);
  }
  const char * get_attr(const char * n) {
    if (attrs) {
      attrs_map::iterator i = attrs->find(n);
      if (i != attrs->end())
	return (*i).second.c_str();
    }
    return NULL;
  }

  node_t * lookup_child(const char * _name) const;
  node_t * lookup_child(const string& _name) const;
  virtual node_t * lookup_child(int /* _name_id */) const {
    return NULL;
  }

  virtual value_t to_value() const {
    throw_(conversion_error, "Cannot convert node to a value");
  }

  virtual void print(std::ostream& out, int depth = 0) const = 0;

private:
  node_t(const node_t&);
  node_t& operator=(const node_t&);
};

class parent_node_t : public node_t
{
public:
  mutable node_t * _children;
  mutable node_t * _last_child;

  parent_node_t(document_t * _document, parent_node_t * _parent = NULL)
    : node_t(_document, _parent, XML_NODE_IS_PARENT),
      _children(NULL), _last_child(NULL)
  {
    TRACE_CTOR(parent_node_t, "document_t *, parent_node_t *");
  }
  virtual ~parent_node_t() {
    TRACE_DTOR(parent_node_t);
    if (_children) clear();
  }

  virtual void	   clear();	// clear out all child nodes
  virtual node_t * children() const {
    return _children;
  }
  virtual node_t * last_child() {
    if (! _children)
      children();
    return _last_child;
  }
  virtual void	   add_child(node_t * node);

  void print(std::ostream& out, int depth = 0) const;

private:
  parent_node_t(const parent_node_t&);
  parent_node_t& operator=(const parent_node_t&);
};

class terminal_node_t : public node_t
{
  string data;

public:
  terminal_node_t(document_t * _document, parent_node_t * _parent = NULL)
    : node_t(_document, _parent)
  {
    TRACE_CTOR(terminal_node_t, "document_t *, parent_node_t *");
  }
  virtual ~terminal_node_t() {
    TRACE_DTOR(terminal_node_t);
  }

  virtual const char * text() const {
    return data.c_str();
  }
  virtual void set_text(const char * _data) {
    data = _data;
  }
  virtual void set_text(const string& _data) {
    data = _data;
  }

  virtual value_t to_value() const {
    return text();
  }

  void print(std::ostream& out, int depth = 0) const;

private:
  terminal_node_t(const node_t&);
  terminal_node_t& operator=(const node_t&);
};

class document_t
{
  static const char * 	   ledger_builtins[];
  static const std::size_t ledger_builtins_size;

public:
  enum ledger_builtins_t {
    ACCOUNT = 10,
    ACCOUNT_PATH,
    AMOUNT,
    CODE,
    COMMODITY,
    ENTRIES,
    ENTRY,
    JOURNAL,
    NAME,
    NOTE,
    PAYEE,
    TRANSACTION
  };

private:
  typedef std::vector<string> names_array;

  names_array names;

  typedef std::map<string, int>  names_map;
  typedef std::pair<string, int> names_pair;

  names_map names_index;

  terminal_node_t stub;

 public:
  node_t * top;

  // Ids 0-9 are reserved.  10-999 are for "builtin" names.  1000+ are
  // for dynamically registered names.
  enum special_names_t {
    CURRENT, PARENT, ROOT, ALL
  };

  document_t(node_t * _top = NULL);
  ~document_t();

  void set_top(node_t * _top);

  int register_name(const string& name);
  int lookup_name_id(const string& name) const;
  static int lookup_builtin_id(const string& name);
  const char * lookup_name(int id) const;

  void print(std::ostream& out) const;

#if defined(HAVE_EXPAT) || defined(HAVE_XMLPARSE)
  class parser_t
  {
  public:
    document_t *	document;
    XML_Parser		parser;
    string		have_error;
    const char *	pending;
    node_t::attrs_map * pending_attrs;
    bool                handled_data;

    std::list<parent_node_t *> node_stack;

    parser_t() : document(NULL), pending(NULL), pending_attrs(NULL),
		 handled_data(false) {}
    virtual ~parser_t() {}

    virtual bool         test(std::istream& in) const;
    virtual document_t * parse(std::istream& in);
  };
#endif
};

#if defined(HAVE_EXPAT) || defined(HAVE_XMLPARSE)

class xml_parser_t : public parser_t
{
 public:
  virtual bool test(std::istream& in) const;

  virtual unsigned int parse(std::istream&	   in,
			     journal_t *	   journal,
			     account_t *	   master   = NULL,
			     const optional<path>& original = optional<path>());
};

DECLARE_EXCEPTION(parse_error);

#endif

class commodity_node_t : public parent_node_t
{
public:
  commodity_t * commodity;

  commodity_node_t(document_t *    _document,
		   commodity_t *   _commodity,
		   parent_node_t * _parent = NULL)
    : parent_node_t(_document, _parent), commodity(_commodity) {
    TRACE_CTOR(commodity_node_t, "document_t *, commodity_t *, parent_node_t *");
    set_name(document_t::COMMODITY);
  }
  virtual ~commodity_node_t() {
    TRACE_DTOR(commodity_node_t);
  }

  virtual node_t * children() const;
};

class amount_node_t : public parent_node_t
{
public:
  amount_t * amount;

  amount_node_t(document_t *    _document,
		amount_t *      _amount,
		parent_node_t * _parent = NULL)
    : parent_node_t(_document, _parent), amount(_amount) {
    TRACE_CTOR(amount_node_t, "document_t *, amount_t *, parent_node_t *");
    set_name(document_t::AMOUNT);
  }
  virtual ~amount_node_t() {
    TRACE_DTOR(amount_node_t);
  }

  virtual node_t * children() const;

  virtual value_t to_value() const {
    return *amount;
  }
};

class transaction_node_t : public parent_node_t
{
  mutable terminal_node_t * payee_virtual_node;

public:
  transaction_t * transaction;

  transaction_node_t(document_t *    _document,
		     transaction_t * _transaction,
		     parent_node_t * _parent = NULL)
    : parent_node_t(_document, _parent), payee_virtual_node(NULL),
      transaction(_transaction) {
    TRACE_CTOR(transaction_node_t, "document_t *, transaction_t *, parent_node_t *");
    set_name(document_t::TRANSACTION);
  }
  virtual ~transaction_node_t() {
    TRACE_DTOR(transaction_node_t);
    if (payee_virtual_node)
      checked_delete(payee_virtual_node);
  }

  virtual node_t * children() const;
  virtual node_t * lookup_child(int _name_id) const;
  virtual value_t  to_value() const;
};

class entry_node_t : public parent_node_t
{
  entry_t *  entry;

public:
  entry_node_t(document_t * _document, entry_t * _entry,
	       parent_node_t * _parent = NULL)
    : parent_node_t(_document, _parent), entry(_entry) {
    TRACE_CTOR(entry_node_t, "document_t *, entry_t *, parent_node_t *");
    set_name(document_t::ENTRY);
  }
  virtual ~entry_node_t() {
    TRACE_DTOR(entry_node_t);
  }

  virtual node_t * children() const;
  virtual node_t * lookup_child(int _name_id) const;

  friend class transaction_node_t;
};

class account_node_t : public parent_node_t
{
  account_t * account;

public:
  account_node_t(document_t * _document, account_t * _account,
		 parent_node_t * _parent = NULL)
    : parent_node_t(_document, _parent), account(_account) {
    TRACE_CTOR(account_node_t, "document_t *, account_t *, parent_node_t *");
    set_name(document_t::ACCOUNT);
  }
  virtual ~account_node_t() {
    TRACE_DTOR(account_node_t);
  }

  virtual node_t * children() const;
};

class journal_node_t : public parent_node_t
{
  journal_t * journal;

public:
  journal_node_t(document_t * _document, journal_t * _journal,
		 parent_node_t * _parent = NULL)
    : parent_node_t(_document, _parent), journal(_journal) {
    TRACE_CTOR(journal_node_t, "document_t *, journal_t *, parent_node_t *");
    set_name(document_t::JOURNAL);
  }
  virtual ~journal_node_t() {
    TRACE_DTOR(journal_node_t);
  }

  virtual node_t * children() const;

  friend class transaction_node_t;
};

template <typename T>
inline typename T::node_type *
wrap_node(document_t * doc, T * item, void * parent_node = NULL) {
  assert(false);
  return NULL;
}

template <>
inline transaction_t::node_type *
wrap_node(document_t * doc, transaction_t * xact, void * parent_node) {
  return new transaction_node_t(doc, xact, (parent_node_t *)parent_node);
}

template <>
inline entry_t::node_type *
wrap_node(document_t * doc, entry_t * entry, void * parent_node) {
  return new entry_node_t(doc, entry, (parent_node_t *)parent_node);
}

template <>
inline account_t::node_type *
wrap_node(document_t * doc, account_t * account, void * parent_node) {
  return new account_node_t(doc, account, (parent_node_t *)parent_node);
}

template <>
inline journal_t::node_type *
wrap_node(document_t * doc, journal_t * journal, void * parent_node) {
  return new journal_node_t(doc, journal, (parent_node_t *)parent_node);
}

} // namespace xml
} // namespace ledger

#endif // _XML_H