1 using System;
2 using System.Xml;
3
4 class XmlStats : XmlContentHandler
5 {
6 public XmlStats(const string& xmlFileName_) : xmlFileName(xmlFileName_)
7 {
8 }
9 public override Result<bool> StartElement(const System.Lex.Span& span, int fileIndex,
10 const ustring& namespaceUri, const ustring& localName, const ustring& qualifiedName, const Attributes& attributes)
11 {
12 ++numElements;
13 numAttributes = numAttributes + attributes.Count();
14 return Result<bool>(true);
15 }
16 public override Result<bool> HandlePI(const System.Lex.Span& span, int fileIndex, const ustring& target, const ustring& data)
17 {
18 ++numProcessingInstructions;
19 return Result<bool>(true);
20 }
21 public override Result<bool> HandleComment(const System.Lex.Span& span, int fileIndex, const ustring& comment)
22 {
23 ++numComments;
24 return Result<bool>(true);
25 }
26 public override Result<bool> HandleCDataSection(const System.Lex.Span& span, int fileIndex, const ustring& cdata)
27 {
28 numCharacters = numCharacters + cdata.Length();
29 return Result<bool>(true);
30 }
31 public override Result<bool> HandleText(const System.Lex.Span& span, int fileIndex, const ustring& text)
32 {
33 numCharacters = numCharacters + text.Length();
34 return Result<bool>(true);
35 }
36 public void Print()
37 {
38 Console.Out() << xmlFileName << " contains:" << endl();
39 Console.Out() << numElements << " elements" << endl();
40 Console.Out() << numAttributes << " attributes" << endl();
41 Console.Out() << numProcessingInstructions << " processing instructions" << endl();
42 Console.Out() << numComments << " comments" << endl();
43 Console.Out() << numCharacters << " characters" << endl();
44 }
45 private string xmlFileName;
46 private long numElements;
47 private long numAttributes;
48 private long numProcessingInstructions;
49 private long numComments;
50 private long numCharacters;
51 }
52
53 int main(int argc, const char** argv)
54 {
55 System.Lex.FileMap fileMap;
56 for (int i = 1; i < argc; ++i;)
57 {
58 string xmlFileName = argv[i];
59 XmlStats stats(xmlFileName);
60 auto result = ParseXmlFile(xmlFileName, &stats, fileMap);
61 if (result.Error())
62 {
63 Console.Error() << result.GetErrorMessage() << endl();
64 return 1;
65 }
66 stats.Print();
67 }
68 return 0;
69 }