summaryrefslogtreecommitdiff
path: root/examples/cxx/tree/streaming/parser.cxx
blob: 41ad7aff366b698d518c83e1d50bdd2a60489ce9 (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
#include <cassert>

#include <xercesc/util/XMLUni.hpp>
#include <xercesc/util/XMLString.hpp>

#include <xercesc/sax2/Attributes.hpp>
#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp>

#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/impl/DOMTextImpl.hpp>

#include <xercesc/validators/common/Grammar.hpp> // xercesc::Grammar
#include <xercesc/framework/XMLGrammarPoolImpl.hpp>

#include <xsd/cxx/auto-array.hxx>

#include <xsd/cxx/xml/sax/std-input-source.hxx>
#include <xsd/cxx/xml/sax/bits/error-handler-proxy.hxx>

#include <xsd/cxx/tree/exceptions.hxx>
#include <xsd/cxx/tree/error-handler.hxx>

#include "parser.hxx"
#include "grammar-input-stream.hxx"

using namespace std;
using namespace xercesc;

namespace xml = xsd::cxx::xml;
namespace tree = xsd::cxx::tree;

typedef parser::document_ptr document_ptr;

class parser_impl: public DefaultHandler
{
public:
  parser_impl (const XMLByte* grammar, size_t grammar_size);

  void
  start (istream& is, const string& id, bool validate);

  document_ptr
  peek ();

  document_ptr
  next (document_ptr doc = document_ptr (),
        document_ptr outer_doc = document_ptr ());

  // SAX event handlers.
  //
private:
  virtual void
  startElement (const XMLCh* const uri,
                const XMLCh* const lname,
                const XMLCh* const qname,
                const Attributes& attributes);

  virtual void
  endElement (const XMLCh* const uri,
              const XMLCh* const lname,
              const XMLCh* const qname);

  virtual void
  characters (const XMLCh* const s,
              const XMLSize_t length);

private:
  // SAX parser.
  //
  bool clean_;
  auto_ptr<XMLGrammarPool> grammar_pool_;
  auto_ptr<SAX2XMLReader> parser_;
  XMLPScanToken token_;
  tree::error_handler<char> error_handler_;
  xml::sax::bits::error_handler_proxy<char> error_proxy_;
  auto_ptr<xml::sax::std_input_source> isrc_;

  size_t depth_;
  size_t whitespace_depth_; // Depth at which to ignore whitespaces.

  bool peek_;
  size_t next_depth_; // Depth at which next() should work.

  // DOM document being built.
  //
  DOMImplementation& dom_impl_;
  document_ptr doc_;
  DOMElement* cur_;
};

const XMLCh ls[] = {chLatin_L, chLatin_S, chNull};

parser_impl::
parser_impl (const XMLByte* grammar, size_t grammar_size)
    : clean_ (true),
      error_proxy_ (error_handler_),
      dom_impl_ (*DOMImplementationRegistry::getDOMImplementation (ls))
{
  MemoryManager* mm (XMLPlatformUtils::fgMemoryManager);

  if (grammar != 0)
  {
    assert (grammar_size != 0);
    grammar_pool_.reset (new XMLGrammarPoolImpl (mm));

    grammar_input_stream is (grammar, grammar_size);
    grammar_pool_->deserializeGrammars(&is);
    grammar_pool_->lockPool ();
  }

  parser_.reset (XMLReaderFactory::createXMLReader (mm, grammar_pool_.get ()));

  parser_->setFeature (XMLUni::fgSAX2CoreNameSpaces, true);
  parser_->setFeature (XMLUni::fgSAX2CoreNameSpacePrefixes, true);
  parser_->setFeature (XMLUni::fgXercesValidationErrorAsFatal, true);
  parser_->setFeature (XMLUni::fgXercesSchemaFullChecking, false);

  // Xerces-C++ 3.1.0 is the first version with working multi import
  // support. It also allows us to disable buffering in the parser
  // so that the data is parsed and returned as soon as it is
  // available.
  //
#if _XERCES_VERSION >= 30100
  parser_->setFeature (XMLUni::fgXercesHandleMultipleImports, true);

  XMLSize_t lwm = 0;
  parser_->setProperty (XMLUni::fgXercesLowWaterMark, &lwm);
#endif

  parser_->setErrorHandler (&error_proxy_);
  parser_->setContentHandler (this);
}

void parser_impl::
start (istream& is, const string& id, bool val)
{
  // Reset our state.
  //
  depth_ = 0;
  peek_ = false;
  doc_.reset ();
  error_handler_.reset ();

  if (!clean_)
    parser_->parseReset (token_);
  else
    clean_ = false;

  isrc_.reset (new xml::sax::std_input_source (is, id));

  parser_->setFeature (XMLUni::fgSAX2CoreValidation, val);
  parser_->setFeature (XMLUni::fgXercesSchema, val);

  if (val && grammar_pool_.get () != 0)
  {
    // Use the loaded grammar during parsing.
    //
    parser_->setFeature (XMLUni::fgXercesUseCachedGrammarInParse, true);

    // Disable loading schemas via other means (e.g., schemaLocation).
    //
    parser_->setFeature (XMLUni::fgXercesLoadSchema, false);
  }

  parser_->parseFirst (*isrc_, token_);
  error_handler_.throw_if_failed<tree::parsing<char> > ();
}

document_ptr parser_impl::
peek ()
{
  bool r (true);

  size_t d (depth_);
  whitespace_depth_ = d;

  peek_ = true;

  // Parse (skip whitespace content) until the depth increases or we get
  // a document. The latter test covers <element/> cases where both start
  // and end events will trigger and therefore leave the depth unchanged.
  //
  while (r && depth_ == d && doc_.get () == 0)
  {
    r = parser_->parseNext (token_);
    error_handler_.throw_if_failed<tree::parsing<char> > ();
  }

  if (!r)
    return document_ptr (0);

  return doc_;
}

document_ptr parser_impl::
next (document_ptr doc, document_ptr outer_doc)
{
  assert (peek_ == (doc.get () != 0));

  // Install doc/outer_doc as the document we are parsing.
  //
  if (doc.get () != 0)
  {
    if (outer_doc.get () != 0)
    {
      // Copy doc to outer_doc.
      //
      doc_ = outer_doc;
      cur_ = static_cast<DOMElement*> (
        doc_->importNode (doc->getDocumentElement (), true));
      doc_->getDocumentElement ()->appendChild (cur_);
    }
    else
    {
      doc_ = doc;
      cur_ = doc_->getDocumentElement ();
    }

    // This handles the <element/> case where we get both start and
    // end events in peek(). In this case the element is fully parsed
    // and next() has nothing to do.
    //
    if (depth_ != next_depth_)
    {
      peek_ = false;
      return doc_;
    }
  }

  bool r (true);

  // If we peeked, then we have already seen the start tag and our
  // return depth is one above the current depth.
  //
  size_t d (peek_ ? depth_ - 1 : depth_);
  whitespace_depth_ = d;

  peek_ = false;

  // Keep calling parseNext() until we either move to a greater depth or
  // get a document. This way we skip the text (presumably whitespaces)
  // that may be preceding this chunk.
  //
  while (r && depth_ == d && doc_.get () == 0)
  {
    parser_->parseNext (token_);
    error_handler_.throw_if_failed<tree::parsing<char> > ();
  }

  if (!r)
    return document_ptr (0);

  // If we are not at our start depth, keep calling parseNext() until we
  // get there again.
  //
  while (r && depth_ != d)
  {
    r = parser_->parseNext (token_);
    error_handler_.throw_if_failed<tree::parsing<char> > ();
  }

  if (!r)
    return document_ptr (0);

  return doc_;
}

// DOM builder.
//

void parser_impl::
startElement (const XMLCh* const uri,
              const XMLCh* const /*lname*/,
              const XMLCh* const qname,
              const Attributes& attr)
{
  if (doc_.get () == 0)
  {
    doc_.reset (dom_impl_.createDocument (uri, qname, 0));
    cur_ = doc_->getDocumentElement ();
  }
  else
  {
    DOMElement* e = doc_->createElementNS (uri, qname);
    cur_->appendChild (e);
    cur_ = e;
  }

  // Set attributes.
  //
  for (XMLSize_t i (0), end (attr.getLength()); i < end; ++i)
  {
    const XMLCh* qn (attr.getQName (i));
    const XMLCh* ns (attr.getURI (i));

    // When SAX2 reports the xmlns attribute, it does not include
    // the proper attribute namespace. So we have to detect and
    // handle this case.
    //
    if (XMLString::equals (qn, XMLUni::fgXMLNSString))
      ns = XMLUni::fgXMLNSURIName;

    cur_->setAttributeNS (ns, qn, attr.getValue (i));
  }

  depth_++;

  if (peek_)
    next_depth_ = depth_;
}

void parser_impl::
endElement (const XMLCh* const /*uri*/,
            const XMLCh* const /*lname*/,
            const XMLCh* const /*qname*/)
{
  // We have an element parent only on depth 2 or greater.
  //
  if (--depth_ > 1)
    cur_ = static_cast<DOMElement*> (cur_->getParentNode ());
}

void parser_impl::
characters (const XMLCh* const s, const XMLSize_t length)
{
  const XMLCh empty[] = {chNull};

  // Ignore text content (presumably whitespaces) while looking for
  // the next element.
  //
  if (depth_ > whitespace_depth_)
  {
    DOMText* t = doc_->createTextNode (empty);
    static_cast<DOMTextImpl*> (t)->appendData (s, length);
    cur_->appendChild (t);
  }
}

//
// parser
//

parser::
~parser ()
{
}

parser::
parser (const XMLByte* grammar, size_t grammar_size)
    : impl_ (new parser_impl (grammar, grammar_size))
{
}

void parser::
start (istream& is, const string& id, bool val)
{
  return impl_->start (is, id, val);
}

document_ptr parser::
peek ()
{
  return impl_->peek ();
}

document_ptr parser::
next (document_ptr doc, document_ptr outer_doc)
{
  return impl_->next (doc, outer_doc);
}