testReader.c 3.2 KB

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