From 2db9cdcc7164114b818749651d221c83895642f2 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Fri, 24 Mar 2006 20:26:17 +0000 Subject: [PATCH] Add per-object validation. --- configure.ac | 10 +-- xmltooling/AbstractAttributeExtensibleXMLObject.h | 11 +-- xmltooling/AbstractDOMCachingXMLObject.h | 14 +--- xmltooling/AbstractElementProxy.h | 11 +-- xmltooling/Makefile.am | 3 + xmltooling/impl/AnyElement.cpp | 4 +- xmltooling/impl/UnknownElement.h | 2 +- .../validation/AbstractValidatingXMLObject.cpp | 83 ++++++++++++++++++++++ .../validation/AbstractValidatingXMLObject.h | 77 ++++++++++++++++++++ xmltooling/validation/ValidatingXMLObject.h | 80 +++++++++++++++++++++ xmltooling/validation/Validator.h | 2 - xmltooling/xmltooling.vcproj | 12 ++++ xmltoolingtest/XMLObjectBaseTestCase.h | 2 +- 13 files changed, 268 insertions(+), 43 deletions(-) create mode 100644 xmltooling/validation/AbstractValidatingXMLObject.cpp create mode 100644 xmltooling/validation/AbstractValidatingXMLObject.h create mode 100644 xmltooling/validation/ValidatingXMLObject.h diff --git a/configure.ac b/configure.ac index 0ee3202..7a2676a 100644 --- a/configure.ac +++ b/configure.ac @@ -11,11 +11,11 @@ AC_ARG_ENABLE(debug, enable_debug=$enableval, enable_debug=no) if test "$enable_debug" = "yes" ; then - GCC_CFLAGS="$CFLAGS -g -D_DEBUG" - GCC_CXXFLAGS="$CXXFLAGS -g -D_DEBUG" + GCC_CFLAGS="$CFLAGS -Wall -g -D_DEBUG" + GCC_CXXFLAGS="$CXXFLAGS -Wall -g -D_DEBUG" else - GCC_CFLAGS="$CFLAGS -O2 -DNDEBUG" - GCC_CXXFLAGS="$CXXFLAGS -O2 -DNDEBUG" + GCC_CFLAGS="$CFLAGS -Wall -O2 -DNDEBUG" + GCC_CXXFLAGS="$CXXFLAGS -Wall -O2 -DNDEBUG" fi AC_PROG_CC([gcc gcc3 cc]) @@ -215,5 +215,7 @@ AC_SUBST(CXXTEST) AC_SUBST(CXXTESTFLAGS) AM_CONDITIONAL(BUILD_UNITTEST,test -f ${CXXTEST}) +LIBTOOL="$LIBTOOL --silent" + # output makefiles AC_OUTPUT(Makefile xmltooling/Makefile xmltoolingtest/Makefile schemas/Makefile) diff --git a/xmltooling/AbstractAttributeExtensibleXMLObject.h b/xmltooling/AbstractAttributeExtensibleXMLObject.h index 458204c..e474ff8 100644 --- a/xmltooling/AbstractAttributeExtensibleXMLObject.h +++ b/xmltooling/AbstractAttributeExtensibleXMLObject.h @@ -56,16 +56,7 @@ namespace xmltooling { virtual void setAttribute(QName& qualifiedName, const XMLCh* value); protected: - /** - * Constructor - * - * @param namespaceURI the namespace the element is in - * @param elementLocalName the local name of the XML element this Object represents - * @param namespacePrefix the namespace prefix to use - */ - AbstractAttributeExtensibleXMLObject( - const XMLCh* namespaceURI=NULL, const XMLCh* elementLocalName=NULL, const XMLCh* namespacePrefix=NULL - ) : AbstractDOMCachingXMLObject(namespaceURI,elementLocalName, namespacePrefix) {} + AbstractAttributeExtensibleXMLObject() {} std::map m_attributeMap; }; diff --git a/xmltooling/AbstractDOMCachingXMLObject.h b/xmltooling/AbstractDOMCachingXMLObject.h index 70f7890..edfc1bf 100644 --- a/xmltooling/AbstractDOMCachingXMLObject.h +++ b/xmltooling/AbstractDOMCachingXMLObject.h @@ -35,9 +35,8 @@ namespace xmltooling { /** * Extension of AbstractXMLObject that implements a DOMCachingXMLObject. - * This is the primary base class for XMLObject implementation classes to use. */ - class XMLTOOL_API AbstractDOMCachingXMLObject : public AbstractXMLObject, public virtual DOMCachingXMLObject + class XMLTOOL_API AbstractDOMCachingXMLObject : public virtual AbstractXMLObject, public virtual DOMCachingXMLObject { public: virtual ~AbstractDOMCachingXMLObject(); @@ -104,16 +103,7 @@ namespace xmltooling { XMLObject* clone() const; protected: - /** - * Constructor - * - * @param namespaceURI the namespace the element is in - * @param elementLocalName the local name of the XML element this Object represents - * @param namespacePrefix the namespace prefix to use - */ - AbstractDOMCachingXMLObject( - const XMLCh* namespaceURI=NULL, const XMLCh* elementLocalName=NULL, const XMLCh* namespacePrefix=NULL - ) : AbstractXMLObject(namespaceURI,elementLocalName, namespacePrefix), m_dom(NULL), m_document(NULL) {} + AbstractDOMCachingXMLObject() : m_dom(NULL), m_document(NULL) {} /** * If a DOM representation exists, this clones it into a new document. diff --git a/xmltooling/AbstractElementProxy.h b/xmltooling/AbstractElementProxy.h index 14ea000..a00b9df 100644 --- a/xmltooling/AbstractElementProxy.h +++ b/xmltooling/AbstractElementProxy.h @@ -60,16 +60,7 @@ namespace xmltooling { virtual ListOf(XMLObject) getXMLObjects(); protected: - /** - * Constructor - * - * @param namespaceURI the namespace the element is in - * @param elementLocalName the local name of the XML element this Object represents - * @param namespacePrefix the namespace prefix to use - */ - AbstractElementProxy( - const XMLCh* namespaceURI=NULL, const XMLCh* elementLocalName=NULL, const XMLCh* namespacePrefix=NULL - ) : AbstractDOMCachingXMLObject(namespaceURI,elementLocalName, namespacePrefix), m_value(NULL) {} + AbstractElementProxy() : m_value(NULL) {} private: XMLCh* m_value; diff --git a/xmltooling/Makefile.am b/xmltooling/Makefile.am index bd9a7fd..ab0661b 100644 --- a/xmltooling/Makefile.am +++ b/xmltooling/Makefile.am @@ -16,6 +16,7 @@ libxmltoolinginclude_HEADERS = \ AbstractAttributeExtensibleXMLObject.h \ AbstractDOMCachingXMLObject.h \ AbstractElementProxy.h \ + AbstractValidatingXMLObject.h \ AbstractXMLObject.h \ AttributeExtensibleXMLObject.h \ base.h \ @@ -27,6 +28,7 @@ libxmltoolinginclude_HEADERS = \ Namespace.h \ QName.h \ unicode.h \ + ValidatingXMLObject.h \ version.h \ XMLObject.h \ XMLObjectBuilder.h \ @@ -83,6 +85,7 @@ libxmltooling_la_SOURCES = \ util/ParserPool.cpp \ util/XMLConstants.cpp \ util/XMLHelper.cpp \ + validation/AbstractValidatingXMLObject.cpp \ validation/Validator.cpp \ ${xmlsec_sources} diff --git a/xmltooling/impl/AnyElement.cpp b/xmltooling/impl/AnyElement.cpp index fea0081..132e49a 100644 --- a/xmltooling/impl/AnyElement.cpp +++ b/xmltooling/impl/AnyElement.cpp @@ -52,9 +52,7 @@ namespace xmltooling { { public: AnyElementImpl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix) - : AbstractDOMCachingXMLObject(nsURI, localName, prefix), - AbstractElementProxy(nsURI, localName, prefix), - AbstractAttributeExtensibleXMLObject(nsURI, localName, prefix) {} + : AbstractXMLObject(nsURI, localName, prefix) {} virtual ~AnyElementImpl() {} AnyElementImpl* clone() const { diff --git a/xmltooling/impl/UnknownElement.h b/xmltooling/impl/UnknownElement.h index d13f22c..3b4862d 100644 --- a/xmltooling/impl/UnknownElement.h +++ b/xmltooling/impl/UnknownElement.h @@ -43,7 +43,7 @@ namespace xmltooling { { public: UnknownElementImpl(const XMLCh* namespaceURI=NULL, const XMLCh* elementLocalName=NULL, const XMLCh* namespacePrefix=NULL) - : AbstractDOMCachingXMLObject(namespaceURI, elementLocalName, namespacePrefix) {} + : AbstractXMLObject(namespaceURI, elementLocalName, namespacePrefix) {} void releaseDOM(); diff --git a/xmltooling/validation/AbstractValidatingXMLObject.cpp b/xmltooling/validation/AbstractValidatingXMLObject.cpp new file mode 100644 index 0000000..78f5cbb --- /dev/null +++ b/xmltooling/validation/AbstractValidatingXMLObject.cpp @@ -0,0 +1,83 @@ +/* + * 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. + */ + +/** + * AbstractValidatingXMLObject.cpp + * + * Extension of AbstractXMLObject that implements a ValidatingXMLObject. + */ + +#include "internal.h" +#include "exceptions.h" +#include "validation/AbstractValidatingXMLObject.h" + +#include +#include + +using namespace xmltooling; +using namespace std; + +AbstractValidatingXMLObject::ValidatorWrapper::~ValidatorWrapper() +{ + for_each(v.begin(),v.end(),cleanup()); +} + +AbstractValidatingXMLObject::~AbstractValidatingXMLObject() +{ + delete m_validators; +} + +void AbstractValidatingXMLObject::registerValidator(Validator* validator) const +{ + if (!m_validators) + m_validators=new ValidatorWrapper(); + m_validators->v.push_back(validator); +} + +void AbstractValidatingXMLObject::deregisterValidator(Validator* validator) const +{ + if (m_validators) { + for (std::vector::iterator i=m_validators->v.begin(); i!=m_validators->v.end(); i++) { + if ((*i)==validator) + m_validators->v.erase(i); + return; + } + } +} + +class _validate : public binary_function { +public: + void operator()(const XMLObject* obj, bool propagate) const { + const ValidatingXMLObject* val = dynamic_cast(obj); + if (val) { + val->validate(propagate); + } + } +}; + +void AbstractValidatingXMLObject::validate(bool validateDescendants) const +{ + if (m_validators) { + for_each( + m_validators->v.begin(),m_validators->v.end(), + bind2nd(mem_fun(&Validator::validate),this) + ); + } + + if (validateDescendants && hasChildren()) { + for_each(m_children.begin(),m_children.end(),bind2nd(_validate(),validateDescendants)); + } +} diff --git a/xmltooling/validation/AbstractValidatingXMLObject.h b/xmltooling/validation/AbstractValidatingXMLObject.h new file mode 100644 index 0000000..2e8d27b --- /dev/null +++ b/xmltooling/validation/AbstractValidatingXMLObject.h @@ -0,0 +1,77 @@ +/* + * 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 AbstractValidatingXMLObject.h + * + * Extension of AbstractXMLObject that implements a ValidatingXMLObject. + */ + +#if !defined(__xmltooling_abstractvalxmlobj_h__) +#define __xmltooling_abstractvalxmlobj_h__ + +#include +#include + +#if defined (_MSC_VER) + #pragma warning( push ) + #pragma warning( disable : 4250 4251 ) +#endif + +namespace xmltooling { + + /** + * Extension of AbstractXMLObject that implements a ValidatingXMLObject. + */ + class XMLTOOL_API AbstractValidatingXMLObject : public virtual AbstractXMLObject, public virtual ValidatingXMLObject + { + public: + virtual ~AbstractValidatingXMLObject(); + + /** + * @see ValidatingXMLObject::registerValidator() + */ + void registerValidator(Validator* validator) const; + + /** + * @see ValidatingXMLObject::deregisterValidator() + */ + void deregisterValidator(Validator* validator) const; + + /** + * @see ValidatingXMLObject::validate() + */ + void validate(bool validateDescendants) const; + + protected: + AbstractValidatingXMLObject() : m_validators(NULL) {} + + private: + struct XMLTOOL_DLLLOCAL ValidatorWrapper { + ~ValidatorWrapper(); + std::vector v; + }; + + mutable ValidatorWrapper* m_validators; + }; + +}; + +#if defined (_MSC_VER) + #pragma warning( pop ) +#endif + +#endif /* __xmltooling_abstractvalxmlobj_h__ */ diff --git a/xmltooling/validation/ValidatingXMLObject.h b/xmltooling/validation/ValidatingXMLObject.h new file mode 100644 index 0000000..07d4a0e --- /dev/null +++ b/xmltooling/validation/ValidatingXMLObject.h @@ -0,0 +1,80 @@ +/* + * 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 ValidatingXMLObject.h + * + * An XMLObject that can evaluate per-object validation rules. + */ + +#if !defined(__xmltooling_valxmlobj_h__) +#define __xmltooling_valxmlobj_h__ + +#include +#include +#include + +#if defined (_MSC_VER) + #pragma warning( push ) + #pragma warning( disable : 4250 4251 ) +#endif + +namespace xmltooling { + + /** + * A functional interface for XMLObjects that offer the ability + * to evaluate validation rules defined per-object. + */ + class XMLTOOL_API ValidatingXMLObject : public virtual XMLObject + { + protected: + ValidatingXMLObject() {} + + public: + virtual ~ValidatingXMLObject() {} + + /** + * Registers a validator for this XMLObject. + * + * @param validator the validator + */ + virtual void registerValidator(Validator* validator) const=0; + + /** + * Deregisters a validator for this XMLObject. + * + * @param validator the validator + */ + virtual void deregisterValidator(Validator* validator) const=0; + + /** + * Validates this XMLObject against all registered validators. + * + * @param validateDescendants true if all the descendants of this object should + * be validated as well, false if not + * + * @throws ValidationException thrown if the object is not valid + */ + virtual void validate(bool validateDescendants) const=0; + }; + +}; + +#if defined (_MSC_VER) + #pragma warning( pop ) +#endif + +#endif /* __xmltooling_valxmlobj_h__ */ diff --git a/xmltooling/validation/Validator.h b/xmltooling/validation/Validator.h index 7119883..0734030 100644 --- a/xmltooling/validation/Validator.h +++ b/xmltooling/validation/Validator.h @@ -28,8 +28,6 @@ #include #include -using namespace xercesc; - #if defined (_MSC_VER) #pragma warning( push ) #pragma warning( disable : 4250 4251 ) diff --git a/xmltooling/xmltooling.vcproj b/xmltooling/xmltooling.vcproj index 5fc189b..5c94f40 100644 --- a/xmltooling/xmltooling.vcproj +++ b/xmltooling/xmltooling.vcproj @@ -265,6 +265,10 @@ Name="validation" > + + @@ -415,6 +419,14 @@ Name="validation" > + + + + diff --git a/xmltoolingtest/XMLObjectBaseTestCase.h b/xmltoolingtest/XMLObjectBaseTestCase.h index e9cface..f59c1a3 100644 --- a/xmltoolingtest/XMLObjectBaseTestCase.h +++ b/xmltoolingtest/XMLObjectBaseTestCase.h @@ -55,7 +55,7 @@ public: SimpleXMLObject( const XMLCh* namespaceURI=NULL, const XMLCh* elementLocalName=NULL, const XMLCh* namespacePrefix=NULL - ) : AbstractDOMCachingXMLObject(namespaceURI, elementLocalName, namespacePrefix), m_id(NULL), m_value(NULL) { + ) : AbstractXMLObject(namespaceURI, elementLocalName, namespacePrefix), m_id(NULL), m_value(NULL) { #ifndef XMLTOOLING_NO_XMLSEC m_children.push_back(NULL); m_signature=m_children.begin(); -- 2.1.4