Line data Source code
1 : /**
2 : * @file this file contains the entry point to the plugin as a gateway between c and c++
3 : *
4 : * @brief Plugin enables storage to xml files via the Xerces library
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include "xerces.hpp"
10 : #include "deserializer.hpp"
11 : #include "serializer.hpp"
12 : #include "util.hpp"
13 :
14 : #include <xercesc/dom/DOM.hpp>
15 : #include <xercesc/util/OutOfMemoryException.hpp>
16 : #include <xercesc/util/PlatformUtils.hpp>
17 :
18 : using namespace ckdb; // required for kdberrors.h
19 : using namespace xerces;
20 :
21 : #include <kdberrors.h>
22 : #include <kdbhelper.h>
23 :
24 : XERCES_CPP_NAMESPACE_USE
25 :
26 74 : int elektraXercesOpen (Plugin * handle ELEKTRA_UNUSED, Key * errorKey ELEKTRA_UNUSED)
27 : {
28 74 : XMLPlatformUtils::Initialize ();
29 74 : return 1;
30 : }
31 :
32 74 : int elektraXercesClose (Plugin * handle ELEKTRA_UNUSED, Key * errorKey ELEKTRA_UNUSED)
33 : {
34 74 : XMLPlatformUtils::Terminate ();
35 74 : return 1;
36 : }
37 :
38 48 : int elektraXercesGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
39 : {
40 48 : if (!elektraStrCmp (keyName (parentKey), "system/elektra/modules/xerces"))
41 : {
42 : KeySet * contract =
43 33 : ksNew (30, keyNew ("system/elektra/modules/xerces", KEY_VALUE, "xerces plugin waits for your orders", KEY_END),
44 : keyNew ("system/elektra/modules/xerces/exports", KEY_END),
45 : keyNew ("system/elektra/modules/xerces/exports/open", KEY_FUNC, elektraXercesOpen, KEY_END),
46 : keyNew ("system/elektra/modules/xerces/exports/close", KEY_FUNC, elektraXercesClose, KEY_END),
47 : keyNew ("system/elektra/modules/xerces/exports/get", KEY_FUNC, elektraXercesGet, KEY_END),
48 : keyNew ("system/elektra/modules/xerces/exports/set", KEY_FUNC, elektraXercesSet, KEY_END),
49 : #include ELEKTRA_README
50 33 : keyNew ("system/elektra/modules/xerces/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
51 33 : ksAppend (returned, contract);
52 33 : ksDel (contract);
53 :
54 : return 1;
55 : }
56 :
57 15 : kdb::KeySet ks (returned);
58 30 : kdb::Key k (parentKey);
59 15 : int ret = 0;
60 : // Bridge the C++ exceptions to elektra error messages
61 : try
62 : {
63 15 : deserialize (k, ks);
64 : ret = 1;
65 : }
66 0 : catch (const OutOfMemoryException & e)
67 : {
68 0 : ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey, asCStr (e.getMessage ()));
69 : }
70 0 : catch (const XMLException & e)
71 : {
72 0 : ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parentKey, asCStr (e.getMessage ()));
73 : }
74 0 : catch (const DOMException & e)
75 : {
76 0 : ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parentKey, asCStr (e.getMessage ()));
77 : }
78 4 : catch (const XercesPluginException & e)
79 : {
80 2 : ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parentKey, e.what ());
81 : }
82 0 : catch (...)
83 : {
84 0 : ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERROR (parentKey, "Unknown exception occurred while reading xml file");
85 : }
86 :
87 : // Avoid destruction of the pointers at the end
88 15 : k.release ();
89 15 : ks.release ();
90 15 : return ret;
91 : }
92 :
93 9 : int elektraXercesSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
94 : {
95 : // Bridge the C++ exceptions to elektra error messages
96 18 : kdb::KeySet ks (returned);
97 18 : kdb::Key k (parentKey);
98 9 : int ret = 0;
99 : try
100 : {
101 9 : serialize (k, ks);
102 : ret = 1;
103 : }
104 0 : catch (const OutOfMemoryException & e)
105 : {
106 0 : ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parentKey, asCStr (e.getMessage ()));
107 : }
108 0 : catch (const XMLException & e)
109 : {
110 0 : ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parentKey, asCStr (e.getMessage ()));
111 : }
112 0 : catch (const DOMException & e)
113 : {
114 0 : ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parentKey, asCStr (e.getMessage ()));
115 : }
116 2 : catch (const XercesPluginException & e)
117 : {
118 1 : ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parentKey, e.what ());
119 : }
120 0 : catch (...)
121 : {
122 0 : ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERROR (parentKey, "Unknown exception occurred while writing xml file");
123 : }
124 :
125 : // Avoid destruction of the pointers at the end
126 9 : k.release ();
127 9 : ks.release ();
128 18 : return ret;
129 : }
130 :
131 73 : Plugin * ELEKTRA_PLUGIN_EXPORT
132 : {
133 : // clang-format off
134 : return elektraPluginExport("xerces",
135 : ELEKTRA_PLUGIN_OPEN, &elektraXercesOpen,
136 : ELEKTRA_PLUGIN_CLOSE, &elektraXercesClose,
137 : ELEKTRA_PLUGIN_GET, &elektraXercesGet,
138 : ELEKTRA_PLUGIN_SET, &elektraXercesSet,
139 73 : ELEKTRA_PLUGIN_END);
140 : }
|