Moved DOM methods up the tree, add copy c'tors, KeyInfo sample
[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* namespaceURI, const XMLCh* elementLocalName, const XMLCh* namespacePrefix)\r
38     : m_elementQname(namespaceURI,elementLocalName, namespacePrefix), m_typeQname(NULL), m_parent(NULL),\r
39         m_log(&log4cpp::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject"))\r
40 {\r
41     addNamespace(Namespace(namespaceURI, namespacePrefix));\r
42 }\r
43 \r
44 AbstractXMLObject::AbstractXMLObject(const AbstractXMLObject& src)\r
45     : m_namespaces(src.m_namespaces), m_log(src.m_log), m_parent(NULL), m_elementQname(src.m_elementQname), m_typeQname(NULL)\r
46 {\r
47     if (src.m_typeQname)\r
48         m_typeQname=new QName(*src.m_typeQname);\r
49 }\r
50 \r
51 XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue) {\r
52 \r
53     if (newValue && newValue->hasParent())\r
54         throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");\r
55 \r
56     if (!oldValue) {\r
57         if (newValue) {\r
58             releaseThisandParentDOM();\r
59             newValue->setParent(this);\r
60             return newValue;\r
61         }\r
62         else {\r
63             return NULL;\r
64         }\r
65     }\r
66 \r
67     if (oldValue != newValue) {\r
68         delete oldValue;\r
69         releaseThisandParentDOM();\r
70         newValue->setParent(this);\r
71     }\r
72 \r
73     return newValue;\r
74 }\r