Set xsi:type during object construction.
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractXMLObject.cpp
1 /*\r
2 *  Copyright 2001-2006 Internet2\r
3  * \r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * AbstractXMLObject.cpp\r
19  * \r
20  * An abstract implementation of XMLObject.\r
21  */\r
22 \r
23 #include "internal.h"\r
24 #include "AbstractXMLObject.h"\r
25 #include "exceptions.h"\r
26 \r
27 #include <algorithm>\r
28 #include <log4cpp/Category.hh>\r
29 \r
30 using namespace xmltooling;\r
31 \r
32 AbstractXMLObject::~AbstractXMLObject() {\r
33     delete m_typeQname;\r
34     std::for_each(m_children.begin(), m_children.end(), cleanup<XMLObject>());\r
35 }\r
36 \r
37 AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType)\r
38     : m_elementQname(nsURI, localName, prefix), m_typeQname(NULL), m_parent(NULL),\r
39         m_log(&log4cpp::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject"))\r
40 {\r
41     addNamespace(Namespace(nsURI, prefix));\r
42     if (schemaType) {\r
43         m_typeQname = new QName(*schemaType);\r
44         addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix()));\r
45     }\r
46 }\r
47 \r
48 AbstractXMLObject::AbstractXMLObject(const AbstractXMLObject& src)\r
49     : m_namespaces(src.m_namespaces), m_log(src.m_log), m_parent(NULL), m_elementQname(src.m_elementQname), m_typeQname(NULL)\r
50 {\r
51     if (src.m_typeQname)\r
52         m_typeQname=new QName(*src.m_typeQname);\r
53 }\r
54 \r
55 XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue) {\r
56 \r
57     if (newValue && newValue->hasParent())\r
58         throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");\r
59 \r
60     if (!oldValue) {\r
61         if (newValue) {\r
62             releaseThisandParentDOM();\r
63             newValue->setParent(this);\r
64             return newValue;\r
65         }\r
66         else {\r
67             return NULL;\r
68         }\r
69     }\r
70 \r
71     if (oldValue != newValue) {\r
72         delete oldValue;\r
73         releaseThisandParentDOM();\r
74         newValue->setParent(this);\r
75     }\r
76 \r
77     return newValue;\r
78 }\r