-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetxmlX.cpp
450 lines (418 loc) · 11.4 KB
/
getxmlX.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* cl.exe /Os /MD getxmlX.cpp
typedef WCHAR OLECHAR;
typedef OLECHAR* BSTR;
typedef BSTR* LPBSTR;
https://msdn.microsoft.com/en-us/library/windows/desktop/ms221069(v=vs.85).aspx
VARIANT
https://msdn.microsoft.com/en-us/library/windows/desktop/ms221627(v=vs.85).aspx
//
gcc -Os -s getxmlX.cpp -luuid -lole32 -loleaut32 -lstdc++ -static
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50888
//
Why c++?
To use _variant_t (comutil.h).
*/
/*
oXMLDOMDocument.load(xmlSource);
//
xmldoc.setProperty("SelectionLanguage", "XPath");
//
oXMLDOMNode.selectNodes(expression);
oXMLDOMNodeList.item(index);
oXMLDOMNode.selectSingleNode(queryString);
//
oXMLDOMNode.nodeValue;
oXMLDOMNode.text;
//
oXMLDOMElement.getElementsByTagName(tagName);
//
get*
oXMLDOMElement.getAttribute(name);
oXMLDOMAttribute.value;
//
? objXMLDOMSelection.getProperty(name);
//
XPath:
https://msdn.microsoft.com/en-us/library/ms256115.aspx
*/
// MUST for COM
#ifndef UNICODE
#define UNICODE
#define _UNICODE
#endif
#include <tchar.h>
#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
#include <mbctype.h>
#ifdef _MSC_VER
#import <msxml6.dll> raw_interfaces_only // portable to gcc
using namespace MSXML2;
#else
#include <comdef.h>
#include <initguid.h>
DEFINE_GUID(CLSID_DOMDocument60, 0x88D96A05, 0xF192, 0x11D4, 0xA6, 0x5F, 0x00, 0x40, 0x96, 0x32, 0x51, 0xE5);
DEFINE_GUID(IID_IXMLDOMDocument2, 0x2933BF95, 0x7B36, 0x11D2, 0xB2, 0x0E, 0x00, 0xC0, 0x4F, 0x98, 0x3E, 0x60);
#define __USE_MSXML2_NAMESPACE__
#include <msxml2.h>
#endif
#include "vector.h"
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMElement *DOM;
void PrintXmlNode(
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMNode *NODE
) {
BSTR xml;
//
if (!NODE) {return;}
//
NODE->get_xml(&xml);
_tprintf(_T("%s\n"), xml);
}
void PrintXmlNodes(
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMNodeList *NODES
) {
long i, n;
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMNode *NODE;
//
if (!NODES) {return;}
//
NODES->get_length(&n);
for (i = 0; i < n; i++) {
NODES->get_item(i, &NODE);
PrintXmlNode(NODE);
}
}
void PrintAttriVARIANT(VARIANT *va) {
if (!va) {return;}
if ((va->vt & VT_BSTR) == VT_BSTR || (va->vt & VT_LPWSTR) == VT_LPWSTR) {wprintf(L"%s\n", va->pbstrVal);}
if ((va->vt & VT_LPSTR) == VT_LPSTR) {printf("%s\n", va->pcVal);}
}
void PrintBSTR(const BSTR s) {
if (s == NULL) {return;}
wprintf(L"%s\n", s);
}
HRESULT SelectionNamespaces(
MSXML2::IXMLDOMDocument2 *pDOMDoc,
BSTR ns
) {
return pDOMDoc->setProperty((BSTR)L"SelectionNamespaces", (_variant_t)ns);
}
void SelectionAllNamespaces(
MSXML2::IXMLDOMDocument2 *pDOMDoc,
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMElement *dom
) {
HRESULT hr;
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMNamedNodeMap *pDomAttrs = NULL;
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMNode *pA = NULL;
BSTR xml;
long i, l;
//
if (!dom) {return;}
dom->get_attributes(&pDomAttrs);
hr = pDomAttrs->get_length(&l);
if (hr != S_OK) {return;}
for (i = 0; i < l; i++) {
hr = pDomAttrs->get_item(i, &pA);
pA->get_xml(&xml);
if (_wcsnicmp(xml, L"xmlns:", 6) == 0) {
SelectionNamespaces(pDOMDoc, xml);
}
}
}
size_t _tcsichrn(_TCHAR *str, long i, _TCHAR c) {
_TCHAR *p;
size_t n;
//
n = 0;
p = str - 1;
if (i < 0) {i = _tcslen(str);}
while ((p = _tcschr(p + 1, c)) != NULL && p - str < i) { n++; }
return n;
}
/* ^_^ . */
void format_operations(_TCHAR* ops) {
_TCHAR* p = ops - 1;
//
while (p = _tcschr(p + 1, _T('.')), p != NULL) {
if (_istalpha(*(p+1))) {
long i = p - ops;
if (_tcsichrn(ops, i, _T('(')) == _tcsichrn(ops, i, _T(')')) &&
_tcsichrn(ops, i, _T('[')) == _tcsichrn(ops, i, _T(']')) &&
_tcsichrn(ops, i, _T('\'')) % 2 == 0 &&
_tcsichrn(ops, i, _T('"')) % 2 == 0
) { *p = _T('\n'); }
}
}
}
/* () not in [] */
void format_opt(_TCHAR* opt) {
_TCHAR* p = opt - 1;
//
while (p = _tcspbrk(p + 1, _T("()")), p != NULL) {
long i, t;
i = p - opt;
t = _tcsichrn(opt, i, _T('['));
if (t == 0 || t == _tcsichrn(opt, i, _T(']'))) { *p = _T('\n'); }
}
}
void parse_operations_on_ws(
MSXML2::IXMLDOMDocument2 *pDOMDoc,
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMNodeList *NODES,
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMNode *NODE, const _TCHAR* ops
) {
Vector operations, opr;
_TCHAR *param;
size_t i, n;
//
operations = _tcs_split(ops, _T("\n"));
n = operations.length;
//
i = n;
for (i = 0; i < n; i++) {
_TCHAR *pp, *opti;
int ii, nn;
long NODES_l;
//
opti = _tcsdup((_TCHAR*)operations.items[i]);
format_opt(opti);
//
opr = _tcs_split(opti, _T("\n"));
free(opti);
//
nn = opr.length;
pp = (_TCHAR*)opr.items[0];
//
if (!NODE) {NODE = DOM;}
if (NODES) {NODES->get_length(&NODES_l);}
//
switch (nn) {
case 3:
goto EACH;
case 2:
param = (_TCHAR*)opr.items[1];
if (_tcscmp(pp, _T("selectNodes")) == 0 || _tcscmp(pp, _T("getElementsByTagName")) == 0) {
NODE->selectNodes(param, &NODES);
NODE = NULL;
if (i == n -1) { PrintXmlNodes(NODES); }
}
else if (_tcscmp(pp, _T("selectSingleNode")) == 0 && NODE) {
NODE->selectSingleNode(param, &NODE);
NODES = NULL;
if (i == n -1) { PrintXmlNode(NODE); }
}
else if (_tcscmp(pp, _T("getAttribute")) == 0 && NODE) {
VARIANT va;
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMElement *pDOMElement = NULL;
NODE->QueryInterface(
#ifdef _MSC_VER
__uuidof(MSXML2::IXMLDOMElement),
#else
IID_IXMLDOMElement,
#endif
(void**)&pDOMElement);
pDOMElement->getAttribute(param, &va);
PrintAttriVARIANT(&va);
NODE = NULL;
}
else if (_tcscmp(pp, _T("item")) == 0 && NODES_l) {
if (isdigit(*param)) {
ii = _ttoi(param);
NODES->get_item(ii, &NODE);
if (i == n -1) { PrintXmlNode(NODE); }
}
else { goto UNKNOWN; }
}
else if (_tcscmp(pp, _T("SelectionNamespaces")) == 0 && DOM) {
SelectionNamespaces(pDOMDoc, param);
}
else
EACH:
if (_tcscmp(pp, _T("each")) == 0 && NODES_l) {
long j, l;
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMNodeList *NODES2 = NULL;
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMNode *NODE2 = NULL;
//
for (j = 0; j < NODES_l; j++) {
NODES->get_item(j, &NODE2);
parse_operations_on_ws(pDOMDoc, NODES2, NODE2, (_TCHAR*)(operations.items[i]) + 5);
}
}
else { goto UNKNOWN; }
break;
case 1:
if (_tcscmp(pp, _T("text")) == 0 && NODE) {
BSTR s;
NODE->get_text(&s);
PrintBSTR(s);
}
else if (_tcscmp(pp, _T("length")) == 0 && NODES) {
_tprintf(_T("%d\n"), NODES_l);
}
else if (_tcscmp(pp, _T("DOM")) == 0) {
if (i == 0 && i == n - 1) {
PrintXmlNode(NODE);
}
}
else if (_tcscmp(pp, _T("childNodes")) == 0 && NODE) {
NODE->get_childNodes(&NODES);
NODE = NULL;
if (i == n -1) { PrintXmlNodes(NODES); }
}
else { goto UNKNOWN; }
break;
default:
UNKNOWN:
_ftprintf(stderr, _T("Err: %s\n"), operations.items[i]);
i = n;
break;
}
vector_free(&opr);
}
vector_free(&operations);
}
/**/
void help(const _TCHAR *app) {
_tprintf(_T("Get information from xml. msxml 6 based, XPath flavor. v0.1.1 by YX Hao\n"));
_tprintf(_T("Usage: %s <operations> <xml-file | URL>\n"), app);
_tprintf(_T("operation examples:\n"));
_tprintf(_T(" selectNodes(data).length\n"));
_tprintf(_T(" selectSingleNode(data).getAttribute(href)\n"));
_tprintf(_T(" selectSingleNode(data).text\n"));
_tprintf(_T(" selectSingleNode(data).childNodes.length\n"));
_tprintf(_T(" selectSingleNode(b:book[@id='bk102'])\n"));
_tprintf(_T(" selectSingleNode(string[@name='permlab_callPhone']).text\n"));
_tprintf(_T(" selectNodes(permission[contains(@android:name,'CONTACTS')])\n"));
_tprintf(_T(" selectNodes(string[starts-with(@name,'permlab_')])\n"));
_tprintf(_T(" DOM\n"));
_tprintf(_T(" [DOM.]childNodes\n"));
_tprintf(_T(" getElementsByTagName(data).item(0).getAttribute(href)\n"));
_tprintf(_T(" SelectionNamespaces(\"xmlns:a='http://myserver.com' xmlns:b='http://yourserver.com'\").selectNodes(a:root/b:branch)\n"));
_tprintf(_T("Tips:.\n"));
_tprintf(_T(" Outputs OEM ANSI.\n"));
_tprintf(_T(" All top namespaces selected.\n"));
_tprintf(_T(" XPath functions: contains, starts-with.\n"));
exit(EXIT_FAILURE);
}
#ifdef _MSC_VER
int wmain(int argc, wchar_t *argv[]) {
#else
typedef struct
{
int newmode;
} _startupinfo;
extern
#ifdef __cplusplus
"C"
#endif
int __cdecl __wgetmainargs(int *pargc, wchar_t ***pargv, wchar_t ***penv, int globb, _startupinfo*);
int main() {
int argc;
_TCHAR **argv, **env;
__wgetmainargs(&argc, &argv, &env, 0, NULL);
#endif
_TCHAR MBCP[8];
//
if (argc != 3) {
help(argv[0]);
return 87; //ERROR_INVALID_PARAMETER
}
// tricks
_stprintf(MBCP, _T(".%d"), _getmbcp());
_tsetlocale(LC_CTYPE, MBCP);
//
CoInitialize(NULL);
//
MSXML2::IXMLDOMDocument2 *pXMLDoc = NULL;
VARIANT_BOOL bLoad = FALSE;
//
HRESULT hr = CoCreateInstance(
#ifdef _MSC_VER
__uuidof(MSXML2::DOMDocument60),
#else
CLSID_DOMDocument60,
#endif
NULL, CLSCTX_INPROC_SERVER,
#ifdef _MSC_VER
__uuidof(MSXML2::IXMLDOMDocument2),
#else
IID_IXMLDOMDocument2,
#endif
(void **)&pXMLDoc
);
//
pXMLDoc->put_async(VARIANT_FALSE);
//
hr = pXMLDoc->load((_variant_t)argv[2], &bLoad);
//
if(hr != S_OK) {
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMParseError *pParseError;
BSTR *errStr = NULL;
pXMLDoc->get_parseError(&pParseError);
_ftprintf(stderr, _T("Err: %d\n"), hr);
pParseError->get_reason(errStr);
_ftprintf(stderr, _T("Reason: %s\n"), errStr);
return 1;
}
//
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMNodeList *NODES = NULL;
#ifdef _MSC_VER
MSXML2::
#endif
IXMLDOMNode *NODE = NULL;
pXMLDoc->get_documentElement(&DOM);
//
pXMLDoc->setProperty((BSTR)L"SelectionLanguage", (_variant_t)L"XPath");
pXMLDoc->setProperty((BSTR)L"AllowDocumentFunction", (_variant_t)VARIANT_TRUE);
//
SelectionAllNamespaces(pXMLDoc, DOM);
//
format_operations(argv[1]);
//
parse_operations_on_ws(pXMLDoc, NODES, NODE, argv[1]);
//
CoUninitialize();
//
return 0;
}