X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=parse-xml.c;h=70a7a89928617c8a854253f55d39586a6cf8ed16;hb=d756f0a8abc31ab634f9aff532021e3eb88374ab;hp=7fc3da2ed1ba84acd2c6867d957129cc2c3f156b;hpb=e1ef9687ac5b24e223f33050bd4ccde69c7cf227;p=pspp diff --git a/parse-xml.c b/parse-xml.c index 7fc3da2ed1..70a7a89928 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -3,6 +3,32 @@ #include #include +static void +print_parents(xmlNode *node) +{ + for (; node; node = node->next) + { + if (node->type == XML_ELEMENT_NODE) + { + for (xmlNode *child = node->children; child; child = child->next) + { + if (child->type == XML_ELEMENT_NODE) + printf ("%s %s\n", node->name, child->name); + else if (child->type == XML_TEXT_NODE) + printf ("%s \n", node->name); + else if (child->type == XML_CDATA_SECTION_NODE) + printf ("%s \n", node->name); + else if (child->type == XML_COMMENT_NODE) + printf ("%s \n", node->name); + else + printf ("%s <%d>\n", node->name, child->type); + } + } + + print_parents (node->children); + } +} + static void print_containment (xmlNode *node) { @@ -10,33 +36,49 @@ print_containment (xmlNode *node) { if (node->type == XML_ELEMENT_NODE) { - printf ("%s", node->name); + const char *child_names[512]; + int child_name_cnt[512]; + int n_names = 0; for (xmlNode *child = node->children; child; child = child->next) { - putchar (' '); + const char *name; + if (child->type == XML_ELEMENT_NODE) - { - printf ("%s", child->name); - - int n = 0; - while (child->next - && child->next->type == XML_ELEMENT_NODE - && !strcmp((char *) child->name, (char *) child->next->name)) - { - child = child->next; - n++; - } - if (n > 0) - putchar ('+'); - } + name = (char *) child->name; else if (child->type == XML_TEXT_NODE) - printf (""); + name = ""; else if (child->type == XML_CDATA_SECTION_NODE) - printf (""); + name = ""; + else if (child->type == XML_COMMENT_NODE) + { + name = ""; + //printf ("comment %s\n", (char *) child->content); + continue; + } else - printf ("<%d>", child->type); + name = ""; + + for (int i = 0; i < n_names; i++) + if (!strcmp(name, child_names[i])) + { + child_name_cnt[i]++; + goto next; + } + child_names[n_names] = name; + child_name_cnt[n_names] = 1; + n_names++; + + next:; } - putchar ('\n'); + + printf ("%s", node->name); + for (int i = 0; i < n_names; i++) + { + printf (" %s", child_names[i]); + if (child_name_cnt[i] > 1) + printf ("+"); + } + printf ("\n"); } print_containment (node->children); @@ -117,7 +159,48 @@ print_attribute (xmlNode *node, const char *attr) } } -static xmlNode * +static void +print_xml (xmlDoc *doc, xmlNode *node) +{ + xmlBuffer *buf = xmlBufferCreate(); + xmlNodeDump (buf, doc, node, 0, 1); + xmlBufferDump (stdout, buf); + xmlBufferFree (buf); + putchar ('\n'); +} + +static void +print_element (xmlDoc *doc, xmlNode *node, const char *element) +{ + for (; node; node = node->next) + { + if (node->name && !strcmp(element, (char *) node->name)) + print_xml (doc, node); + + print_element (doc, node->children, element); + } +} + +static void +print_id (xmlDoc *doc, xmlNode *node, const char *id) +{ + for (; node; node = node->next) + { + if (node->type == XML_ELEMENT_NODE) + { + const char *node_id = (char *) xmlGetProp (node, (xmlChar *) "id"); + if (node_id && !strcmp (node_id, id)) + { + print_xml (doc, node); + break; + } + } + + print_id (doc, node->children, id); + } +} + +static __attribute__((unused)) xmlNode * find_page_setup (xmlNode *node) { for (; node; node = node->next) @@ -168,7 +251,7 @@ main (int argc, char **argv) LIBXML_TEST_VERSION; - xmlDoc *doc = xmlReadFile(argv[1], NULL, 0); + xmlDoc *doc = xmlReadFile(argv[1], NULL, XML_PARSE_NOBLANKS); if (doc == NULL) { fprintf (stderr, "error: could not parse file %s\n", argv[1]); @@ -176,12 +259,18 @@ main (int argc, char **argv) } xmlNode *root = xmlDocGetRootElement(doc); + +#if 0 + /* Limit what we look at to pageSetup node and below. */ root = find_page_setup(root); if (!root) return 0; root->next = NULL; +#endif - if (!strcmp(argv[2], "containment")) + if (!strcmp(argv[2], "parents")) + print_parents (root); + else if (!strcmp(argv[2], "containment")) print_containment (root); else if (!strcmp(argv[2], "attributes")) print_attributes (root); @@ -189,8 +278,12 @@ main (int argc, char **argv) print_cdata (root); else if (!strcmp(argv[2], "text")) print_text (root); + else if (!strncmp(argv[2], "element:", 8)) + print_element (doc, root, argv[2] + 8); else if (!strncmp(argv[2], "attr:", 5)) print_attribute (root, argv[2] + 5); + else if (!strncmp(argv[2], "id:", 3)) + print_id (doc, root, argv[2] + 3); else if (!strcmp(argv[2], "labels")) print_labels (root); else