Newer
Older
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
{
if(fPSVIElemContext.fCurrentTypeInfo->getAnonymous())
typeName = XMLUni::fgZeroLenString;
else
typeName = fPSVIElemContext.fCurrentTypeInfo->getTypeLocalName();
typeURI = fPSVIElemContext.fCurrentTypeInfo->getTypeUri();
}
else
{
DatatypeValidator *actualDV = (psviMemberType)?psviMemberType:fPSVIElemContext.fCurrentDV;
if(actualDV)
{
if(actualDV->getAnonymous())
typeName = XMLUni::fgZeroLenString;
else
typeName = actualDV->getTypeLocalName();
typeURI = actualDV->getTypeUri();
}
}
}
else
{
// idiosyncratically, if there's an
// error but this element was simpleType-validated,
// the tests demand anySimpleType be returned
if(fPSVIElemContext.fCurrentDV)
typeName = SchemaSymbols::fgDT_ANYSIMPLETYPE;
}
fDocHandler->elementTypeInfo(typeName, typeURI);
}
else
{
Alberto Massari
committed
fDocHandler->elementTypeInfo(0, 0);
Gareth Reakes
committed
// reset xsi:type ComplexTypeInfo
if (fGrammarType == Grammar::SchemaGrammarType) {
Gareth Reakes
committed
((SchemaElementDecl*)elemDecl)->reset();
if (!isRoot)
Gareth Reakes
committed
((SchemaElementDecl*)(fElemStack.topElement()->fThisElement))->
setXsiComplexTypeInfo(((SchemaValidator*)fValidator)->getCurrentTypeInfo());
}
// If the elem stack is empty, then it was an empty root
if (isRoot)
gotData = false;
else
{
// Restore the grammar
fGrammar = fElemStack.getCurrentGrammar();
fGrammarType = fGrammar->getGrammarType();
if (fGrammarType == Grammar::SchemaGrammarType && !fValidator->handlesSchema()) {
if (fValidatorFromUser)
David Abram Cargill
committed
ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Gen_NoSchemaValidator, fMemoryManager);
else {
fValidator = fSchemaValidator;
}
}
else if (fGrammarType == Grammar::DTDGrammarType && !fValidator->handlesDTD()) {
if (fValidatorFromUser)
David Abram Cargill
committed
ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Gen_NoDTDValidator, fMemoryManager);
else {
fValidator = fDTDValidator;
}
}
fValidator->setGrammar(fGrammar);
// Restore the validation flag
fValidate = fElemStack.getValidationFlag();
}
}
else if (fGrammarType == Grammar::SchemaGrammarType)
{
// not empty
fErrorStack->push(fPSVIElemContext.fErrorOccurred);
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
return true;
}
unsigned int
IGXMLScanner::resolveQName(const XMLCh* const qName
, XMLBuffer& prefixBuf
, const short mode
, int& prefixColonPos)
{
// Lets split out the qName into a URI and name buffer first. The URI
// can be empty.
prefixColonPos = XMLString::indexOf(qName, chColon);
if (prefixColonPos == -1)
{
// Its all name with no prefix, so put the whole thing into the name
// buffer. Then map the empty string to a URI, since the empty string
// represents the default namespace. This will either return some
// explicit URI which the default namespace is mapped to, or the
// the default global namespace.
bool unknown = false;
prefixBuf.reset();
return fElemStack.mapPrefixToURI(XMLUni::fgZeroLenString, (ElemStack::MapModes) mode, unknown);
}
else
{
// Copy the chars up to but not including the colon into the prefix
// buffer.
prefixBuf.set(qName, prefixColonPos);
// Watch for the special namespace prefixes. We always map these to
// special URIs. 'xml' gets mapped to the official URI that its defined
// to map to by the NS spec. xmlns gets mapped to a special place holder
// URI that we define (so that it maps to something checkable.)
const XMLCh* prefixRawBuf = prefixBuf.getRawBuffer();
if (XMLString::equals(prefixRawBuf, XMLUni::fgXMLNSString)) {
// if this is an element, it is an error to have xmlns as prefix
if (mode == ElemStack::Mode_Element)
emitError(XMLErrs::NoXMLNSAsElementPrefix, qName);
return fXMLNSNamespaceId;
}
else if (XMLString::equals(prefixRawBuf, XMLUni::fgXMLString)) {
return fXMLNamespaceId;
}
else
{
bool unknown = false;
unsigned int uriId = fElemStack.mapPrefixToURI(prefixRawBuf, (ElemStack::MapModes) mode, unknown);
if (unknown)
emitError(XMLErrs::UnknownPrefix, prefixRawBuf);
return uriId;
}
}
}
// ---------------------------------------------------------------------------
// IGXMLScanner: Helper methos
// ---------------------------------------------------------------------------
void IGXMLScanner::resizeElemState() {
unsigned int newSize = fElemStateSize * 2;
unsigned int* newElemState = (unsigned int*) fMemoryManager->allocate
(
newSize * sizeof(unsigned int)
); //new unsigned int[newSize];
// Copy the existing values
unsigned int index = 0;
for (; index < fElemStateSize; index++)
newElemState[index] = fElemState[index];
for (; index < newSize; index++)
newElemState[index] = 0;
// Delete the old array and udpate our members
fMemoryManager->deallocate(fElemState); //delete [] fElemState;
fElemState = newElemState;
fElemStateSize = newSize;
}
// ---------------------------------------------------------------------------
// IGXMLScanner: Grammar preparsing
// ---------------------------------------------------------------------------
Grammar* IGXMLScanner::loadGrammar(const InputSource& src
, const short grammarType
, const bool toCache)
{
Grammar* loadedGrammar = 0;
try
{
fGrammarResolver->cacheGrammarFromParse(false);
Alberto Massari
committed
// if the new grammar has to be cached, better use the already cached
// grammars, or the an exception will be thrown when caching an already
// cached grammar
fGrammarResolver->useCachedGrammarInParse(toCache);
fRootGrammar = 0;
if (fValScheme == Val_Auto) {
fValidate = true;
}
// Reset some status flags
fInException = false;
fStandalone = false;
fErrorCount = 0;
fHasNoDTD = true;
fSeeXsi = false;
if (grammarType == Grammar::SchemaGrammarType) {
loadedGrammar = loadXMLSchemaGrammar(src, toCache);
}
else if (grammarType == Grammar::DTDGrammarType) {
loadedGrammar = loadDTDGrammar(src, toCache);
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
}
// Reset the reader manager to close all files, sockets, etc...
fReaderMgr.reset();
}
// NOTE:
//
// In all of the error processing below, the emitError() call MUST come
// before the flush of the reader mgr, or it will fail because it tries
// to find out the position in the XML source of the error.
catch(const XMLErrs::Codes)
{
// This is a 'first fatal error' type exit, so reset and fall through
fReaderMgr.reset();
}
catch(const XMLValid::Codes)
{
// This is a 'first fatal error' type exit, so reset and fall through
fReaderMgr.reset();
}
catch(const XMLException& excToCatch)
{
// Emit the error and catch any user exception thrown from here. Make
// sure in all cases we flush the reader manager.
fInException = true;
try
{
if (excToCatch.getErrorType() == XMLErrorReporter::ErrType_Warning)
emitError
(
XMLErrs::DisplayErrorMessage
, excToCatch.getMessage()
);
else if (excToCatch.getErrorType() >= XMLErrorReporter::ErrType_Fatal)
emitError
(
XMLErrs::XMLException_Fatal
, excToCatch.getType()
, excToCatch.getMessage()
);
else
emitError
(
XMLErrs::XMLException_Error
, excToCatch.getType()
, excToCatch.getMessage()
);
}
Neil Graham
committed
catch(const OutOfMemoryException&)
{
throw;
}
catch(...)
{
// Flush the reader manager and rethrow user's error
fReaderMgr.reset();
throw;
}
// If it returned, then reset the reader manager and fall through
fReaderMgr.reset();
}
Neil Graham
committed
catch(const OutOfMemoryException&)
{
throw;
}
catch(...)
{
// Reset and rethrow
fReaderMgr.reset();
throw;
}
return loadedGrammar;
}
Grammar* IGXMLScanner::loadDTDGrammar(const InputSource& src,
const bool toCache)
{
// Reset the validators
fDTDValidator->reset();
if (fValidatorFromUser)
fValidator->reset();
if (!fValidator->handlesDTD()) {
if (fValidatorFromUser && fValidate)
David Abram Cargill
committed
ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Gen_NoDTDValidator, fMemoryManager);
else {
fValidator = fDTDValidator;
}
}
fDTDGrammar = (DTDGrammar*) fGrammarResolver->getGrammar(XMLUni::fgDTDEntityString);
if (fDTDGrammar) {
fDTDGrammar->reset();
}
else {
fDTDGrammar = new (fGrammarPoolMemoryManager) DTDGrammar(fGrammarPoolMemoryManager);
fGrammarResolver->putGrammar(fDTDGrammar);
}
fGrammar = fDTDGrammar;
fGrammarType = fGrammar->getGrammarType();
fValidator->setGrammar(fGrammar);
// And for all installed handlers, send reset events. This gives them
// a chance to flush any cached data.
if (fDocHandler)
fDocHandler->resetDocument();
if (fEntityHandler)
fEntityHandler->resetEntities();
if (fErrorReporter)
fErrorReporter->resetErrors();
// Clear out the id reference list
Neil Graham
committed
// and clear out the darned undeclared DTD element pool...
fDTDElemNonDeclPool->removeAll();
if (toCache) {
unsigned int sysId = fGrammarResolver->getStringPool()->addOrFind(src.getSystemId());
const XMLCh* sysIdStr = fGrammarResolver->getStringPool()->getValueForId(sysId);
fGrammarResolver->orphanGrammar(XMLUni::fgDTDEntityString);
((XMLDTDDescription*) (fGrammar->getGrammarDescription()))->setSystemId(sysIdStr);
fGrammarResolver->putGrammar(fGrammar);
}
// Handle the creation of the XML reader object for this input source.
// This will provide us with transcoding and basic lexing services.
XMLReader* newReader = fReaderMgr.createReader
(
src
, false
, XMLReader::RefFrom_NonLiteral
, XMLReader::Type_General
, XMLReader::Source_External
, fCalculateSrcOfs
);
if (!newReader) {
if (src.getIssueFatalErrorIfNotFound())
David Abram Cargill
committed
ThrowXMLwithMemMgr1(RuntimeException, XMLExcepts::Scan_CouldNotOpenSource, src.getSystemId(), fMemoryManager);
David Abram Cargill
committed
ThrowXMLwithMemMgr1(RuntimeException, XMLExcepts::Scan_CouldNotOpenSource_Warning, src.getSystemId(), fMemoryManager);
}
// In order to make the processing work consistently, we have to
// make this look like an external entity. So create an entity
// decl and fill it in and push it with the reader, as happens
// with an external entity. Put a janitor on it to insure it gets
// cleaned up. The reader manager does not adopt them.
const XMLCh gDTDStr[] = { chLatin_D, chLatin_T, chLatin_D , chNull };
DTDEntityDecl* declDTD = new (fGrammarPoolMemoryManager) DTDEntityDecl(gDTDStr, false, fGrammarPoolMemoryManager);
declDTD->setSystemId(src.getSystemId());
Janitor<DTDEntityDecl> janDecl(declDTD);
// Mark this one as a throw at end
newReader->setThrowAtEnd(true);
// And push it onto the stack, with its pseudo name
fReaderMgr.pushReader(newReader, declDTD);
// If we have a doc type handler and advanced callbacks are enabled,
// call the doctype event.
if (fDocTypeHandler) {
// Create a dummy root
DTDElementDecl* rootDecl = new (fGrammarPoolMemoryManager) DTDElementDecl
(
gDTDStr
, fEmptyNamespaceId
, DTDElementDecl::Any
, fGrammarPoolMemoryManager
rootDecl->setCreateReason(DTDElementDecl::AsRootElem);
rootDecl->setExternalElemDeclaration(true);
Janitor<DTDElementDecl> janSrc(rootDecl);
fDocTypeHandler->doctypeDecl(*rootDecl, src.getPublicId(), src.getSystemId(), false, true);
}
// Create DTDScanner
DTDScanner dtdScanner
(
(DTDGrammar*) fGrammar
, fDocTypeHandler
, fGrammarPoolMemoryManager
, fMemoryManager
);
dtdScanner.setScannerInfo(this, &fReaderMgr, &fBufMgr);
// Tell it its not in an include section
dtdScanner.scanExtSubsetDecl(false, true);
if (fValidate) {
// validate the DTD scan so far
fValidator->preContentValidation(false, true);
}
if (toCache)
fGrammarResolver->cacheGrammars();
return fDTDGrammar;
}
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
// ---------------------------------------------------------------------------
// IGXMLScanner: Helper methods
// ---------------------------------------------------------------------------
void IGXMLScanner::processSchemaLocation(XMLCh* const schemaLoc)
{
XMLCh* locStr = schemaLoc;
XMLReader* curReader = fReaderMgr.getCurrentReader();
fLocationPairs->removeAllElements();
while (*locStr)
{
do {
if (!curReader->isWhitespace(*locStr))
break;
*locStr = chNull;
} while (*++locStr);
if (*locStr) {
fLocationPairs->addElement(locStr);
while (*++locStr) {
if (curReader->isWhitespace(*locStr))
break;
}
}
}
}
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
void IGXMLScanner::endElementPSVI(SchemaElementDecl* const elemDecl,
DatatypeValidator* const memberDV)
{
PSVIElement::ASSESSMENT_TYPE validationAttempted;
PSVIElement::VALIDITY_STATE validity = PSVIElement::VALIDITY_NOTKNOWN;
if (fPSVIElemContext.fElemDepth > fPSVIElemContext.fFullValidationDepth)
validationAttempted = PSVIElement::VALIDATION_FULL;
else if (fPSVIElemContext.fElemDepth > fPSVIElemContext.fNoneValidationDepth)
validationAttempted = PSVIElement::VALIDATION_NONE;
else
{
validationAttempted = PSVIElement::VALIDATION_PARTIAL;
fPSVIElemContext.fFullValidationDepth =
fPSVIElemContext.fNoneValidationDepth = fPSVIElemContext.fElemDepth - 1;
}
if (fValidate && elemDecl->isDeclared())
{
validity = (fPSVIElemContext.fErrorOccurred)
? PSVIElement::VALIDITY_INVALID : PSVIElement::VALIDITY_VALID;
}
XSTypeDefinition* typeDef = 0;
bool isMixed = false;
if (fPSVIElemContext.fCurrentTypeInfo)
typeDef = (XSTypeDefinition*) fModel->getXSObject(fPSVIElemContext.fCurrentTypeInfo);
SchemaElementDecl::ModelTypes modelType = (SchemaElementDecl::ModelTypes)fPSVIElemContext.fCurrentTypeInfo->getContentType();
isMixed = (modelType == SchemaElementDecl::Mixed_Simple
|| modelType == SchemaElementDecl::Mixed_Complex);
}
else if (fPSVIElemContext.fCurrentDV)
typeDef = (XSTypeDefinition*) fModel->getXSObject(fPSVIElemContext.fCurrentDV);
XMLCh* canonicalValue = 0;
if (fPSVIElemContext.fNormalizedValue && !isMixed &&
validity == PSVIElement::VALIDITY_VALID)
{
if (memberDV)
canonicalValue = (XMLCh*) memberDV->getCanonicalRepresentation(fPSVIElemContext.fNormalizedValue, fMemoryManager);
else if (fPSVIElemContext.fCurrentDV)
canonicalValue = (XMLCh*) fPSVIElemContext.fCurrentDV->getCanonicalRepresentation(fPSVIElemContext.fNormalizedValue, fMemoryManager);
}
fPSVIElement->reset
(
validity
, validationAttempted
, fPSVIElemContext.fIsSpecified
, (elemDecl->isDeclared())
? (XSElementDeclaration*) fModel->getXSObject(elemDecl) : 0
, typeDef
, (memberDV) ? (XSSimpleTypeDefinition*) fModel->getXSObject(memberDV) : 0
, fModel
, elemDecl->getDefaultValue()
, fPSVIElemContext.fNormalizedValue
, canonicalValue
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
);
fPSVIHandler->handleElementPSVI
(
elemDecl->getBaseName()
, fURIStringPool->getValueForId(elemDecl->getURI())
, fPSVIElement
);
// decrease element depth
fPSVIElemContext.fElemDepth--;
}
void IGXMLScanner::resetPSVIElemContext()
{
fPSVIElemContext.fIsSpecified = false;
fPSVIElemContext.fErrorOccurred = false;
fPSVIElemContext.fElemDepth = -1;
fPSVIElemContext.fFullValidationDepth = -1;
fPSVIElemContext.fNoneValidationDepth = -1;
fPSVIElemContext.fCurrentDV = 0;
fPSVIElemContext.fCurrentTypeInfo = 0;
fPSVIElemContext.fNormalizedValue = 0;