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