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

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

#include <dbxml/DbXml.hpp>

#include "library.hxx"

using std::cerr;
using std::endl;
using std::string;
using std::auto_ptr;

using namespace DbXml;
using namespace xsd::cxx; // for xml::string

void
print_document (const string& desc,
                XmlContainer container,
                const string& name)
{
  XmlDocument doc (container.getDocument (name));

  string content;
  doc.getContent (content);

  cerr << endl
       << desc << endl
       << content << endl;
}

int
main ()
{
  try
  {
    using namespace library;
    using xml_schema::date;

    XmlManager manager;

    {
      XmlContainer container (manager.createContainer ("new.bdbxml"));

      XmlUpdateContext update_context (manager.createUpdateContext ());

      XmlQueryContext context (manager.createQueryContext ());
      context.setNamespace ("lib", "http://www.codesynthesis.com/library");


      // Create a new document from an object model.
      //
      {
        // Create a new catalog with one book.
        //
        catalog c;

        book b (20530902,                // ISBN
                "The Elements of Style", // Title
                genre::reference,        // Genre
                "ES");                   // ID

        author strunk ("William Strunk, Jr.", date (1869, 7, 1));
        strunk.died (date (1946, 9, 26));

        b.author ().push_back (strunk);
        c.book ().push_back (b);


        // Create a new XML document.
        //
        XmlDocument doc (manager.createDocument ());
        doc.setName ("new.xml");


        // Obtain its DOM representation and add the root element.
        //
        xercesc::DOMDocument& dom_doc (*doc.getContentAsDOM ());

        dom_doc.appendChild (
          dom_doc.createElementNS (
            xml::string ("http://www.codesynthesis.com/library").c_str (),
            xml::string ("lib:catalog").c_str ()));


        // Serialize the object model to the XML document. Also avoid
	// re-initializing the Xerces-C++ runtime since XmlManager has
	// it initialized.
        //
        catalog_ (dom_doc, c, xml_schema::flags::dont_initialize);


        // Place the document into the container.
        //
        container.putDocument (doc, update_context);

        print_document ("after create:", container, "new.xml");
      }

      // Create an object model from a document in DB.
      //
      {
        // Resolve the document in the container.
        //
        XmlDocument doc (container.getDocument ("new.xml"));


        // Create the object model from the document's DOM. Also avoid
	// re-initializing the Xerces-C++ runtime since XmlManager has
	// it initialized.
        //
        auto_ptr<catalog> c (catalog_ (*doc.getContentAsDOM (),
                                       xml_schema::flags::dont_initialize));

        cerr << *c << endl;
      }


      // Lookup a document fragment.
      //

      string query ("collection('new.bdbxml')/lib:catalog/book[@id='ES']");

      // Find "The Elements of Style".
      //
      XmlValue v;
      XmlResults results (manager.query (query, context));

      if (results.next (v))
      {
        // Create an object model from the document fragment.
        //
        auto_ptr<book> b (
          new book (
            *static_cast<xercesc::DOMElement*> (v.asNode ())));

        cerr << *b << endl;


        // Add another author, change the availability status.
        //
        author white ("E.B. White", date (1899, 7, 11));
        white.died (date (1985, 10, 1));

        b->author ().push_back (white);
        b->available (false);


        // Update the document fragment from the object model.
        //
        *static_cast<xercesc::DOMElement*> (v.asNode ()) << *b;


        // Update the document in the container.
        //
        XmlDocument doc (v.asDocument ());
        container.updateDocument (doc, update_context);
      }

      print_document ("after update:", container, "new.xml");
    }

    manager.removeContainer ("new.bdbxml");
  }
  catch (const std::exception& e)
  {
    cerr << e.what () << endl;
    return 1;
  }
}