Refactored simple content and child-handling into mixin classes.
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractXMLObject.h
1 /*\r
2 *  Copyright 2001-2006 Internet2\r
3  * \r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * @file AbstractXMLObject.h\r
19  * \r
20  * An abstract implementation of XMLObject.\r
21  */\r
22 \r
23 #if !defined(__xmltooling_abstractxmlobj_h__)\r
24 #define __xmltooling_abstractxmlobj_h__\r
25 \r
26 #include <xmltooling/XMLObject.h>\r
27 \r
28 #if defined (_MSC_VER)\r
29     #pragma warning( push )\r
30     #pragma warning( disable : 4250 4251 )\r
31 #endif\r
32 \r
33 namespace xmltooling {\r
34 \r
35     /**\r
36      * An abstract implementation of XMLObject.\r
37      * This is the primary concrete base class, and supplies basic namespace,\r
38      * type, and parent handling. Most implementation classes should not\r
39      * directly inherit from this class, but rather from the various mixins\r
40      * that supply the rest of the XMLObject interface, as required.\r
41      */\r
42     class XMLTOOL_API AbstractXMLObject : public virtual XMLObject\r
43     {\r
44     public:\r
45         virtual ~AbstractXMLObject() {\r
46             delete m_typeQname;\r
47         }\r
48 \r
49         const QName& getElementQName() const {\r
50             return m_elementQname;\r
51         }\r
52 \r
53         const std::set<Namespace>& getNamespaces() const {\r
54             return m_namespaces;\r
55         }\r
56     \r
57         void addNamespace(const Namespace& ns) const {\r
58             if (ns.alwaysDeclare() || m_namespaces.find(ns)==m_namespaces.end()) {\r
59                 m_namespaces.insert(ns);\r
60             }\r
61         }\r
62     \r
63         void removeNamespace(const Namespace& ns) {\r
64             m_namespaces.erase(ns);\r
65         }\r
66         \r
67         const QName* getSchemaType() const {\r
68             return m_typeQname;\r
69         }\r
70     \r
71         bool hasParent() const {\r
72             return m_parent != NULL;\r
73         }\r
74      \r
75         XMLObject* getParent() const {\r
76             return m_parent;\r
77         }\r
78     \r
79         void setParent(XMLObject* parent) {\r
80             m_parent = parent;\r
81         }\r
82 \r
83      protected:\r
84         /**\r
85          * Constructor\r
86          * \r
87          * @param nsURI         the namespace of the element\r
88          * @param localName     the local name of the XML element this Object represents\r
89          * @param prefix        the namespace prefix to use\r
90          * @param schemaType    the xsi:type to use\r
91          */\r
92         AbstractXMLObject(\r
93             const XMLCh* nsURI=NULL, const XMLCh* localName=NULL, const XMLCh* prefix=NULL, const QName* schemaType=NULL\r
94             );\r
95 \r
96         /** Copy constructor. */\r
97         AbstractXMLObject(const AbstractXMLObject& src);\r
98         \r
99         /**\r
100          * A helper function for derived classes.\r
101          * This 'normalizes' newString, and then if it is different from oldString,\r
102          * it invalidates the DOM, frees the old string, and return the new.\r
103          * If not different, it frees the new string and just returns the old value.\r
104          * \r
105          * @param oldValue - the current value\r
106          * @param newValue - the new value\r
107          * \r
108          * @return the value that should be assigned\r
109          */\r
110         XMLCh* prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue);\r
111 \r
112         /**\r
113          * A helper function for derived classes, for assignment of (singleton) XML objects.\r
114          * \r
115          * It is indifferent to whether either the old or the new version of the value is null. \r
116          * This method will do a safe compare of the objects and will also invalidate the DOM if appropriate.\r
117          * Note that since the new value (even if NULL) is always returned, it may be more efficient\r
118          * to discard the return value and just assign independently if a dynamic cast would be involved.\r
119          * \r
120          * @param oldValue - current value\r
121          * @param newValue - proposed new value\r
122          * @return the new value \r
123          * \r
124          * @throws XMLObjectException if the new child already has a parent.\r
125          */\r
126         XMLObject* prepareForAssignment(XMLObject* oldValue, XMLObject* newValue);\r
127 \r
128         /**\r
129          * Set of namespaces associated with the object.\r
130          */\r
131         mutable std::set<Namespace> m_namespaces;\r
132 \r
133         /**\r
134          * Logging object.\r
135          */\r
136         void* m_log;\r
137 \r
138     private:\r
139         XMLObject* m_parent;\r
140         QName m_elementQname;\r
141         QName* m_typeQname;\r
142     };\r
143 \r
144 };\r
145 \r
146 #if defined (_MSC_VER)\r
147     #pragma warning( pop )\r
148 #endif\r
149 \r
150 #endif /* __xmltooling_abstractxmlobj_h__ */\r