forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xerces.cpp
140 lines (126 loc) · 3.74 KB
/
xerces.cpp
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
/**
* @file this file contains the entry point to the plugin as a gateway between c and c++
*
* @brief Plugin enables storage to xml files via the Xerces library
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "xerces.hpp"
#include "deserializer.hpp"
#include "serializer.hpp"
#include "util.hpp"
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/util/PlatformUtils.hpp>
using namespace ckdb; // required for kdberrors.h
using namespace xerces;
#include <kdberrors.h>
#include <kdbhelper.h>
XERCES_CPP_NAMESPACE_USE
int elektraXercesOpen (Plugin * handle ELEKTRA_UNUSED, Key * errorKey ELEKTRA_UNUSED)
{
XMLPlatformUtils::Initialize ();
return 1;
}
int elektraXercesClose (Plugin * handle ELEKTRA_UNUSED, Key * errorKey ELEKTRA_UNUSED)
{
XMLPlatformUtils::Terminate ();
return 1;
}
int elektraXercesGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
if (!elektraStrCmp (keyName (parentKey), "system/elektra/modules/xerces"))
{
KeySet * contract =
ksNew (30, keyNew ("system/elektra/modules/xerces", KEY_VALUE, "xerces plugin waits for your orders", KEY_END),
keyNew ("system/elektra/modules/xerces/exports", KEY_END),
keyNew ("system/elektra/modules/xerces/exports/open", KEY_FUNC, elektraXercesOpen, KEY_END),
keyNew ("system/elektra/modules/xerces/exports/close", KEY_FUNC, elektraXercesClose, KEY_END),
keyNew ("system/elektra/modules/xerces/exports/get", KEY_FUNC, elektraXercesGet, KEY_END),
keyNew ("system/elektra/modules/xerces/exports/set", KEY_FUNC, elektraXercesSet, KEY_END),
#include ELEKTRA_README
keyNew ("system/elektra/modules/xerces/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return 1;
}
kdb::KeySet ks (returned);
kdb::Key k (parentKey);
int ret = 0;
// Bridge the C++ exceptions to elektra error messages
try
{
deserialize (k, ks);
ret = 1;
}
catch (const OutOfMemoryException & e)
{
ELEKTRA_SET_ERROR (174, parentKey, asCStr (e.getMessage ()));
}
catch (const XMLException & e)
{
ELEKTRA_SET_ERROR (174, parentKey, asCStr (e.getMessage ()));
}
catch (const DOMException & e)
{
ELEKTRA_SET_ERROR (174, parentKey, asCStr (e.getMessage ()));
}
catch (const XercesPluginException & e)
{
ELEKTRA_SET_ERROR (174, parentKey, e.what ());
}
catch (...)
{
ELEKTRA_SET_ERROR (174, parentKey, "Unknown exception occurred while reading xml file");
}
// Avoid destruction of the pointers at the end
k.release ();
ks.release ();
return ret;
}
int elektraXercesSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
// Bridge the C++ exceptions to elektra error messages
kdb::KeySet ks (returned);
kdb::Key k (parentKey);
int ret = 0;
try
{
serialize (k, ks);
ret = 1;
}
catch (const OutOfMemoryException & e)
{
ELEKTRA_SET_ERROR (174, parentKey, asCStr (e.getMessage ()));
}
catch (const XMLException & e)
{
ELEKTRA_SET_ERROR (174, parentKey, asCStr (e.getMessage ()));
}
catch (const DOMException & e)
{
ELEKTRA_SET_ERROR (174, parentKey, asCStr (e.getMessage ()));
}
catch (const XercesPluginException & e)
{
ELEKTRA_SET_ERROR (174, parentKey, e.what ());
}
catch (...)
{
ELEKTRA_SET_ERROR (174, parentKey, "Unknown exception occurred while writing xml file");
}
// Avoid destruction of the pointers at the end
k.release ();
ks.release ();
return ret;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("xerces",
ELEKTRA_PLUGIN_OPEN, &elektraXercesOpen,
ELEKTRA_PLUGIN_CLOSE, &elektraXercesClose,
ELEKTRA_PLUGIN_GET, &elektraXercesGet,
ELEKTRA_PLUGIN_SET, &elektraXercesSet,
ELEKTRA_PLUGIN_END);
}