![]() ![]() | ![]() |
1.2 misho 1: /*************************************************************************
2: * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
3: * by Michael Pounov <misho@aitbg.com>
4: *
5: * $Author: misho $
1.2.2.1 ! misho 6: * $Id: xmler.c,v 1.2 2011/06/08 12:45:41 misho Exp $
1.2 misho 7: *
8: *************************************************************************/
9: #include "global.h"
10: #include <axl.h>
11: #include <axl_ns.h>
12: #include <axl_decl.h>
13:
14:
15: int Verbose;
16: extern char compiled[], compiledby[], compilehost[];
17:
18:
19: static void
20: Usage()
21: {
22:
23: printf( "XMLer is tool for managment R/W operation with XMLs\n"
24: "=== %s === %s@%s ===\n\n"
25: " Syntax: xmler [options] <file.xml> [data]\n\n"
26: "\t-v\t\tVerbose ...\n"
27: "\t-l <av_pair>\tList node\n"
28: "\t-d <av_pair>\tDelete node\n"
29: "\t-s <av_pair>\tSet node command\n"
30: "\t-g <av_pair>\tGet node command\n"
31: "*\"av_pair\" format: [ns:]node[[|attribute[=value]]?data]\n"
32: "\n", compiled, compiledby, compilehost);
33: }
34:
35: static int
36: ShowXML(axlDoc *doc, const char *csNode)
37: {
38: axlNode *node = NULL;
39: int ctxlen;
40: char *ctx = NULL;
41:
42: if (csNode) {
43: if (!(node = axl_doc_find_called(doc, csNode))) {
44: printf("GET:: node %s - not found!\n", csNode);
45: return 1;
46: }
47:
48: axl_node_dump_pretty(node, &ctx, &ctxlen, 4);
49: VERB(1) printf("Verbose:: Node length=%d\n", ctxlen);
50: } else {
51: axl_doc_dump_pretty(doc, &ctx, &ctxlen, 4);
52: VERB(1) printf("Verbose:: Document length=%d\n", ctxlen);
53: }
54:
55: VERB(1) printf("\n");
56: if (ctx) {
57: printf("%s", ctx);
58: free(ctx);
59: }
60: VERB(1) printf("\n");
61: return 0;
62: }
63:
64: static int
65: ShowItem(axlNode *node, int lvl)
66: {
67: register int i;
68: int ctxlen;
69: axlNode *child;
70:
71: for (i = 0; i < axl_node_get_child_num(node); i++) {
72: child = axl_node_get_child_nth(node, i);
73:
74: if (!lvl)
75: printf("%s %s \"%s\"", axl_node_get_name(node), axl_node_get_name(child),
76: axl_node_get_content(child, &ctxlen));
77: else
78: printf(" %s \"%s\"", axl_node_get_name(child), axl_node_get_content(child, &ctxlen));
79:
80: ShowItem(child, 1);
81:
82: if (!lvl)
83: printf("\n");
84: }
85:
86: return 0;
87: }
88:
89: int
90: main(int argc, char **argv)
91: {
92: char ch, str[STRSIZ], szName[MAXPATHLEN], *ctx = NULL, m = 0;
93: int ctxlen, n, ret = 0;
94: axlDoc *doc = NULL;
95: axlError *err = NULL;
96: axlNode *nnode = NULL, *node = NULL;
97: struct tagReqXML xr;
98:
99: memset(str, 0, STRSIZ);
100: memset(&xr, 0, sizeof xr);
101: while ((ch = getopt(argc, argv, "hvl:s:g:d:")) != -1)
102: switch (ch) {
103: case 'v':
104: Verbose++;
105: break;
106: case 'l':
107: if (m) {
108: Usage();
109: return 1;
110: } else
111: m = 'l';
112: strlcpy(str, optarg, STRSIZ);
113: if ((ret = ioXMLGet(str, &xr)) < 1) {
114: printf("Error:: in XML request %s\n", str);
115: return 1;
116: }
117: VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
118: break;
119: case 'd':
120: if (m) {
121: Usage();
122: return 1;
123: } else
124: m = 'd';
125: strlcpy(str, optarg, STRSIZ);
126: if ((ret = ioXMLGet(str, &xr)) < 1) {
127: printf("Error:: in XML request %s\n", str);
128: return 1;
129: }
130: VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
131: break;
132: case 's':
133: if (m) {
134: Usage();
135: return 1;
136: } else
137: m = 's';
138: strlcpy(str, optarg, STRSIZ);
139: if ((ret = ioXMLGet(str, &xr)) < 1) {
140: printf("Error:: in XML request %s\n", str);
141: return 1;
142: }
143: VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
144: break;
145: case 'g':
146: if (m) {
147: Usage();
148: return 1;
149: } else
150: m = 'g';
151: strlcpy(str, optarg, STRSIZ);
152: if ((ret = ioXMLGet(str, &xr)) < 1) {
153: printf("Error:: in XML request %s\n", str);
154: return 1;
155: }
156: VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
157: break;
158: case 'h':
159: default:
160: Usage();
161: return 1;
162: }
163: argc -= optind;
164: argv += optind;
165: if (!argc) {
166: Usage();
167: return 1;
168: } else
169: strlcpy(szName, *argv, MAXPATHLEN);
170: if (argc > 1 && argv[1]) {
171: xr.xml_data.value = argv[1];
172: xr.xml_data.vallen = strlen(argv[1]);
173: }
174:
175: axl_init();
176: if (!(doc = axl_doc_parse_from_file(szName, &err))) {
177: printf("Error:: xml file %s #%d - %s\n", szName,
178: axl_error_get_code(err), axl_error_get(err));
179: axl_error_free(err);
180: axl_end();
181: return 2;
182: }
183: if (!axl_ns_doc_validate(doc, &err)) {
184: printf("Error:: xml file %s namespace validation #%d - %s\n", szName,
185: axl_error_get_code(err), axl_error_get(err));
186: axl_error_free(err);
187: axl_end();
188: return 2;
189: }
190:
191: switch (m) {
192: case 'g':
193: if (!xr.xml_namespace.vallen) {
194: if (ret == 32) {
195: if (!(ctx = (char*) axl_doc_get_content_at(doc, xr.xml_node.path.value, &ctxlen))) {
196: printf("GET:: path %s - not found!\n", xr.xml_node.path.value);
197: ret = 1;
198: goto end;
199: }
200: } else {
201: if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
202: printf("GET:: node %s - not found!\n", xr.xml_node.container.value);
203: ret = 1;
204: goto end;
205: }
206: }
207: } else {
208: strlcpy(str, xr.xml_namespace.value, sizeof str);
209: strlcat(str, ":", sizeof str);
210: strlcat(str, xr.xml_node.container.value, sizeof str);
211: if (ret == 32) {
212: if (!(ctx = (char*) axl_doc_get_content_at(doc, str, &ctxlen))) {
213: printf("GET:: path %s:%s - not found!\n", xr.xml_namespace.value,
214: xr.xml_node.path.value);
215: ret = 1;
216: goto end;
217: }
218: } else {
219: if (!(node = axl_doc_find_called(doc, str))) {
220: printf("GET:: node %s:%s - not found!\n", xr.xml_namespace.value,
221: xr.xml_node.container.value);
222: ret = 1;
223: goto end;
224: }
225: }
226: }
227:
228: if (!(ret & 32) && xr.xml_data.vallen) {
229: if (!(ctx = (char*) axl_node_get_content(node, &ctxlen))) {
230: printf("GET:: data %s for node %s - not found!\n",
231: xr.xml_data.value, xr.xml_node.container.value);
232: ret = 1;
233: goto end;
234: } else
235: VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen);
236:
237: VERB(1) printf("\n");
238: if (!strcmp(ctx, xr.xml_data.value))
239: printf("DATA::1\n");
240: else
241: printf("DATA::0\n");
242: }
243:
244: if (!(ret & 32) && xr.xml_attribute.vallen) {
245: if ((n = axl_node_num_attributes(node)) < 1) {
246: printf("GET:: attribute %s for node %s - not found!\n",
247: xr.xml_attribute.value, xr.xml_node.container.value);
248: ret = 1;
249: goto end;
250: } else {
251: VERB(1) printf("Verbose:: node have %d attributes\n", n);
252:
253: if (!(ctx = (char*) axl_node_get_attribute_value(node, xr.xml_attribute.value))) {
254: printf("GET:: attribute %s for node %s - not found!\n",
255: xr.xml_attribute.value, xr.xml_node.container.value);
256: ret = 1;
257: goto end;
258: }
259:
260: if (xr.xml_value.vallen) {
261: if (!strcmp(ctx, xr.xml_value.value))
262: ctx = "VALUE::1";
263: else
264: ctx = "VALUE::0";
265: }
266: }
267: } else {
268: if (!(ret & 32) && !(ctx = (char*) axl_node_get_content(node, &ctxlen))) {
269: printf("GET:: data for node %s - not found!\n", xr.xml_node.container.value);
270: ret = 1;
271: goto end;
272: } else
273: VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen);
274: }
275:
276: VERB(1) printf("\n");
277: printf("%s\n", ctx);
278: ret = 0;
279: break;
280: case 'd':
281: if (!xr.xml_namespace.vallen) {
282: if (ret == 32) {
283: if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) {
284: printf("DEL:: path %s - not found!\n", xr.xml_node.path.value);
285: ret = 1;
286: goto end;
287: }
288: } else {
289: if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
290: printf("DEL:: node %s - not found!\n", xr.xml_node.container.value);
291: ret = 1;
292: goto end;
293: }
294: }
295: } else {
296: strlcpy(str, xr.xml_namespace.value, sizeof str);
297: strlcat(str, ":", sizeof str);
298: strlcat(str, xr.xml_node.container.value, sizeof str);
299: if (ret == 32) {
300: if (!(node = axl_doc_get(doc, str))) {
301: printf("DEL:: path %s:%s - not found!\n", xr.xml_namespace.value,
302: xr.xml_node.path.value);
303: ret = 1;
304: goto end;
305: }
306: } else {
307: if (!(node = axl_doc_find_called(doc, str))) {
308: printf("DEL:: node %s:%s - not found!\n", xr.xml_namespace.value,
309: xr.xml_node.container.value);
310: ret = 1;
311: goto end;
312: }
313: }
314: }
315:
316: axl_node_remove(node, 1);
317: ret = ShowXML(doc, NULL);
318: break;
319: case 's':
320: if (ret == 32) {
321: if (!xr.xml_data.vallen || !(nnode = axl_node_create(xr.xml_data.value))) {
322: printf("SET:: container %s at path %s - Error!\n",
323: xr.xml_data.value, xr.xml_node.path.value);
324: ret = 1;
325: goto end;
326: }
327: }
328: if (!xr.xml_namespace.vallen) {
329: if (ret == 32) {
330: // insert new
331: if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) {
332: printf("SET:: path %s - not found!\n", xr.xml_node.path.value);
333: axl_node_free(nnode);
334: ret = 1;
335: goto end;
336: }
337: } else {
338: // update old
339: if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
340: printf("SET:: node %s - not found!\n", xr.xml_node.container.value);
341: ret = 1;
342: goto end;
343: }
344: }
345: } else {
346: strlcpy(str, xr.xml_namespace.value, sizeof str);
347: strlcat(str, ":", sizeof str);
348: strlcat(str, xr.xml_node.container.value, sizeof str);
349: if (ret == 32) {
350: // insert new
351: if (!(node = axl_doc_get(doc, str))) {
352: printf("SET:: path %s:%s - not found!\n", xr.xml_namespace.value,
353: xr.xml_node.path.value);
354: axl_node_free(nnode);
355: ret = 1;
356: goto end;
357: }
358: } else {
359: // update old
360: if (!(node = axl_doc_find_called(doc, str))) {
361: printf("SET:: node %s:%s - not found!\n", xr.xml_namespace.value,
362: xr.xml_node.container.value);
363: ret = 1;
364: goto end;
365: }
366: }
367: }
368:
369: if (!(ret & 32) && xr.xml_data.vallen) {
370: axl_node_set_is_empty(node, 1);
371: axl_node_set_content(node, xr.xml_data.value, xr.xml_data.vallen);
372: }
373: if (!(ret & 32) && xr.xml_attribute.vallen) {
374: axl_node_remove_attribute(node, xr.xml_attribute.value);
375: axl_node_set_attribute(node, xr.xml_attribute.value, xr.xml_value.value);
376: }
377:
378: if (ret & 32)
379: axl_node_set_child(node, nnode);
380: ret = ShowXML(doc, NULL);
381: break;
382: case 'l':
383: if (!xr.xml_namespace.vallen) {
384: if (ret == 32) {
385: if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) {
386: printf("LST:: path %s - not found!\n", xr.xml_node.path.value);
387: ret = 1;
388: goto end;
389: }
390: } else {
391: if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
392: printf("LST:: node %s - not found!\n", xr.xml_node.container.value);
393: ret = 1;
394: goto end;
395: }
396: }
397: } else {
398: strlcpy(str, xr.xml_namespace.value, sizeof str);
399: strlcat(str, ":", sizeof str);
400: strlcat(str, xr.xml_node.container.value, sizeof str);
401: if (ret == 32) {
402: if (!(node = axl_doc_get(doc, str))) {
403: printf("LST:: path %s:%s - not found!\n", xr.xml_namespace.value,
404: xr.xml_node.path.value);
405: ret = 1;
406: goto end;
407: }
408: } else {
409: if (!(node = axl_doc_find_called(doc, str))) {
410: printf("LST:: node %s:%s - not found!\n", xr.xml_namespace.value,
411: xr.xml_node.container.value);
412: ret = 1;
413: goto end;
414: }
415: }
416: }
417:
418: ret = ShowItem(node, 0);
419: break;
420: default:
421: ret = ShowXML(doc, xr.xml_data.vallen ? xr.xml_data.value : NULL);
422: }
423: end:
424: axl_doc_free(doc);
425: axl_end();
426: return ret;
427: }