Unix porting fixes
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractXMLObject.cpp
1 /*
2 *  Copyright 2001-2006 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * AbstractXMLObject.cpp
19  * 
20  * An abstract implementation of XMLObject.
21  */
22
23 #include "internal.h"
24 #include "AbstractXMLObject.h"
25 #include "exceptions.h"
26
27 #include <algorithm>
28 #include <log4cpp/Category.hh>
29
30 using namespace xmltooling;
31
32 AbstractXMLObject::~AbstractXMLObject() {
33     delete m_typeQname;
34     std::for_each(m_children.begin(), m_children.end(), cleanup<XMLObject>());
35 }
36
37 AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType)
38     : m_log(&log4cpp::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")),
39         m_parent(NULL), m_elementQname(nsURI, localName, prefix), m_typeQname(NULL)
40 {
41     addNamespace(Namespace(nsURI, prefix));
42     if (schemaType) {
43         m_typeQname = new QName(*schemaType);
44         addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix()));
45     }
46 }
47
48 AbstractXMLObject::AbstractXMLObject(const AbstractXMLObject& src)
49     : m_namespaces(src.m_namespaces), m_log(src.m_log), m_parent(NULL), m_elementQname(src.m_elementQname), m_typeQname(NULL)
50 {
51     if (src.m_typeQname)
52         m_typeQname=new QName(*src.m_typeQname);
53 }
54
55 XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue) {
56
57     if (newValue && newValue->hasParent())
58         throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");
59
60     if (!oldValue) {
61         if (newValue) {
62             releaseThisandParentDOM();
63             newValue->setParent(this);
64             return newValue;
65         }
66         else {
67             return NULL;
68         }
69     }
70
71     if (oldValue != newValue) {
72         delete oldValue;
73         releaseThisandParentDOM();
74         newValue->setParent(this);
75     }
76
77     return newValue;
78 }