gcc const fix, converted linefeeds
[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(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType)
33     : m_log(&log4cpp::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")), m_schemaLocation(NULL),
34         m_parent(NULL), m_elementQname(nsURI, localName, prefix), m_typeQname(NULL)
35 {
36     addNamespace(Namespace(nsURI, prefix));
37     if (schemaType) {
38         m_typeQname = new QName(*schemaType);
39         addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix()));
40     }
41 }
42
43 AbstractXMLObject::AbstractXMLObject(const AbstractXMLObject& src)
44     : m_namespaces(src.m_namespaces), m_log(src.m_log), m_schemaLocation(XMLString::replicate(src.m_schemaLocation)),
45         m_parent(NULL), m_elementQname(src.m_elementQname), m_typeQname(NULL)
46 {
47     if (src.m_typeQname)
48         m_typeQname=new QName(*src.m_typeQname);
49 }
50
51 XMLCh* AbstractXMLObject::prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue)
52 {
53     if (!XMLString::equals(oldValue,newValue)) {
54         releaseThisandParentDOM();
55         XMLCh* newString = XMLString::replicate(newValue);
56         XMLString::release(&oldValue);
57         return newString;
58     }
59     return oldValue;
60 }
61
62 QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* newValue)
63 {
64     if (!oldValue) {
65         if (newValue) {
66             releaseThisandParentDOM();
67             Namespace newNamespace(newValue->getNamespaceURI(), newValue->getPrefix());
68             addNamespace(newNamespace);
69             return new QName(*newValue);
70         }
71         return NULL;
72     }
73
74     delete oldValue;
75     releaseThisandParentDOM();
76     if (newValue) {
77         Namespace newNamespace(newValue->getNamespaceURI(), newValue->getPrefix());
78         addNamespace(newNamespace);
79         return new QName(*newValue);
80     }
81     return NULL;
82 }
83
84 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const DateTime* newValue)
85 {
86     if (!oldValue) {
87         if (newValue) {
88             releaseThisandParentDOM();
89             return new DateTime(*newValue);
90         }
91         return NULL;
92     }
93
94     delete oldValue;
95     releaseThisandParentDOM();
96     return newValue ? new DateTime(*newValue) : NULL;
97 }
98
99 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, time_t newValue)
100 {
101     delete oldValue;
102     releaseThisandParentDOM();
103     DateTime* ret = new DateTime(newValue);
104     ret->parseDateTime();
105     return ret;
106 }
107
108 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const XMLCh* newValue)
109 {
110     delete oldValue;
111     releaseThisandParentDOM();
112     DateTime* ret = new DateTime(newValue);
113     ret->parseDateTime();
114     return ret;
115 }
116
117 XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue)
118 {
119     if (newValue && newValue->hasParent())
120         throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");
121
122     if (!oldValue) {
123         if (newValue) {
124             releaseThisandParentDOM();
125             newValue->setParent(this);
126         }
127         return newValue;
128     }
129
130     if (oldValue != newValue) {
131         delete oldValue;
132         releaseThisandParentDOM();
133         if (newValue)
134             newValue->setParent(this);
135     }
136
137     return newValue;
138 }
139
140 void AbstractXMLObject::detach()
141 {
142     if (!getParent())
143         return;
144     else if (getParent()->hasParent())
145         throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
146
147     // Pull ourselves out of the parent and then blast him.
148     getParent()->removeChild(this);
149     delete m_parent;
150     m_parent = NULL;
151 }