testReader.c revision e1ca50330b59d32b228ad9c05a874a96cef9748f
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
36int debug = 0;
37int dump = 0;
38int noent = 0;
39int count = 0;
40
41static void usage(const char *progname) {
42    printf("Usage : %s [options] XMLfiles ...\n", progname);
43    printf("\tParse the XML files using the xmlTextReader API\n");
44    printf("\tand output the result of the parsing\n");
45    exit(1);
46}
47static int elem, attrs;
48
49static void processNode(xmlTextReaderPtr reader) {
50    int type;
51
52    type = xmlTextReaderNodeType(reader);
53    if (count) {
54	if (type == 1) {
55	    elem++;
56	    attrs += xmlTextReaderAttributeCount(reader);
57	}
58    } else {
59	xmlChar *name = xmlTextReaderName(reader);
60	if (name != NULL) {
61	    printf("%s : %d", name, xmlTextReaderNodeType(reader));
62	    xmlFree(name);
63	} else {
64	    printf("NULL: %d", xmlTextReaderNodeType(reader));
65	}
66	if (xmlTextReaderIsEmptyElement(reader))
67	    printf(" empty");
68	printf("\n");
69    }
70}
71
72static void handleFile(const char *filename) {
73    xmlTextReaderPtr reader;
74    int ret;
75
76    if (count) {
77	elem = 0;
78	attrs = 0;
79    }
80
81    reader = xmlNewTextReaderFilename(filename);
82    if (reader != NULL) {
83	/*
84	 * Process all nodes in sequence
85	 */
86	ret = xmlTextReaderRead(reader);
87	while (ret == 1) {
88	    processNode(reader);
89	    ret = xmlTextReaderRead(reader);
90	}
91
92	/*
93	 * Done, cleanup and status
94	 */
95	xmlFreeTextReader(reader);
96	if (ret != 0) {
97	    printf("%s : failed to parse\n", filename);
98	} else if (count)
99	    printf("%s : %d elements, %d attributes\n", filename, elem, attrs);
100    } else {
101	fprintf(stderr, "Unable to open %s\n", filename);
102    }
103}
104
105int main(int argc, char **argv) {
106    int i;
107    int files = 0;
108
109    if (argc <= 1) {
110	usage(argv[0]);
111	return(1);
112    }
113    LIBXML_TEST_VERSION
114    for (i = 1; i < argc ; i++) {
115	if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
116	    debug++;
117	else if ((!strcmp(argv[i], "-dump")) || (!strcmp(argv[i], "--dump")))
118	    dump++;
119	else if ((!strcmp(argv[i], "-count")) || (!strcmp(argv[i], "--count")))
120	    count++;
121	else if ((!strcmp(argv[i], "-noent")) ||
122	         (!strcmp(argv[i], "--noent")))
123	    noent++;
124    }
125    if (noent != 0) xmlSubstituteEntitiesDefault(1);
126    for (i = 1; i < argc ; i++) {
127	if (argv[i][0] != '-') {
128	    handleFile(argv[i]);
129	    files ++;
130	}
131    }
132    xmlCleanupParser();
133    xmlMemoryDump();
134
135    return(0);
136}
137