-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml2jsonpoco.cpp
233 lines (198 loc) · 6.06 KB
/
xml2jsonpoco.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "xml2jsonpoco.h"
#ifdef WITH_POCO
using namespace std;
#include <Poco/XML/XML.h>
#include <Poco/XML/Content.h>
#include <Poco/SAX/Attributes.h>
#include <Poco/SAX/ContentHandler.h>
#include <Poco/SAX/SAXParser.h>
#include <Poco/JSON/JSON.h>
#include <algorithm>
#include <queue>
#include <mutex>
#include <stack>
#include <fstream>
#include <thread>
#include <jsoncpp/json/json.h>
#include "common.h"
#define TEST
class Xml2JsonPocoPrivate: public Poco::XML::ContentHandler{
public:
void parseXml(const std::string &input, const std::string &output)
{
thread parser([this, input, output](){
parseXmlThr(input, output);
});
thread writer([this, output](){
writeJson(output);
});
parser.join();
writer.join();
}
void parseXmlThr(const std::string &input, const std::string &output)
{
Poco::XML::SAXParser parser;
parser.setContentHandler(this);
try{
parser.parse(input);
}catch(...){
}
mDone = true;
}
void writeJson(const string& output){
Json::Value root;
#ifdef TEST
stack <vmap> _stack;
_stack.push(vmap());
#endif
stack < Json::Value> jstack;
jstack.push(Json::Value());
while(!mDone || !mQueue.empty()){
if(mQueue.empty()){
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}
mMutex.lock();
auto val = mQueue.front();
mQueue.pop();
mMutex.unlock();
if(val.state == Val::IN){
#ifdef TEST
vmap m;
_stack.push(m);
#endif
Json::Value j;
jstack.push(j);
}
if(val.state == Val::OUT){
#ifdef TEST
/// test code
vmap t = _stack.top();
_stack.pop();
vmap& tn = _stack.top();
if(!val.attrs.empty()){
t.insert(val.attrs.begin(), val.attrs.end());
}
if(contains(tn, val.key) && !t.empty()){
auto obj = tn[val.key];
vlist lst;
lst.push_back(obj);
lst.push_back(t);
tn[val.key] = lst;
}else{
tn[val.key] = t;
}
/// end test code
#endif
auto jt = jstack.top();
jstack.pop();
auto& jtn = jstack.top();
if(!val.attrs.empty()){
for(auto it: val.attrs){
jt[it.first] = it.second.toText();
}
}
if(!jtn[val.key]){
jtn[val.key] = jt;
}else{
// if key exists then need to create array
auto obj = jtn[val.key];
if(obj.isArray()){
obj[obj.size()] = jt;
}else{
Json::Value arr;
arr[0].copy(obj);
arr[1] = jt;
obj = arr;
}
jtn[val.key] = obj;
}
}
if(!val.value.empty()){
#ifdef TEST
vmap& t = _stack.top();
t[val.key] = val.value;
#endif
auto& jt = jstack.top();
jt[val.key] = val.value;
}else{
}
// printf("%d: %s, %s, %s\n", Val.depth, Val.key.c_str(), Val.value.c_str(), parent.c_str());
}
root = jstack.top();
std::ofstream file_id;
file_id.open(output);
Json::StyledWriter writer;
file_id << writer.write(root);
file_id.close();
printf("\n");
}
private:
int mDepth = 0;
string mNode;
string mValue;
queue< Val > mQueue;
mutex mMutex;
stack<vmap> mAttrs;
bool mDone = false;
// ContentHandler interface
public:
void setDocumentLocator(const Poco::XML::Locator *loc)
{
}
void startDocument()
{
}
void endDocument()
{
}
void startElement(const Poco::XML::XMLString &uri, const Poco::XML::XMLString &localName,
const Poco::XML::XMLString &qname, const Poco::XML::Attributes &attrList)
{
mNode = localName;
{
lock_guard<mutex> lock(mMutex);
mQueue.push({mNode, "", mDepth, Val::IN}); // go deeper
}
vmap Attrs;
for(int i = 0; i < attrList.getLength(); ++i){
string key = attrList.getLocalName(i);
string value = attrList.getValue(i);
Attrs[key] = value;
}
mAttrs.push(Attrs);
mDepth++;
}
void endElement(const Poco::XML::XMLString &uri, const Poco::XML::XMLString &localName, const Poco::XML::XMLString &qname)
{
mNode = localName;
mDepth--;
printf("%d: %s %s\n", mDepth, mNode.c_str(), mValue.c_str());
vmap Attrs = mAttrs.top();
mAttrs.pop();
{
lock_guard<mutex> lock(mMutex);
mQueue.push({mNode, mValue, mDepth, Val::OUT, Attrs}); // out of children node
}
}
void characters(const Poco::XML::XMLChar ch[], int start, int length)
{
mValue.resize(length);
std::copy((char*)ch + start, (char*)ch + start + length, &mValue[0]);
trim(mValue);
}
void ignorableWhitespace(const Poco::XML::XMLChar ch[], int start, int length) { }
void processingInstruction(const Poco::XML::XMLString &target, const Poco::XML::XMLString &data) { }
void startPrefixMapping(const Poco::XML::XMLString &prefix, const Poco::XML::XMLString &uri) { }
void endPrefixMapping(const Poco::XML::XMLString &prefix) { }
void skippedEntity(const Poco::XML::XMLString &name) { }
};
Xml2JsonPoco::Xml2JsonPoco()
{
}
void Xml2JsonPoco::parseXml(const std::string &input, const std::string &output)
{
Xml2JsonPocoPrivate parser;
parser.parseXml(input, output);
}
#endif