https://issues.shibboleth.net/jira/browse/CPPXT-18
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractXMLObject.h
1 /*
2 *  Copyright 2001-2007 Internet2
3  *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file xmltooling/AbstractXMLObject.h
19  *
20  * An abstract implementation of XMLObject.
21  */
22
23 #ifndef __xmltooling_abstractxmlobj_h__
24 #define __xmltooling_abstractxmlobj_h__
25
26 #include <xmltooling/logging.h>
27 #include <xmltooling/XMLObject.h>
28 #include <xmltooling/util/DateTime.h>
29
30 #if defined (_MSC_VER)
31     #pragma warning( push )
32     #pragma warning( disable : 4250 4251 )
33 #endif
34
35 namespace xmltooling {
36
37     /**
38      * An abstract implementation of XMLObject.
39      * This is the primary concrete base class, and supplies basic namespace,
40      * type, and parent handling. Most implementation classes should not
41      * directly inherit from this class, but rather from the various mixins
42      * that supply the rest of the XMLObject interface, as required.
43      */
44     class XMLTOOL_API AbstractXMLObject : public virtual XMLObject
45     {
46     public:
47         virtual ~AbstractXMLObject();
48
49         void detach();
50
51         const QName& getElementQName() const {
52             return m_elementQname;
53         }
54
55         const std::set<Namespace>& getNamespaces() const {
56             return m_namespaces;
57         }
58
59         void addNamespace(const Namespace& ns) const {
60             std::set<Namespace>::iterator i = m_namespaces.find(ns);
61             if (i == m_namespaces.end())
62                 m_namespaces.insert(ns);
63             else if (ns.alwaysDeclare())
64                 const_cast<Namespace&>(*i).setAlwaysDeclare(true);
65         }
66
67         void removeNamespace(const Namespace& ns) {
68             m_namespaces.erase(ns);
69         }
70
71         const QName* getSchemaType() const {
72             return m_typeQname;
73         }
74
75         const XMLCh* getXMLID() const {
76             return NULL;
77         }
78
79         xmlconstants::xmltooling_bool_t getNil() const {
80                 return m_nil;
81         }
82
83         void nil(xmlconstants::xmltooling_bool_t value) {
84             if (m_nil != value) {
85                 releaseThisandParentDOM();
86                 m_nil = value;
87             }
88         }
89
90         bool hasParent() const {
91             return m_parent != NULL;
92         }
93
94         XMLObject* getParent() const {
95             return m_parent;
96         }
97
98         void setParent(XMLObject* parent) {
99             m_parent = parent;
100         }
101
102      protected:
103         /**
104          * Constructor
105          *
106          * @param nsURI         the namespace of the element
107          * @param localName     the local name of the XML element this Object represents
108          * @param prefix        the namespace prefix to use
109          * @param schemaType    the xsi:type to use
110          */
111         AbstractXMLObject(
112             const XMLCh* nsURI=NULL, const XMLCh* localName=NULL, const XMLCh* prefix=NULL, const QName* schemaType=NULL
113             );
114
115         /** Copy constructor. */
116         AbstractXMLObject(const AbstractXMLObject& src);
117
118         /**
119          * A helper function for derived classes, for assignment of strings.
120          *
121          * This 'normalizes' newString, and then if it is different from oldString,
122          * it invalidates the DOM, frees the old string, and returns the new.
123          * If not different, it frees the new string and just returns the old value.
124          *
125          * @param oldValue the current value
126          * @param newValue the new value
127          *
128          * @return the value that should be assigned
129          */
130         XMLCh* prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue);
131
132         /**
133          * A helper function for derived classes, for assignment of date/time data.
134          *
135          * It invalidates the DOM, frees the old object, and returns the new.
136          *
137          * @param oldValue the current value
138          * @param newValue the new value
139          *
140          * @return the value that should be assigned
141          */
142         DateTime* prepareForAssignment(DateTime* oldValue, const DateTime* newValue);
143
144         /**
145          * A helper function for derived classes, for assignment of date/time data.
146          *
147          * It invalidates the DOM, frees the old object, and returns the new.
148          *
149          * @param oldValue the current value
150          * @param newValue the epoch to assign as the new value
151          * @param duration true iff the value is a duration rather than an absolute timestamp
152          *
153          * @return the value that should be assigned
154          */
155         DateTime* prepareForAssignment(DateTime* oldValue, time_t newValue, bool duration=false);
156
157         /**
158          * A helper function for derived classes, for assignment of date/time data.
159          *
160          * It invalidates the DOM, frees the old object, and returns the new.
161          *
162          * @param oldValue the current value
163          * @param newValue the new value in string form
164          * @param duration true iff the value is a duration rather than an absolute timestamp
165          *
166          * @return the value that should be assigned
167          */
168         DateTime* prepareForAssignment(DateTime* oldValue, const XMLCh* newValue, bool duration=false);
169
170         /**
171          * A helper function for derived classes, for assignment of QName data.
172          *
173          * It invalidates the DOM, frees the old object, and returns the new.
174          *
175          * @param oldValue the current value
176          * @param newValue the new value
177          *
178          * @return the value that should be assigned
179          */
180         QName* prepareForAssignment(QName* oldValue, const QName* newValue);
181
182         /**
183          * A helper function for derived classes, for assignment of (singleton) XML objects.
184          *
185          * It is indifferent to whether either the old or the new version of the value is null.
186          * This method will do a safe compare of the objects and will also invalidate the DOM if appropriate.
187          * Note that since the new value (even if NULL) is always returned, it may be more efficient
188          * to discard the return value and just assign independently if a dynamic cast would be involved.
189          *
190          * @param oldValue current value
191          * @param newValue proposed new value
192          * @return the new value
193          *
194          * @throws XMLObjectException if the new child already has a parent.
195          */
196         XMLObject* prepareForAssignment(XMLObject* oldValue, XMLObject* newValue);
197
198         /**
199          * Set of namespaces associated with the object.
200          */
201         mutable std::set<Namespace> m_namespaces;
202
203         /**
204          * Logging object.
205          */
206         logging::Category& m_log;
207
208         /**
209          * Stores off xsi:schemaLocation attribute.
210          */
211         XMLCh* m_schemaLocation;
212
213         /**
214          * Stores off xsi:noNamespaceSchemaLocation attribute.
215          */
216         XMLCh* m_noNamespaceSchemaLocation;
217
218         /**
219          * Stores off xsi:nil attribute.
220          */
221         xmlconstants::xmltooling_bool_t m_nil;
222
223     private:
224         XMLObject* m_parent;
225         QName m_elementQname;
226         QName* m_typeQname;
227     };
228
229 };
230
231 #if defined (_MSC_VER)
232     #pragma warning( pop )
233 #endif
234
235 #endif /* __xmltooling_abstractxmlobj_h__ */