Validator interface.
authorcantor <cantor@de75baf8-a10c-0410-a50a-987c0e22f00f>
Wed, 8 Mar 2006 05:36:00 +0000 (05:36 +0000)
committercantor <cantor@de75baf8-a10c-0410-a50a-987c0e22f00f>
Wed, 8 Mar 2006 05:36:00 +0000 (05:36 +0000)
git-svn-id: https://svn.middleware.georgetown.edu/cpp-xmltooling/trunk@49 de75baf8-a10c-0410-a50a-987c0e22f00f

.cdtproject
xmltooling/Makefile.am
xmltooling/exceptions.h
xmltooling/validation/Validator.cpp [new file with mode: 0644]
xmltooling/validation/Validator.h [new file with mode: 0644]
xmltooling/xmltooling.vcproj

index ddee21c..3db5ea8 100644 (file)
 <pathentry kind="mac" name="WIN32" path="" value=""/>\r
 <pathentry kind="out" path=""/>\r
 <pathentry kind="con" path="org.eclipse.cdt.make.core.DISCOVERED_SCANNER_INFO"/>\r
-<pathentry excluding="util/|io/|impl/" kind="src" path="xmltooling"/>\r
+<pathentry excluding="util/|io/|impl/|validation/" kind="src" path="xmltooling"/>\r
 <pathentry kind="src" path="xmltooling/util"/>\r
 <pathentry kind="src" path="xmltooling/io"/>\r
 <pathentry kind="src" path="xmltooling/impl"/>\r
 <pathentry kind="src" path="xmltoolingtest"/>\r
+<pathentry kind="src" path="xmltooling/validation"/>\r
 </item>\r
 </data>\r
 </cdtproject>\r
index a179d82..4749b15 100644 (file)
@@ -2,8 +2,13 @@ AUTOMAKE_OPTIONS = foreign
 
 lib_LTLIBRARIES = libxmltooling.la
 
-libxmltoolingincludedir = \
-    $(includedir)/xmltooling
+libxmltoolingincludedir = $(includedir)/xmltooling
+
+ioincludedir = $(includedir)/xmltooling/io
+
+utilincludedir = $(includedir)/xmltooling/util
+
+valincludedir = $(includedir)/xmltooling/validation
 
 libxmltoolinginclude_HEADERS = \
     AbstractAttributeExtensibleXMLObject.h \
@@ -25,8 +30,10 @@ libxmltoolinginclude_HEADERS = \
     XMLObjectBuilder.h \
     XMLToolingConfig.h
 
-utilincludedir = \
-    $(includedir)/xmltooling/util
+ioinclude_HEADERS = \
+    io/AbstractXMLObjectUnmarshaller.h \
+    io/Marshaller.h \
+    io/Unmarshaller.h
 
 utilinclude_HEADERS = \
     util/NDC.h \
@@ -35,13 +42,8 @@ utilinclude_HEADERS = \
     util/XMLHelper.h \
     util/XMLObjectChildrenList.h
 
-ioincludedir = \
-    $(includedir)/xmltooling/io
-
-ioinclude_HEADERS = \
-    io/AbstractXMLObjectUnmarshaller.h \
-    io/Marshaller.h \
-    io/Unmarshaller.h
+valinclude_HEADERS = \
+    validation/Validator.h
 
 noinst_HEADERS = \
     internal.h \
@@ -64,7 +66,8 @@ libxmltooling_la_SOURCES = \
     util/NDC.cpp \
     util/ParserPool.cpp \
     util/XMLConstants.cpp \
-    util/XMLHelper.cpp
+    util/XMLHelper.cpp \
+    validation/Validator.cpp
 
 # this is different from the project version
 # http://sources.redhat.com/autobook/autobook/autobook_91.html
index 2be4cf8..84f47be 100644 (file)
@@ -59,6 +59,7 @@ namespace xmltooling {
     DECL_XMLTOOLING_EXCEPTION(UnmarshallingException);\r
     DECL_XMLTOOLING_EXCEPTION(UnknownElementException);\r
     DECL_XMLTOOLING_EXCEPTION(UnknownAttributeException);\r
+    DECL_XMLTOOLING_EXCEPTION(ValidationException);\r
 \r
 };\r
 \r
