Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:27:32

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 /*
0019  * $Id$
0020  */
0021 
0022 #if !defined(XERCESC_INCLUDE_GUARD_SCHEMAVALIDATOR_HPP)
0023 #define XERCESC_INCLUDE_GUARD_SCHEMAVALIDATOR_HPP
0024 
0025 #include <xercesc/framework/XMLValidator.hpp>
0026 #include <xercesc/framework/XMLBuffer.hpp>
0027 #include <xercesc/util/ValueStackOf.hpp>
0028 #include <xercesc/validators/common/ContentSpecNode.hpp>
0029 #include <xercesc/validators/schema/SchemaGrammar.hpp>
0030 #include <xercesc/validators/schema/XSDErrorReporter.hpp>
0031 
0032 XERCES_CPP_NAMESPACE_BEGIN
0033 
0034 class GrammarResolver;
0035 class DatatypeValidator;
0036 class SchemaElementDecl;
0037 
0038 //
0039 //  This is a derivative of the abstract validator interface. This class
0040 //  implements a validator that supports standard XML Schema semantics.
0041 //  This class handles scanning the of the schema, and provides
0042 //  the standard validation services against the Schema info it found.
0043 //
0044 class VALIDATORS_EXPORT SchemaValidator : public XMLValidator
0045 {
0046 public:
0047     // -----------------------------------------------------------------------
0048     //  Constructors and Destructor
0049     // -----------------------------------------------------------------------
0050     SchemaValidator
0051     (
0052           XMLErrorReporter* const errReporter = 0
0053           , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
0054     );
0055     virtual ~SchemaValidator();
0056 
0057     // -----------------------------------------------------------------------
0058     //  Implementation of the XMLValidator interface
0059     // -----------------------------------------------------------------------
0060     virtual bool checkContent
0061     (
0062         XMLElementDecl* const   elemDecl
0063         , QName** const         children
0064         , XMLSize_t             childCount
0065         , XMLSize_t*            indexFailingChild
0066     );
0067 
0068     virtual void faultInAttr
0069     (
0070                 XMLAttr&    toFill
0071         , const XMLAttDef&  attDef
0072     )   const;
0073 
0074     virtual void preContentValidation(bool reuseGrammar,
0075                                       bool validateDefAttr = false);
0076 
0077     virtual void postParseValidation();
0078 
0079     virtual void reset();
0080 
0081     virtual bool requiresNamespaces() const;
0082 
0083     virtual void validateAttrValue
0084     (
0085         const   XMLAttDef*                  attDef
0086         , const XMLCh* const                attrValue
0087         , bool                              preValidation = false
0088         , const XMLElementDecl*             elemDecl = 0
0089     );
0090 
0091     virtual void validateElement
0092     (
0093         const   XMLElementDecl*             elemDef
0094     );
0095 
0096     virtual Grammar* getGrammar() const;
0097     virtual void setGrammar(Grammar* aGrammar);
0098 
0099     // -----------------------------------------------------------------------
0100     //  Virtual DTD handler interface.
0101     // -----------------------------------------------------------------------
0102     virtual bool handlesDTD() const;
0103 
0104     // -----------------------------------------------------------------------
0105     //  Virtual Schema handler interface. handlesSchema() always return false.
0106     // -----------------------------------------------------------------------
0107     virtual bool handlesSchema() const;
0108 
0109     // -----------------------------------------------------------------------
0110     //  Schema Validator methods
0111     // -----------------------------------------------------------------------
0112     void normalizeWhiteSpace(DatatypeValidator* dV, const XMLCh* const value, XMLBuffer& toFill, bool bStandalone = false);
0113 
0114     // -----------------------------------------------------------------------
0115     //  Setter methods
0116     // -----------------------------------------------------------------------
0117     void setGrammarResolver(GrammarResolver* grammarResolver);
0118 
0119     void setXsiType(const XMLCh* const        prefix
0120       , const XMLCh* const        localPart
0121        , const unsigned int        uriId);
0122 
0123     void setNillable(bool isNil);
0124     void resetNillable();
0125     void setErrorReporter(XMLErrorReporter* const errorReporter);
0126     void setExitOnFirstFatal(const bool newValue);
0127     void setDatatypeBuffer(const XMLCh* const value);
0128     void clearDatatypeBuffer();
0129 
0130     // -----------------------------------------------------------------------
0131     //  Getter methods
0132     // -----------------------------------------------------------------------
0133     ComplexTypeInfo* getCurrentTypeInfo() const;
0134     DatatypeValidator *getCurrentDatatypeValidator() const;
0135     DatatypeValidator *getMostRecentAttrValidator() const;
0136     bool getErrorOccurred() const;
0137     bool getIsElemSpecified() const;
0138     bool getIsXsiTypeSet() const;
0139     const XMLCh* getNormalizedValue() const;
0140 
0141 private:
0142     // -----------------------------------------------------------------------
0143     //  Unimplemented constructors and operators
0144     // -----------------------------------------------------------------------
0145     SchemaValidator(const SchemaValidator&);
0146     SchemaValidator& operator=(const SchemaValidator&);
0147 
0148     // -----------------------------------------------------------------------
0149     //  Element Consistency Checking methods
0150     // -----------------------------------------------------------------------
0151     void checkRefElementConsistency(SchemaGrammar* const currentGrammar,
0152                                     const ComplexTypeInfo* const curTypeInfo,
0153                                     const XercesGroupInfo* const curGroup = 0);
0154 
0155     // -----------------------------------------------------------------------
0156     //  Particle Derivation Checking methods
0157     // -----------------------------------------------------------------------
0158     void checkParticleDerivation(SchemaGrammar* const currentGrammar,
0159                                  const ComplexTypeInfo* const typeInfo);
0160     void checkParticleDerivationOk(SchemaGrammar* const currentGrammar,
0161                                    ContentSpecNode* const curNode,
0162                                    const int derivedScope,
0163                                    ContentSpecNode* const baseNode,
0164                                    const int baseScope,
0165                                    const ComplexTypeInfo* const baseInfo = 0,
0166                                    const bool toCheckOccurrence = true);
0167     ContentSpecNode* checkForPointlessOccurrences(ContentSpecNode* const specNode,
0168                                                   const ContentSpecNode::NodeTypes nodeType,
0169                                                   ValueVectorOf<ContentSpecNode*>* const nodes);
0170     void gatherChildren(const ContentSpecNode::NodeTypes parentNodeType,
0171                         ContentSpecNode* const specNode,
0172                         ValueVectorOf<ContentSpecNode*>* const nodes);
0173     bool isOccurrenceRangeOK(const int min1, const int max1, const int min2, const int max2);
0174     void checkNSCompat(const ContentSpecNode* const derivedSpecNode,
0175                        const ContentSpecNode* const baseSpecNode,
0176                        const bool toCheckOccurence);
0177     bool wildcardEltAllowsNamespace(const ContentSpecNode* const baseSpecNode,
0178                                     const unsigned int derivedURI);
0179     void checkNameAndTypeOK(SchemaGrammar* const currentGrammar,
0180                             const ContentSpecNode* const derivedSpecNode,
0181                             const int derivedScope,
0182                             const ContentSpecNode* const baseSpecNode,
0183                             const int baseScope,
0184                             const ComplexTypeInfo* const baseInfo = 0);
0185     SchemaElementDecl* findElement(const int scope,
0186                                    const unsigned int uriIndex,
0187                                    const XMLCh* const name,
0188                                    SchemaGrammar* const grammar,
0189                                    const ComplexTypeInfo* const typeInfo = 0);
0190     void checkICRestriction(const SchemaElementDecl* const derivedElemDecl,
0191                             const SchemaElementDecl* const baseElemDecl,
0192                             const XMLCh* const derivedElemName,
0193                             const XMLCh* const baseElemName);
0194     void checkTypesOK(const SchemaElementDecl* const derivedElemDecl,
0195                       const SchemaElementDecl* const baseElemDecl,
0196                       const XMLCh* const derivedElemName);
0197     void checkRecurseAsIfGroup(SchemaGrammar* const currentGrammar,
0198                                ContentSpecNode* const derivedSpecNode,
0199                                const int derivedScope,
0200                                const ContentSpecNode* const baseSpecNode,
0201                                const int baseScope,
0202                                ValueVectorOf<ContentSpecNode*>* const nodes,
0203                                const ComplexTypeInfo* const baseInfo);
0204     void checkRecurse(SchemaGrammar* const currentGrammar,
0205                       const ContentSpecNode* const derivedSpecNode,
0206                       const int derivedScope,
0207                       ValueVectorOf<ContentSpecNode*>* const derivedNodes,
0208                       const ContentSpecNode* const baseSpecNode,
0209                       const int baseScope,
0210                       ValueVectorOf<ContentSpecNode*>* const baseNodes,
0211                       const ComplexTypeInfo* const baseInfo,
0212                       const bool toLax = false);
0213     void checkNSSubset(const ContentSpecNode* const derivedSpecNode,
0214                        const ContentSpecNode* const baseSpecNode);
0215     bool checkNSSubsetChoiceRoot(const ContentSpecNode* const derivedSpecNode,
0216                        const ContentSpecNode* const baseSpecNode);
0217     bool checkNSSubsetChoice(const ContentSpecNode* const derivedSpecNode,
0218                        const ContentSpecNode* const baseSpecNode);
0219     bool isWildCardEltSubset(const ContentSpecNode* const derivedSpecNode,
0220                              const ContentSpecNode* const baseSpecNode);
0221     void checkNSRecurseCheckCardinality(SchemaGrammar* const currentGrammar,
0222                                         const ContentSpecNode* const derivedSpecNode,
0223                                         ValueVectorOf<ContentSpecNode*>* const derivedNodes,
0224                                         const int derivedScope,
0225                                         ContentSpecNode* const baseSpecNode,
0226                                         const bool toCheckOccurence);
0227     void checkRecurseUnordered(SchemaGrammar* const currentGrammar,
0228                                const ContentSpecNode* const derivedSpecNode,
0229                                ValueVectorOf<ContentSpecNode*>* const derivedNodes,
0230                                const int derivedScope,
0231                                ContentSpecNode* const baseSpecNode,
0232                                ValueVectorOf<ContentSpecNode*>* const baseNodes,
0233                                const int baseScope,
0234                                const ComplexTypeInfo* const baseInfo);
0235     void checkMapAndSum(SchemaGrammar* const currentGrammar,
0236                         const ContentSpecNode* const derivedSpecNode,
0237                         ValueVectorOf<ContentSpecNode*>* const derivedNodes,
0238                         const int derivedScope,
0239                         ContentSpecNode* const baseSpecNode,
0240                         ValueVectorOf<ContentSpecNode*>* const baseNodes,
0241                         const int baseScope,
0242                         const ComplexTypeInfo* const baseInfo);
0243     ContentSpecNode* getNonUnaryGroup(ContentSpecNode* const pNode);
0244 
0245     // -----------------------------------------------------------------------
0246     //  Private data members
0247     //
0248     // -----------------------------------------------------------------------
0249     //  The following comes from or set by the Scanner
0250     //  fSchemaGrammar
0251     //      The current schema grammar used by the validator
0252     //
0253     //  fGrammarResolver
0254     //      All the schema grammar stored
0255     //
0256     //  fXsiType
0257     //      Store the Schema Type Attribute Value if schema type is specified
0258     //
0259     //  fNil
0260     //      Indicates if a nil value is acceptable
0261     //  fNilFound
0262     //      Indicates if Nillable has been set
0263     // -----------------------------------------------------------------------
0264     //  The following used internally in the validator
0265     //
0266     //  fCurrentDatatypeValidator
0267     //      The validator used for validating the content of elements
0268     //      with simple types
0269     //
0270     //  fDatatypeBuffer
0271     //      Buffer for simple type element string content
0272     //
0273     //  fTrailing
0274     //      Previous chunk had a trailing space
0275     //
0276     //  fSeenNonWhiteSpace
0277     //      Seen a non-whitespace character in the previous chunk
0278     //
0279     //  fSeenId
0280     //      Indicate if an attribute of ID type has been seen already, reset per element.
0281     //
0282     //  fSchemaErrorReporter
0283     //      Report schema process errors
0284     //
0285     //  fTypeStack
0286     //      Stack of complex type declarations.
0287     //
0288     //  fMostRecentAttrValidator
0289     //      DatatypeValidator that validated attribute most recently processed
0290     //
0291     //  fErrorOccurred
0292     //      whether an error occurred in the most recent operation
0293     // -----------------------------------------------------------------------
0294     MemoryManager*                  fMemoryManager;
0295     SchemaGrammar*                  fSchemaGrammar;
0296     GrammarResolver*                fGrammarResolver;
0297     QName*                          fXsiType;
0298     bool                            fNil;
0299     bool                            fNilFound;
0300     DatatypeValidator*              fCurrentDatatypeValidator;
0301     XMLBuffer*                      fNotationBuf;
0302     XMLBuffer                       fDatatypeBuffer;
0303     bool                            fTrailing;
0304     bool                            fSeenNonWhiteSpace;
0305     bool                            fSeenId;
0306     XSDErrorReporter                fSchemaErrorReporter;
0307     ValueStackOf<ComplexTypeInfo*>* fTypeStack;
0308     DatatypeValidator *             fMostRecentAttrValidator;
0309     bool                            fErrorOccurred;
0310     bool                            fElemIsSpecified;
0311 };
0312 
0313 
0314 // ---------------------------------------------------------------------------
0315 //  SchemaValidator: Setter methods
0316 // ---------------------------------------------------------------------------
0317 inline void SchemaValidator::setGrammarResolver(GrammarResolver* grammarResolver) {
0318     fGrammarResolver = grammarResolver;
0319 }
0320 
0321 inline void SchemaValidator::setXsiType(const XMLCh* const        prefix
0322       , const XMLCh* const        localPart
0323        , const unsigned int        uriId)
0324 {
0325     delete fXsiType;
0326     fXsiType = new (fMemoryManager) QName(prefix, localPart, uriId, fMemoryManager);
0327 }
0328 
0329 inline void SchemaValidator::setNillable(bool isNil) {
0330     fNil = isNil;
0331     fNilFound = true;
0332 }
0333 
0334 inline void SchemaValidator::resetNillable() {
0335     fNil = false;
0336     fNilFound = false;
0337 }
0338 
0339 inline void SchemaValidator::setExitOnFirstFatal(const bool newValue) {
0340 
0341     fSchemaErrorReporter.setExitOnFirstFatal(newValue);
0342 }
0343 
0344 inline void SchemaValidator::setDatatypeBuffer(const XMLCh* const value)
0345 {
0346     fDatatypeBuffer.append(value);
0347 }
0348 
0349 inline void SchemaValidator::clearDatatypeBuffer()
0350 {
0351     fDatatypeBuffer.reset();
0352 }
0353 
0354 // ---------------------------------------------------------------------------
0355 //  SchemaValidator: Getter methods
0356 // ---------------------------------------------------------------------------
0357 inline ComplexTypeInfo* SchemaValidator::getCurrentTypeInfo() const {
0358     if (fTypeStack->empty())
0359         return 0;
0360     return fTypeStack->peek();
0361 }
0362 
0363 inline DatatypeValidator * SchemaValidator::getCurrentDatatypeValidator() const
0364 {
0365     return fCurrentDatatypeValidator;
0366 }
0367 inline DatatypeValidator *SchemaValidator::getMostRecentAttrValidator() const
0368 {
0369     return fMostRecentAttrValidator;
0370 }
0371 
0372 // ---------------------------------------------------------------------------
0373 //  Virtual interface
0374 // ---------------------------------------------------------------------------
0375 inline Grammar* SchemaValidator::getGrammar() const {
0376     return fSchemaGrammar;
0377 }
0378 
0379 inline void SchemaValidator::setGrammar(Grammar* aGrammar) {
0380     fSchemaGrammar = (SchemaGrammar*) aGrammar;
0381 }
0382 
0383 inline void SchemaValidator::setErrorReporter(XMLErrorReporter* const errorReporter) {
0384 
0385     XMLValidator::setErrorReporter(errorReporter);
0386     fSchemaErrorReporter.setErrorReporter(errorReporter);
0387 }
0388 
0389 // ---------------------------------------------------------------------------
0390 //  SchemaValidator: DTD handler interface
0391 // ---------------------------------------------------------------------------
0392 inline bool SchemaValidator::handlesDTD() const
0393 {
0394     // No DTD scanning
0395     return false;
0396 }
0397 
0398 // ---------------------------------------------------------------------------
0399 //  SchemaValidator: Schema handler interface
0400 // ---------------------------------------------------------------------------
0401 inline bool SchemaValidator::handlesSchema() const
0402 {
0403     return true;
0404 }
0405 
0406 // ---------------------------------------------------------------------------
0407 //  SchemaValidator: Particle derivation checking
0408 // ---------------------------------------------------------------------------
0409 inline bool
0410 SchemaValidator::isOccurrenceRangeOK(const int min1, const int max1,
0411                                      const int min2, const int max2) {
0412 
0413     if (min1 >= min2 &&
0414         (max2 == SchemaSymbols::XSD_UNBOUNDED ||
0415          (max1 != SchemaSymbols::XSD_UNBOUNDED && max1 <= max2))) {
0416         return true;
0417     }
0418     return false;
0419 }
0420 
0421 inline bool SchemaValidator::getErrorOccurred() const
0422 {
0423     return fErrorOccurred;
0424 }
0425 
0426 inline bool SchemaValidator::getIsElemSpecified() const
0427 {
0428     return fElemIsSpecified;
0429 }
0430 
0431 inline const XMLCh* SchemaValidator::getNormalizedValue() const
0432 {
0433     return fDatatypeBuffer.getRawBuffer();
0434 }
0435 
0436 inline bool SchemaValidator::getIsXsiTypeSet() const
0437 {
0438     return (fXsiType!=0);
0439 }
0440 
0441 XERCES_CPP_NAMESPACE_END
0442 
0443 #endif