-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathXmlParser.cpp
247 lines (211 loc) · 7.49 KB
/
XmlParser.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "stdafx.h"
#include "XmlParser.h"
#include "encoding.h"
#include "SystemError.h"
#include <msxml6.h>
#pragma comment(lib, "msxml6.lib")
#define RELEASE(iface) \
if (iface) \
{ \
iface->Release(); \
iface = NULL; \
}
std::vector<XmlParser::Error> XmlParser::getErrors(const std::string &xml)
{
std::vector<XmlParser::Error> errors;
IXMLDOMNodeList *XMLNodeList(NULL);
IXMLDOMNode *XMLNode(NULL);
IXMLDOMDocument2 *XMLDocument(NULL);
HRESULT hr;
try
{
hr = CoCreateInstance(__uuidof(DOMDocument), NULL, CLSCTX_SERVER, IID_IXMLDOMDocument2, (LPVOID *)(&XMLDocument));
if (!SUCCEEDED(hr) || !XMLDocument)
{
throw ::Linter::SystemError("Linter: Can't create IID_IXMLDOMDocument2");
}
hr = XMLDocument->put_async(VARIANT_FALSE);
if (!SUCCEEDED(hr))
{
throw ::Linter::SystemError("Linter: Can't XMLDOMDocument2::put_async");
}
const std::wstring &string = Encoding::toUnicode(xml);
BSTR bstrValue(bstr_t(string.c_str()));
short resultCode = FALSE;
hr = XMLDocument->loadXML(bstrValue, &resultCode);
if (!SUCCEEDED(hr) || (resultCode != VARIANT_TRUE))
{
throw ::Linter::SystemError("Linter: Invalid output format. Only checkstyle-compatible output allowed.");
}
// <error line="12" column="19" severity="error" message="Unexpected identifier" source="jscs" />
hr = XMLDocument->selectNodes(bstr_t(L"//error"), &XMLNodeList);
if (!SUCCEEDED(hr))
{
throw ::Linter::SystemError("Linter: Can't execute XPath //error");
}
LONG uLength;
hr = XMLNodeList->get_length(&uLength);
if (!SUCCEEDED(hr))
{
throw ::Linter::SystemError("Linter: Can't get XPath //error length");
}
for (int iIndex = 0; iIndex < uLength; iIndex++)
{
IXMLDOMNode *node;
hr = XMLNodeList->nextNode(&node);
if (!SUCCEEDED(hr))
{
throw ::Linter::SystemError("Linter: Can't get next XPath element");
}
CComQIPtr<IXMLDOMElement> element(node);
if (!SUCCEEDED(hr) && element)
{
throw ::Linter::SystemError("Linter: XPath Element error");
}
Error error;
CComVariant value;
element->getAttribute(bstr_t(L"line"), &value);
error.m_line = _wtoi(value.bstrVal);
element->getAttribute(bstr_t(L"column"), &value);
error.m_column = _wtoi(value.bstrVal);
element->getAttribute(bstr_t(L"message"), &value);
error.m_message = value.bstrVal;
errors.push_back(error);
RELEASE(node);
}
RELEASE(XMLNodeList);
RELEASE(XMLDocument);
}
catch (::Linter::SystemError &)
{
RELEASE(XMLNode);
RELEASE(XMLDocument);
RELEASE(XMLNodeList);
throw;
}
catch (...)
{
RELEASE(XMLNode);
RELEASE(XMLDocument);
RELEASE(XMLNodeList);
}
return errors;
}
XmlParser::Settings XmlParser::getLinters(std::wstring file)
{
XmlParser::Settings settings;
IXMLDOMNodeList *XMLNodeList(NULL), *styleNode(NULL);
IXMLDOMNode *XMLNode(NULL);
IXMLDOMDocument2 *XMLDocument(NULL);
HRESULT hr;
LONG uLength;
try
{
hr = CoCreateInstance(__uuidof(DOMDocument), NULL, CLSCTX_SERVER, IID_IXMLDOMDocument2, (LPVOID *)(&XMLDocument));
if (!SUCCEEDED(hr) || !XMLDocument)
{
throw ::Linter::SystemError("Linter: Can't create IID_IXMLDOMDocument2");
}
hr = XMLDocument->put_async(VARIANT_FALSE);
if (!SUCCEEDED(hr))
{
throw ::Linter::SystemError("Linter: XMLDOMDocument2::put_async error.");
}
BSTR bstrValue(bstr_t(file.c_str()));
CComVariant value(bstrValue);
short resultCode = FALSE;
hr = XMLDocument->load(value, &resultCode);
if (!SUCCEEDED(hr) || (resultCode != VARIANT_TRUE))
{
throw ::Linter::SystemError("Linter: linter.xml load error. Check file format.");
}
hr = XMLDocument->selectNodes(bstr_t(L"//style"), &styleNode);
if (!SUCCEEDED(hr))
{
throw ::Linter::SystemError("Linter: Can't execute XPath //style");
}
hr = styleNode->get_length(&uLength);
if (!SUCCEEDED(hr))
{
throw ::Linter::SystemError("Linter: Can't get XPath root length");
}
else if (uLength)
{
IXMLDOMNode *node;
hr = styleNode->nextNode(&node);
if (SUCCEEDED(hr))
{
CComQIPtr<IXMLDOMElement> element(node);
CComVariant alpha;
if (element->getAttribute(bstr_t(L"alpha"), &alpha) == S_OK)
{
std::wstringstream data(std::wstring(alpha.bstrVal ? alpha.bstrVal : L""));
data >> settings.m_alpha;
}
if (element->getAttribute(bstr_t(L"color"), &alpha) == S_OK)
{
std::wstringstream data(std::wstring(alpha.bstrVal ? alpha.bstrVal : L""));
unsigned int color(0);
data >> std::hex >> color;
// reverse colors for scintilla's LE order
settings.m_color = color & 0xFF;
settings.m_color <<= 8;
color >>= 8;
settings.m_color |= color & 0xFF;
settings.m_color <<= 8;
color >>= 8;
settings.m_color |= color & 0xFF;
}
}
}
// <error line="12" column="19" severity="error" message="Unexpected identifier" source="jscs" />
hr = XMLDocument->selectNodes(bstr_t(L"//linter"), &XMLNodeList);
if (!SUCCEEDED(hr))
{
throw ::Linter::SystemError("Linter: Can't execute XPath //linter");
}
hr = XMLNodeList->get_length(&uLength);
if (!SUCCEEDED(hr))
{
throw ::Linter::SystemError("Linter: Can't get XPath length");
}
for (int iIndex = 0; iIndex < uLength; iIndex++)
{
IXMLDOMNode *node;
hr = XMLNodeList->nextNode(&node);
if (SUCCEEDED(hr))
{
CComQIPtr<IXMLDOMElement> element(node);
if (SUCCEEDED(hr) && element)
{
Linter linter;
CComVariant extension;
element->getAttribute(bstr_t(L"extension"), &extension);
linter.m_extension = extension.bstrVal;
element->getAttribute(bstr_t(L"command"), &extension);
linter.m_command = extension.bstrVal;
element->getAttribute(bstr_t(L"stdin"), &extension);
linter.m_useStdin = !!extension.boolVal;
settings.m_linters.push_back(linter);
}
RELEASE(node);
}
}
RELEASE(XMLNodeList);
RELEASE(XMLDocument);
}
catch (::Linter::SystemError &)
{
RELEASE(XMLNode);
RELEASE(XMLDocument);
RELEASE(XMLNodeList);
throw;
}
catch (...)
{
RELEASE(XMLNode);
RELEASE(XMLDocument);
RELEASE(XMLNodeList);
}
return settings;
}