Early marshalling code.
authorScott Cantor <cantor.2@osu.edu>
Mon, 20 Feb 2006 08:16:56 +0000 (08:16 +0000)
committerScott Cantor <cantor.2@osu.edu>
Mon, 20 Feb 2006 08:16:56 +0000 (08:16 +0000)
xmltooling/io/AbstractXMLObjectUnmarshaller.h [new file with mode: 0644]
xmltooling/io/Marshaller.h [new file with mode: 0644]
xmltooling/io/Unmarshaller.h [new file with mode: 0644]

diff --git a/xmltooling/io/AbstractXMLObjectUnmarshaller.h b/xmltooling/io/AbstractXMLObjectUnmarshaller.h
new file mode 100644 (file)
index 0000000..0bc665a
--- /dev/null
@@ -0,0 +1,161 @@
+/*\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 AbstractXMLObjectUnmarshaller.h\r
+ * \r
+ * A thread-safe abstract unmarshaller.\r
+ */\r
+\r
+#if !defined(__xmltooling_xmlunmarshaller_h__)\r
+#define __xmltooling_xmlunmarshaller_h__\r
+\r
+#include <xmltooling/DOMCachingXMLObject.h>\r
+#include <xmltooling/exceptions.h>\r
+#include <xmltooling/XMLObjectBuilder.h>\r
+#include <xmltooling/io/Unmarshaller.h>\r
+#include <xmltooling/util/XMLConstants.h>\r
+#include <xmltooling/util/XMLHelper.h>\r
+\r
+namespace xmltooling {\r
+\r
+    /**\r
+     * A thread-safe abstract unmarshaller.\r
+     */\r
+    class XMLTOOL_API AbstractXMLObjectUnmarshaller : public virtual Unmarshaller\r
+    {\r
+    public:\r
+        virtual ~AbstractXMLObjectUnmarshaller() {}\r
+\r
+        /**\r
+         * @see Unmarshaller::unmarshall()\r
+         */\r
+        XMLObject* unmarshall(DOMElement* element, bool bindDocument=false) const;\r
+    \r
+        \r
+    protected:\r
+        /**\r
+         * Constructor.\r
+         * \r
+         * @param targetNamespaceURI the namespace URI of either the schema type QName or element QName of the elements this\r
+         *            unmarshaller operates on\r
+         * @param targetLocalName the local name of either the schema type QName or element QName of the elements this\r
+         *            unmarshaller operates on\r
+         */\r
+        AbstractXMLObjectUnmarshaller(const XMLCh* targetNamespaceURI, const XMLCh* targetLocalName)\r
+                : m_targetQName(targetNamespaceURI, targetLocalName) {\r
+            if (!targetLocalName || !*targetLocalName)\r
+                throw UnmarshallerException("targetLocalName cannot be null or empty");\r
+        }\r
+\r
+        /**\r
+         * Checks that the given DOM Element's XSI type or namespace qualified element name matches the target QName of this\r
+         * unmarshaller.\r
+         * \r
+         * @param domElement the DOM element to check\r
+         * \r
+         * @throws UnmarshallingException thrown if the DOM Element does not match the target of this unmarshaller\r
+         */\r
+        void checkElementIsTarget(DOMElement* domElement) const;\r
+        \r
+        /**\r
+         * Constructs the XMLObject that the given DOM Element will be unmarshalled into. If the DOM element has an XML\r
+         * Schema type defined this method will attempt to retrieve an XMLObjectBuilder using the schema type. If no\r
+         * schema type is present or no builder is registered for the schema type, the element's QName is used. Once\r
+         * the builder is found the XMLObject is created by invoking XMLObjectBuilder::buildObject().\r
+         * Extending classes may wish to override this logic if more than just schema type or element name\r
+         * (e.g. element attributes or content) need to be used to determine how to create the XMLObject.\r
+         * \r
+         * @param domElement the DOM Element the created XMLObject will represent\r
+         * @return the empty XMLObject that DOM Element can be unmarshalled into\r
+         * \r
+         * @throws UnmarshallingException thrown if there is now XMLObjectBuilder registered for the given DOM Element\r
+         */\r
+        virtual XMLObject* buildXMLObject(DOMElement* domElement) const;\r
+        \r
+        /**\r
+         * Unmarshalls the attributes from the given DOM Element into the given XMLObject. If the attribute is an XML\r
+         * namespace declaration the namespace is added to the given element via XMLObject::addNamespace().\r
+         * If it is a schema type (xsi:type) the schema type is added to the element via XMLObject::setSchemaType().\r
+         * All other attributes are passed to the processAttribute hook.\r
+         * \r
+         * @param domElement the DOM Element whose attributes will be unmarshalled\r
+         * @param xmlObject the XMLObject that will recieve information from the DOM attribute\r
+         * \r
+         * @throws UnmarshallingException thrown if there is a problem unmarshalling an attribute\r
+         */\r
+        virtual void unmarshallAttributes(DOMElement* domElement, XMLObject* xmlObject) const;\r
+\r
+        /**\r
+         * Unmarshalls a given Element's children. For each child an unmarshaller is retrieved using\r
+         * getUnmarshaller(). The unmarshaller is then used to unmarshall the child element and the\r
+         * resulting XMLObject is passed to processChildElement() for further processing.\r
+         * \r
+         * @param domElement the DOM Element whose children will be unmarshalled\r
+         * @param xmlObject the parent object of the unmarshalled children\r
+         * \r
+         * @throws UnmarshallingException thrown if an error occurs unmarshalling the child elements\r
+         */\r
+        virtual void unmarshallChildElements(DOMElement* domElement, XMLObject* xmlObject) const;\r
+\r
+        /**\r
+         * Gets the Unmarshaller for the given Element. If the child element has an explicit XML Schema type,\r
+         * that is used to get the unmarshaller. If there is no unmarshaller registered for the schema type,\r
+         * or the element does not have an explicit schema type, the element's QName is used.\r
+         * \r
+         * @param domElement the DOM Element to get the Unmarshaller for\r
+         * @return the Unmarshaller for the given DOM Element\r
+         * \r
+         * @throws UnmarshallingException thrown if no unmarshaller is available for the given DOM Element\r
+         */\r
+        const Unmarshaller* getUnmarshaller(DOMElement* domElement) const;\r
+\r
+        /**\r
+         * Called after a child element has been unmarshalled so that it can be added to the parent XMLObject.\r
+         * \r
+         * @param parent the parent XMLObject\r
+         * @param child the child XMLObject\r
+         * \r
+         * @throws UnmarshallingException thrown if there is a problem adding the child to the parent\r
+         */\r
+        virtual void processChildElement(XMLObject* parent, XMLObject* child) const=0;\r
+    \r
+        /**\r
+         * Called after an attribute has been unmarshalled so that it can be added to the XMLObject.\r
+         * \r
+         * @param xmlObject the XMLObject\r
+         * @param name the attribute's name\r
+         * @param value the attribute's value\r
+         * \r
+         * @throws UnmarshallingException thrown if there is a problem adding the attribute to the XMLObject\r
+         */\r
+        virtual void processAttribute(XMLObject* xmlObject, const XMLCh* name, const XMLCh* value) const=0;\r
+    \r
+        /**\r
+         * Called if the element being unmarshalled contained textual content so that it can be added to the XMLObject.\r
+         * \r
+         * @param xmlObject XMLObject the content will be given to\r
+         * @param elementContent the Element's text content\r
+         */\r
+        virtual void processElementContent(XMLObject* xmlObject, const XMLCh* elementContent) const=0;\r
+\r
+    private:\r
+        QName m_targetQName;\r
+    };\r
+    \r
+};\r
+\r
+#endif /* __xmltooling_xmlunmarshaller_h__ */\r
diff --git a/xmltooling/io/Marshaller.h b/xmltooling/io/Marshaller.h
new file mode 100644 (file)
index 0000000..bc0be37
--- /dev/null
@@ -0,0 +1,117 @@
+/*\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 Marshaller.h\r
+ * \r
+ * Transforms XMLObjects into DOM trees.\r
+ */\r
+\r
+#if !defined(__xmltooling_marshaller_h__)\r
+#define __xmltooling_marshaller_h__\r
+\r
+#include <xercesc/dom/DOM.hpp>\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
+     * Marshallers are used to marshall an XMLObject into a W3C DOM element.\r
+     */\r
+    class XMLTOOL_API Marshaller\r
+    {\r
+    MAKE_NONCOPYABLE(Marshaller);\r
+    public:\r
+        Marshaller() {}\r
+        virtual ~Marshaller() {}\r
+        \r
+        /**\r
+         * Marshall this element, and its children, into a W3C DOM element rooted in a given document.\r
+         * If no document is provided, then the marshaller will create one, and bind it to the\r
+         * object being marshalled.\r
+         * \r
+         * @param xmlObject the object to marshall\r
+         * @param document the DOM document the marshalled element will be rooted in\r
+         * \r
+         * @return the W3C DOM element representing this XML element\r
+         * \r
+         * @throws MarshallingException thrown if there is a problem marshalling the given object\r
+         */\r
+        virtual DOMElement* marshall(XMLObject* xmlObject, DOMDocument* document) const=0;\r
+\r
+        /**\r
+         * Retrieves a Marshaller using the key it was registered with.\r
+         * \r
+         * @param key the key used to register the marshaller\r
+         * @return the marshaller\r
+         */\r
+        static const Marshaller* getMarshaller(const QName& key) {\r
+            std::map<QName,Marshaller*>::const_iterator i=m_map.find(key);\r
+            return (i==m_map.end()) ? NULL : i->second;\r
+        }\r
+    \r
+        /**\r
+         * Gets an immutable list of all the marshallers currently registered.\r
+         * \r
+         * @return list of all the marshallers currently registered\r
+         */\r
+        static const std::map<QName,Marshaller*>& getMarshaller() {\r
+            return m_map;\r
+        }\r
+    \r
+        /**\r
+         * Registers a new marshaller for the given key.\r
+         * \r
+         * @param key the key used to retrieve this marshaller later\r
+         * @param marshaller the marshaller\r
+         */\r
+        static void registerMarshaller(const QName& key, Marshaller* marshaller) {\r
+            m_map[key]=marshaller;\r
+        }\r
+    \r
+        /**\r
+         * Deregisters a marshaller.\r
+         * \r
+         * @param key the key for the marshaller to be deregistered\r
+         */\r
+        static void deregisterMarshaller(const QName& key) {\r
+            delete getMarshaller(key);\r
+            m_map.erase(key);\r
+        }\r
+        \r
+        /**\r
+         * Unregisters and destroys all registered marshallers. \r
+         */\r
+        static void destroyMarshallers();\r
+    \r
+    private:\r
+        static std::map<QName,Marshaller*> m_map;\r
+    };\r
+    \r
+};\r
+\r
+#if defined (_MSC_VER)\r
+    #pragma warning( pop )\r
+#endif\r
+\r
+#endif /* __xmltooling_marshaller_h__ */\r
diff --git a/xmltooling/io/Unmarshaller.h b/xmltooling/io/Unmarshaller.h
new file mode 100644 (file)
index 0000000..b6e3446
--- /dev/null
@@ -0,0 +1,117 @@
+/*\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 Unmarshaller.h\r
+ * \r
+ * Transforms DOM trees into XMLObjects\r
+ */\r
+\r
+#if !defined(__xmltooling_unmarshaller_h__)\r
+#define __xmltooling_unmarshaller_h__\r
+\r
+#include <xercesc/dom/DOM.hpp>\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
+     * Unmarshallers are used to unmarshall a W3C DOM element into an XMLObject.\r
+     */\r
+    class XMLTOOL_API Unmarshaller\r
+    {\r
+    MAKE_NONCOPYABLE(Unmarshaller);\r
+    public:\r
+        Unmarshaller() {}\r
+        virtual ~Unmarshaller() {}\r
+        \r
+        /**\r
+         * Unmarshalls the given W3C DOM element into an XMLObject.\r
+         * The root of a given XML construct should be unmarshalled with the bindDocument parameter\r
+         * set to true.\r
+         * \r
+         * @param element       the DOM element to unmarshall\r
+         * @param bindDocument  true iff the resulting XMLObject should take ownership of the DOM's Document \r
+         * \r
+         * @return the unmarshalled XMLObject\r
+         * \r
+         * @throws UnmarshallingException thrown if an error occurs unmarshalling the DOM element into the XMLObject\r
+         */\r
+        virtual XMLObject* unmarshall(DOMElement* element, bool bindDocument=false) const=0;\r
+\r
+        /**\r
+         * Retrieves a unmarshaller using the key it was registered with.\r
+         * \r
+         * @param key the key used to register the unmarshaller\r
+         * @return the unmarshaller\r
+         */\r
+        static const Unmarshaller* getUnmarshaller(const QName& key) {\r
+            std::map<QName,Unmarshaller*>::const_iterator i=m_map.find(key);\r
+            return (i==m_map.end()) ? NULL : i->second;\r
+        }\r
+    \r
+        /**\r
+         * Gets an immutable list of all the unmarshallers currently registered.\r
+         * \r
+         * @return list of all the unmarshallers currently registered\r
+         */\r
+        static const std::map<QName,Unmarshaller*>& getUnmarshaller() {\r
+            return m_map;\r
+        }\r
+    \r
+        /**\r
+         * Registers a new unmarshaller for the given key.\r
+         * \r
+         * @param key the key used to retrieve this unmarshaller later\r
+         * @param unmarshaller the unmarshaller\r
+         */\r
+        static void registerUnmarshaller(const QName& key, Unmarshaller* unmarshaller) {\r
+            m_map[key]=unmarshaller;\r
+        }\r
+    \r
+        /**\r
+         * Deregisters a unmarshaller.\r
+         * \r
+         * @param key the key for the unmarshaller to be deregistered\r
+         */\r
+        static void deregisterUnmarshaller(const QName& key) {\r
+            delete getUnmarshaller(key);\r
+            m_map.erase(key);\r
+        }\r
+        \r
+        /**\r
+         * Unregisters and destroys all registered unmarshallers. \r
+         */\r
+        static void destroyUnmarshallers();\r
+    \r
+    private:\r
+        static std::map<QName,Unmarshaller*> m_map;\r
+    };\r
+    \r
+};\r
+\r
+#if defined (_MSC_VER)\r
+    #pragma warning( pop )\r
+#endif\r
+\r
+#endif /* __xmltooling_unmarshaller_h__ */\r