aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/tree/mixed/driver.cxx
blob: 3347d7fd1f8ed16f9900838c05f60040b2f79baf (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
// file      : examples/cxx/tree/mixed/driver.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : not copyrighted - public domain

#include <memory>   // std::auto_ptr
#include <iostream>

#include <xercesc/dom/DOM.hpp>

#include "text.hxx"

// The following transcode() utility function is handy when working with
// Xerces. Include it after the generated header in order to get only char
// or wchar_t version depending on how you compiled your schemas.
//
#include <xsd/cxx/xml/string.hxx>

using std::cout;
using std::cerr;
using std::endl;
using std::auto_ptr;

int
main (int argc, char* argv[])
{
  if (argc != 2)
  {
    cerr << "usage: " << argv[0] << " text.xml" << endl;
    return 1;
  }

  using namespace xercesc;

  int r (0);

  // The Xerces-C++ DOM objects that will be associated with the
  // document tree "out-live" the call to the parsing function.
  // Therefore we need to initialize the Xerces-C++ runtime
  // ourselves.
  //
  XMLPlatformUtils::Initialize ();

  try
  {
    auto_ptr<text> t (
      text_ (argv[1],
             xml_schema::flags::keep_dom |
             xml_schema::flags::dont_initialize));

    // The DOM association can be recreated in a copy (the underlying
    // DOM document is cloned) if explicitly requested with the keep_dom
    // flag and only if this copy is "complete", i.e., made from the root
    // of the tree.
    //
    text copy (*t, xml_schema::flags::keep_dom);

    // Print text.
    //
    {
      namespace xml = xsd::cxx::xml;

      unsigned long ref (0);
      DOMNode* root (copy._node ());

      for (DOMNode* n (root->getFirstChild ());
           n != 0;
           n = n->getNextSibling ())
      {
        switch (n->getNodeType ())
        {
        case DOMNode::TEXT_NODE:
          {
            cout << xml::transcode<char> (n->getTextContent ());
            break;
          }
        case DOMNode::ELEMENT_NODE:
          {
            // Let's get back to a tree node from this DOM node.
            //
            xml_schema::type& t (
              *reinterpret_cast<xml_schema::type*> (
                n->getUserData (xml_schema::dom::tree_node_key)));

	    anchor& a (dynamic_cast<anchor&> (t));

            cout << a << "[" << ref << "]";

            // Or we could continue using DOM interface:
            //
            //cout << xml::transcode<char> (n->getTextContent ())
            //     << "[" << ref << "]";

            ++ref;
            break;
          }
        default:
          break; // Ignore all other nodes (e.g., comments, etc).
        }
      }
    }

    // Print references.
    //
    {
      unsigned long r (0);

      for (text::a_const_iterator i (copy.a ().begin ());
           i != copy.a ().end ();
           ++i, ++r)
      {
        cout << "[" << r << "] "  << i->href () << endl;
      }
    }
  }
  catch (const xml_schema::exception& e)
  {
    cerr << e << endl;
    r = 1;
  }

  XMLPlatformUtils::Terminate ();
  return r;
}