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 const XMLCh* XMLObject::getLang() const
64 void XMLObject::setNil(const XMLCh* value)
68 case xercesc::chLatin_t:
69 nil(xmlconstants::XML_BOOL_TRUE);
71 case xercesc::chLatin_f:
72 nil(xmlconstants::XML_BOOL_FALSE);
74 case xercesc::chDigit_1:
75 nil(xmlconstants::XML_BOOL_ONE);
77 case xercesc::chDigit_0:
78 nil(xmlconstants::XML_BOOL_ZERO);
81 nil(xmlconstants::XML_BOOL_NULL);
85 nil(xmlconstants::XML_BOOL_NULL);
89 AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType)
90 : m_log(logging::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")),
91 m_schemaLocation(nullptr), m_noNamespaceSchemaLocation(nullptr), m_nil(xmlconstants::XML_BOOL_NULL),
92 m_parent(nullptr), m_elementQname(nsURI, localName, prefix)
94 addNamespace(Namespace(nsURI, prefix, false, Namespace::VisiblyUsed));
96 m_typeQname.reset(new QName(*schemaType));
97 addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix(), false, Namespace::NonVisiblyUsed));
101 AbstractXMLObject::AbstractXMLObject(const AbstractXMLObject& src)
102 : m_namespaces(src.m_namespaces), m_log(src.m_log), m_schemaLocation(XMLString::replicate(src.m_schemaLocation)),
103 m_noNamespaceSchemaLocation(XMLString::replicate(src.m_noNamespaceSchemaLocation)), m_nil(src.m_nil),
104 m_parent(nullptr), m_elementQname(src.m_elementQname),
105 m_typeQname(src.m_typeQname.get() ? new QName(*src.m_typeQname) : nullptr)
109 AbstractXMLObject::~AbstractXMLObject()
111 xercesc::XMLString::release(&m_schemaLocation);
112 xercesc::XMLString::release(&m_noNamespaceSchemaLocation);
115 void AbstractXMLObject::detach()
119 else if (getParent()->hasParent())
120 throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
122 // Pull ourselves out of the parent and then blast him.
123 getParent()->removeChild(this);
128 const QName& AbstractXMLObject::getElementQName() const
130 return m_elementQname;
133 const set<Namespace>& AbstractXMLObject::getNamespaces() const
138 void AbstractXMLObject::addNamespace(const Namespace& ns) const
140 for (set<Namespace>::const_iterator n = m_namespaces.begin(); n != m_namespaces.end(); ++n) {
141 // Look for the prefix in the existing set.
142 if (XMLString::equals(ns.getNamespacePrefix(), n->getNamespacePrefix())) {
143 // See if it's the same declaration, and overlay various properties if so.
144 if (XMLString::equals(ns.getNamespaceURI(), n->getNamespaceURI())) {
145 if (ns.alwaysDeclare())
146 const_cast<Namespace&>(*n).setAlwaysDeclare(true);
147 switch (ns.usage()) {
148 case Namespace::Indeterminate:
150 case Namespace::VisiblyUsed:
151 const_cast<Namespace&>(*n).setUsage(Namespace::VisiblyUsed);
153 case Namespace::NonVisiblyUsed:
154 if (n->usage() == Namespace::Indeterminate)
155 const_cast<Namespace&>(*n).setUsage(Namespace::NonVisiblyUsed);
163 // If the prefix is now, go ahead and add it.
164 m_namespaces.insert(ns);
167 void AbstractXMLObject::removeNamespace(const Namespace& ns)
169 m_namespaces.erase(ns);
172 const QName* AbstractXMLObject::getSchemaType() const
174 return m_typeQname.get();
177 const XMLCh* AbstractXMLObject::getXMLID() const
182 xmlconstants::xmltooling_bool_t AbstractXMLObject::getNil() const
187 void AbstractXMLObject::nil(xmlconstants::xmltooling_bool_t value)
189 if (m_nil != value) {
190 releaseThisandParentDOM();
195 bool AbstractXMLObject::hasParent() const
197 return m_parent != nullptr;
200 XMLObject* AbstractXMLObject::getParent() const
205 void AbstractXMLObject::setParent(XMLObject* parent)
210 XMLCh* AbstractXMLObject::prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue)
212 if (!XMLString::equals(oldValue,newValue)) {
213 releaseThisandParentDOM();
214 XMLCh* newString = XMLString::replicate(newValue);
215 XMLString::release(&oldValue);
221 QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* newValue)
225 releaseThisandParentDOM();
226 addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed));
227 return new QName(*newValue);
233 releaseThisandParentDOM();
235 // Attach a non-visibly used namespace.
236 addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed));
237 return new QName(*newValue);
242 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const DateTime* newValue)
246 releaseThisandParentDOM();
247 return new DateTime(*newValue);
253 releaseThisandParentDOM();
254 return newValue ? new DateTime(*newValue) : nullptr;
257 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, time_t newValue, bool duration)
260 releaseThisandParentDOM();
261 DateTime* ret = new DateTime(newValue, duration);
263 ret->parseDuration();
265 ret->parseDateTime();
269 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const XMLCh* newValue, bool duration)
272 releaseThisandParentDOM();
273 if (!newValue || !*newValue)
275 DateTime* ret = new DateTime(newValue);
277 ret->parseDuration();
279 ret->parseDateTime();
283 XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue)
285 if (newValue && newValue->hasParent())
286 throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");
290 releaseThisandParentDOM();
291 newValue->setParent(this);
296 if (oldValue != newValue) {
298 releaseThisandParentDOM();
300 newValue->setParent(this);