https://issues.shibboleth.net/jira/browse/CPPXT-41
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractXMLObject.cpp
1 /*
2 *  Copyright 2001-2009 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
29 using namespace xmltooling;
30
31 using xercesc::XMLString;
32
33 AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType)
34     : m_log(logging::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")),
35         m_schemaLocation(NULL), m_noNamespaceSchemaLocation(NULL), m_nil(xmlconstants::XML_BOOL_NULL),
36         m_parent(NULL), m_elementQname(nsURI, localName, prefix), m_typeQname(NULL)
37 {
38     addNamespace(Namespace(nsURI, prefix));
39     if (schemaType) {
40         m_typeQname = new QName(*schemaType);
41         addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix()));
42     }
43 }
44
45 AbstractXMLObject::AbstractXMLObject(const AbstractXMLObject& src)
46     : m_namespaces(src.m_namespaces), m_log(src.m_log), m_schemaLocation(XMLString::replicate(src.m_schemaLocation)),
47         m_noNamespaceSchemaLocation(XMLString::replicate(src.m_noNamespaceSchemaLocation)), m_nil(src.m_nil),
48         m_parent(NULL), m_elementQname(src.m_elementQname), m_typeQname(NULL)
49 {
50     if (src.m_typeQname)
51         m_typeQname=new QName(*src.m_typeQname);
52 }
53
54 AbstractXMLObject::~AbstractXMLObject()
55 {
56     delete m_typeQname;
57     xercesc::XMLString::release(&m_schemaLocation);
58     xercesc::XMLString::release(&m_noNamespaceSchemaLocation);
59 }
60
61 void XMLObject::setNil(const XMLCh* value)
62 {
63     if (value) {
64         switch (*value) {
65             case xercesc::chLatin_t:
66                 nil(xmlconstants::XML_BOOL_TRUE);
67                 break;
68             case xercesc::chLatin_f:
69                 nil(xmlconstants::XML_BOOL_FALSE);
70                 break;
71             case xercesc::chDigit_1:
72                 nil(xmlconstants::XML_BOOL_ONE);
73                 break;
74             case xercesc::chDigit_0:
75                 nil(xmlconstants::XML_BOOL_ZERO);
76                 break;
77             default:
78                 nil(xmlconstants::XML_BOOL_NULL);
79         }
80     }
81     else {
82         nil(xmlconstants::XML_BOOL_NULL);
83     }
84 }
85
86 void AbstractXMLObject::addNamespace(const Namespace& ns) const
87 {
88     std::set<Namespace>::iterator i = m_namespaces.find(ns);
89     if (i == m_namespaces.end())
90         m_namespaces.insert(ns);
91     else if (ns.alwaysDeclare())
92         const_cast<Namespace&>(*i).setAlwaysDeclare(true);
93 }
94
95 XMLCh* AbstractXMLObject::prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue)
96 {
97     if (!XMLString::equals(oldValue,newValue)) {
98         releaseThisandParentDOM();
99         XMLCh* newString = XMLString::replicate(newValue);
100         XMLString::release(&oldValue);
101         return newString;
102     }
103     return oldValue;
104 }
105
106 QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* newValue)
107 {
108     if (!oldValue) {
109         if (newValue) {
110             releaseThisandParentDOM();
111             Namespace newNamespace(newValue->getNamespaceURI(), newValue->getPrefix());
112             addNamespace(newNamespace);
113             return new QName(*newValue);
114         }
115         return NULL;
116     }
117
118     delete oldValue;
119     releaseThisandParentDOM();
120     if (newValue) {
121         Namespace newNamespace(newValue->getNamespaceURI(), newValue->getPrefix());
122         addNamespace(newNamespace);
123         return new QName(*newValue);
124     }
125     return NULL;
126 }
127
128 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const DateTime* newValue)
129 {
130     if (!oldValue) {
131         if (newValue) {
132             releaseThisandParentDOM();
133             return new DateTime(*newValue);
134         }
135         return NULL;
136     }
137
138     delete oldValue;
139     releaseThisandParentDOM();
140     return newValue ? new DateTime(*newValue) : NULL;
141 }
142
143 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, time_t newValue, bool duration)
144 {
145     delete oldValue;
146     releaseThisandParentDOM();
147     DateTime* ret = new DateTime(newValue, duration);
148     if (duration)
149         ret->parseDuration();
150     else
151         ret->parseDateTime();
152     return ret;
153 }
154
155 DateTime* AbstractXMLObject::prepareForAssignment(DateTime* oldValue, const XMLCh* newValue, bool duration)
156 {
157     delete oldValue;
158     releaseThisandParentDOM();
159     DateTime* ret = new DateTime(newValue);
160     if (duration)
161         ret->parseDuration();
162     else
163         ret->parseDateTime();
164     return ret;
165 }
166
167 XMLObject* AbstractXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue)
168 {
169     if (newValue && newValue->hasParent())
170         throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");
171
172     if (!oldValue) {
173         if (newValue) {
174             releaseThisandParentDOM();
175             newValue->setParent(this);
176         }
177         return newValue;
178     }
179
180     if (oldValue != newValue) {
181         delete oldValue;
182         releaseThisandParentDOM();
183         if (newValue)
184             newValue->setParent(this);
185     }
186
187     return newValue;
188 }
189
190 void AbstractXMLObject::detach()
191 {
192     if (!getParent())
193         return;
194     else if (getParent()->hasParent())
195         throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
196
197     // Pull ourselves out of the parent and then blast him.
198     getParent()->removeChild(this);
199     delete m_parent;
200     m_parent = NULL;
201 }