Added built-in ID attribute support to base 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 #include <xmltooling/util/DateTime.h>\r
28 \r
29 #if defined (_MSC_VER)\r
30     #pragma warning( push )\r
31     #pragma warning( disable : 4250 4251 )\r
32 #endif\r
33 \r
34 namespace xmltooling {\r
35 \r
36     /**\r
37      * An abstract implementation of XMLObject.\r
38      * This is the primary concrete base class, and supplies basic namespace,\r
39      * type, and parent handling. Most implementation classes should not\r
40      * directly inherit from this class, but rather from the various mixins\r
41      * that supply the rest of the XMLObject interface, as required.\r
42      */\r
43     class XMLTOOL_API AbstractXMLObject : public virtual XMLObject\r
44     {\r
45     public:\r
46         virtual ~AbstractXMLObject() {\r
47             delete m_typeQname;\r
48             XMLString::release(&m_schemaLocation);\r
49         }\r
50 \r
51         const QName& getElementQName() const {\r
52             return m_elementQname;\r
53         }\r
54 \r
55         const std::set<Namespace>& getNamespaces() const {\r
56             return m_namespaces;\r
57         }\r
58     \r
59         void addNamespace(const Namespace& ns) const {\r
60             if (ns.alwaysDeclare() || m_namespaces.find(ns)==m_namespaces.end()) {\r
61                 m_namespaces.insert(ns);\r
62             }\r
63         }\r
64     \r
65         void removeNamespace(const Namespace& ns) {\r
66             m_namespaces.erase(ns);\r
67         }\r
68         \r
69         const QName* getSchemaType() const {\r
70             return m_typeQname;\r
71         }\r
72         \r
73         const XMLCh* getXMLID() const {\r
74             return NULL;\r
75         }\r
76     \r
77         bool hasParent() const {\r
78             return m_parent != NULL;\r
79         }\r
80      \r
81         XMLObject* getParent() const {\r
82             return m_parent;\r
83         }\r
84     \r
85         void setParent(XMLObject* parent) {\r
86             m_parent = parent;\r
87         }\r
88 \r
89      protected:\r
90         /**\r
91          * Constructor\r
92          * \r
93          * @param nsURI         the namespace of the element\r
94          * @param localName     the local name of the XML element this Object represents\r
95          * @param prefix        the namespace prefix to use\r
96          * @param schemaType    the xsi:type to use\r
97          */\r
98         AbstractXMLObject(\r
99             const XMLCh* nsURI=NULL, const XMLCh* localName=NULL, const XMLCh* prefix=NULL, const QName* schemaType=NULL\r
100             );\r
101 \r
102         /** Copy constructor. */\r
103         AbstractXMLObject(const AbstractXMLObject& src);\r
104         \r
105         /**\r
106          * A helper function for derived classes, for assignment of strings.\r
107          *\r
108          * This 'normalizes' newString, and then if it is different from oldString,\r
109          * it invalidates the DOM, frees the old string, and returns the new.\r
110          * If not different, it frees the new string and just returns the old value.\r
111          * \r
112          * @param oldValue - the current value\r
113          * @param newValue - the new value\r
114          * \r
115          * @return the value that should be assigned\r
116          */\r
117         XMLCh* prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue);\r
118 \r
119         /**\r
120          * A helper function for derived classes, for assignment of date/time data.\r
121          *\r
122          * It invalidates the DOM, frees the old object, and returns the new.\r
123          * \r
124          * @param oldValue - the current value\r
125          * @param newValue - the new value\r
126          * \r
127          * @return the value that should be assigned\r
128          */\r
129         DateTime* prepareForAssignment(DateTime* oldValue, const DateTime* newValue);\r
130 \r
131         /**\r
132          * A helper function for derived classes, for assignment of date/time data.\r
133          *\r
134          * It invalidates the DOM, frees the old object, and returns the new.\r
135          * \r
136          * @param oldValue - the current value\r
137          * @param newValue - the epoch to assign as the new value\r
138          * \r
139          * @return the value that should be assigned\r
140          */\r
141         DateTime* prepareForAssignment(DateTime* oldValue, time_t newValue);\r
142 \r
143         /**\r
144          * A helper function for derived classes, for assignment of date/time data.\r
145          *\r
146          * It invalidates the DOM, frees the old object, and returns the new.\r
147          * \r
148          * @param oldValue - the current value\r
149          * @param newValue - the new value in string form\r
150          * \r
151          * @return the value that should be assigned\r
152          */\r
153         DateTime* prepareForAssignment(DateTime* oldValue, const XMLCh* newValue);\r
154 \r
155         /**\r
156          * A helper function for derived classes, for assignment of QName data.\r
157          *\r
158          * It invalidates the DOM, frees the old object, and returns the new.\r
159          * \r
160          * @param oldValue - the current value\r
161          * @param newValue - the new value\r
162          * \r
163          * @return the value that should be assigned\r
164          */\r
165         QName* prepareForAssignment(QName* oldValue, const QName* newValue);\r
166 \r
167         /**\r
168          * A helper function for derived classes, for assignment of (singleton) XML objects.\r
169          * \r
170          * It is indifferent to whether either the old or the new version of the value is null. \r
171          * This method will do a safe compare of the objects and will also invalidate the DOM if appropriate.\r
172          * Note that since the new value (even if NULL) is always returned, it may be more efficient\r
173          * to discard the return value and just assign independently if a dynamic cast would be involved.\r
174          * \r
175          * @param oldValue - current value\r
176          * @param newValue - proposed new value\r
177          * @return the new value \r
178          * \r
179          * @throws XMLObjectException if the new child already has a parent.\r
180          */\r
181         XMLObject* prepareForAssignment(XMLObject* oldValue, XMLObject* newValue);\r
182 \r
183         /**\r
184          * Set of namespaces associated with the object.\r
185          */\r
186         mutable std::set<Namespace> m_namespaces;\r
187 \r
188         /**\r
189          * Logging object.\r
190          */\r
191         void* m_log;\r
192 \r
193         /**\r
194          * Stores off xsi:schemaLocation attribute.\r
195          */\r
196         XMLCh* m_schemaLocation;\r
197 \r
198     private:\r
199         XMLObject* m_parent;\r
200         QName m_elementQname;\r
201         QName* m_typeQname;\r
202     };\r
203 \r
204 };\r
205 \r
206 #if defined (_MSC_VER)\r
207     #pragma warning( pop )\r
208 #endif\r
209 \r
210 #endif /* __xmltooling_abstractxmlobj_h__ */\r