testC14N.c revision 38ae7e4efe803ea78b6499cd05a394db32623e41
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Canonical XML implementation test program
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * (http://www.w3.org/TR/2001/REC-xml-c14n-20010315)
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * See Copyright for the status of this software.
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Author: Aleksey Sanin <aleksey@aleksey.com>
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#if defined(LIBXML_C14N_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync "Usage: %s <mode> <xml-file> [<xpath-expr>] [<inclusive-ns-list>]\n",
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "where <mode> is one of following:\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync "--with-comments \t XML file canonization w comments\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync "--without-comments \t XML file canonization w/o comments\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync "--exc-with-comments \t Exclusive XML file canonization w comments\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync "--exc-without-comments\t Exclusive XML file canonization w/o comments\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncload_xpath_expr (xmlDocPtr parent_doc, const char* filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync/* static void print_xpath_nodes(xmlNodeSetPtr nodes); */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsynctest_c14n(const char* xml_filename, int with_comments, int exclusive,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync const char* xpath_filename, xmlChar **inclusive_namespaces) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * build an XML tree from a the file; we need to add default
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * attributes and resolve all character and entities references
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync doc = xmlReadFile(xml_filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(-1);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Check the document is of the right kind
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: empty document for file \"%s\"\n", xml_filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(-1);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * load xpath file if specified
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: unable to evaluate xpath expression\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(-1);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Canonical form
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /* fprintf(stderr,"File \"%s\" loaded: start canonization\n", xml_filename); */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: failed to canonicalize XML file \"%s\" (ret=%d)\n", xml_filename, ret);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync return(-1);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Init libxml
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Parse command line and process file
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "Error: wrong number of arguments.\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else if(strcmp(argv[1], "--with-comments") == 0) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ret = test_c14n(argv[2], 1, 0, (argc > 3) ? argv[3] : NULL, NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else if(strcmp(argv[1], "--without-comments") == 0) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ret = test_c14n(argv[2], 0, 0, (argc > 3) ? argv[3] : NULL, NULL);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else if(strcmp(argv[1], "--exc-with-comments") == 0) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /* load exclusive namespace from command line */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ret = test_c14n(argv[2], 1, 1, (argc > 3) ? argv[3] : NULL, list);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else if(strcmp(argv[1], "--exc-without-comments") == 0) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /* load exclusive namespace from command line */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync list = (argc > 4) ? parse_list((xmlChar *)argv[4]) : NULL;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ret = test_c14n(argv[2], 0, 1, (argc > 3) ? argv[3] : NULL, list);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Shutdown libxml
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Macro used to grow the current buffer.
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlRealloc(buffer, buffer_size * sizeof(xmlChar*)); \
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * allocate an translation buffer.
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*));
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncload_xpath_expr (xmlDocPtr parent_doc, const char* filename) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * load XPath expr as a file
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync doc = xmlReadFile(filename, NULL, XML_PARSE_DTDATTR | XML_PARSE_NOENT);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Check the document is of the right kind
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: empty document for file \"%s\"\n", filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync while(node != NULL && !xmlStrEqual(node->name, (const xmlChar *)"XPath")) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: XPath element expected in the file \"%s\"\n", filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: XPath content element is NULL \"%s\"\n", filename);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: unable to create new context\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Register namespaces
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(xmlXPathRegisterNs(ctx, ns->prefix, ns->href) != 0) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->href);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync * Evaluate xpath
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr,"Error: unable to evaluate xpath expression\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync /* print_xpath_nodes(xpath->nodesetval); */
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncprint_xpath_nodes(xmlNodeSetPtr nodes) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlNodePtr cur;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(nodes == NULL ){
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "Error: no nodes set defined\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "Nodes Set:\n-----\n");
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync for(i = 0; i < nodes->nodeNr; ++i) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync xmlNsPtr ns;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ns = (xmlNsPtr)nodes->nodeTab[i];
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync cur = (xmlNodePtr)ns->next;
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "namespace \"%s\"=\"%s\" for node %s:%s\n",
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync ns->prefix, ns->href,
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync } else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync cur = nodes->nodeTab[i];
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "element node \"%s:%s\"\n",
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync (cur->ns) ? cur->ns->prefix : BAD_CAST "", cur->name);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync cur = nodes->nodeTab[i];
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync fprintf(stderr, "node \"%s\": type %d\n", cur->name, cur->type);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsyncint main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync printf("%s : XPath/Canonicalization and output support not compiled in\n", argv[0]);
38ae7e4efe803ea78b6499cd05a394db32623e41vboxsync#endif /* LIBXML_C14N_ENABLED */