author | jjkang |
Fri, 11 Jun 2010 15:22:40 +0800 | |
changeset 2 | 9da1e5517a66 |
parent 0 | ae805ac0140d |
permissions | -rw-r--r-- |
0
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
1 |
"""A parser for HTML and XHTML.""" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
2 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
3 |
# This file is based on sgmllib.py, but the API is slightly different. |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
4 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
5 |
# XXX There should be a way to distinguish between PCDATA (parsed |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
6 |
# character data -- the normal case), RCDATA (replaceable character |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
7 |
# data -- only char and entity references and end tags are special) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
8 |
# and CDATA (character data -- only end tags are special). |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
9 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
10 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
11 |
import markupbase |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
12 |
import re |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
13 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
14 |
# Regular expressions used for parsing |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
15 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
16 |
interesting_normal = re.compile('[&<]') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
17 |
interesting_cdata = re.compile(r'<(/|\Z)') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
18 |
incomplete = re.compile('&[a-zA-Z#]') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
19 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
20 |
entityref = re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
21 |
charref = re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
22 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
23 |
starttagopen = re.compile('<[a-zA-Z]') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
24 |
piclose = re.compile('>') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
25 |
commentclose = re.compile(r'--\s*>') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
26 |
tagfind = re.compile('[a-zA-Z][-.a-zA-Z0-9:_]*') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
27 |
attrfind = re.compile( |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
28 |
r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*' |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
29 |
r'(\'[^\']*\'|"[^"]*"|[-a-zA-Z0-9./,:;+*%?!&$\(\)_#=~@]*))?') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
30 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
31 |
locatestarttagend = re.compile(r""" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
32 |
<[a-zA-Z][-.a-zA-Z0-9:_]* # tag name |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
33 |
(?:\s+ # whitespace before attribute name |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
34 |
(?:[a-zA-Z_][-.:a-zA-Z0-9_]* # attribute name |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
35 |
(?:\s*=\s* # value indicator |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
36 |
(?:'[^']*' # LITA-enclosed value |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
37 |
|\"[^\"]*\" # LIT-enclosed value |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
38 |
|[^'\">\s]+ # bare value |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
39 |
) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
40 |
)? |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
41 |
) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
42 |
)* |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
43 |
\s* # trailing whitespace |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
44 |
""", re.VERBOSE) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
45 |
endendtag = re.compile('>') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
46 |
endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
47 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
48 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
49 |
class HTMLParseError(Exception): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
50 |
"""Exception raised for all parse errors.""" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
51 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
52 |
def __init__(self, msg, position=(None, None)): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
53 |
assert msg |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
54 |
self.msg = msg |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
55 |
self.lineno = position[0] |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
56 |
self.offset = position[1] |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
57 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
58 |
def __str__(self): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
59 |
result = self.msg |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
60 |
if self.lineno is not None: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
61 |
result = result + ", at line %d" % self.lineno |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
62 |
if self.offset is not None: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
63 |
result = result + ", column %d" % (self.offset + 1) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
64 |
return result |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
65 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
66 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
67 |
class HTMLParser(markupbase.ParserBase): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
68 |
"""Find tags and other markup and call handler functions. |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
69 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
70 |
Usage: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
71 |
p = HTMLParser() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
72 |
p.feed(data) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
73 |
... |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
74 |
p.close() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
75 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
76 |
Start tags are handled by calling self.handle_starttag() or |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
77 |
self.handle_startendtag(); end tags by self.handle_endtag(). The |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
78 |
data between tags is passed from the parser to the derived class |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
79 |
by calling self.handle_data() with the data as argument (the data |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
80 |
may be split up in arbitrary chunks). Entity references are |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
81 |
passed by calling self.handle_entityref() with the entity |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
82 |
reference as the argument. Numeric character references are |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
83 |
passed to self.handle_charref() with the string containing the |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
84 |
reference as the argument. |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
85 |
""" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
86 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
87 |
CDATA_CONTENT_ELEMENTS = ("script", "style") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
88 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
89 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
90 |
def __init__(self): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
91 |
"""Initialize and reset this instance.""" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
92 |
self.reset() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
93 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
94 |
def reset(self): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
95 |
"""Reset this instance. Loses all unprocessed data.""" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
96 |
self.rawdata = '' |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
97 |
self.lasttag = '???' |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
98 |
self.interesting = interesting_normal |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
99 |
markupbase.ParserBase.reset(self) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
100 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
101 |
def feed(self, data): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
102 |
"""Feed data to the parser. |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
103 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
104 |
Call this as often as you want, with as little or as much text |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
105 |
as you want (may include '\n'). |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
106 |
""" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
107 |
self.rawdata = self.rawdata + data |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
108 |
self.goahead(0) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
109 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
110 |
def close(self): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
111 |
"""Handle any buffered data.""" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
112 |
self.goahead(1) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
113 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
114 |
def error(self, message): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
115 |
raise HTMLParseError(message, self.getpos()) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
116 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
117 |
__starttag_text = None |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
118 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
119 |
def get_starttag_text(self): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
120 |
"""Return full source of start tag: '<...>'.""" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
121 |
return self.__starttag_text |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
122 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
123 |
def set_cdata_mode(self): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
124 |
self.interesting = interesting_cdata |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
125 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
126 |
def clear_cdata_mode(self): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
127 |
self.interesting = interesting_normal |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
128 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
129 |
# Internal -- handle data as far as reasonable. May leave state |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
130 |
# and data to be processed by a subsequent call. If 'end' is |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
131 |
# true, force handling all data as if followed by EOF marker. |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
132 |
def goahead(self, end): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
133 |
rawdata = self.rawdata |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
134 |
i = 0 |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
135 |
n = len(rawdata) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
136 |
while i < n: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
137 |
match = self.interesting.search(rawdata, i) # < or & |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
138 |
if match: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
139 |
j = match.start() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
140 |
else: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
141 |
j = n |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
142 |
if i < j: self.handle_data(rawdata[i:j]) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
143 |
i = self.updatepos(i, j) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
144 |
if i == n: break |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
145 |
startswith = rawdata.startswith |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
146 |
if startswith('<', i): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
147 |
if starttagopen.match(rawdata, i): # < + letter |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
148 |
k = self.parse_starttag(i) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
149 |
elif startswith("</", i): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
150 |
k = self.parse_endtag(i) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
151 |
elif startswith("<!--", i): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
152 |
k = self.parse_comment(i) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
153 |
elif startswith("<?", i): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
154 |
k = self.parse_pi(i) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
155 |
elif startswith("<!", i): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
156 |
k = self.parse_declaration(i) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
157 |
elif (i + 1) < n: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
158 |
self.handle_data("<") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
159 |
k = i + 1 |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
160 |
else: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
161 |
break |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
162 |
if k < 0: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
163 |
if end: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
164 |
self.error("EOF in middle of construct") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
165 |
break |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
166 |
i = self.updatepos(i, k) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
167 |
elif startswith("&#", i): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
168 |
match = charref.match(rawdata, i) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
169 |
if match: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
170 |
name = match.group()[2:-1] |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
171 |
self.handle_charref(name) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
172 |
k = match.end() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
173 |
if not startswith(';', k-1): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
174 |
k = k - 1 |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
175 |
i = self.updatepos(i, k) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
176 |
continue |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
177 |
else: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
178 |
break |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
179 |
elif startswith('&', i): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
180 |
match = entityref.match(rawdata, i) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
181 |
if match: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
182 |
name = match.group(1) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
183 |
self.handle_entityref(name) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
184 |
k = match.end() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
185 |
if not startswith(';', k-1): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
186 |
k = k - 1 |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
187 |
i = self.updatepos(i, k) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
188 |
continue |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
189 |
match = incomplete.match(rawdata, i) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
190 |
if match: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
191 |
# match.group() will contain at least 2 chars |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
192 |
if end and match.group() == rawdata[i:]: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
193 |
self.error("EOF in middle of entity or char ref") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
194 |
# incomplete |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
195 |
break |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
196 |
elif (i + 1) < n: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
197 |
# not the end of the buffer, and can't be confused |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
198 |
# with some other construct |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
199 |
self.handle_data("&") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
200 |
i = self.updatepos(i, i + 1) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
201 |
else: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
202 |
break |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
203 |
else: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
204 |
assert 0, "interesting.search() lied" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
205 |
# end while |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
206 |
if end and i < n: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
207 |
self.handle_data(rawdata[i:n]) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
208 |
i = self.updatepos(i, n) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
209 |
self.rawdata = rawdata[i:] |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
210 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
211 |
# Internal -- parse processing instr, return end or -1 if not terminated |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
212 |
def parse_pi(self, i): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
213 |
rawdata = self.rawdata |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
214 |
assert rawdata[i:i+2] == '<?', 'unexpected call to parse_pi()' |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
215 |
match = piclose.search(rawdata, i+2) # > |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
216 |
if not match: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
217 |
return -1 |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
218 |
j = match.start() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
219 |
self.handle_pi(rawdata[i+2: j]) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
220 |
j = match.end() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
221 |
return j |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
222 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
223 |
# Internal -- handle starttag, return end or -1 if not terminated |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
224 |
def parse_starttag(self, i): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
225 |
self.__starttag_text = None |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
226 |
endpos = self.check_for_whole_start_tag(i) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
227 |
if endpos < 0: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
228 |
return endpos |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
229 |
rawdata = self.rawdata |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
230 |
self.__starttag_text = rawdata[i:endpos] |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
231 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
232 |
# Now parse the data between i+1 and j into a tag and attrs |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
233 |
attrs = [] |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
234 |
match = tagfind.match(rawdata, i+1) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
235 |
assert match, 'unexpected call to parse_starttag()' |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
236 |
k = match.end() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
237 |
self.lasttag = tag = rawdata[i+1:k].lower() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
238 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
239 |
while k < endpos: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
240 |
m = attrfind.match(rawdata, k) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
241 |
if not m: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
242 |
break |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
243 |
attrname, rest, attrvalue = m.group(1, 2, 3) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
244 |
if not rest: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
245 |
attrvalue = None |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
246 |
elif attrvalue[:1] == '\'' == attrvalue[-1:] or \ |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
247 |
attrvalue[:1] == '"' == attrvalue[-1:]: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
248 |
attrvalue = attrvalue[1:-1] |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
249 |
attrvalue = self.unescape(attrvalue) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
250 |
attrs.append((attrname.lower(), attrvalue)) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
251 |
k = m.end() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
252 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
253 |
end = rawdata[k:endpos].strip() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
254 |
if end not in (">", "/>"): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
255 |
lineno, offset = self.getpos() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
256 |
if "\n" in self.__starttag_text: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
257 |
lineno = lineno + self.__starttag_text.count("\n") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
258 |
offset = len(self.__starttag_text) \ |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
259 |
- self.__starttag_text.rfind("\n") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
260 |
else: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
261 |
offset = offset + len(self.__starttag_text) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
262 |
self.error("junk characters in start tag: %r" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
263 |
% (rawdata[k:endpos][:20],)) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
264 |
if end.endswith('/>'): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
265 |
# XHTML-style empty tag: <span attr="value" /> |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
266 |
self.handle_startendtag(tag, attrs) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
267 |
else: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
268 |
self.handle_starttag(tag, attrs) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
269 |
if tag in self.CDATA_CONTENT_ELEMENTS: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
270 |
self.set_cdata_mode() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
271 |
return endpos |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
272 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
273 |
# Internal -- check to see if we have a complete starttag; return end |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
274 |
# or -1 if incomplete. |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
275 |
def check_for_whole_start_tag(self, i): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
276 |
rawdata = self.rawdata |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
277 |
m = locatestarttagend.match(rawdata, i) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
278 |
if m: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
279 |
j = m.end() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
280 |
next = rawdata[j:j+1] |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
281 |
if next == ">": |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
282 |
return j + 1 |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
283 |
if next == "/": |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
284 |
if rawdata.startswith("/>", j): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
285 |
return j + 2 |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
286 |
if rawdata.startswith("/", j): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
287 |
# buffer boundary |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
288 |
return -1 |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
289 |
# else bogus input |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
290 |
self.updatepos(i, j + 1) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
291 |
self.error("malformed empty start tag") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
292 |
if next == "": |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
293 |
# end of input |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
294 |
return -1 |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
295 |
if next in ("abcdefghijklmnopqrstuvwxyz=/" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
296 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
297 |
# end of input in or before attribute value, or we have the |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
298 |
# '/' from a '/>' ending |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
299 |
return -1 |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
300 |
self.updatepos(i, j) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
301 |
self.error("malformed start tag") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
302 |
raise AssertionError("we should not get here!") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
303 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
304 |
# Internal -- parse endtag, return end or -1 if incomplete |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
305 |
def parse_endtag(self, i): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
306 |
rawdata = self.rawdata |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
307 |
assert rawdata[i:i+2] == "</", "unexpected call to parse_endtag" |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
308 |
match = endendtag.search(rawdata, i+1) # > |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
309 |
if not match: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
310 |
return -1 |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
311 |
j = match.end() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
312 |
match = endtagfind.match(rawdata, i) # </ + tag + > |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
313 |
if not match: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
314 |
self.error("bad end tag: %r" % (rawdata[i:j],)) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
315 |
tag = match.group(1) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
316 |
self.handle_endtag(tag.lower()) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
317 |
self.clear_cdata_mode() |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
318 |
return j |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
319 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
320 |
# Overridable -- finish processing of start+end tag: <tag.../> |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
321 |
def handle_startendtag(self, tag, attrs): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
322 |
self.handle_starttag(tag, attrs) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
323 |
self.handle_endtag(tag) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
324 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
325 |
# Overridable -- handle start tag |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
326 |
def handle_starttag(self, tag, attrs): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
327 |
pass |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
328 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
329 |
# Overridable -- handle end tag |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
330 |
def handle_endtag(self, tag): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
331 |
pass |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
332 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
333 |
# Overridable -- handle character reference |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
334 |
def handle_charref(self, name): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
335 |
pass |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
336 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
337 |
# Overridable -- handle entity reference |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
338 |
def handle_entityref(self, name): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
339 |
pass |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
340 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
341 |
# Overridable -- handle data |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
342 |
def handle_data(self, data): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
343 |
pass |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
344 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
345 |
# Overridable -- handle comment |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
346 |
def handle_comment(self, data): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
347 |
pass |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
348 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
349 |
# Overridable -- handle declaration |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
350 |
def handle_decl(self, decl): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
351 |
pass |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
352 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
353 |
# Overridable -- handle processing instruction |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
354 |
def handle_pi(self, data): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
355 |
pass |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
356 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
357 |
def unknown_decl(self, data): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
358 |
self.error("unknown declaration: %r" % (data,)) |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
359 |
|
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
360 |
# Internal -- helper to remove special character quoting |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
361 |
def unescape(self, s): |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
362 |
if '&' not in s: |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
363 |
return s |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
364 |
s = s.replace("<", "<") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
365 |
s = s.replace(">", ">") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
366 |
s = s.replace("'", "'") |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
367 |
s = s.replace(""", '"') |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
368 |
s = s.replace("&", "&") # Must be last |
ae805ac0140d
DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff
changeset
|
369 |
return s |