xerces-c-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 16747] New: - Parser loses ValidationScheme setting between parse attempts
Date Tue, 04 Feb 2003 02:52:01 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16747>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16747

Parser loses ValidationScheme setting between parse attempts

           Summary: Parser loses ValidationScheme setting between parse
                    attempts
           Product: Xerces-C++
           Version: 2.1.0
          Platform: Sun
        OS/Version: Solaris
            Status: NEW
          Severity: Minor
          Priority: Other
         Component: Validating Parser (Schema) (Xerces 1.5 or up only)
        AssignedTo: xerces-c-dev@xml.apache.org
        ReportedBy: andrew@unico.com.au


A XercesDOMParser is created with ValidationScheme set to Val_Always. 

An instance document of a schema that contains an 'any' element is created. This 
instance document contains invalid XML within the 'any' element.

When parsing the instance document the parser detects the error correctly, but 
it appears to reset the ValidationScheme. This means that in following calls to 
the parser no validation occurs.

The workaround is to reset the ValidationScheme to Val_Always before doing 
another parse, but I believe the parser should retain the setting between the 
two parse attempts?

Host - various sparcs
OS - Solaris 8 Generic_108528-14
Compiler - CC: WorkShop Compilers 4.2 18 Sep 1997 C++ 4.2 patch 104631-04

The following is a quick and dirty example that exercises the feature.

//----------------------------------------------------------------------------
// xerces_test.cpp
//
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/validators/common/Grammar.hpp>
#include <xercesc/sax/ErrorHandler.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>

#include <string.h>
#include <stdlib.h>
#include <iostream.h>

//----------------------------------------------------------------------------
// Simple schema with 'any' element...
//
static const char *schema = 
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
    <xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" >\
      <xsd:complexType name=\"data\" mixed=\"true\">\
	<xsd:sequence>\
	  <xsd:any namespace=\"##any\" minOccurs=\"0\"\
		    maxOccurs=\"unbounded\" processContents=\"lax\"/>\
	</xsd:sequence>\
	<xsd:attribute name=\"bar\" type=\"xsd:integer\" use=\"required\"/>\
      </xsd:complexType>\
      <xsd:element name=\"FOO\">\
	<xsd:complexType>\
	  <xsd:sequence>\
	    <xsd:element name=\"DATA\" type=\"data\"/>\
	  </xsd:sequence>\
	</xsd:complexType>\
      </xsd:element>\
    </xsd:schema>";

//----------------------------------------------------------------------------
// Two invalid instance documents. The first should fail because the content of
// its DATA element is not valid XML. The second should fail because its 'bar'
// attribute is not an integer.
//
static const char *doc01 = 
  "<FOO><DATA bar=\"1\">Random Junk<BAD></DATA></FOO>";
static const char *doc02 = 
  "<FOO><DATA bar=\"wibble\"><GOOD>Random Junk</GOOD></DATA></FOO>";

//----------------------------------------------------------------------------
// Minimal error handler...
//
class LocalErrorHandler : public ErrorHandler
    {
public:
    LocalErrorHandler() : errors(false) { } 

    ~LocalErrorHandler() { }
 
    void warning( const SAXParseException &) { };   // Ignore warnings
    void error( const SAXParseException &) { errors = true; }
    void fatalError( const SAXParseException &) { errors = true; }
    void resetErrors() { errors = false; }
    bool getErrors() const { return errors; }
 
private:
    bool errors;
    };

//----------------------------------------------------------------------------
// Mainline...
//
int main( int argc, char **argv )
    {
    // Initialize the XML4C2 system
    //
    try { XMLPlatformUtils::Initialize(); }

    catch( const XMLException & )
	{
	cerr << "Error during Xerces-C Initialization" << endl;
	return 1;
	}

    XercesDOMParser *parser = new XercesDOMParser;

    parser->setValidationScheme( XercesDOMParser::Val_Always );
    parser->setDoNamespaces( true );
    parser->setDoSchema( true );
    parser->setValidationSchemaFullChecking( true );

    // Add an error handler...
    //
    LocalErrorHandler *error_handler = new LocalErrorHandler();

    parser->setErrorHandler( error_handler );

    // Preload the schema...
    //
    parser->setExternalNoNamespaceSchemaLocation( "foo.xsd" );
    parser->cacheGrammarFromParse( true );
    parser->useCachedGrammarInParse( true );

    MemBufInputSource *membuf_schema =
	    new MemBufInputSource( (const XMLByte *)schema, strlen(schema),
				    "foo.xsd", false );
    
    parser->loadGrammar( *membuf_schema, Grammar::SchemaGrammarType, true );

    // Parse the first XML doc...
    //  
    MemBufInputSource* membuf01 = 
	new MemBufInputSource( (const XMLByte*)doc01, strlen(doc01),
				"doc01", false );

    try { parser->parse( *membuf01 ); }

    catch( ... )
        {
	cerr << "An exception occurred during parsing" << endl;
	return 1;
	}

    if( error_handler->getErrors() )
	cout << "Errors found in document one - Parse Successful" << endl;
    else
	cout << "No errors found in document one - Parse Failure" << endl;

    // Reset the document pool to free the memory assosciated with the parse.
    //			  
    parser->resetDocumentPool();

    // Reset the error reporter so we start our next job with a clean slate
    //
    error_handler->resetErrors();

    // Parse the second XML doc...
    //  
    MemBufInputSource* membuf02 = 
	new MemBufInputSource( (const XMLByte*)doc02, strlen(doc02),
				"doc02", false );

#ifdef INCLUDE_WORKAROUND
    // Reset the validation scheme here, since it appears to be munged by
    // parsing failures inside 'xsd:any' elements.
    //
    parser->setValidationScheme( XercesDOMParser::Val_Always );
#endif

    try { parser->parse( *membuf02 ); }
	 
    catch( ... )
        {
	cerr << "An exception occurred during parsing" << endl;
	return 1;
	}

    if( error_handler->getErrors() )
	cout << "Errors found in document two - Parse Successful" << endl;
    else
	cout << "No errors found in document two - Parse Failure" << endl;

    return 0;
    }

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Mime
View raw message