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