forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeserializer.cpp
213 lines (180 loc) · 6.27 KB
/
deserializer.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @file
*
* @brief deserialization implementation for xerces plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "deserializer.hpp"
#include "util.hpp"
#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/DOMAttr.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMNode.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <algorithm>
#include <iostream>
#include <locale>
#include <map>
#include <kdbease.h>
#include <kdblogger.h>
#include <key.hpp>
XERCES_CPP_NAMESPACE_USE
using namespace std;
using namespace kdb;
using namespace xerces;
namespace
{
XercesPtr<DOMDocument> doc2dom (std::string const & src)
{
XercesDOMParser parser;
parser.setValidationScheme (XercesDOMParser::Val_Auto);
parser.parse (asXMLCh (src));
return XercesPtr<DOMDocument> (parser.adoptDocument ());
}
string trim (string const & str)
{
stringstream ss (str);
string to;
string trimmed;
while (getline (ss, to, '\n'))
{
// Remove whitespace lines, most likely caused by pretty printing
if (!all_of (to.begin (), to.end (), [] (char c) { return isspace (c, locale ()); })) trimmed += to;
}
return trimmed;
}
string getElementText (DOMNode const * parent)
{
string str;
for (auto child = parent->getFirstChild (); child != NULL; child = child->getNextSibling ())
{
if (DOMNode::NodeType::TEXT_NODE == child->getNodeType () || DOMNode::NodeType::CDATA_SECTION_NODE == child->getNodeType ())
{
DOMText * data = dynamic_cast<DOMText *> (child);
if (!data->getIsElementContentWhitespace ()) str += toStr (data->getData ());
}
}
// Trim whitespace that is most likely due to pretty printing
return trim (str);
}
Key newNodeKey (Key const & parent, DOMNode const * node)
{
Key childKey (parent.getName (), KEY_END);
const string keyName = toStr (node->getNodeName ());
childKey.addBaseName (keyName);
return childKey;
}
void node2key (DOMNode const * n, Key const & parent, KeySet const & ks, Key & current)
{
const string keyName = toStr (n->getNodeName ());
ELEKTRA_LOG_DEBUG ("Encountered Element: %s with parent %s", keyName.c_str (), current.getName ().c_str ());
if (!ks.size ())
{ // we map the parent key to the xml root element
// preserve the original name if it is different
auto parentName = parent.rbegin ();
if (parentName != parent.rend () && (*parentName) != keyName)
{
ELEKTRA_LOG_DEBUG ("parent name %s differs from root element name %s", (*parentName).c_str (), keyName.c_str ());
current.setMeta (ELEKTRA_XERCES_ORIGINAL_ROOT_NAME, keyName);
}
}
else
current.addBaseName (keyName);
const string text = getElementText (n);
current.set<string> (text);
if (!current.isValid ()) throw XercesPluginException ("Given keyset contains invalid keys to serialize");
ELEKTRA_LOG_DEBUG ("new parent is %s with value %s", current.getName ().c_str (), current.get<string> ().c_str ());
if (n->hasAttributes ())
{
// get all the attributes of the node
DOMNamedNodeMap * pAttributes = n->getAttributes ();
const XMLSize_t nSize = pAttributes->getLength ();
ELEKTRA_LOG_DEBUG ("\tAttributes");
for (XMLSize_t i = 0; i < nSize; ++i)
{
DOMAttr * pAttributeNode = dynamic_cast<DOMAttr *> (pAttributes->item (i));
ELEKTRA_LOG_DEBUG ("\t%s=%s", asCStr (pAttributeNode->getName ()), asCStr (pAttributeNode->getValue ()));
current.setMeta (toStr (pAttributeNode->getName ()), toStr (pAttributeNode->getValue ()));
}
}
}
void analyzeMultipleElements (DOMNode const * n, Key const & current, map<Key, bool> & arrays)
{
for (auto child = n->getFirstChild (); child != 0; child = child->getNextSibling ())
{
Key childKey = newNodeKey (current, child);
auto it = arrays.find (childKey);
if (it != arrays.end ())
{
if (!it->second)
{
ELEKTRA_LOG_DEBUG ("There are multiple elements of %s, mapping this as an array",
childKey.getName ().c_str ());
arrays[childKey] = true;
}
}
else
arrays[childKey] = false;
}
}
Key newArrayKey (Key const & arrayKey, KeySet & ks)
{
KeySet result (elektraArrayGet (arrayKey.getKey (), ks.getKeySet ()));
if (!result.size ())
{
Key arrayBaseKey = arrayKey.dup ();
Key parentArrayKey = Key (arrayKey.getName (), KEY_END);
parentArrayKey.setMeta ("array", "empty");
ks.append (parentArrayKey);
arrayBaseKey.addBaseName ("#");
result.append (arrayBaseKey);
}
return elektraArrayGetNextKey (result.getKeySet ());
}
void dom2keyset (DOMNode const * n, Key const & parent, KeySet & ks, map<Key, bool> & arrays)
{
if (n)
{
Key current (parent.getName (), KEY_END);
if (n->getNodeType () == DOMNode::ELEMENT_NODE)
{
node2key (n, parent, ks, current);
auto it = arrays.find (current);
const bool array = it != arrays.end () && it->second;
string parentArrayName = current.getName ();
// Multiple elements with that name, map as an array
if (array) current.addBaseName (newArrayKey (current, ks).getBaseName ());
// Only add keys with a value, attributes or leafs or the root to preserve the original name or array keys
if (n->hasAttributes () || !current.getString ().empty () || !n->getFirstChild () || !ks.size () || array)
{
ELEKTRA_LOG_DEBUG ("adding %s", current.getName ().c_str ());
if (array)
{
Key parentArrayKey = ks.lookup (parentArrayName);
parentArrayKey.setMeta ("array", current.getBaseName ());
}
ks.append (current);
}
else
{
ELEKTRA_LOG_DEBUG ("skipping %s", current.getName ().c_str ());
}
}
// the first level cannot have more children so its enough to check that here
analyzeMultipleElements (n, current, arrays);
for (auto child = n->getFirstChild (); child != 0; child = child->getNextSibling ())
dom2keyset (child, current, ks, arrays);
}
}
} // namespace
void xerces::deserialize (Key const & parentKey, KeySet & ks)
{
if (!parentKey.isValid ()) throw XercesPluginException ("Parent key is invalid");
if (parentKey.get<string> ().empty ()) throw XercesPluginException ("No source file specified as key value");
ELEKTRA_LOG_DEBUG ("deserializing relative to %s from file %s", parentKey.getName ().c_str (), parentKey.get<string> ().c_str ());
auto document = doc2dom (parentKey.get<string> ());
map<Key, bool> arrays;
if (document) dom2keyset (document->getDocumentElement (), parentKey, ks, arrays);
}