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