|
|
| version 1.1.2.1, 2010/09/27 10:51:01 | version 1.1.2.4, 2010/09/28 11:37:04 |
|---|---|
| Line 1 | Line 1 |
| #include "global.h" | #include "global.h" |
| #include <axl.h> | |
| #include <axl_ns.h> | |
| #include <axl_decl.h> | |
| #include "xmler.h" | |
| int Verbose; | |
| extern char compiled[], compiledby[], compilehost[]; | |
| static void Usage() | |
| { | |
| printf( "XMLer is tool for managment R/W operation with XMLs\n" | |
| "=== %s === %s@%s ===\n\n" | |
| " Syntax: xmler [options] <file.xml> [data]\n\n" | |
| "\t-v\t\tVerbose ...\n" | |
| "\t-s <av_pair>\tSet Attribute/Value pair\n" | |
| "\t-g <av_pair>\tGet Attribute/Value pair\n" | |
| "*\"av_pair\" format: [ns:]node[[|attribute[=value]]?data]\n" | |
| "\n", compiled, compiledby, compilehost); | |
| } | |
| int | int |
| main(int argc, char **argv) | main(int argc, char **argv) |
| { | { |
| return 0; | char ch, *ctx, str[STRSIZ], szName[MAXPATHLEN], m = 0; |
| int ctxlen, n, ret = 0; | |
| axlDoc *doc = NULL; | |
| axlError *err = NULL; | |
| axlNode *node = NULL; | |
| struct tagReqXML xr; | |
| memset(str, 0, STRSIZ); | |
| memset(&xr, 0, sizeof xr); | |
| while ((ch = getopt(argc, argv, "hvs:g:")) != -1) | |
| switch (ch) { | |
| case 'v': | |
| Verbose++; | |
| break; | |
| case 's': | |
| if (m) { | |
| Usage(); | |
| return 1; | |
| } else | |
| m = 's'; | |
| strlcpy(str, optarg, STRSIZ); | |
| if ((ret = ioXMLGet(str, &xr)) < 1) { | |
| printf("Error:: in XML request %s\n", str); | |
| return 1; | |
| } | |
| VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret); | |
| break; | |
| case 'g': | |
| if (m) { | |
| Usage(); | |
| return 1; | |
| } else | |
| m = 'g'; | |
| strlcpy(str, optarg, STRSIZ); | |
| if ((ret = ioXMLGet(str, &xr)) < 1) { | |
| printf("Error:: in XML request %s\n", str); | |
| return 1; | |
| } | |
| VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret); | |
| break; | |
| case 'h': | |
| default: | |
| Usage(); | |
| return 1; | |
| } | |
| argc -= optind; | |
| argv += optind; | |
| if (!argc) { | |
| Usage(); | |
| return 1; | |
| } else | |
| strlcpy(szName, *argv, MAXPATHLEN); | |
| if (argc > 1 && argv[1]) { | |
| xr.xml_data.value = argv[1]; | |
| xr.xml_data.vallen = strlen(argv[1]); | |
| } | |
| axl_init(); | |
| if (!(doc = axl_doc_parse_from_file(szName, &err))) { | |
| printf("Error:: xml file %s #%d - %s\n", szName, | |
| axl_error_get_code(err), axl_error_get(err)); | |
| axl_error_free(err); | |
| axl_end(); | |
| return 2; | |
| } | |
| if (!axl_ns_doc_validate(doc, &err)) { | |
| printf("Error:: xml file %s namespace validation #%d - %s\n", szName, | |
| axl_error_get_code(err), axl_error_get(err)); | |
| axl_error_free(err); | |
| axl_end(); | |
| return 2; | |
| } | |
| switch (m) { | |
| case 'g': | |
| if (!xr.xml_namespace.vallen) { | |
| if (ret == 32) { | |
| if (!(ctx = (char*) axl_doc_get_content_at(doc, xr.xml_node.path.value, &ctxlen))) { | |
| printf("GET:: path %s - not found!\n", xr.xml_node.path.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } else { | |
| if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { | |
| printf("GET:: node %s - not found!\n", xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } else { | |
| strlcpy(str, xr.xml_namespace.value, sizeof str); | |
| strlcat(str, ":", sizeof str); | |
| strlcat(str, xr.xml_node.container.value, sizeof str); | |
| if (ret == 32) { | |
| if (!(ctx = (char*) axl_doc_get_content_at(doc, str, &ctxlen))) { | |
| printf("GET:: path %s:%s - not found!\n", xr.xml_namespace.value, | |
| xr.xml_node.path.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } else { | |
| if (!(node = axl_doc_find_called(doc, str))) { | |
| printf("GET:: node %s:%s - not found!\n", xr.xml_namespace.value, | |
| xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } | |
| if (!(ret & 32) && xr.xml_data.vallen) { | |
| if (!(ctx = (char*) axl_node_get_content(node, &ctxlen))) { | |
| printf("GET:: data %s for node %s - not found!\n", | |
| xr.xml_data.value, xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } else | |
| VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen); | |
| VERB(1) printf("\n"); | |
| if (!strcmp(ctx, xr.xml_data.value)) | |
| printf("DATA::1\n"); | |
| else | |
| printf("DATA::0\n"); | |
| } | |
| if (!(ret & 32) && xr.xml_attribute.vallen) { | |
| if ((n = axl_node_num_attributes(node)) < 1) { | |
| printf("GET:: attribute %s for node %s - not found!\n", | |
| xr.xml_attribute.value, xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } else { | |
| VERB(1) printf("Verbose:: node have %d attributes\n", n); | |
| if (!(ctx = (char*) axl_node_get_attribute_value(node, xr.xml_attribute.value))) { | |
| printf("GET:: attribute %s for node %s - not found!\n", | |
| xr.xml_attribute.value, xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| if (xr.xml_value.vallen) { | |
| if (!strcmp(ctx, xr.xml_value.value)) | |
| ctx = "VALUE::1"; | |
| else | |
| ctx = "VALUE::0"; | |
| } | |
| } | |
| } else { | |
| if (!(ret & 32) && !(ctx = (char*) axl_node_get_content(node, &ctxlen))) { | |
| printf("GET:: data for node %s - not found!\n", xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } else | |
| VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen); | |
| } | |
| VERB(1) printf("\n"); | |
| printf("%s\n", ctx); | |
| ret = 0; | |
| break; | |
| case 's': | |
| if (!xr.xml_namespace.vallen) { | |
| if (ret == 32) { | |
| // insert new | |
| } else { | |
| // update old | |
| if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { | |
| printf("SET:: node %s - not found!\n", xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } else { | |
| strlcpy(str, xr.xml_namespace.value, sizeof str); | |
| strlcat(str, ":", sizeof str); | |
| strlcat(str, xr.xml_node.container.value, sizeof str); | |
| if (ret == 32) { | |
| // insert new | |
| } else { | |
| // update old | |
| if (!(node = axl_doc_find_called(doc, str))) { | |
| printf("SET:: node %s:%s - not found!\n", xr.xml_namespace.value, | |
| xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } | |
| if (!(ret & 32) && xr.xml_data.vallen) { | |
| axl_node_set_is_empty(node, 1); | |
| axl_node_set_content(node, xr.xml_data.value, xr.xml_data.vallen); | |
| } | |
| if (!(ret & 32) && xr.xml_attribute.vallen) { | |
| axl_node_remove_attribute(node, xr.xml_attribute.value); | |
| axl_node_set_attribute(node, xr.xml_attribute.value, xr.xml_value.value); | |
| } | |
| xr.xml_data = xr.xml_node.container; | |
| ret = 0; | |
| default: | |
| if (xr.xml_data.vallen) { | |
| if (!(node = axl_doc_find_called(doc, xr.xml_data.value))) { | |
| printf("GET:: node %s - not found!\n", xr.xml_data.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| axl_node_dump_pretty(node, &ctx, &ctxlen, 4); | |
| VERB(1) printf("Verbose:: Node length=%d\n", ctxlen); | |
| } else { | |
| axl_doc_dump_pretty(doc, &ctx, &ctxlen, 4); | |
| VERB(1) printf("Verbose:: Document length=%d\n", ctxlen); | |
| } | |
| VERB(1) printf("\n"); | |
| printf("%s\n", ctx); | |
| if (ctx) | |
| free(ctx); | |
| } | |
| end: | |
| axl_doc_free(doc); | |
| axl_end(); | |
| return ret; | |
| } | } |