Merge branch '1.x' of ssh://authdev.it.ohio-state.edu/~scantor/git/cpp-xmltooling...
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractXMLObject.cpp
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  * AbstractXMLObject.cpp
23  *
24  * An abstract implementation of XMLObject.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "AbstractXMLObject.h"
30 #include "util/DateTime.h"
31
32 #include <algorithm>
33
34 using namespace xmltooling;
35 using std::set;
36
37 using xercesc::XMLString;
38
39 XMLObject::XMLObject()
40 {
41 }
42
43 XMLObject::~XMLObject()
44 {
45 }
46
47 void XMLObject::releaseThisandParentDOM() const
48 {
49     releaseDOM();
50     releaseParentDOM(true);
51 }
52
53 void XMLObject::releaseThisAndChildrenDOM() const
54 {
55     releaseChildrenDOM(true);
56     releaseDOM();
57 }
58
59 AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType)
60     : m_log(logging::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")),
61         m_schemaLocation(nullptr), m_noNamespaceSchemaLocation(nullptr), m_nil(xmlconstants::XML_BOOL_NULL),
62         m_parent(nullptr), m_elementQname(nsURI, localName, prefix), m_typeQname(nullptr)
63 {
64     addNamespace(Namespace(nsURI, prefix, false, Namespace::VisiblyUsed));
65     if (schemaType) {
66         m_typeQname = new QName(*schemaType);
67         addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix(), false, Namespace::NonVisiblyUsed));
68     }
69 }
70
71 AbstractXMLObject::AbstractXMLObject(const AbstractXMLObject& src)
72     : m_namespaces(src.m_namespaces), m_log(src.m_log), m_schemaLocation(XMLString::replicate(src.m_schemaLocation)),
73         m_noNamespaceSchemaLocation(XMLString::replicate(src.m_noNamespaceSchemaLocation)), m_nil(src.m_nil),
74         m_parent(nullptr), m_elementQname(src.m_elementQname), m_typeQname(nullptr)
75 {
76     if (src.m_typeQname)
77         m_typeQname=new QName(*src.m_typeQname);
78 }
79
80 AbstractXMLObject::~AbstractXMLObject()
81 {
82     delete m_typeQname;
83     xercesc::XMLString::release(&m_schemaLocation);
84     xercesc::XMLString::release(&m_noNamespaceSchemaLocation);
85 }
86
87 void AbstractXMLObject::detach()
88 {
89     if (!getParent())
90         return;
91     else if (getParent()->hasParent())
92         throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
93
94     // Pull ourselves out of the parent and then blast him.
95     getParent()->removeChild(this);
96     delete m_parent;
97     m_parent = nullptr;
98 }
99
100 const QName& AbstractXMLObject::getElementQName() const
101 {
102     return m_elementQname;
103 }
104
105 const set<Namespace>& AbstractXMLObject::getNamespaces() const
106 {
107     return m_namespaces;
108 }
109
110 void XMLObject::setNil(const XMLCh* value)
111 {
112     if (value) {
113         switch (*value) {
114             case xercesc::chLatin_t:
115                 nil(xmlconstants::XML_BOOL_TRUE);
116                 break;
117             case xercesc::chLatin_f:
118                 nil(xmlconstants::XML_BOOL_FALSE);
119                 break;
120             case xercesc::chDigit_1:
121                 nil(xmlconstants::XML_BOOL_ONE);
122                 break;
123             case xercesc::chDigit_0:
124                 nil(xmlconstants::XML_BOOL_ZERO);
125                 break;
126             default:
127                 nil(xmlconstants::XML_BOOL_NULL);
128         }
129     }
130     else {
131         nil(xmlconstants::XML_BOOL_NULL);
132     }
133 }
134
135 void AbstractXMLObject::addNamespace(const Namespace& ns) const
136 {
137     for (set<Namespace>::const_iterator n = m_namespaces.begin(); n != m_namespaces.end(); ++n) {
138         // Look for the prefix in the existing set.
139         if (XMLString::equals(ns.getNamespacePrefix(), n->getNamespacePrefix())) {
140             // See if it's the same declaration, and overlay various properties if so.
141             if (XMLString::equals(ns.getNamespaceURI(), n->getNamespaceURI())) {
142                 if (ns.alwaysDeclare())
143                     const_cast<Namespace&>(*n).setAlwaysDeclare(true);
144                 switch (ns.usage()) {
145                     case Namespace::Indeterminate:
146                         break;
147                     case Namespace::VisiblyUsed:
148                         const_cast<Namespace&>(*n).setUsage(Namespace::VisiblyUsed);
149                         break;
150                     case Namespace::NonVisiblyUsed:
151                         if (n->usage() == Namespace::Indeterminate)
152                             const_cast<Namespace&>(*n).setUsage(Namespace::NonVisiblyUsed);
153                         break;
154                 }
155             }
156             return;
157         }
158     }
159
160     // If the prefix is now, go ahead and add it.
161     m_namespaces.insert(ns);
162 }
163
164 void AbstractXMLObject::removeNamespace(const Namespace& ns)
165 {
166     m_namespaces.erase(ns);
167 }
168
169 const QName* AbstractXMLObject::getSchemaType() const
170 {
171     return m_typeQname;
172 }
173
174 const XMLCh* AbstractXMLObject::getXMLID() const
175 {
176     return nullptr;
177 }
178
179 xmlconstants::xmltooling_bool_t AbstractXMLObject::getNil() const
180 {
181     return m_nil;
182 }
183
184 void AbstractXMLObject::nil(xmlconstants::xmltooling_bool_t value)
185 {
186     if (m_nil != value) {
187         releaseThisandParentDOM();
188         m_nil = value;
189     }
190 }
191
192 bool AbstractXMLObject::hasParent() const
193 {
194     return m_parent != nullptr;
195 }
196
197 XMLObject* AbstractXMLObject::getParent() const
198 {
199     return m_parent;
200 }
201
202 void AbstractXMLObject::setParent(XMLObject* parent)
203 {
204     m_parent = parent;
205 }
206
207 XMLCh* AbstractXMLObject::prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue)
208 {
209     if (!XMLString::equals(oldValue,newValue)) {
210         releaseThisandParentDOM();
211         XMLCh* newString = XMLString::replicate(newValue);
212         XMLString::release(&oldValue);
213         return newString;
214     }
215     return oldValue;
216 }
217
218 QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* newValue)
219 {
220     if (!oldValue) {
221         if (newValue) {
222             releaseThisandParentDOM();
223             addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed));
224             return new QName(*newValue);
225         }
226         return nullptr;
227     }
228
229     delete oldValue;
230     releaseThisandParentDOM();
231     if (newValue) {
232         // Attach a non-visibly used namespace.
233         addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed));
234         return new QName(*newValue);
235     }
236     return nullptr;
237 }
238
239 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const DateTime* newValue)
240 {
241     if (!oldValue) {
242         if (newValue) {
243             releaseThisandParentDOM();
244             return new DateTime(*newValue);
245         }
246         return nullptr;
247     }
248
249     delete oldValue;
250     releaseThisandParentDOM();
251     return newValue ? new DateTime(*newValue) : nullptr;
252 }
253
254 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, time_t newValue, bool duration)
255 {
256     delete oldValue;
257     releaseThisandParentDOM();
258     DateTime* ret = new DateTime(newValue, duration);
259     if (duration)
260         ret->parseDuration();
261     else
262         ret->parseDateTime();
263     return ret;
264 }
265
266 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const XMLCh* newValue, bool duration)
267 {
268     delete oldValue;
269     releaseThisandParentDOM();
270     if (!newValue || !*newValue)
271         return nullptr;
272     DateTime* ret = new DateTime(newValue);
273     if (duration)
274         ret->parseDuration();
275     else
276         ret->parseDateTime();
277     return ret;
278 }
279
280 XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue)
281 {
282     if (newValue && newValue->hasParent())
283         throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");
284
285     if (!oldValue) {
286         if (newValue) {
287             releaseThisandParentDOM();
288             newValue->setParent(this);
289         }
290         return newValue;
291     }
292
293     if (oldValue != newValue) {
294         delete oldValue;
295         releaseThisandParentDOM();
296         if (newValue)
297             newValue->setParent(this);
298     }
299
300     return newValue;
301 }