5
|
1 |
#
|
|
2 |
# Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
# Example of a Filter class using the SAX parser base class
|
|
16 |
#
|
|
17 |
|
|
18 |
import filter_interface
|
|
19 |
|
|
20 |
class FilterTagCounter(filter_interface.FilterSAX):
|
|
21 |
|
|
22 |
def startDocument(self):
|
|
23 |
# for each element name count the number of occurences
|
|
24 |
# and the amount of body text contained.
|
|
25 |
self.names = []
|
|
26 |
self.count = {}
|
|
27 |
self.errors = 0
|
|
28 |
self.fatals = 0
|
|
29 |
self.warns = 0
|
|
30 |
|
|
31 |
def startElement(self, name, attributes):
|
|
32 |
if name == "buildlog":
|
|
33 |
# print out the attributes of the "top" element
|
|
34 |
print "version:"
|
|
35 |
for a,v in attributes.items():
|
|
36 |
print a, "=", v
|
|
37 |
|
|
38 |
# push name onto the stack of names and increment the count
|
|
39 |
self.names.append(name)
|
|
40 |
if name in self.count:
|
|
41 |
self.count[name][0] += 1
|
|
42 |
else:
|
|
43 |
self.count[name] = [1, 0] # occurs, characters
|
|
44 |
|
|
45 |
def characters(self, char):
|
|
46 |
# these are for the current element
|
|
47 |
current = self.names[-1]
|
|
48 |
self.count[current][1] += len(char)
|
|
49 |
|
|
50 |
def endElement(self, name):
|
|
51 |
# pop the name off the stack
|
|
52 |
self.names.pop()
|
|
53 |
|
|
54 |
def endDocument(self):
|
|
55 |
# report
|
|
56 |
print "\nsummary:"
|
|
57 |
for name,nos in sorted(self.count.items()):
|
|
58 |
print name, nos[0], nos[1]
|
|
59 |
|
|
60 |
print "\nparsing:"
|
|
61 |
print "errors =", self.errors
|
|
62 |
print "fatals =", self.fatals
|
|
63 |
print "warnings =", self.warns
|
|
64 |
|
|
65 |
def error(self, exception):
|
|
66 |
self.errors += 1
|
|
67 |
|
|
68 |
def fatalError(self, exception):
|
|
69 |
self.fatals += 1
|
|
70 |
|
|
71 |
def warning(self, exception):
|
|
72 |
self.warns += 1
|
|
73 |
|
|
74 |
# the end
|