*** empty log message ***
[m17n/m17n-test.git] / reader2.c
1 /**
2  * section: xmlReader
3  * synopsis: Parse and validate an XML file with an xmlReader
4  * purpose: Demonstrate the use of xmlReaderForFile() to parse an XML file
5  *          validating the content in the process and activating options
6  *          like entities substitution, and DTD attributes defaulting.
7  *          (Note that the XMLReader functions require libxml2 version later
8  *          than 2.6.)
9  * usage: reader2 <valid_xml_filename>
10  * test: reader2 test2.xml > reader1.tmp ; diff reader1.tmp reader1.res ; rm reader1.tmp
11  * author: Daniel Veillard
12  * copy: see Copyright for the status of this software.
13  */
14
15 #include <stdio.h>
16 #include <libxml/xmlreader.h>
17
18 #ifdef LIBXML_READER_ENABLED
19
20 /**
21  * processNode:
22  * @reader: the xmlReader
23  *
24  * Dump information about the current node
25  */
26 static void
27 processNode(xmlTextReaderPtr reader) {
28     const xmlChar *name, *value;
29
30     name = xmlTextReaderConstName(reader);
31     if (name == NULL)
32         name = BAD_CAST "--";
33
34     value = xmlTextReaderConstValue(reader);
35
36     printf("%d %d %s %d %d", 
37             xmlTextReaderDepth(reader),
38             xmlTextReaderNodeType(reader),
39             name,
40             xmlTextReaderIsEmptyElement(reader),
41             xmlTextReaderHasValue(reader));
42     if (value == NULL)
43         printf("\n");
44     else {
45         if (xmlStrlen(value) > 40)
46             printf(" %.40s...\n", value);
47         else
48             printf(" %s\n", value);
49     }
50 }
51
52 /**
53  * streamFile:
54  * @filename: the file name to parse
55  *
56  * Parse, validate and print information about an XML file.
57  */
58 static void
59 streamFile(const char *filename) {
60     xmlTextReaderPtr reader;
61     int ret;
62
63
64     /*
65      * Pass some special parsing options to activate DTD attribute defaulting,
66      * entities substitution and DTD validation
67      */
68     reader = xmlReaderForFile(filename, NULL,
69                               XML_PARSE_DTDATTR  /* default DTD attributes */
70                               | XML_PARSE_NOENT    /* substitute entities */
71                               | XML_PARSE_NOBLANKS
72 #if 0
73                               | XML_PARSE_DTDVALID /* validate with the DTD */
74 #endif
75                               );
76     if (reader != NULL) {
77         ret = xmlTextReaderRead(reader);
78         while (ret == 1) {
79             processNode(reader);
80             ret = xmlTextReaderRead(reader);
81         }
82         /*
83          * Once the document has been fully parsed check the validation results
84          */
85         if (xmlTextReaderIsValid(reader) != 1) {
86             fprintf(stderr, "Document %s does not validate\n", filename);
87         }
88         xmlFreeTextReader(reader);
89         if (ret != 0) {
90             fprintf(stderr, "%s : failed to parse\n", filename);
91         }
92     } else {
93         fprintf(stderr, "Unable to open %s\n", filename);
94     }
95 }
96
97 int main(int argc, char **argv) {
98     if (argc != 2)
99         return(1);
100
101     /*
102      * this initialize the library and check potential ABI mismatches
103      * between the version it was compiled for and the actual shared
104      * library used.
105      */
106     LIBXML_TEST_VERSION
107
108     streamFile(argv[1]);
109
110     /*
111      * Cleanup function for the XML library.
112      */
113     xmlCleanupParser();
114     /*
115      * this is to debug memory for regression tests
116      */
117     xmlMemoryDump();
118     return(0);
119 }
120
121 #else
122 int main(void) {
123     fprintf(stderr, "XInclude support not compiled in\n");
124     exit(1);
125 }
126 #endif