[2674] | 1 | /* |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
---|
| 4 | * this work for additional information regarding copyright ownership. |
---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 6 | * (the "License"); you may not use this file except in compliance with |
---|
| 7 | * the License. You may obtain a copy of the License at |
---|
| 8 | * |
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | * |
---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | * See the License for the specific language governing permissions and |
---|
| 15 | * limitations under the License. |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | /* |
---|
| 19 | * $Id: SchemaValidator.hpp 568078 2007-08-21 11:43:25Z amassari $ |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | #if !defined(SCHEMAVALIDATOR_HPP) |
---|
| 25 | #define SCHEMAVALIDATOR_HPP |
---|
| 26 | |
---|
| 27 | #include <xercesc/framework/XMLValidator.hpp> |
---|
| 28 | #include <xercesc/framework/XMLBuffer.hpp> |
---|
| 29 | #include <xercesc/util/ValueStackOf.hpp> |
---|
| 30 | #include <xercesc/validators/common/ContentSpecNode.hpp> |
---|
| 31 | #include <xercesc/validators/schema/SchemaGrammar.hpp> |
---|
| 32 | #include <xercesc/validators/schema/XSDErrorReporter.hpp> |
---|
| 33 | |
---|
| 34 | XERCES_CPP_NAMESPACE_BEGIN |
---|
| 35 | |
---|
| 36 | class GrammarResolver; |
---|
| 37 | class DatatypeValidator; |
---|
| 38 | class SchemaElementDecl; |
---|
| 39 | |
---|
| 40 | // |
---|
| 41 | // This is a derivative of the abstract validator interface. This class |
---|
| 42 | // implements a validator that supports standard XML Schema semantics. |
---|
| 43 | // This class handles scanning the of the schema, and provides |
---|
| 44 | // the standard validation services against the Schema info it found. |
---|
| 45 | // |
---|
| 46 | class VALIDATORS_EXPORT SchemaValidator : public XMLValidator |
---|
| 47 | { |
---|
| 48 | public: |
---|
| 49 | // ----------------------------------------------------------------------- |
---|
| 50 | // Constructors and Destructor |
---|
| 51 | // ----------------------------------------------------------------------- |
---|
| 52 | SchemaValidator |
---|
| 53 | ( |
---|
| 54 | XMLErrorReporter* const errReporter = 0 |
---|
| 55 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
---|
| 56 | ); |
---|
| 57 | virtual ~SchemaValidator(); |
---|
| 58 | |
---|
| 59 | // ----------------------------------------------------------------------- |
---|
| 60 | // Implementation of the XMLValidator interface |
---|
| 61 | // ----------------------------------------------------------------------- |
---|
| 62 | virtual int checkContent |
---|
| 63 | ( |
---|
| 64 | XMLElementDecl* const elemDecl |
---|
| 65 | , QName** const children |
---|
| 66 | , const unsigned int childCount |
---|
| 67 | ); |
---|
| 68 | |
---|
| 69 | virtual void faultInAttr |
---|
| 70 | ( |
---|
| 71 | XMLAttr& toFill |
---|
| 72 | , const XMLAttDef& attDef |
---|
| 73 | ) const; |
---|
| 74 | |
---|
| 75 | virtual void preContentValidation(bool reuseGrammar, |
---|
| 76 | bool validateDefAttr = false); |
---|
| 77 | |
---|
| 78 | virtual void postParseValidation(); |
---|
| 79 | |
---|
| 80 | virtual void reset(); |
---|
| 81 | |
---|
| 82 | virtual bool requiresNamespaces() const; |
---|
| 83 | |
---|
| 84 | virtual void validateAttrValue |
---|
| 85 | ( |
---|
| 86 | const XMLAttDef* attDef |
---|
| 87 | , const XMLCh* const attrValue |
---|
| 88 | , bool preValidation = false |
---|
| 89 | , const XMLElementDecl* elemDecl = 0 |
---|
| 90 | ); |
---|
| 91 | |
---|
| 92 | virtual void validateElement |
---|
| 93 | ( |
---|
| 94 | const XMLElementDecl* elemDef |
---|
| 95 | ); |
---|
| 96 | |
---|
| 97 | virtual Grammar* getGrammar() const; |
---|
| 98 | virtual void setGrammar(Grammar* aGrammar); |
---|
| 99 | |
---|
| 100 | // ----------------------------------------------------------------------- |
---|
| 101 | // Virtual DTD handler interface. |
---|
| 102 | // ----------------------------------------------------------------------- |
---|
| 103 | virtual bool handlesDTD() const; |
---|
| 104 | |
---|
| 105 | // ----------------------------------------------------------------------- |
---|
| 106 | // Virtual Schema handler interface. handlesSchema() always return false. |
---|
| 107 | // ----------------------------------------------------------------------- |
---|
| 108 | virtual bool handlesSchema() const; |
---|
| 109 | |
---|
| 110 | // ----------------------------------------------------------------------- |
---|
| 111 | // Schema Validator methods |
---|
| 112 | // ----------------------------------------------------------------------- |
---|
| 113 | void normalizeWhiteSpace(DatatypeValidator* dV, const XMLCh* const value, XMLBuffer& toFill); |
---|
| 114 | |
---|
| 115 | // ----------------------------------------------------------------------- |
---|
| 116 | // Setter methods |
---|
| 117 | // ----------------------------------------------------------------------- |
---|
| 118 | void setGrammarResolver(GrammarResolver* grammarResolver); |
---|
| 119 | |
---|
| 120 | void setXsiType(const XMLCh* const prefix |
---|
| 121 | , const XMLCh* const localPart |
---|
| 122 | , const unsigned int uriId); |
---|
| 123 | |
---|
| 124 | void setNillable(bool isNil); |
---|
| 125 | void setErrorReporter(XMLErrorReporter* const errorReporter); |
---|
| 126 | void setExitOnFirstFatal(const bool newValue); |
---|
| 127 | void setDatatypeBuffer(const XMLCh* const value); |
---|
| 128 | void clearDatatypeBuffer(); |
---|
| 129 | |
---|
| 130 | // ----------------------------------------------------------------------- |
---|
| 131 | // Getter methods |
---|
| 132 | // ----------------------------------------------------------------------- |
---|
| 133 | ComplexTypeInfo* getCurrentTypeInfo() const; |
---|
| 134 | DatatypeValidator *getCurrentDatatypeValidator() const; |
---|
| 135 | DatatypeValidator *getMostRecentAttrValidator() const; |
---|
| 136 | bool getErrorOccurred() const; |
---|
| 137 | bool getIsElemSpecified() const; |
---|
| 138 | const XMLCh* getNormalizedValue() const; |
---|
| 139 | |
---|
| 140 | private: |
---|
| 141 | // ----------------------------------------------------------------------- |
---|
| 142 | // Unimplemented constructors and operators |
---|
| 143 | // ----------------------------------------------------------------------- |
---|
| 144 | SchemaValidator(const SchemaValidator&); |
---|
| 145 | SchemaValidator& operator=(const SchemaValidator&); |
---|
| 146 | |
---|
| 147 | // ----------------------------------------------------------------------- |
---|
| 148 | // Element Consitency Checking methods |
---|
| 149 | // ----------------------------------------------------------------------- |
---|
| 150 | void checkRefElementConsistency(SchemaGrammar* const currentGrammar, |
---|
| 151 | const ComplexTypeInfo* const curTypeInfo, |
---|
| 152 | const XercesGroupInfo* const curGroup = 0); |
---|
| 153 | |
---|
| 154 | // ----------------------------------------------------------------------- |
---|
| 155 | // Particle Derivation Checking methods |
---|
| 156 | // ----------------------------------------------------------------------- |
---|
| 157 | void checkParticleDerivation(SchemaGrammar* const currentGrammar, |
---|
| 158 | const ComplexTypeInfo* const typeInfo); |
---|
| 159 | void checkParticleDerivationOk(SchemaGrammar* const currentGrammar, |
---|
| 160 | ContentSpecNode* const curNode, |
---|
| 161 | const int derivedScope, |
---|
| 162 | ContentSpecNode* const baseNode, |
---|
| 163 | const int baseScope, |
---|
| 164 | const ComplexTypeInfo* const baseInfo = 0, |
---|
| 165 | const bool toCheckOccurrence = true); |
---|
| 166 | ContentSpecNode* checkForPointlessOccurrences(ContentSpecNode* const specNode, |
---|
| 167 | const ContentSpecNode::NodeTypes nodeType, |
---|
| 168 | ValueVectorOf<ContentSpecNode*>* const nodes); |
---|
| 169 | void gatherChildren(const ContentSpecNode::NodeTypes parentNodeType, |
---|
| 170 | ContentSpecNode* const specNode, |
---|
| 171 | ValueVectorOf<ContentSpecNode*>* const nodes); |
---|
| 172 | bool isOccurrenceRangeOK(const int min1, const int max1, const int min2, const int max2); |
---|
| 173 | void checkNSCompat(const ContentSpecNode* const derivedSpecNode, |
---|
| 174 | const ContentSpecNode* const baseSpecNode, |
---|
| 175 | const bool toCheckOccurence); |
---|
| 176 | bool wildcardEltAllowsNamespace(const ContentSpecNode* const baseSpecNode, |
---|
| 177 | const unsigned int derivedURI); |
---|
| 178 | void checkNameAndTypeOK(SchemaGrammar* const currentGrammar, |
---|
| 179 | const ContentSpecNode* const derivedSpecNode, |
---|
| 180 | const int derivedScope, |
---|
| 181 | const ContentSpecNode* const baseSpecNode, |
---|
| 182 | const int baseScope, |
---|
| 183 | const ComplexTypeInfo* const baseInfo = 0); |
---|
| 184 | SchemaElementDecl* findElement(const int scope, |
---|
| 185 | const unsigned int uriIndex, |
---|
| 186 | const XMLCh* const name, |
---|
| 187 | SchemaGrammar* const grammar, |
---|
| 188 | const ComplexTypeInfo* const typeInfo = 0); |
---|
| 189 | void checkICRestriction(const SchemaElementDecl* const derivedElemDecl, |
---|
| 190 | const SchemaElementDecl* const baseElemDecl, |
---|
| 191 | const XMLCh* const derivedElemName, |
---|
| 192 | const XMLCh* const baseElemName); |
---|
| 193 | void checkTypesOK(const SchemaElementDecl* const derivedElemDecl, |
---|
| 194 | const SchemaElementDecl* const baseElemDecl, |
---|
| 195 | const XMLCh* const derivedElemName); |
---|
| 196 | void checkRecurseAsIfGroup(SchemaGrammar* const currentGrammar, |
---|
| 197 | ContentSpecNode* const derivedSpecNode, |
---|
| 198 | const int derivedScope, |
---|
| 199 | const ContentSpecNode* const baseSpecNode, |
---|
| 200 | const int baseScope, |
---|
| 201 | ValueVectorOf<ContentSpecNode*>* const nodes, |
---|
| 202 | const ComplexTypeInfo* const baseInfo); |
---|
| 203 | void checkRecurse(SchemaGrammar* const currentGrammar, |
---|
| 204 | const ContentSpecNode* const derivedSpecNode, |
---|
| 205 | const int derivedScope, |
---|
| 206 | ValueVectorOf<ContentSpecNode*>* const derivedNodes, |
---|
| 207 | const ContentSpecNode* const baseSpecNode, |
---|
| 208 | const int baseScope, |
---|
| 209 | ValueVectorOf<ContentSpecNode*>* const baseNodes, |
---|
| 210 | const ComplexTypeInfo* const baseInfo, |
---|
| 211 | const bool toLax = false); |
---|
| 212 | void checkNSSubset(const ContentSpecNode* const derivedSpecNode, |
---|
| 213 | const ContentSpecNode* const baseSpecNode); |
---|
| 214 | bool isWildCardEltSubset(const ContentSpecNode* const derivedSpecNode, |
---|
| 215 | const ContentSpecNode* const baseSpecNode); |
---|
| 216 | void checkNSRecurseCheckCardinality(SchemaGrammar* const currentGrammar, |
---|
| 217 | const ContentSpecNode* const derivedSpecNode, |
---|
| 218 | ValueVectorOf<ContentSpecNode*>* const derivedNodes, |
---|
| 219 | const int derivedScope, |
---|
| 220 | ContentSpecNode* const baseSpecNode, |
---|
| 221 | const bool toCheckOccurence); |
---|
| 222 | void checkRecurseUnordered(SchemaGrammar* const currentGrammar, |
---|
| 223 | const ContentSpecNode* const derivedSpecNode, |
---|
| 224 | ValueVectorOf<ContentSpecNode*>* const derivedNodes, |
---|
| 225 | const int derivedScope, |
---|
| 226 | ContentSpecNode* const baseSpecNode, |
---|
| 227 | ValueVectorOf<ContentSpecNode*>* const baseNodes, |
---|
| 228 | const int baseScope, |
---|
| 229 | const ComplexTypeInfo* const baseInfo); |
---|
| 230 | void checkMapAndSum(SchemaGrammar* const currentGrammar, |
---|
| 231 | const ContentSpecNode* const derivedSpecNode, |
---|
| 232 | ValueVectorOf<ContentSpecNode*>* const derivedNodes, |
---|
| 233 | const int derivedScope, |
---|
| 234 | ContentSpecNode* const baseSpecNode, |
---|
| 235 | ValueVectorOf<ContentSpecNode*>* const baseNodes, |
---|
| 236 | const int baseScope, |
---|
| 237 | const ComplexTypeInfo* const baseInfo); |
---|
| 238 | ContentSpecNode* getNonUnaryGroup(ContentSpecNode* const pNode); |
---|
| 239 | |
---|
| 240 | // ----------------------------------------------------------------------- |
---|
| 241 | // Private data members |
---|
| 242 | // |
---|
| 243 | // ----------------------------------------------------------------------- |
---|
| 244 | // The following comes from or set by the Scanner |
---|
| 245 | // fSchemaGrammar |
---|
| 246 | // The current schema grammar used by the validator |
---|
| 247 | // |
---|
| 248 | // fGrammarResolver |
---|
| 249 | // All the schema grammar stored |
---|
| 250 | // |
---|
| 251 | // fXsiType |
---|
| 252 | // Store the Schema Type Attribute Value if schema type is specified |
---|
| 253 | // |
---|
| 254 | // fNil |
---|
| 255 | // Indicates if Nillable has been set |
---|
| 256 | // ----------------------------------------------------------------------- |
---|
| 257 | // The following used internally in the validator |
---|
| 258 | // |
---|
| 259 | // fCurrentDatatypeValidator |
---|
| 260 | // The validator used for validating the content of elements |
---|
| 261 | // with simple types |
---|
| 262 | // |
---|
| 263 | // fDatatypeBuffer |
---|
| 264 | // Buffer for simple type element string content |
---|
| 265 | // |
---|
| 266 | // fTrailing |
---|
| 267 | // Previous chunk had a trailing space |
---|
| 268 | // |
---|
| 269 | // fSeenId |
---|
| 270 | // Indicate if an attribute of ID type has been seen already, reset per element. |
---|
| 271 | // |
---|
| 272 | // fSchemaErrorReporter |
---|
| 273 | // Report schema process errors |
---|
| 274 | // |
---|
| 275 | // fTypeStack |
---|
| 276 | // Stack of complex type declarations. |
---|
| 277 | // |
---|
| 278 | // fMostRecentAttrValidator |
---|
| 279 | // DatatypeValidator that validated attribute most recently processed |
---|
| 280 | // |
---|
| 281 | // fErrorOccurred |
---|
| 282 | // whether an error occurred in the most recent operation |
---|
| 283 | // ----------------------------------------------------------------------- |
---|
| 284 | MemoryManager* fMemoryManager; |
---|
| 285 | SchemaGrammar* fSchemaGrammar; |
---|
| 286 | GrammarResolver* fGrammarResolver; |
---|
| 287 | QName* fXsiType; |
---|
| 288 | bool fNil; |
---|
| 289 | DatatypeValidator* fCurrentDatatypeValidator; |
---|
| 290 | XMLBuffer* fNotationBuf; |
---|
| 291 | XMLBuffer fDatatypeBuffer; |
---|
| 292 | bool fTrailing; |
---|
| 293 | bool fSeenId; |
---|
| 294 | XSDErrorReporter fSchemaErrorReporter; |
---|
| 295 | ValueStackOf<ComplexTypeInfo*>* fTypeStack; |
---|
| 296 | DatatypeValidator * fMostRecentAttrValidator; |
---|
| 297 | bool fErrorOccurred; |
---|
| 298 | bool fElemIsSpecified; |
---|
| 299 | }; |
---|
| 300 | |
---|
| 301 | |
---|
| 302 | // --------------------------------------------------------------------------- |
---|
| 303 | // SchemaValidator: Setter methods |
---|
| 304 | // --------------------------------------------------------------------------- |
---|
| 305 | inline void SchemaValidator::setGrammarResolver(GrammarResolver* grammarResolver) { |
---|
| 306 | fGrammarResolver = grammarResolver; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | inline void SchemaValidator::setXsiType(const XMLCh* const prefix |
---|
| 310 | , const XMLCh* const localPart |
---|
| 311 | , const unsigned int uriId) |
---|
| 312 | { |
---|
| 313 | delete fXsiType; |
---|
| 314 | fXsiType = new (fMemoryManager) QName(prefix, localPart, uriId, fMemoryManager); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | inline void SchemaValidator::setNillable(bool isNil) { |
---|
| 318 | fNil = isNil; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | inline void SchemaValidator::setExitOnFirstFatal(const bool newValue) { |
---|
| 322 | |
---|
| 323 | fSchemaErrorReporter.setExitOnFirstFatal(newValue); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | inline void SchemaValidator::setDatatypeBuffer(const XMLCh* const value) |
---|
| 327 | { |
---|
| 328 | fDatatypeBuffer.append(value); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | inline void SchemaValidator::clearDatatypeBuffer() |
---|
| 332 | { |
---|
| 333 | fDatatypeBuffer.reset(); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | // --------------------------------------------------------------------------- |
---|
| 337 | // SchemaValidator: Getter methods |
---|
| 338 | // --------------------------------------------------------------------------- |
---|
| 339 | inline ComplexTypeInfo* SchemaValidator::getCurrentTypeInfo() const { |
---|
| 340 | if (fTypeStack->empty()) |
---|
| 341 | return 0; |
---|
| 342 | return fTypeStack->peek(); |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | inline DatatypeValidator * SchemaValidator::getCurrentDatatypeValidator() const |
---|
| 346 | { |
---|
| 347 | return fCurrentDatatypeValidator; |
---|
| 348 | } |
---|
| 349 | inline DatatypeValidator *SchemaValidator::getMostRecentAttrValidator() const |
---|
| 350 | { |
---|
| 351 | return fMostRecentAttrValidator; |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | // --------------------------------------------------------------------------- |
---|
| 355 | // Virtual interface |
---|
| 356 | // --------------------------------------------------------------------------- |
---|
| 357 | inline Grammar* SchemaValidator::getGrammar() const { |
---|
| 358 | return fSchemaGrammar; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | inline void SchemaValidator::setGrammar(Grammar* aGrammar) { |
---|
| 362 | fSchemaGrammar = (SchemaGrammar*) aGrammar; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | inline void SchemaValidator::setErrorReporter(XMLErrorReporter* const errorReporter) { |
---|
| 366 | |
---|
| 367 | XMLValidator::setErrorReporter(errorReporter); |
---|
| 368 | fSchemaErrorReporter.setErrorReporter(errorReporter); |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | // --------------------------------------------------------------------------- |
---|
| 372 | // SchemaValidator: DTD handler interface |
---|
| 373 | // --------------------------------------------------------------------------- |
---|
| 374 | inline bool SchemaValidator::handlesDTD() const |
---|
| 375 | { |
---|
| 376 | // No DTD scanning |
---|
| 377 | return false; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | // --------------------------------------------------------------------------- |
---|
| 381 | // SchemaValidator: Schema handler interface |
---|
| 382 | // --------------------------------------------------------------------------- |
---|
| 383 | inline bool SchemaValidator::handlesSchema() const |
---|
| 384 | { |
---|
| 385 | return true; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | // --------------------------------------------------------------------------- |
---|
| 389 | // SchemaValidator: Particle derivation checking |
---|
| 390 | // --------------------------------------------------------------------------- |
---|
| 391 | inline bool |
---|
| 392 | SchemaValidator::isOccurrenceRangeOK(const int min1, const int max1, |
---|
| 393 | const int min2, const int max2) { |
---|
| 394 | |
---|
| 395 | if (min1 >= min2 && |
---|
| 396 | (max2 == SchemaSymbols::XSD_UNBOUNDED || |
---|
| 397 | (max1 != SchemaSymbols::XSD_UNBOUNDED && max1 <= max2))) { |
---|
| 398 | return true; |
---|
| 399 | } |
---|
| 400 | return false; |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | inline bool SchemaValidator::getErrorOccurred() const |
---|
| 404 | { |
---|
| 405 | return fErrorOccurred; |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | inline bool SchemaValidator::getIsElemSpecified() const |
---|
| 409 | { |
---|
| 410 | return fElemIsSpecified; |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | inline const XMLCh* SchemaValidator::getNormalizedValue() const |
---|
| 414 | { |
---|
| 415 | return fDatatypeBuffer.getRawBuffer(); |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | XERCES_CPP_NAMESPACE_END |
---|
| 419 | |
---|
| 420 | #endif |
---|