You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by "Peter A. Volchek" <Pe...@ti.com.od.ua> on 2002/04/23 12:44:07 UTC

Setting the default validator.

I need a way to tell the XMLScanner to use the default validator.
The one is actually created during the XMLScanner creation (fDTDValidator) when valToAdopt=NULL is passed to its constructor.
But for my needs I need to change the validator dynamically. 
The XMLScanner::setValidator(XMLValidator* const valToAdopt) method allows to set the user defined validator, but I did not find any ways how to set the default validator back 
( rather then creating new DTDValidator() and passing it to setValidation() as a parameter )

I have extended the setValidator() method to allow NULL to be passed as an argument, meaning to set the default validator. If you find this reasonable, please include itin your code.

inline void XMLScanner::setValidator(XMLValidator* const valToAdopt)
{
    if (fValidatorFromUser)
        delete fValidator;

    if( valToAdopt ) {
        fValidator = valToAdopt;
        fValidatorFromUser = true;
        initValidator(fValidator);
    }
    else {
        fValidatorFromUser = false;
        fValidator = fDTDValidator;
    }
}

-------------------------------------------------------------------------------------
P.S. (regardless the topic " XMLScanner performance" )
I have run the Rational Quantify against the DOM/SAXParser creation code and discovered that the most critical time is spent on XMLScanner creation. I have modified the DOM/SAXParser constructors to keep the XMLScanner as a static object and use it instead of creating the XMLScanner from scratch. The questions are: Is it safe to do? Are there any caveats that I might miss ?

The brief code I did is the following:

XMLMutex* DOMParser::fgScanMutex = 0;
static XMLScanner* gScanner = 0;


DOMParser::DOMParser(XMLValidator* const valToAdopt) :

fErrorHandler(0)
, fEntityResolver(0)
, fCreateEntityReferenceNodes(false)
, fToCreateXMLDeclTypeNode(false)
, fIncludeIgnorableWhitespace(true)
, fNodeStack(0)
, fScanner(0)
{
    if( !fgScanMutex )
    {
        fgScanMutex = new XMLMutex;
    }

    if( gScanner )
    {
        XMLMutexLock lock( fgScanMutex );
        if( gScanner )
        {
            fScanner = gScanner;
            gScanner = 0;
            fScanner->setDocHandler(0);
            fScanner->setDocTypeHandler(0);
            fScanner->setDoNamespaces(false);
            fScanner->setEntityHandler(0);
            fScanner->setEntityResolver(0);
            fScanner->setErrorReporter(0);
            fScanner->setErrorHandler(0);
            fScanner->setExitOnFirstFatal(true);
            fScanner->setValidationConstraintFatal(false);
            fScanner->setValidationScheme(XMLScanner::Val_Never);

            // Using the fixed method I have described above
            fScanner->setValidator( valToAdopt );
        }
    }

    //
    //  Create a scanner and tell it what validator to use. Then set us
    //  as the document event handler so we can fill the DOM document.
    //
    if( !fScanner )
    {
        fScanner = new XMLScanner(valToAdopt);
    }

    fScanner->setDocHandler(this);
    fScanner->setDocTypeHandler(this);

    fNodeStack = new ValueStackOf<DOM_Node>(64);
    this->reset();


}

DOMParser::~DOMParser()
{
    if( !gScanner )
    {
        XMLMutexLock lock( fgScanMutex );
        if( !gScanner )
        {
            // Keep the XMLScanner
            gScanner = fScanner;
            fScanner = 0;
        }
    }

    delete fNodeStack;
    delete fScanner;
}