2 * Copyright 2001-2009 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * AbstractXMLObject.cpp
20 * An abstract implementation of XMLObject.
24 #include "exceptions.h"
25 #include "AbstractXMLObject.h"
26 #include "util/DateTime.h"
30 using namespace xmltooling;
33 using xercesc::XMLString;
35 XMLObject::XMLObject()
39 XMLObject::~XMLObject()
43 void XMLObject::releaseThisandParentDOM() const
46 releaseParentDOM(true);
49 void XMLObject::releaseThisAndChildrenDOM() const
51 releaseChildrenDOM(true);
55 AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType)
56 : m_log(logging::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")),
57 m_schemaLocation(NULL), m_noNamespaceSchemaLocation(NULL), m_nil(xmlconstants::XML_BOOL_NULL),
58 m_parent(NULL), m_elementQname(nsURI, localName, prefix), m_typeQname(NULL)
60 addNamespace(Namespace(nsURI, prefix));
62 m_typeQname = new QName(*schemaType);
63 addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix()));
67 AbstractXMLObject::AbstractXMLObject(const AbstractXMLObject& src)
68 : m_namespaces(src.m_namespaces), m_log(src.m_log), m_schemaLocation(XMLString::replicate(src.m_schemaLocation)),
69 m_noNamespaceSchemaLocation(XMLString::replicate(src.m_noNamespaceSchemaLocation)), m_nil(src.m_nil),
70 m_parent(NULL), m_elementQname(src.m_elementQname), m_typeQname(NULL)
73 m_typeQname=new QName(*src.m_typeQname);
76 AbstractXMLObject::~AbstractXMLObject()
79 xercesc::XMLString::release(&m_schemaLocation);
80 xercesc::XMLString::release(&m_noNamespaceSchemaLocation);
83 void AbstractXMLObject::detach()
87 else if (getParent()->hasParent())
88 throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
90 // Pull ourselves out of the parent and then blast him.
91 getParent()->removeChild(this);
96 const QName& AbstractXMLObject::getElementQName() const
98 return m_elementQname;
101 const set<Namespace>& AbstractXMLObject::getNamespaces() const
106 void XMLObject::setNil(const XMLCh* value)
110 case xercesc::chLatin_t:
111 nil(xmlconstants::XML_BOOL_TRUE);
113 case xercesc::chLatin_f:
114 nil(xmlconstants::XML_BOOL_FALSE);
116 case xercesc::chDigit_1:
117 nil(xmlconstants::XML_BOOL_ONE);
119 case xercesc::chDigit_0:
120 nil(xmlconstants::XML_BOOL_ZERO);
123 nil(xmlconstants::XML_BOOL_NULL);
127 nil(xmlconstants::XML_BOOL_NULL);
131 void AbstractXMLObject::addNamespace(const Namespace& ns) const
133 std::set<Namespace>::iterator i = m_namespaces.find(ns);
134 if (i == m_namespaces.end())
135 m_namespaces.insert(ns);
136 else if (ns.alwaysDeclare())
137 const_cast<Namespace&>(*i).setAlwaysDeclare(true);
140 void AbstractXMLObject::removeNamespace(const Namespace& ns)
142 m_namespaces.erase(ns);
145 const QName* AbstractXMLObject::getSchemaType() const
150 const XMLCh* AbstractXMLObject::getXMLID() const
155 xmlconstants::xmltooling_bool_t AbstractXMLObject::getNil() const
160 void AbstractXMLObject::nil(xmlconstants::xmltooling_bool_t value)
162 if (m_nil != value) {
163 releaseThisandParentDOM();
168 bool AbstractXMLObject::hasParent() const
170 return m_parent != NULL;
173 XMLObject* AbstractXMLObject::getParent() const
178 void AbstractXMLObject::setParent(XMLObject* parent)
183 XMLCh* AbstractXMLObject::prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue)
185 if (!XMLString::equals(oldValue,newValue)) {
186 releaseThisandParentDOM();
187 XMLCh* newString = XMLString::replicate(newValue);
188 XMLString::release(&oldValue);
194 QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* newValue)
198 releaseThisandParentDOM();
199 Namespace newNamespace(newValue->getNamespaceURI(), newValue->getPrefix());
200 addNamespace(newNamespace);
201 return new QName(*newValue);
207 releaseThisandParentDOM();
209 Namespace newNamespace(newValue->getNamespaceURI(), newValue->getPrefix());
210 addNamespace(newNamespace);
211 return new QName(*newValue);
216 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const DateTime* newValue)
220 releaseThisandParentDOM();
221 return new DateTime(*newValue);
227 releaseThisandParentDOM();
228 return newValue ? new DateTime(*newValue) : NULL;
231 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, time_t newValue, bool duration)
234 releaseThisandParentDOM();
235 DateTime* ret = new DateTime(newValue, duration);
237 ret->parseDuration();
239 ret->parseDateTime();
243 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const XMLCh* newValue, bool duration)
246 releaseThisandParentDOM();
247 if (!newValue || !*newValue)
249 DateTime* ret = new DateTime(newValue);
251 ret->parseDuration();
253 ret->parseDateTime();
257 XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue)
259 if (newValue && newValue->hasParent())
260 throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");
264 releaseThisandParentDOM();
265 newValue->setParent(this);
270 if (oldValue != newValue) {
272 releaseThisandParentDOM();
274 newValue->setParent(this);