blob: a874250197799044585714f4365ba8814b879d03 [file] [log] [blame]
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001/*
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
Daniel Veillard81273902003-09-30 00:43:48 +000011#ifdef LIBXML_READER_ENABLED
Daniel Veillarde1ca5032002-12-09 14:13:43 +000012#include <string.h>
13#include <stdarg.h>
14
15#ifdef HAVE_SYS_TYPES_H
16#include <sys/types.h>
17#endif
18#ifdef HAVE_SYS_STAT_H
19#include <sys/stat.h>
20#endif
21#ifdef HAVE_FCNTL_H
22#include <fcntl.h>
23#endif
24#ifdef HAVE_UNISTD_H
25#include <unistd.h>
26#endif
27#ifdef HAVE_STDLIB_H
28#include <stdlib.h>
29#endif
30#ifdef HAVE_STRING_H
31#include <string.h>
32#endif
33
34
35#include <libxml/xmlreader.h>
36
37int debug = 0;
38int dump = 0;
39int noent = 0;
40int count = 0;
Daniel Veillardea7751d2002-12-20 00:16:24 +000041int valid = 0;
Daniel Veillarde1ca5032002-12-09 14:13:43 +000042
43static void usage(const char *progname) {
44 printf("Usage : %s [options] XMLfiles ...\n", progname);
45 printf("\tParse the XML files using the xmlTextReader API\n");
Daniel Veillardea7751d2002-12-20 00:16:24 +000046 printf("\t --count: count the number of attribute and elements\n");
47 printf("\t --valid: validate the document\n");
Daniel Veillarde1ca5032002-12-09 14:13:43 +000048 exit(1);
49}
50static int elem, attrs;
51
52static void processNode(xmlTextReaderPtr reader) {
53 int type;
54
55 type = xmlTextReaderNodeType(reader);
56 if (count) {
57 if (type == 1) {
58 elem++;
59 attrs += xmlTextReaderAttributeCount(reader);
60 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +000061 }
62}
63
64static void handleFile(const char *filename) {
65 xmlTextReaderPtr reader;
66 int ret;
67
68 if (count) {
69 elem = 0;
70 attrs = 0;
71 }
72
73 reader = xmlNewTextReaderFilename(filename);
74 if (reader != NULL) {
Daniel Veillardea7751d2002-12-20 00:16:24 +000075 if (valid)
76 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
77
Daniel Veillarde1ca5032002-12-09 14:13:43 +000078 /*
79 * Process all nodes in sequence
80 */
81 ret = xmlTextReaderRead(reader);
82 while (ret == 1) {
83 processNode(reader);
84 ret = xmlTextReaderRead(reader);
85 }
86
87 /*
88 * Done, cleanup and status
89 */
90 xmlFreeTextReader(reader);
91 if (ret != 0) {
92 printf("%s : failed to parse\n", filename);
93 } else if (count)
94 printf("%s : %d elements, %d attributes\n", filename, elem, attrs);
95 } else {
96 fprintf(stderr, "Unable to open %s\n", filename);
97 }
98}
99
100int main(int argc, char **argv) {
101 int i;
102 int files = 0;
103
104 if (argc <= 1) {
105 usage(argv[0]);
106 return(1);
107 }
108 LIBXML_TEST_VERSION
109 for (i = 1; i < argc ; i++) {
110 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
111 debug++;
112 else if ((!strcmp(argv[i], "-dump")) || (!strcmp(argv[i], "--dump")))
113 dump++;
114 else if ((!strcmp(argv[i], "-count")) || (!strcmp(argv[i], "--count")))
115 count++;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000116 else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
117 valid++;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000118 else if ((!strcmp(argv[i], "-noent")) ||
119 (!strcmp(argv[i], "--noent")))
120 noent++;
121 }
122 if (noent != 0) xmlSubstituteEntitiesDefault(1);
123 for (i = 1; i < argc ; i++) {
124 if (argv[i][0] != '-') {
125 handleFile(argv[i]);
126 files ++;
127 }
128 }
129 xmlCleanupParser();
130 xmlMemoryDump();
131
132 return(0);
133}
Daniel Veillard81273902003-09-30 00:43:48 +0000134#else
135int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
136 printf("%s : xmlReader parser support not compiled in\n", argv[0]);
137 return(0);
138}
139#endif /* LIBXML_READER_ENABLED */