Exemplo n.º 1
0
def handle_incorrect(node):
    global log
    global nb_schemas_success
    global nb_schemas_failed

    schema = ""
    child = node.children
    while child != None:
        if child.type != 'text':
	    schema = schema + child.serialize()
	child = child.next

    try:
	rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
	rngs = rngp.relaxNGParse()
    except:
        rngs = None
    if rngs != None:
        log.write("\nFailed to detect schema error in:\n-----\n")
	log.write(schema)
        log.write("\n-----\n")
	nb_schemas_failed = nb_schemas_failed + 1
    else:
#	log.write("\nSuccess detecting schema error in:\n-----\n")
#	log.write(schema)
#	log.write("\n-----\n")
	nb_schemas_success = nb_schemas_success + 1
    return None
def handle_incorrect(node):
    global log
    global nb_schemas_success
    global nb_schemas_failed

    schema = ""
    child = node.children
    while child != None:
        if child.type != 'text':
            schema = schema + child.serialize()
        child = child.next

    try:
        rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
        rngs = rngp.relaxNGParse()
    except:
        rngs = None
    if rngs != None:
        log.write("\nFailed to detect schema error in:\n-----\n")
        log.write(schema)
        log.write("\n-----\n")
        nb_schemas_failed = nb_schemas_failed + 1
    else:
        #	log.write("\nSuccess detecting schema error in:\n-----\n")
        #	log.write(schema)
        #	log.write("\n-----\n")
        nb_schemas_success = nb_schemas_success + 1
    return None
Exemplo n.º 3
0
Arquivo: test_tdl.py Projeto: rwsu/oz
def validate_schema(tdl_file):

    # Locate relaxng schema
    rng_file = None
    for tryme in ["../../docs/tdl.rng", "../docs/tdl.rng", "docs/tdl.rng", "tdl.rng"]:
        if os.path.isfile(tryme):
            rng_file = tryme
            break

    if rng_file is None:
        raise Exception("RelaxNG schema file not found: tdl.rng")

    # Load relaxng schema
    schema = open(rng_file, "r").read()
    rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
    rngs = rngp.relaxNGParse()

    # Define callback for error handling
    def error_cb(ctx, str):
        print "%s: %s" % (ctx, str.strip())

    libxml2.registerErrorHandler(error_cb, tdl_file)

    # Attempt to validate
    reader = libxml2.newTextReaderFilename(tdl_file)
    # reader.SetParserProp(libxml2.PARSER_VALIDATE, 1)
    reader.RelaxNGSetSchema(rngs)
    ret = reader.Read()
    while ret == 1:
        ret = reader.Read()

    if ret != 0:
        raise Exception("Error parsing the document: %s" % tdl_file)
    if reader.IsValid() != 1:
        raise Exception("Document failed to validate: %s" % tdl_file)
Exemplo n.º 4
0
def handle_correct(node):
    global log
    global nb_schemas_success
    global nb_schemas_failed

    schema = ""
    child = node.children
    while child != None:
        if child.type != 'text':
            schema = schema + child.serialize()
        child = child.__next__

    try:
        rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
        rngs = rngp.relaxNGParse()
    except:
        rngs = None
    if rngs == None:
        log.write("\nFailed to compile correct schema:\n-----\n")
        log.write(schema)
        log.write("\n-----\n")
        nb_schemas_failed = nb_schemas_failed + 1
    else:
        nb_schemas_success = nb_schemas_success + 1
    return rngs
Exemplo n.º 5
0
def handle_correct(node):
    global log
    global nb_schemas_success
    global nb_schemas_failed

    schema = ""
    child = node.children
    while child is not None:
        if child.type != 'text':
	    schema = schema + child.serialize()
	child = child.next

    try:
	rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
	rngs = rngp.relaxNGParse()
    except:
        rngs = None
    if rngs is None:
        log.write("\nFailed to compile correct schema:\n-----\n")
	log.write(schema)
        log.write("\n-----\n")
	nb_schemas_failed = nb_schemas_failed + 1
    else:
	nb_schemas_success = nb_schemas_success + 1
    return rngs
Exemplo n.º 6
0
    def validate(self, str):
        """
        Validate the given string 
        """
        if not self.schema:
            return False

        file = open(self.schema)
        try:
            schema = file.read()
        finally:
            file.close()

        rngParser = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
        rngSchema = rngParser.relaxNGParse()
        ctxt = rngSchema.relaxNGNewValidCtxt()
        doc = libxml2.parseDoc(str)
        is_valid = doc.relaxNGValidateDoc(ctxt)

        # Clean up
        doc.freeDoc()
        del rngParser, rngSchema, ctxt
        libxml2.relaxNGCleanupTypes()
        libxml2.cleanupParser()
        return is_valid == 0
Exemplo n.º 7
0
def validate_schema(tdl_file):

    # Locate relaxng schema
    rng_file = None
    for tryme in [
            '../../docs/tdl.rng',
            '../docs/tdl.rng',
            'docs/tdl.rng',
            'tdl.rng',
    ]:
        if os.path.isfile(tryme):
            rng_file = tryme
            break

    if rng_file is None:
        raise Exception('RelaxNG schema file not found: tdl.rng')

    # Load relaxng schema
    schema = open(rng_file, 'r').read()
    rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
    rngs = rngp.relaxNGParse()

    # Define callback for error handling
    def error_cb(ctx, str):
        print "%s: %s" % (ctx, str.strip())

    libxml2.registerErrorHandler(error_cb, tdl_file)

    # Attempt to validate
    reader = libxml2.newTextReaderFilename(tdl_file)
    #reader.SetParserProp(libxml2.PARSER_VALIDATE, 1)
    reader.RelaxNGSetSchema(rngs)
    ret = reader.Read()
    while ret == 1:
        ret = reader.Read()

    if ret != 0:
        raise Exception('Error parsing the document: %s' % tdl_file)
    if reader.IsValid() != 1:
        raise Exception('Document failed to validate: %s' % tdl_file)
Exemplo n.º 8
0
        <empty/>
      </element>
    </optional>
    <element name="item">
      <data type="byte"/>
    </element>
  </oneOrMore>
</element>
"""
# Memory debug specific
libxml2.debugMemory(1)

#
# Parse the Relax NG Schemas
# 
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
del rngp

#
# Parse and validate the correct document
#
docstr="""<foo>
<label>some text</label>
<item>100</item>
</foo>"""

f = StringIO.StringIO(docstr)
input = libxml2.inputBuffer(f)
reader = input.newTextReader("correct")
reader.RelaxNGSetSchema(rngs)
Exemplo n.º 9
0
         xmlns:a="http://relaxng.org/ns/annotation/1.0"
         xmlns:ex1="http://www.example.com/n1"
         xmlns:ex2="http://www.example.com/n2">
  <a:documentation>A foo element.</a:documentation>
  <element name="ex1:bar1">
    <empty/>
  </element>
  <element name="ex2:bar2">
    <empty/>
  </element>
</element>
"""
instance="""<?xml version="1.0"?>
<foo><pre1:bar1 xmlns:pre1="http://www.example.com/n1"/><pre2:bar2 xmlns:pre2="http://www.example.com/n2"/></foo>"""

rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()
ctxt = rngs.relaxNGNewValidCtxt()
doc = libxml2.parseDoc(instance)
ret = doc.relaxNGValidateDoc(ctxt)
if ret != 0:
    print "error doing RelaxNG validation"
    sys.exit(1)

doc.freeDoc()
del rngp
del rngs
del ctxt
libxml2.relaxNGCleanupTypes()

# Memory debug specific