Set xsi:type during object construction.
[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      */\r
38     class XMLTOOL_API AbstractXMLObject : public virtual XMLObject\r
39     {\r
40     public:\r
41         virtual ~AbstractXMLObject();\r
42 \r
43         const QName& getElementQName() const {\r
44             return m_elementQname;\r
45         }\r
46 \r
47         const std::set<Namespace>& getNamespaces() const {\r
48             return m_namespaces;\r
49         }\r
50     \r
51         void addNamespace(const Namespace& ns) const {\r
52             if (ns.alwaysDeclare() || m_namespaces.find(ns)==m_namespaces.end()) {\r
53                 m_namespaces.insert(ns);\r
54             }\r
55         }\r
56     \r
57         void removeNamespace(const Namespace& ns) {\r
58             m_namespaces.erase(ns);\r
59         }\r
60         \r
61         const QName* getSchemaType() const {\r
62             return m_typeQname;\r
63         }\r
64     \r
65         bool hasParent() const {\r
66             return m_parent != NULL;\r
67         }\r
68      \r
69         XMLObject* getParent() const {\r
70             return m_parent;\r
71         }\r
72     \r
73         void setParent(XMLObject* parent) {\r
74             m_parent = parent;\r
75         }\r
76 \r
77         bool hasChildren() const {\r
78             return !m_children.empty();\r
79         }\r
80 \r
81         const std::list<XMLObject*>& getOrderedChildren() const {\r
82             return m_children;\r
83         }\r
84 \r
85      protected:\r
86         /**\r
87          * Constructor\r
88          * \r
89          * @param nsURI         the namespace of the element\r
90          * @param localName     the local name of the XML element this Object represents\r
91          * @param prefix        the namespace prefix to use\r
92          * @param schemaType    the xsi:type to use\r
93          */\r
94         AbstractXMLObject(\r
95             const XMLCh* nsURI=NULL, const XMLCh* localName=NULL, const XMLCh* prefix=NULL, const QName* schemaType=NULL\r
96             );\r
97 \r
98         /** Copy constructor. */\r
99         AbstractXMLObject(const AbstractXMLObject& src);\r
100         \r
101         /**\r
102          * A helper function for derived classes.\r
103          * This 'normalizes' newString, and then if it is different from oldString,\r
104          * it invalidates the DOM, frees the old string, and return the new.\r
105          * If not different, it frees the new string and just returns the old value.\r
106          * \r
107          * @param oldValue - the current value\r
108          * @param newValue - the new value\r
109          * \r
110          * @return the value that should be assigned\r
111          */\r
112         XMLCh* prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue) {\r
113             XMLCh* newString = XMLString::replicate(newValue);\r
114             XMLString::trim(newString);\r
115             if (!XMLString::equals(oldValue,newValue)) {\r
116                 releaseThisandParentDOM();\r
117                 XMLString::release(&oldValue);\r
118                 return newString;\r
119             }\r
120             XMLString::release(&newString);\r
121             return oldValue;            \r
122         }\r
123 \r
124         /**\r
125          * A helper function for derived classes, for assignment of (singleton) XML objects.\r
126          * \r
127          * It is indifferent to whether either the old or the new version of the value is null. \r
128          * This method will do a safe compare of the objects and will also invalidate the DOM if appropriate\r
129          * \r
130          * @param oldValue - current value\r
131          * @param newValue - proposed new value\r
132          * @return the value to assign \r
133          * \r
134          * @throws XMLObjectException if the new child already has a parent.\r
135          */\r
136         XMLObject* prepareForAssignment(XMLObject* oldValue, XMLObject* newValue);\r
137 \r
138         /**\r
139          * Underlying list of child objects.\r
140          * Manages the lifetime of the children.\r
141          */\r
142         std::list<XMLObject*> m_children;\r
143 \r
144         /**\r
145          * Set of namespaces associated with the object.\r
146          */\r
147         mutable std::set<Namespace> m_namespaces;\r
148 \r
149         /**\r
150          * Logging object.\r
151          */\r
152         void* m_log;\r
153 \r
154     private:\r
155         XMLObject* m_parent;\r
156         QName m_elementQname;\r
157         QName* m_typeQname;\r
158     };\r
159 \r
160 };\r
161 \r
162 #if defined (_MSC_VER)\r
163     #pragma warning( pop )\r
164 #endif\r
165 \r
166 #endif /* __xmltooling_abstractxmlobj_h__ */\r