2 * Copyright 2001-2007 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 "AbstractXMLObject.h"
25 #include "exceptions.h"
29 using namespace xmltooling;
31 AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType)
32 : m_log(logging::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")), m_schemaLocation(NULL), m_noNamespaceSchemaLocation(NULL),
33 m_parent(NULL), m_elementQname(nsURI, localName, prefix), m_typeQname(NULL)
35 addNamespace(Namespace(nsURI, prefix));
37 m_typeQname = new QName(*schemaType);
38 addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix()));
42 AbstractXMLObject::AbstractXMLObject(const AbstractXMLObject& src)
43 : m_namespaces(src.m_namespaces), m_log(src.m_log), m_schemaLocation(XMLString::replicate(src.m_schemaLocation)),
44 m_noNamespaceSchemaLocation(XMLString::replicate(src.m_noNamespaceSchemaLocation)),
45 m_parent(NULL), m_elementQname(src.m_elementQname), m_typeQname(NULL)
48 m_typeQname=new QName(*src.m_typeQname);
51 XMLCh* AbstractXMLObject::prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue)
53 if (!XMLString::equals(oldValue,newValue)) {
54 releaseThisandParentDOM();
55 XMLCh* newString = XMLString::replicate(newValue);
56 XMLString::release(&oldValue);
62 QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* newValue)
66 releaseThisandParentDOM();
67 Namespace newNamespace(newValue->getNamespaceURI(), newValue->getPrefix());
68 addNamespace(newNamespace);
69 return new QName(*newValue);
75 releaseThisandParentDOM();
77 Namespace newNamespace(newValue->getNamespaceURI(), newValue->getPrefix());
78 addNamespace(newNamespace);
79 return new QName(*newValue);
84 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const DateTime* newValue)
88 releaseThisandParentDOM();
89 return new DateTime(*newValue);
95 releaseThisandParentDOM();
96 return newValue ? new DateTime(*newValue) : NULL;
99 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, time_t newValue)
102 releaseThisandParentDOM();
103 DateTime* ret = new DateTime(newValue);
104 ret->parseDateTime();
108 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const XMLCh* newValue)
111 releaseThisandParentDOM();
112 DateTime* ret = new DateTime(newValue);
113 ret->parseDateTime();
117 XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue)
119 if (newValue && newValue->hasParent())
120 throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");
124 releaseThisandParentDOM();
125 newValue->setParent(this);
130 if (oldValue != newValue) {
132 releaseThisandParentDOM();
134 newValue->setParent(this);
140 void AbstractXMLObject::detach()
144 else if (getParent()->hasParent())
145 throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
147 // Pull ourselves out of the parent and then blast him.
148 getParent()->removeChild(this);