File: test_xmlproc.py

package info (click to toggle)
python-xml 0.8.4-10.1%2Blenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,972 kB
  • ctags: 10,628
  • sloc: python: 46,730; ansic: 14,354; xml: 968; makefile: 201; sh: 20
file content (56 lines) | stat: -rw-r--r-- 1,421 bytes parent folder | download | duplicates (3)
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
import os
import sys

from xml.parsers.xmlproc import xmlval
from xml.parsers.xmlproc.utils import validate_doc, load_dtd, ErrorPrinter

dtd = load_dtd("xmlval_illformed.dtd")

f = open("doc.xml", "w")
f.write("""<?xml version="1.0"?>
<!DOCTYPE configuration SYSTEM "xmlval_illformed.dtd">
<configuration><notallowed/></configuration>""")
f.close()

try:
    # validate_doc(dtd, "doc.xml")
    # validate_doc is not suitable since it prints to stderr

    parser = xmlval.XMLValidator()
    parser.dtd = dtd # FIXME: what to do if there is a !DOCTYPE?
    parser.set_error_handler(ErrorPrinter(parser, out=sys.stdout))
    parser.parse_resource("doc.xml")

finally:
    os.unlink("doc.xml")


DOC_TEXT ="""\
<?xml version='1.0' encoding='ISO-8859-1'?>
<!DOCTYPE root [
  <!ELEMENT root EMPTY>
  <!ENTITY % big-ent SYSTEM 'larger-than-16K.ent'>
  %big-ent;
]>
<root/>
"""

LINE = "<!-- This is a really long entity; it doesn't say much, though. -->\n"

f1 = open("doc.xml", "w")
f2 = open("larger-than-16K.ent", "w")

try:
    f1.write(DOC_TEXT)
    f1.close()
    for i in range(int(17*1024 / len(LINE))):
        f2.write(LINE)
    f2.close()
    parser = xmlval.XMLValidator()
    #parser.dtd = dtd # FIXME: what to do if there is a !DOCTYPE?
    parser.set_error_handler(ErrorPrinter(parser, out=sys.stdout))
    parser.parse_resource("doc.xml")

finally:
    os.unlink("doc.xml")
    os.unlink("larger-than-16K.ent")