aboutsummaryrefslogtreecommitdiff
path: root/xml/parser.ixx
blob: c0fe25611647063e2d70c22a83aeb67a744232e9 (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
// file      : xml/parser.ixx
// copyright : Copyright (c) 2013-2014 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <cassert>

#include <xml/value-traits.hxx>

namespace xml
{
  inline parser::
  parser (std::istream& is, const std::string& iname, feature_type f)
      : size_ (0), iname_ (iname), feature_ (f)
  {
    data_.is = &is;
    init ();
  }

  inline parser::
  parser (const void* data,
          std::size_t size,
          const std::string& iname,
          feature_type f)
      : size_ (size), iname_ (iname), feature_ (f)
  {
    assert (data != 0 && size != 0);

    data_.buf = data;
    init ();
  }

  template <typename T>
  inline T parser::
  value () const
  {
    return value_traits<T>::parse (value (), *this);
  }

  inline const std::string& parser::
  attribute (const std::string& n) const
  {
    return attribute (qname_type (n));
  }

  template <typename T>
  inline T parser::
  attribute (const std::string& n) const
  {
    return attribute<T> (qname_type (n));
  }

  inline std::string parser::
  attribute (const std::string& n, const std::string& dv) const
  {
    return attribute (qname_type (n), dv);
  }

  template <typename T>
  inline T parser::
  attribute (const std::string& n, const T& dv) const
  {
    return attribute<T> (qname_type (n), dv);
  }

  template <typename T>
  inline T parser::
  attribute (const qname_type& qn) const
  {
    return value_traits<T>::parse (attribute (qn), *this);
  }

  inline bool parser::
  attribute_present (const std::string& n) const
  {
    return attribute_present (qname_type (n));
  }

  inline const parser::attribute_map_type& parser::
  attribute_map () const
  {
    if (const element_entry* e = get_element ())
    {
      e->attr_unhandled_ = 0; // Assume all handled.
      return e->attr_map_;
    }

    return empty_attr_map_;
  }

  inline void parser::
  next_expect (event_type e, const qname_type& qn)
  {
    next_expect (e, qn.namespace_ (), qn.name ());
  }

  inline void parser::
  next_expect (event_type e, const std::string& n)
  {
    next_expect (e, std::string (), n);
  }

  inline void parser::
  next_expect (event_type e, const qname_type& qn, content_type c)
  {
    next_expect (e, qn);
    assert (e == start_element);
    content (c);
  }

  inline void parser::
  next_expect (event_type e, const std::string& n, content_type c)
  {
    next_expect (e, std::string (), n);
    assert (e == start_element);
    content (c);
  }

  inline void parser::
  next_expect (event_type e,
               const std::string& ns, const std::string& n,
               content_type c)
  {
    next_expect (e, ns, n);
    assert (e == start_element);
    content (c);
  }

  template <typename T>
  inline T parser::
  element ()
  {
    return value_traits<T>::parse (element (), *this);
  }

  inline std::string parser::
  element (const std::string& n)
  {
    next_expect (start_element, n);
    return element ();
  }

  inline std::string parser::
  element (const qname_type& qn)
  {
    next_expect (start_element, qn);
    return element ();
  }

  template <typename T>
  inline T parser::
  element (const std::string& n)
  {
    return value_traits<T>::parse (element (n), *this);
  }

  template <typename T>
  inline T parser::
  element (const qname_type& qn)
  {
    return value_traits<T>::parse (element (qn), *this);
  }

  inline std::string parser::
  element (const std::string& n, const std::string& dv)
  {
    return element (qname_type (n), dv);
  }

  template <typename T>
  inline T parser::
  element (const std::string& n, const T& dv)
  {
    return element<T> (qname_type (n), dv);
  }

  inline const parser::element_entry* parser::
  get_element () const
  {
    return element_state_.empty () ? 0 : get_element_ ();
  }
}