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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * AbstractXMLObject.cpp
24 * An abstract implementation of XMLObject.
28 #include "exceptions.h"
29 #include "AbstractXMLObject.h"
30 #include "util/DateTime.h"
34 using namespace xmltooling;
37 using xercesc::XMLString;
39 XMLObject::XMLObject()
43 XMLObject::~XMLObject()
47 void XMLObject::releaseThisandParentDOM() const
50 releaseParentDOM(true);
53 void XMLObject::releaseThisAndChildrenDOM() const
55 releaseChildrenDOM(true);
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)
64 addNamespace(Namespace(nsURI, prefix, false, Namespace::VisiblyUsed));
66 m_typeQname.reset(new QName(*schemaType));
67 addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix(), false, Namespace::NonVisiblyUsed));
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)
79 AbstractXMLObject::~AbstractXMLObject()
81 xercesc::XMLString::release(&m_schemaLocation);
82 xercesc::XMLString::release(&m_noNamespaceSchemaLocation);
85 void AbstractXMLObject::detach()
89 else if (getParent()->hasParent())
90 throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
92 // Pull ourselves out of the parent and then blast him.
93 getParent()->removeChild(this);
98 const QName& AbstractXMLObject::getElementQName() const
100 return m_elementQname;
103 const set<Namespace>& AbstractXMLObject::getNamespaces() const
108 void XMLObject::setNil(const XMLCh* value)
112 case xercesc::chLatin_t:
113 nil(xmlconstants::XML_BOOL_TRUE);
115 case xercesc::chLatin_f:
116 nil(xmlconstants::XML_BOOL_FALSE);
118 case xercesc::chDigit_1:
119 nil(xmlconstants::XML_BOOL_ONE);
121 case xercesc::chDigit_0:
122 nil(xmlconstants::XML_BOOL_ZERO);
125 nil(xmlconstants::XML_BOOL_NULL);
129 nil(xmlconstants::XML_BOOL_NULL);
133 void AbstractXMLObject::addNamespace(const Namespace& ns) const
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:
145 case Namespace::VisiblyUsed:
146 const_cast<Namespace&>(*n).setUsage(Namespace::VisiblyUsed);
148 case Namespace::NonVisiblyUsed:
149 if (n->usage() == Namespace::Indeterminate)
150 const_cast<Namespace&>(*n).setUsage(Namespace::NonVisiblyUsed);
158 // If the prefix is now, go ahead and add it.
159 m_namespaces.insert(ns);
162 void AbstractXMLObject::removeNamespace(const Namespace& ns)
164 m_namespaces.erase(ns);
167 const QName* AbstractXMLObject::getSchemaType() const
169 return m_typeQname.get();
172 const XMLCh* AbstractXMLObject::getXMLID() const
177 xmlconstants::xmltooling_bool_t AbstractXMLObject::getNil() const
182 void AbstractXMLObject::nil(xmlconstants::xmltooling_bool_t value)
184 if (m_nil != value) {
185 releaseThisandParentDOM();
190 bool AbstractXMLObject::hasParent() const
192 return m_parent != nullptr;
195 XMLObject* AbstractXMLObject::getParent() const
200 void AbstractXMLObject::setParent(XMLObject* parent)
205 XMLCh* AbstractXMLObject::prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue)
207 if (!XMLString::equals(oldValue,newValue)) {
208 releaseThisandParentDOM();
209 XMLCh* newString = XMLString::replicate(newValue);
210 XMLString::release(&oldValue);
216 QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* newValue)
220 releaseThisandParentDOM();
221 addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed));
222 return new QName(*newValue);
228 releaseThisandParentDOM();
230 // Attach a non-visibly used namespace.
231 addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed));
232 return new QName(*newValue);
237 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const DateTime* newValue)
241 releaseThisandParentDOM();
242 return new DateTime(*newValue);
248 releaseThisandParentDOM();
249 return newValue ? new DateTime(*newValue) : nullptr;
252 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, time_t newValue, bool duration)
255 releaseThisandParentDOM();
256 DateTime* ret = new DateTime(newValue, duration);
258 ret->parseDuration();
260 ret->parseDateTime();
264 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const XMLCh* newValue, bool duration)
267 releaseThisandParentDOM();
268 if (!newValue || !*newValue)
270 DateTime* ret = new DateTime(newValue);
272 ret->parseDuration();
274 ret->parseDateTime();
278 XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue)
280 if (newValue && newValue->hasParent())
281 throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");
285 releaseThisandParentDOM();
286 newValue->setParent(this);
291 if (oldValue != newValue) {
293 releaseThisandParentDOM();
295 newValue->setParent(this);