From 38003505e53ef5b8c2590af38cfbb0d405ad245f Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Sun, 2 Jul 2006 22:21:52 +0000 Subject: [PATCH] Added ValidatorSuite around groups of static validators. --- xmltooling/Makefile.am | 5 +- xmltooling/XMLToolingConfig.cpp | 3 +- xmltooling/encryption/Encryption.h | 5 + .../encryption/impl/EncryptionSchemaValidators.cpp | 6 +- xmltooling/signature/KeyInfo.h | 6 ++ .../signature/impl/KeyInfoSchemaValidators.cpp | 6 +- xmltooling/validation/Validator.h | 52 +--------- .../{Validator.cpp => ValidatorSuite.cpp} | 61 +++++------ xmltooling/validation/ValidatorSuite.h | 115 +++++++++++++++++++++ xmltooling/xmltooling.vcproj | 6 +- xmltoolingtest/EncryptionTest.h | 2 +- xmltoolingtest/KeyInfoTest.h | 6 +- 12 files changed, 180 insertions(+), 93 deletions(-) rename xmltooling/validation/{Validator.cpp => ValidatorSuite.cpp} (66%) create mode 100644 xmltooling/validation/ValidatorSuite.h diff --git a/xmltooling/Makefile.am b/xmltooling/Makefile.am index a907fb2..ed8c919 100644 --- a/xmltooling/Makefile.am +++ b/xmltooling/Makefile.am @@ -73,7 +73,8 @@ utilinclude_HEADERS = \ valinclude_HEADERS = \ validation/AbstractValidatingXMLObject.h \ validation/ValidatingXMLObject.h \ - validation/Validator.h + validation/Validator.h \ + validation/ValidatorSuite.h noinst_HEADERS = \ internal.h @@ -120,7 +121,7 @@ libxmltooling_la_SOURCES = \ util/XMLConstants.cpp \ util/XMLHelper.cpp \ validation/AbstractValidatingXMLObject.cpp \ - validation/Validator.cpp \ + validation/ValidatorSuite.cpp \ ${xmlsec_sources} \ $(thread_sources) diff --git a/xmltooling/XMLToolingConfig.cpp b/xmltooling/XMLToolingConfig.cpp index e8e6ad8..b22b664 100644 --- a/xmltooling/XMLToolingConfig.cpp +++ b/xmltooling/XMLToolingConfig.cpp @@ -190,7 +190,8 @@ bool XMLToolingInternalConfig::init() void XMLToolingInternalConfig::term() { XMLObjectBuilder::destroyBuilders(); - Validator::destroyValidators(); + KeyInfoSchemaValidators.destroyValidators(); + EncryptionSchemaValidators.destroyValidators(); XMLToolingException::deregisterFactories(); for (vector::reverse_iterator i=m_libhandles.rbegin(); i!=m_libhandles.rend(); i++) { diff --git a/xmltooling/encryption/Encryption.h b/xmltooling/encryption/Encryption.h index b6d5fc6..e5eda15 100644 --- a/xmltooling/encryption/Encryption.h +++ b/xmltooling/encryption/Encryption.h @@ -149,6 +149,11 @@ namespace xmlencryption { * Registers builders and validators for XML Encryption classes into the runtime. */ void XMLTOOL_API registerEncryptionClasses(); + + /** + * Validator suite for XML Encryption schema validation. + */ + extern XMLTOOL_API xmltooling::ValidatorSuite EncryptionSchemaValidators; }; #endif /* __xmltooling_encryption_h__ */ diff --git a/xmltooling/encryption/impl/EncryptionSchemaValidators.cpp b/xmltooling/encryption/impl/EncryptionSchemaValidators.cpp index 90407b8..1abf065 100644 --- a/xmltooling/encryption/impl/EncryptionSchemaValidators.cpp +++ b/xmltooling/encryption/impl/EncryptionSchemaValidators.cpp @@ -111,12 +111,14 @@ namespace xmlencryption { #define REGISTER_ELEMENT(namespaceURI,cname) \ q=QName(namespaceURI,cname::LOCAL_NAME); \ XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \ - Validator::registerValidator(q,new cname##SchemaValidator()) + EncryptionSchemaValidators.registerValidator(q,new cname##SchemaValidator()) #define REGISTER_TYPE(namespaceURI,cname) \ q=QName(namespaceURI,cname::TYPE_NAME); \ XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \ - Validator::registerValidator(q,new cname##SchemaValidator()) + EncryptionSchemaValidators.registerValidator(q,new cname##SchemaValidator()) + +ValidatorSuite xmlencryption::EncryptionSchemaValidators("EncryptionSchemaValidators"); void xmlencryption::registerEncryptionClasses() { diff --git a/xmltooling/signature/KeyInfo.h b/xmltooling/signature/KeyInfo.h index 0f157e5..8c6bc96 100644 --- a/xmltooling/signature/KeyInfo.h +++ b/xmltooling/signature/KeyInfo.h @@ -29,6 +29,7 @@ #include #include #include +#include #define DECL_XMLSIGOBJECTBUILDER(cname) \ DECL_XMLOBJECTBUILDER(XMLTOOL_API,cname,xmltooling::XMLConstants::XMLSIG_NS,xmltooling::XMLConstants::XMLSIG_PREFIX) @@ -193,6 +194,11 @@ namespace xmlsignature { * Registers builders and validators for KeyInfo classes into the runtime. */ void XMLTOOL_API registerKeyInfoClasses(); + + /** + * Validator suite for KeyInfo schema validation. + */ + extern XMLTOOL_API xmltooling::ValidatorSuite KeyInfoSchemaValidators; }; #endif /* __xmltooling_keyinfo_h__ */ diff --git a/xmltooling/signature/impl/KeyInfoSchemaValidators.cpp b/xmltooling/signature/impl/KeyInfoSchemaValidators.cpp index ec4dbaa..44d6635 100644 --- a/xmltooling/signature/impl/KeyInfoSchemaValidators.cpp +++ b/xmltooling/signature/impl/KeyInfoSchemaValidators.cpp @@ -124,12 +124,14 @@ namespace xmlsignature { #define REGISTER_ELEMENT(namespaceURI,cname) \ q=QName(namespaceURI,cname::LOCAL_NAME); \ XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \ - Validator::registerValidator(q,new cname##SchemaValidator()) + KeyInfoSchemaValidators.registerValidator(q,new cname##SchemaValidator()) #define REGISTER_TYPE(namespaceURI,cname) \ q=QName(namespaceURI,cname::TYPE_NAME); \ XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \ - Validator::registerValidator(q,new cname##SchemaValidator()) + KeyInfoSchemaValidators.registerValidator(q,new cname##SchemaValidator()) + +ValidatorSuite xmlsignature::KeyInfoSchemaValidators("KeyInfoSchemaValidators"); void xmlsignature::registerKeyInfoClasses() { diff --git a/xmltooling/validation/Validator.h b/xmltooling/validation/Validator.h index 6962bb8..7c8f6d7 100644 --- a/xmltooling/validation/Validator.h +++ b/xmltooling/validation/Validator.h @@ -20,19 +20,11 @@ * Rules checking of XMLObjects */ -#if !defined(__xmltooling_validator_h__) +#ifndef __xmltooling_validator_h__ #define __xmltooling_validator_h__ -#include -#include -#include #include -#if defined (_MSC_VER) - #pragma warning( push ) - #pragma warning( disable : 4250 4251 ) -#endif - namespace xmltooling { /** @@ -61,52 +53,10 @@ namespace xmltooling { */ virtual Validator* clone() const=0; - /** - * Evaluates the registered validators against the given XMLObject and it's children. - * - * @param xmlObject the XMLObject tree to validate - * - * @throws ValidationException thrown if the element tree is not valid - */ - static void checkValidity(const XMLObject* xmlObject); - - /** - * Registers a new validator for the given key. - * - * @param key the key used to retrieve the validator - * @param validator the validator - */ - static void registerValidator(const QName& key, Validator* validator) { - std::map< QName, std::vector >::iterator i=m_map.find(key); - if (i==m_map.end()) - m_map.insert(std::make_pair(key,std::vector(1,validator))); - else - i->second.push_back(validator); - } - - /** - * Deregisters validators. - * - * @param key the key for the validators to be deregistered - */ - static void deregisterValidators(const QName& key); - - /** - * Unregisters and destroys all registered validators. - */ - static void destroyValidators(); - protected: Validator() {} - - private: - static std::map< QName, std::vector > m_map; }; }; -#if defined (_MSC_VER) - #pragma warning( pop ) -#endif - #endif /* __xmltooling_validator_h__ */ diff --git a/xmltooling/validation/Validator.cpp b/xmltooling/validation/ValidatorSuite.cpp similarity index 66% rename from xmltooling/validation/Validator.cpp rename to xmltooling/validation/ValidatorSuite.cpp index a39a332..1ff5a29 100644 --- a/xmltooling/validation/Validator.cpp +++ b/xmltooling/validation/ValidatorSuite.cpp @@ -15,26 +15,49 @@ */ /** - * Validator.cpp + * ValidatorSuite.cpp * - * Rules checking of XMLObjects + * Groups of rule checkers of XMLObjects based on type or element name. */ #include "internal.h" -#include "validation/Validator.h" +#include "validation/ValidatorSuite.h" #include "util/XMLHelper.h" using namespace xmltooling; using namespace std; -map< QName, vector > Validator::m_map; +namespace { + class XMLTOOL_DLLLOCAL _clearvector { + public: + void operator()(const pair< QName, vector >& p) const { + for_each(p.second.begin(),p.second.end(),xmltooling::cleanup()); + } + }; +} + +void ValidatorSuite::deregisterValidators(const QName& key) +{ + map< QName, vector >::iterator i=m_map.find(key); + if (i!=m_map.end()) { + _clearvector f; + f(*i); + m_map.erase(i); + } +} + +void ValidatorSuite::destroyValidators() +{ + for_each(m_map.begin(),m_map.end(),_clearvector()); + m_map.clear(); +} -void Validator::checkValidity(const XMLObject* xmlObject) +void ValidatorSuite::validate(const XMLObject* xmlObject) const { if (!xmlObject) return; - map< QName, vector >::iterator i; + map< QName, vector >::const_iterator i; if (xmlObject->getSchemaType()) { i=m_map.find(*(xmlObject->getSchemaType())); if (i!=m_map.end()) @@ -45,28 +68,6 @@ void Validator::checkValidity(const XMLObject* xmlObject) for_each(i->second.begin(),i->second.end(),bind2nd(mem_fun(&Validator::validate),xmlObject)); const list& kids=xmlObject->getOrderedChildren(); - for_each(kids.begin(),kids.end(),Validator::checkValidity); -} - -class _clearvector { -public: - void operator()(const pair< QName, vector >& p) const { - for_each(p.second.begin(),p.second.end(),xmltooling::cleanup()); - } -}; - -void Validator::deregisterValidators(const QName& key) -{ - map< QName, vector >::iterator i=m_map.find(key); - if (i!=m_map.end()) { - _clearvector f; - f(*i); - m_map.erase(i); - } -} - -void Validator::destroyValidators() -{ - for_each(m_map.begin(),m_map.end(),_clearvector()); - m_map.clear(); + for (list::const_iterator j=kids.begin(); j!=kids.end(); j++) + validate(*j); } diff --git a/xmltooling/validation/ValidatorSuite.h b/xmltooling/validation/ValidatorSuite.h new file mode 100644 index 0000000..6049966 --- /dev/null +++ b/xmltooling/validation/ValidatorSuite.h @@ -0,0 +1,115 @@ +/* + * Copyright 2001-2006 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file ValidatorSuite.h + * + * Groups of rule checkers of XMLObjects based on type or element name. + */ + +#ifndef __xmltooling_valsuite_h__ +#define __xmltooling_valsuite_h__ + +#include +#include +#include +#include + +#if defined (_MSC_VER) + #pragma warning( push ) + #pragma warning( disable : 4250 4251 ) +#endif + +namespace xmltooling { + + /** + * A collection of validators that can be applied to an XMLObject and its children. These collections can represent + * usage specific checks, such as those outlined in schemas or profiles of specific XML specifications. + * + * Registered Validators must be stateless and cloneable. Validators are fetched based on schema type and + * element name, in that order. + */ + class XMLTOOL_API ValidatorSuite + { + MAKE_NONCOPYABLE(ValidatorSuite); + public: + /** + * Creates a new suite. + * + * @param id an identifier for the suite + */ + ValidatorSuite(const char* id) : m_id(id) {} + + ~ValidatorSuite() { + destroyValidators(); + } + + /** + * Gets a unique ID for this suite. + * + * @return a unique ID for this suite + */ + const char* getId() { + return m_id.c_str(); + } + + /** + * Evaluates the registered validators against the given XMLObject and it's children. + * + * @param xmlObject the XMLObject tree to validate + * + * @throws ValidationException thrown if the element tree is not valid + */ + void validate(const XMLObject* xmlObject) const; + + /** + * Registers a new validator for the given key. + * + * @param key the key used to retrieve the validator + * @param validator the validator + */ + void registerValidator(const QName& key, Validator* validator) { + std::map< QName, std::vector >::iterator i=m_map.find(key); + if (i==m_map.end()) + m_map.insert(std::make_pair(key,std::vector(1,validator))); + else + i->second.push_back(validator); + } + + /** + * Deregisters validators. + * + * @param key the key for the validators to be deregistered + */ + void deregisterValidators(const QName& key); + + /** + * Unregisters and destroys all registered validators. + */ + void destroyValidators(); + + private: + std::string m_id; + std::map< QName, std::vector > m_map; + }; + +}; + +#if defined (_MSC_VER) + #pragma warning( pop ) +#endif + +#endif /* __xmltooling_valsuite_h__ */ diff --git a/xmltooling/xmltooling.vcproj b/xmltooling/xmltooling.vcproj index 34b78c8..0199f6b 100644 --- a/xmltooling/xmltooling.vcproj +++ b/xmltooling/xmltooling.vcproj @@ -285,7 +285,7 @@ > @@ -506,6 +506,10 @@ RelativePath=".\validation\Validator.h" > + + clone())); DOMDocumentFragment* frag = decrypter.decryptData(encData2.get()); XMLHelper::serialize(static_cast(frag->getFirstChild()), buf); - TS_TRACE(buf.c_str()); + //TS_TRACE(buf.c_str()); TS_ASSERT(doc->getDocumentElement()->isEqualNode(frag->getFirstChild())); frag->release(); doc->release(); diff --git a/xmltoolingtest/KeyInfoTest.h b/xmltoolingtest/KeyInfoTest.h index 2d2ec97..8672906 100644 --- a/xmltoolingtest/KeyInfoTest.h +++ b/xmltoolingtest/KeyInfoTest.h @@ -59,7 +59,7 @@ public: TSM_ASSERT_SAME_DATA("KeyName was not expected value", expected.get(), kiObject->getKeyNames().front()->getName(), XMLString::stringLen(expected.get())); - Validator::checkValidity(kiObject.get()); + KeyInfoSchemaValidators.validate(kiObject.get()); } void testKeyInfo2() { @@ -84,7 +84,7 @@ public: TSM_ASSERT_EQUALS("Number of child elements was not expected value", 2, kiObject->getSPKIDatas().front()->getSPKISexps().size()); - Validator::checkValidity(kiObject.get()); + KeyInfoSchemaValidators.validate(kiObject.get()); } void testKeyInfo3() { @@ -102,6 +102,6 @@ public: dynamic_cast(b->buildFromDocument(doc)) ); TS_ASSERT(kiObject.get()!=NULL); - TS_ASSERT_THROWS(Validator::checkValidity(kiObject.get()),ValidationException); + TS_ASSERT_THROWS(KeyInfoSchemaValidators.validate(kiObject.get()),ValidationException); } }; -- 2.1.4