Annotation of embedaddon/libxml2/python/tests/reader2.py, revision 1.1

1.1     ! misho       1: #!/usr/bin/python -u
        !             2: #
        !             3: # this tests the DTD validation with the XmlTextReader interface
        !             4: #
        !             5: import sys
        !             6: import glob
        !             7: import string
        !             8: import StringIO
        !             9: import libxml2
        !            10: 
        !            11: # Memory debug specific
        !            12: libxml2.debugMemory(1)
        !            13: 
        !            14: err=""
        !            15: expect="""../../test/valid/rss.xml:177: element rss: validity error : Element rss does not carry attribute version
        !            16: </rss>
        !            17:       ^
        !            18: ../../test/valid/xlink.xml:450: element termdef: validity error : ID dt-arc already defined
        !            19:        <p><termdef id="dt-arc" term="Arc">An <ter
        !            20:                                          ^
        !            21: ../../test/valid/xlink.xml:530: validity error : attribute def line 199 references an unknown ID "dt-xlg"
        !            22: 
        !            23: ^
        !            24: """
        !            25: def callback(ctx, str):
        !            26:     global err
        !            27:     err = err + "%s" % (str)
        !            28: libxml2.registerErrorHandler(callback, "")
        !            29: 
        !            30: valid_files = glob.glob("../../test/valid/*.x*")
        !            31: valid_files.sort()
        !            32: for file in valid_files:
        !            33:     if string.find(file, "t8") != -1:
        !            34:         continue
        !            35:     reader = libxml2.newTextReaderFilename(file)
        !            36:     #print "%s:" % (file)
        !            37:     reader.SetParserProp(libxml2.PARSER_VALIDATE, 1)
        !            38:     ret = reader.Read()
        !            39:     while ret == 1:
        !            40:         ret = reader.Read()
        !            41:     if ret != 0:
        !            42:         print "Error parsing and validating %s" % (file)
        !            43:        #sys.exit(1)
        !            44: 
        !            45: if err != expect:
        !            46:     print err
        !            47: 
        !            48: #
        !            49: # another separate test based on Stephane Bidoul one
        !            50: #
        !            51: s = """
        !            52: <!DOCTYPE test [
        !            53: <!ELEMENT test (x,b)>
        !            54: <!ELEMENT x (c)>
        !            55: <!ELEMENT b (#PCDATA)>
        !            56: <!ELEMENT c (#PCDATA)>
        !            57: <!ENTITY x "<x><c>xxx</c></x>">
        !            58: ]>
        !            59: <test>
        !            60:     &x;
        !            61:     <b>bbb</b>
        !            62: </test>
        !            63: """
        !            64: expect="""10,test
        !            65: 1,test
        !            66: 14,#text
        !            67: 1,x
        !            68: 1,c
        !            69: 3,#text
        !            70: 15,c
        !            71: 15,x
        !            72: 14,#text
        !            73: 1,b
        !            74: 3,#text
        !            75: 15,b
        !            76: 14,#text
        !            77: 15,test
        !            78: """
        !            79: res=""
        !            80: err=""
        !            81: 
        !            82: input = libxml2.inputBuffer(StringIO.StringIO(s))
        !            83: reader = input.newTextReader("test2")
        !            84: reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
        !            85: reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
        !            86: reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
        !            87: reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
        !            88: while reader.Read() == 1:
        !            89:     res = res + "%s,%s\n" % (reader.NodeType(),reader.Name())
        !            90: 
        !            91: if res != expect:
        !            92:     print "test2 failed: unexpected output"
        !            93:     print res
        !            94:     sys.exit(1)
        !            95: if err != "":
        !            96:     print "test2 failed: validation error found"
        !            97:     print err
        !            98:     sys.exit(1)
        !            99: 
        !           100: #
        !           101: # Another test for external entity parsing and validation
        !           102: #
        !           103: 
        !           104: s = """<!DOCTYPE test [
        !           105: <!ELEMENT test (x)>
        !           106: <!ELEMENT x (#PCDATA)>
        !           107: <!ENTITY e SYSTEM "tst.ent">
        !           108: ]>
        !           109: <test>
        !           110:   &e;
        !           111: </test>
        !           112: """
        !           113: tst_ent = """<x>hello</x>"""
        !           114: expect="""10 test
        !           115: 1 test
        !           116: 14 #text
        !           117: 1 x
        !           118: 3 #text
        !           119: 15 x
        !           120: 14 #text
        !           121: 15 test
        !           122: """
        !           123: res=""
        !           124: 
        !           125: def myResolver(URL, ID, ctxt):
        !           126:     if URL == "tst.ent":
        !           127:         return(StringIO.StringIO(tst_ent))
        !           128:     return None
        !           129: 
        !           130: libxml2.setEntityLoader(myResolver)
        !           131: 
        !           132: input = libxml2.inputBuffer(StringIO.StringIO(s))
        !           133: reader = input.newTextReader("test3")
        !           134: reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
        !           135: reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
        !           136: reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
        !           137: reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
        !           138: while reader.Read() == 1:
        !           139:     res = res + "%s %s\n" % (reader.NodeType(),reader.Name())
        !           140: 
        !           141: if res != expect:
        !           142:     print "test3 failed: unexpected output"
        !           143:     print res
        !           144:     sys.exit(1)
        !           145: if err != "":
        !           146:     print "test3 failed: validation error found"
        !           147:     print err
        !           148:     sys.exit(1)
        !           149: 
        !           150: #
        !           151: # Another test for recursive entity parsing, validation, and replacement of
        !           152: # entities, making sure the entity ref node doesn't show up in that case
        !           153: #
        !           154: 
        !           155: s = """<!DOCTYPE test [
        !           156: <!ELEMENT test (x, x)>
        !           157: <!ELEMENT x (y)>
        !           158: <!ELEMENT y (#PCDATA)>
        !           159: <!ENTITY x "<x>&y;</x>">
        !           160: <!ENTITY y "<y>yyy</y>">
        !           161: ]>
        !           162: <test>
        !           163:   &x;
        !           164:   &x;
        !           165: </test>"""
        !           166: expect="""10 test 0
        !           167: 1 test 0
        !           168: 14 #text 1
        !           169: 1 x 1
        !           170: 1 y 2
        !           171: 3 #text 3
        !           172: 15 y 2
        !           173: 15 x 1
        !           174: 14 #text 1
        !           175: 1 x 1
        !           176: 1 y 2
        !           177: 3 #text 3
        !           178: 15 y 2
        !           179: 15 x 1
        !           180: 14 #text 1
        !           181: 15 test 0
        !           182: """
        !           183: res=""
        !           184: err=""
        !           185: 
        !           186: input = libxml2.inputBuffer(StringIO.StringIO(s))
        !           187: reader = input.newTextReader("test4")
        !           188: reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
        !           189: reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
        !           190: reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
        !           191: reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
        !           192: while reader.Read() == 1:
        !           193:     res = res + "%s %s %d\n" % (reader.NodeType(),reader.Name(),reader.Depth())
        !           194: 
        !           195: if res != expect:
        !           196:     print "test4 failed: unexpected output"
        !           197:     print res
        !           198:     sys.exit(1)
        !           199: if err != "":
        !           200:     print "test4 failed: validation error found"
        !           201:     print err
        !           202:     sys.exit(1)
        !           203: 
        !           204: #
        !           205: # The same test but without entity substitution this time
        !           206: #
        !           207: 
        !           208: s = """<!DOCTYPE test [
        !           209: <!ELEMENT test (x, x)>
        !           210: <!ELEMENT x (y)>
        !           211: <!ELEMENT y (#PCDATA)>
        !           212: <!ENTITY x "<x>&y;</x>">
        !           213: <!ENTITY y "<y>yyy</y>">
        !           214: ]>
        !           215: <test>
        !           216:   &x;
        !           217:   &x;
        !           218: </test>"""
        !           219: expect="""10 test 0
        !           220: 1 test 0
        !           221: 14 #text 1
        !           222: 5 x 1
        !           223: 14 #text 1
        !           224: 5 x 1
        !           225: 14 #text 1
        !           226: 15 test 0
        !           227: """
        !           228: res=""
        !           229: err=""
        !           230: 
        !           231: input = libxml2.inputBuffer(StringIO.StringIO(s))
        !           232: reader = input.newTextReader("test5")
        !           233: reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
        !           234: while reader.Read() == 1:
        !           235:     res = res + "%s %s %d\n" % (reader.NodeType(),reader.Name(),reader.Depth())
        !           236: 
        !           237: if res != expect:
        !           238:     print "test5 failed: unexpected output"
        !           239:     print res
        !           240: if err != "":
        !           241:     print "test5 failed: validation error found"
        !           242:     print err
        !           243: 
        !           244: #
        !           245: # cleanup
        !           246: #
        !           247: del input
        !           248: del reader
        !           249: 
        !           250: # Memory debug specific
        !           251: libxml2.cleanupParser()
        !           252: if libxml2.debugMemory(1) == 0:
        !           253:     print "OK"
        !           254: else:
        !           255:     print "Memory leak %d bytes" % (libxml2.debugMemory(1))
        !           256:     libxml2.dumpMemory()

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>