diff --git a/xmltooling/validation/Validator.cpp b/xmltooling/validation/Validator.cpp
new file mode 100644 (file)
index 0000000..c398c62
--- /dev/null
@@ -0,0 +1,69 @@
+/*\r
+ *  Copyright 2001-2006 Internet2\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/**\r
+ * Validator.cpp\r
+ * \r
+ * Rules checking of XMLObjects \r
+ */\r
+\r
+#include "internal.h"\r
+#include "validation/Validator.h"\r
+#include "util/XMLHelper.h"\r
+\r
+using namespace xmltooling;\r
+using namespace std;\r
+\r
+map< QName, vector<Validator*> > Validator::m_map;\r
+\r
+void Validator::checkValidity(const XMLObject* xmlObject)\r
+{\r
+    map< QName, vector<Validator*> >::iterator i;\r
+    if (xmlObject->getSchemaType()) {\r
+        i=m_map.find(*(xmlObject->getSchemaType()));\r
+        if (i!=m_map.end())\r
+            for_each(i->second.begin(),i->second.end(),bind2nd(mem_fun<void,Validator,const XMLObject*>(&Validator::validate),xmlObject));\r
+    }\r
+    i=m_map.find(xmlObject->getElementQName());\r
+    if (i!=m_map.end())\r
+        for_each(i->second.begin(),i->second.end(),bind2nd(mem_fun<void,Validator,const XMLObject*>(&Validator::validate),xmlObject));\r
+\r
+    const list<XMLObject*>& kids=xmlObject->getOrderedChildren();\r
+    for_each(kids.begin(),kids.end(),Validator::checkValidity);\r
+}\r
+\r
+class _clearvector {\r
+public:\r
+    void operator()(const pair< QName, vector<Validator*> >& p) const {\r
+        for_each(p.second.begin(),p.second.end(),xmltooling::cleanup<Validator>());\r
+    }\r
+};\r
+\r
+void Validator::deregisterValidators(const QName& key)\r
+{\r
+    map< QName, vector<Validator*> >::iterator i=m_map.find(key);\r
+    if (i!=m_map.end()) {\r
+        _clearvector f;\r
+        f(*i);\r
+        m_map.erase(i);\r
+    }\r
+}\r
+\r
+void Validator::destroyValidators()\r
+{\r
+    for_each(m_map.begin(),m_map.end(),_clearvector());\r
+    m_map.clear();\r
+}\r
diff --git a/xmltooling/validation/Validator.h b/xmltooling/validation/Validator.h
new file mode 100644 (file)
index 0000000..7119883
--- /dev/null
@@ -0,0 +1,107 @@
+/*\r
+ *  Copyright 2001-2006 Internet2\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/**\r
+ * @file Validator.h\r
+ * \r
+ * Rules checking of XMLObjects \r
+ */\r
+\r
+#if !defined(__xmltooling_validator_h__)\r
+#define __xmltooling_validator_h__\r
+\r
+#include <map>\r
+#include <vector>\r
+#include <xmltooling/QName.h>\r
+#include <xmltooling/XMLObject.h>\r
+\r
+using namespace xercesc;\r
+\r
+#if defined (_MSC_VER)\r
+    #pragma warning( push )\r
+    #pragma warning( disable : 4250 4251 )\r
+#endif\r
+\r
+namespace xmltooling {\r
+\r
+    /**\r
+     * An interface for classes that implement rules for checking the \r
+     * validity of XMLObjects.\r
+     */\r
+    class XMLTOOL_API Validator\r
+    {\r
+    MAKE_NONCOPYABLE(Validator);\r
+    public:\r
+        virtual ~Validator() {}\r
+        \r
+        /**\r
+         * Checks to see if an XMLObject is valid.\r
+         * \r
+         * @param xmlObject the XMLObject to validate\r
+\r
+         * @throws ValidationException thrown if the element is not valid\r
+         */\r
+        virtual void validate(const XMLObject* xmlObject) const=0;\r
+\r
+        /**\r
+         * Evaluates the registered validators against the given XMLObject and it's children.\r
+         * \r
+         * @param xmlObject the XMLObject tree to validate\r
+         * \r
+         * @throws ValidationException thrown if the element tree is not valid\r
+         */\r
+        static void checkValidity(const XMLObject* xmlObject);\r
+\r
+        /**\r
+         * Registers a new validator for the given key.\r
+         * \r
+         * @param key       the key used to retrieve the validator\r
+         * @param validator the validator\r
+         */\r
+        static void registerValidator(const QName& key, Validator* validator) {\r
+            std::map< QName, std::vector<Validator*> >::iterator i=m_map.find(key);\r
+            if (i==m_map.end())\r
+                m_map.insert(std::make_pair(key,std::vector<Validator*>(1,validator)));\r
+            else\r
+                i->second.push_back(validator);\r
+        }\r
+\r
+        /**\r
+         * Deregisters validators.\r
+         * \r
+         * @param key       the key for the validators to be deregistered\r
+         */\r
+        static void deregisterValidators(const QName& key);\r
+\r
+        /**\r
+         * Unregisters and destroys all registered validators. \r
+         */\r
+        static void destroyValidators();\r
+\r
+    protected:\r
+        Validator() {}\r
+    \r
+    private:\r
+        static std::map< QName, std::vector<Validator*> > m_map;\r
+    };\r
+\r
+};\r
+\r
+#if defined (_MSC_VER)\r
+    #pragma warning( pop )\r
+#endif\r
+\r
+#endif /* __xmltooling_validator_h__ */\r
index d08bede..705d8f0 100644 (file)
                                        >\r
                                </File>\r
                        </Filter>\r
+                       <Filter\r
+                               Name="validation"\r
+                               >\r
+                               <File\r
+                                       RelativePath=".\validation\Validator.cpp"\r
+                                       >\r
+                               </File>\r
+                       </Filter>\r
                </Filter>\r
                <Filter\r
                        Name="Header Files"\r
                                        >\r
                                </File>\r
                        </Filter>\r
+                       <Filter\r
+                               Name="validation"\r
+                               >\r
+                               <File\r
+                                       RelativePath=".\validation\Validator.h"\r
+                                       >\r
+                               </File>\r
+                       </Filter>\r
                </Filter>\r
                <Filter\r
                        Name="Resource Files"\r