blob: 7b19bb2aee1ba50233867faa40eb489b87bfd9a9 [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
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
36int debug = 0;
37int dump = 0;
38int noent = 0;
39int count = 0;
Daniel Veillardea7751d2002-12-20 00:16:24 +000040int valid = 0;
Daniel Veillarde1ca5032002-12-09 14:13:43 +000041
42static 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 Veillardea7751d2002-12-20 00:16:24 +000045 printf("\t --count: count the number of attribute and elements\n");
46 printf("\t --valid: validate the document\n");
Daniel Veillarde1ca5032002-12-09 14:13:43 +000047 exit(1);
48}
49static int elem, attrs;
50
51static 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 Veillarde1ca5032002-12-09 14:13:43 +000060 }
61}
62
63static 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 Veillardea7751d2002-12-20 00:16:24 +000074 if (valid)
75 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
76
Daniel Veillarde1ca5032002-12-09 14:13:43 +000077 /*
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
99int 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 Veillardea7751d2002-12-20 00:16:24 +0000115 else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
116 valid++;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000117 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}