Macro adjustments, date/time class, typed XML attributes.
[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         }\r
49 \r
50         const QName& getElementQName() const {\r
51             return m_elementQname;\r
52         }\r
53 \r
54         const std::set<Namespace>& getNamespaces() const {\r
55             return m_namespaces;\r
56         }\r
57     \r
58         void addNamespace(const Namespace& ns) const {\r
59             if (ns.alwaysDeclare() || m_namespaces.find(ns)==m_namespaces.end()) {\r
60                 m_namespaces.insert(ns);\r
61             }\r
62         }\r
63     \r
64         void removeNamespace(const Namespace& ns) {\r
65             m_namespaces.erase(ns);\r
66         }\r
67         \r
68         const QName* getSchemaType() const {\r
69             return m_typeQname;\r
70         }\r
71     \r
72         bool hasParent() const {\r
73             return m_parent != NULL;\r
74         }\r
75      \r
76         XMLObject* getParent() const {\r
77             return m_parent;\r
78         }\r
79     \r
80         void setParent(XMLObject* parent) {\r
81             m_parent = parent;\r
82         }\r
83 \r
84      protected:\r
85         /**\r
86          * Constructor\r
87          * \r
88          * @param nsURI         the namespace of the element\r
89          * @param localName     the local name of the XML element this Object represents\r
90          * @param prefix        the namespace prefix to use\r
91          * @param schemaType    the xsi:type to use\r
92          */\r
93         AbstractXMLObject(\r
94             const XMLCh* nsURI=NULL, const XMLCh* localName=NULL, const XMLCh* prefix=NULL, const QName* schemaType=NULL\r
95             );\r
96 \r
97         /** Copy constructor. */\r
98         AbstractXMLObject(const AbstractXMLObject& src);\r
99         \r
100         /**\r
101          * A helper function for derived classes, for assignment of strings.\r
102          *\r
103          * This 'normalizes' newString, and then if it is different from oldString,\r
104          * it invalidates the DOM, frees the old string, and returns 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 \r
114         /**\r
115          * A helper function for derived classes, for assignment of date/time data.\r
116          *\r
117          * It invalidates the DOM, frees the old object, and returns the new.\r
118          * \r
119          * @param oldValue - the current value\r
120          * @param newValue - the new value\r
121          * \r
122          * @return the value that should be assigned\r
123          */\r
124         DateTime* prepareForAssignment(DateTime* oldValue, const DateTime* newValue);\r
125 \r
126         /**\r
127          * A helper function for derived classes, for assignment of date/time data.\r
128          *\r
129          * It invalidates the DOM, frees the old object, and returns the new.\r
130          * \r
131          * @param oldValue - the current value\r
132          * @param newValue - the epoch to assign as the new value\r
133          * \r
134          * @return the value that should be assigned\r
135          */\r
136         DateTime* prepareForAssignment(DateTime* oldValue, time_t newValue);\r
137 \r
138         /**\r
139          * A helper function for derived classes, for assignment of date/time data.\r
140          *\r
141          * It invalidates the DOM, frees the old object, and returns the new.\r
142          * \r
143          * @param oldValue - the current value\r
144          * @param newValue - the new value in string form\r
145          * \r
146          * @return the value that should be assigned\r
147          */\r
148         DateTime* prepareForAssignment(DateTime* oldValue, const XMLCh* newValue);\r
149 \r
150         /**\r
151          * A helper function for derived classes, for assignment of QName data.\r
152          *\r
153          * It invalidates the DOM, frees the old object, and returns the new.\r
154          * \r
155          * @param oldValue - the current value\r
156          * @param newValue - the new value\r
157          * \r
158          * @return the value that should be assigned\r
159          */\r
160         QName* prepareForAssignment(QName* oldValue, const QName* newValue);\r
161 \r
162         /**\r
163          * A helper function for derived classes, for assignment of (singleton) XML objects.\r
164          * \r
165          * It is indifferent to whether either the old or the new version of the value is null. \r
166          * This method will do a safe compare of the objects and will also invalidate the DOM if appropriate.\r
167          * Note that since the new value (even if NULL) is always returned, it may be more efficient\r
168          * to discard the return value and just assign independently if a dynamic cast would be involved.\r
169          * \r
170          * @param oldValue - current value\r
171          * @param newValue - proposed new value\r
172          * @return the new value \r
173          * \r
174          * @throws XMLObjectException if the new child already has a parent.\r
175          */\r
176         XMLObject* prepareForAssignment(XMLObject* oldValue, XMLObject* newValue);\r
177 \r
178         /**\r
179          * Set of namespaces associated with the object.\r
180          */\r
181         mutable std::set<Namespace> m_namespaces;\r
182 \r
183         /**\r
184          * Logging object.\r
185          */\r
186         void* m_log;\r
187 \r
188     private:\r
189         XMLObject* m_parent;\r
190         QName m_elementQname;\r
191         QName* m_typeQname;\r
192     };\r
193 \r
194 };\r
195 \r
196 #if defined (_MSC_VER)\r
197     #pragma warning( pop )\r
198 #endif\r
199 \r
200 #endif /* __xmltooling_abstractxmlobj_h__ */\r