aboutsummaryrefslogtreecommitdiff
path: root/tests/cxx/tree/any-type/driver.cxx
blob: af7b80a56c76580a2d96c387f995cfcf97190c85 (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
// file      : tests/cxx/tree/any-type/driver.cxx
// copyright : Copyright (c) 2006-2017 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

// Test anyType and anySimpleType content extraction.
//

#include <memory>  // std::auto_ptr/unique_ptr
#include <utility> // std::move
#include <sstream>
#include <iostream>

#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/PlatformUtils.hpp>

#include "test.hxx" // Get XSD_CXX11 defined.

#include <xsd/cxx/xml/string.hxx>

using namespace std;
using namespace test;
using namespace xercesc;

namespace xml = xsd::cxx::xml;

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

  XMLPlatformUtils::Initialize ();

  try
  {
    // Test parsing
    //
    XSD_AUTO_PTR<type> r (root (argv[1]));

    // Test API.
    //
    {
      assert (type::a_default_value ().text_content () == "default value");
    }

    {
      xml_schema::simple_type x ("fox");
      assert (x.text_content () == "fox");
      x.text_content ("foo");
      assert (x.text_content () == "foo");
      x.text_content ().clear ();
      assert (x.text_content () == "");
      x.text_content () = "baz";
      r->s ().push_back (x);
    }

    {
      xml_schema::type x;

      DOMDocument& doc (x.dom_content_document ());

      // Copy.
      //
      DOMElement* e (doc.createElement (xml::string ("dummy").c_str ()));
      e->setAttribute (xml::string ("x").c_str (),
                       xml::string ("foo").c_str ());
      e->setTextContent (xml::string ("data").c_str ());
      x.dom_content ().set (*e);
      e->release ();

      r->t ().push_back (x);
    }

    {
      XSD_AUTO_PTR<xml_schema::type> x (new xml_schema::type);

      DOMDocument& doc (x->dom_content_document ());

      // Assume ownership.
      //
      DOMElement* e (doc.createElement (xml::string ("dummy").c_str ()));
      e->setAttribute (xml::string ("x").c_str (),
                       xml::string ("foo").c_str ());
      e->setTextContent (xml::string ("data").c_str ());
      x->dom_content ().set (e);

#ifdef XSD_CXX11
      r->t ().push_back (std::move (x));
#else
      r->t ().push_back (x);
#endif
    }

    // Test printing.
    //
    cout << *r << endl
         << endl;

    // Test serialization.
    //
    xml_schema::namespace_infomap map;

    map["t"].name   = "test";
    map["t"].schema = "test.xsd";
    map["o"].name   = "other";

    stringstream iostr;
    root (iostr, *r, map);

    cout << iostr.str () << endl
         << endl;

    {
      XSD_AUTO_PTR<type> r1 (root (iostr, argv[1]));

      // Xerces-C++ mis-indentation of mixed content messes this up.
      // assert (*r == *r);

      stringstream iostr;
      root (iostr, *r1, map);

      cout << iostr.str () << endl
           << endl;
    }

    // Test comparison.
    //
    assert (*r == *r);

    // Test copy c-tor.
    //
    type copy (*r);
    assert (copy == *r);
  }
  catch (xml_schema::exception const& e)
  {
    cerr << e << endl;
    return 1;
  }

  XMLPlatformUtils::Terminate ();
}