Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * testSAX.c : a small tester program for parsing using the SAX API. |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * daniel@veillard.com |
| 7 | */ |
| 8 | |
| 9 | #include "libxml.h" |
| 10 | |
| 11 | #include <string.h> |
| 12 | #include <stdarg.h> |
| 13 | |
| 14 | #ifdef HAVE_SYS_TYPES_H |
| 15 | #include <sys/types.h> |
| 16 | #endif |
| 17 | #ifdef HAVE_SYS_STAT_H |
| 18 | #include <sys/stat.h> |
| 19 | #endif |
| 20 | #ifdef HAVE_FCNTL_H |
| 21 | #include <fcntl.h> |
| 22 | #endif |
| 23 | #ifdef HAVE_UNISTD_H |
| 24 | #include <unistd.h> |
| 25 | #endif |
| 26 | #ifdef HAVE_STDLIB_H |
| 27 | #include <stdlib.h> |
| 28 | #endif |
| 29 | #ifdef HAVE_STRING_H |
| 30 | #include <string.h> |
| 31 | #endif |
| 32 | |
| 33 | |
| 34 | #include <libxml/xmlreader.h> |
| 35 | |
| 36 | int debug = 0; |
| 37 | int dump = 0; |
| 38 | int noent = 0; |
| 39 | int count = 0; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 40 | int valid = 0; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 41 | |
| 42 | static void usage(const char *progname) { |
| 43 | printf("Usage : %s [options] XMLfiles ...\n", progname); |
| 44 | printf("\tParse the XML files using the xmlTextReader API\n"); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 45 | printf("\t --count: count the number of attribute and elements\n"); |
| 46 | printf("\t --valid: validate the document\n"); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 47 | exit(1); |
| 48 | } |
| 49 | static int elem, attrs; |
| 50 | |
| 51 | static void processNode(xmlTextReaderPtr reader) { |
| 52 | int type; |
| 53 | |
| 54 | type = xmlTextReaderNodeType(reader); |
| 55 | if (count) { |
| 56 | if (type == 1) { |
| 57 | elem++; |
| 58 | attrs += xmlTextReaderAttributeCount(reader); |
| 59 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | static void handleFile(const char *filename) { |
| 64 | xmlTextReaderPtr reader; |
| 65 | int ret; |
| 66 | |
| 67 | if (count) { |
| 68 | elem = 0; |
| 69 | attrs = 0; |
| 70 | } |
| 71 | |
| 72 | reader = xmlNewTextReaderFilename(filename); |
| 73 | if (reader != NULL) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 74 | if (valid) |
| 75 | xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1); |
| 76 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 77 | /* |
| 78 | * Process all nodes in sequence |
| 79 | */ |
| 80 | ret = xmlTextReaderRead(reader); |
| 81 | while (ret == 1) { |
| 82 | processNode(reader); |
| 83 | ret = xmlTextReaderRead(reader); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Done, cleanup and status |
| 88 | */ |
| 89 | xmlFreeTextReader(reader); |
| 90 | if (ret != 0) { |
| 91 | printf("%s : failed to parse\n", filename); |
| 92 | } else if (count) |
| 93 | printf("%s : %d elements, %d attributes\n", filename, elem, attrs); |
| 94 | } else { |
| 95 | fprintf(stderr, "Unable to open %s\n", filename); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | int main(int argc, char **argv) { |
| 100 | int i; |
| 101 | int files = 0; |
| 102 | |
| 103 | if (argc <= 1) { |
| 104 | usage(argv[0]); |
| 105 | return(1); |
| 106 | } |
| 107 | LIBXML_TEST_VERSION |
| 108 | for (i = 1; i < argc ; i++) { |
| 109 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 110 | debug++; |
| 111 | else if ((!strcmp(argv[i], "-dump")) || (!strcmp(argv[i], "--dump"))) |
| 112 | dump++; |
| 113 | else if ((!strcmp(argv[i], "-count")) || (!strcmp(argv[i], "--count"))) |
| 114 | count++; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 115 | else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid"))) |
| 116 | valid++; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 117 | else if ((!strcmp(argv[i], "-noent")) || |
| 118 | (!strcmp(argv[i], "--noent"))) |
| 119 | noent++; |
| 120 | } |
| 121 | if (noent != 0) xmlSubstituteEntitiesDefault(1); |
| 122 | for (i = 1; i < argc ; i++) { |
| 123 | if (argv[i][0] != '-') { |
| 124 | handleFile(argv[i]); |
| 125 | files ++; |
| 126 | } |
| 127 | } |
| 128 | xmlCleanupParser(); |
| 129 | xmlMemoryDump(); |
| 130 | |
| 131 | return(0); |
| 132 | } |