-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrv_xmlproc_val.py
74 lines (51 loc) · 1.77 KB
/
drv_xmlproc_val.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
A SAX driver for xmlproc with validation and DTD information.
$Id: drv_xmlproc_val.py,v 1.2 1999/10/12 21:32:50 larsga Exp $
"""
version="0.92"
from xml.sax import saxlib,saxutils
from xml.parsers.xmlproc import xmlval
from xml.sax.drivers.drv_xmlproc import *
import types
# --- SAX_XPValParser
class SAX_XPValParser(SAX_XPParser):
def __init__(self):
SAX_XPParser.__init__(self)
def _create_parser(self):
return xmlval.XMLValidator()
def handle_start_tag(self, name, attrs):
try:
self.doc_handler.startElement(name,
XPAttributes(attrs,\
self.parser.dtd.get_elem(name)))
except KeyError,e:
self.doc_handler.startElement(name,XPAttributes(attrs,None))
# --- EXPERIMENTAL PYTHON SAX EXTENSIONS:
def get_parser_name(self):
return "xmlproc_val"
def get_driver_version(self):
return version
def is_validating(self):
return 1
# --- XPAttributes
class XPAttributes(saxutils.AttributeMap):
def __init__(self,map,elemdecl):
saxutils.AttributeMap.__init__(self,map)
self.elemdecl=elemdecl
if elemdecl==None:
self.getType=self.getTypeStatic
def getTypeStatic(self,i):
return "CDATA" # Used for undeclared elements
def getType(self, i):
if type(i)==types.IntType:
try:
i=self.map.keys()[i]
except KeyError,e:
return "CDATA"
try:
return self.elemdecl.get_attr(i).get_type()
except KeyError,e:
return "CDATA"
# --- Global functions
def create_parser():
return SAX_XPValParser()