aboutsummaryrefslogtreecommitdiff
path: root/tests/xml/roundtrip/driver.cxx
blob: b0b64973c4d1491d0c387c3ffac1081a0a813ed0 (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
// file      : tests/xml/roundtrip/driver.cxx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <string>
#include <fstream>
#include <cassert>
#include <iostream>

#include <cutl/xml/parser.hxx>
#include <cutl/xml/serializer.hxx>

using namespace std;
namespace xml = cutl::xml;
using namespace xml;

const bool trace = false;

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

  try
  {
    ifstream ifs;
    ifs.exceptions (ifstream::badbit | ifstream::failbit);
    ifs.open (argv[1], ifstream::in | ifstream::binary);

    parser p (ifs,
              argv[1],
              parser::receive_default |
              parser::receive_attributes_event |
              parser::receive_namespace_decls);

    serializer s (cout, "out", 0);

    bool in_attr (false);
    for (parser::event_type e (p.next ()); e != parser::eof; e = p.next ())
    {
      switch (e)
      {
      case parser::start_element:
        {
          if (trace)
            cerr << p.line () << ':' << p.column () << ": " << e << " "
                 << p.namespace_() << (p.namespace_().empty () ? "" : "#")
                 << p.prefix () << (p.prefix ().empty () ? "" : ":")
                 << p.name () << endl;

          s.start_element (p.qname ());
          break;
        }
      case parser::end_element:
        {
          if (trace)
            cerr << p.line () << ':' << p.column () << ": " << e << " "
                 << p.namespace_() << (p.namespace_().empty () ? "" : "#")
                 << p.prefix () << (p.prefix ().empty () ? "" : ":")
                 << p.name () << endl;

          s.end_element ();
          break;
        }
      case parser::start_namespace_decl:
        {
          if (trace)
            cerr << "  " << p.prefix () << "->" << p.namespace_ () << endl;

          s.namespace_decl (p.namespace_ (), p.prefix ());
          break;
        }
      case parser::end_namespace_decl:
        {
          if (trace)
            cerr << "  " << p.prefix () << "-x" << endl;

          break;
        }
      case parser::start_attribute:
        {
          if (trace)
            cerr << "  " << p.qname () << "=";

          s.start_attribute (p.qname ());
          in_attr = true;
          break;
        }
      case parser::end_attribute:
        {
          s.end_attribute ();
          in_attr = false;
          break;
        }
      case parser::characters:
        {
          if (trace)
          {
            if (!in_attr)
              cerr << p.line () << ':' << p.column () << ": " << e << " ";

            cerr << "'" << p.value () << "'" << endl;
          }

          s.characters (p.value ());
          break;
        }
      default:
        break;
      }
    }
  }
  catch (const ios_base::failure& e)
  {
    cerr << "io failure" << endl;
    return 1;
  }
  catch (const xml::exception& e)
  {
    cerr << e.what () << endl;
    return 1;
  }
}