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), m_typeQname(nullptr)
64 addNamespace(Namespace(nsURI, prefix, false, Namespace::VisiblyUsed));
66 m_typeQname = 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), m_typeQname(nullptr)
77 m_typeQname=new QName(*src.m_typeQname);
80 AbstractXMLObject::~AbstractXMLObject()
83 xercesc::XMLString::release(&m_schemaLocation);
84 xercesc::XMLString::release(&m_noNamespaceSchemaLocation);
87 void AbstractXMLObject::detach()
91 else if (getParent()->hasParent())
92 throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
94 // Pull ourselves out of the parent and then blast him.
95 getParent()->removeChild(this);
100 const QName& AbstractXMLObject::getElementQName() const
102 return m_elementQname;
105 const set<Namespace>& AbstractXMLObject::getNamespaces() const
110 void XMLObject::setNil(const XMLCh* value)
114 case xercesc::chLatin_t:
115 nil(xmlconstants::XML_BOOL_TRUE);
117 case xercesc::chLatin_f:
118 nil(xmlconstants::XML_BOOL_FALSE);
120 case xercesc::chDigit_1:
121 nil(xmlconstants::XML_BOOL_ONE);
123 case xercesc::chDigit_0:
124 nil(xmlconstants::XML_BOOL_ZERO);
127 nil(xmlconstants::XML_BOOL_NULL);
131 nil(xmlconstants::XML_BOOL_NULL);
135 void AbstractXMLObject::addNamespace(const Namespace& ns) const
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:
147 case Namespace::VisiblyUsed:
148 const_cast<Namespace&>(*n).setUsage(Namespace::VisiblyUsed);
150 case Namespace::NonVisiblyUsed:
151 if (n->usage() == Namespace::Indeterminate)
152 const_cast<Namespace&>(*n).setUsage(Namespace::NonVisiblyUsed);
160 // If the prefix is now, go ahead and add it.
161 m_namespaces.insert(ns);
164 void AbstractXMLObject::removeNamespace(const Namespace& ns)
166 m_namespaces.erase(ns);
169 const QName* AbstractXMLObject::getSchemaType() const
174 const XMLCh* AbstractXMLObject::getXMLID() const
179 xmlconstants::xmltooling_bool_t AbstractXMLObject::getNil() const
184 void AbstractXMLObject::nil(xmlconstants::xmltooling_bool_t value)
186 if (m_nil != value) {
187 releaseThisandParentDOM();
192 bool AbstractXMLObject::hasParent() const
194 return m_parent != nullptr;
197 XMLObject* AbstractXMLObject::getParent() const
202 void AbstractXMLObject::setParent(XMLObject* parent)
207 XMLCh* AbstractXMLObject::prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue)
209 if (!XMLString::equals(oldValue,newValue)) {
210 releaseThisandParentDOM();
211 XMLCh* newString = XMLString::replicate(newValue);
212 XMLString::release(&oldValue);
218 QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* newValue)
222 releaseThisandParentDOM();
223 addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed));
224 return new QName(*newValue);
230 releaseThisandParentDOM();
232 // Attach a non-visibly used namespace.
233 addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed));
234 return new QName(*newValue);
239 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const DateTime* newValue)
243 releaseThisandParentDOM();
244 return new DateTime(*newValue);
250 releaseThisandParentDOM();
251 return newValue ? new DateTime(*newValue) : nullptr;
254 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, time_t newValue, bool duration)
257 releaseThisandParentDOM();
258 DateTime* ret = new DateTime(newValue, duration);
260 ret->parseDuration();
262 ret->parseDateTime();
266 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const XMLCh* newValue, bool duration)
269 releaseThisandParentDOM();
270 if (!newValue || !*newValue)
272 DateTime* ret = new DateTime(newValue);
274 ret->parseDuration();
276 ret->parseDateTime();
280 XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue)
282 if (newValue && newValue->hasParent())
283 throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");
287 releaseThisandParentDOM();
288 newValue->setParent(this);
293 if (oldValue != newValue) {
295 releaseThisandParentDOM();
297 newValue->setParent(this);