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